extracting opensr5

This commit is contained in:
rusefi 2017-03-01 16:00:13 -05:00
parent a9e703ce4a
commit 3d1f560ed7
6 changed files with 115 additions and 79 deletions

View File

@ -9,5 +9,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="annotations" level="project" />
<orderEntry type="library" name="jssc" level="project" />
<orderEntry type="library" name="junit" level="project" />
</component>
</module>

View File

@ -0,0 +1,54 @@
package com.opensr5.io;
import java.util.ArrayList;
/**
* (c) Andrey Belomutskiy
* 3/1/2017
*/
public class IniFileReader {
private IniFileReader() {
}
/**
* <pre>
* This method reads
* x = y, "Z"
* lines returning
* {x, y, "Z"}
* array of Strings
*
* equals sign, space, tab and commas are all equally considered not parts of the token, quotes are supportd
*
* Technically a line like
* x, y = z
* is also supported and would produce same valid output
*
* </pre>
*/
public static String[] split(String str) {
ArrayList<String> strings = new ArrayList<>();
boolean inQuote = false;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\"' || isTokenSeparator(c) && !inQuote) {
if (c == '\"')
inQuote = !inQuote;
if (!inQuote && sb.length() > 0) {
strings.add(sb.toString());
sb.delete(0, sb.length());
}
} else
sb.append(c);
}
if (sb.length() > 0)
strings.add(sb.toString());
return strings.toArray(new String[strings.size()]);
}
private static boolean isTokenSeparator(int c) {
return c == ' ' || c == '\t' || c == '=' || c == ',';
}
}

View File

@ -0,0 +1,55 @@
package com.opensr5.io.test;
import com.opensr5.io.IniFileReader;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* (c) Andrey Belomutskiy
* 3/1/2017
*/
public class IniFileReaderTest {
@Test
public void testSplit() {
{
String[] s = IniFileReader.split("1");
assertEquals(s.length, 1);
assertEquals("1", s[0]);
}
{
String[] s = IniFileReader.split("hello");
assertEquals(s.length, 1);
assertEquals("hello", s[0]);
}
{
String[] s = IniFileReader.split("\"hello\"");
assertEquals(s.length, 1);
assertEquals("hello", s[0]);
}
{
String[] s = IniFileReader.split("\"hello\",\"w\"");
assertEquals(s.length, 2);
assertEquals("hello", s[0]);
assertEquals("w", s[1]);
}
}
@Test
public void testQuotedTokens() {
{
String[] result = IniFileReader.split("\"hel lo\"");
assertEquals(result.length, 1);
assertEquals("hel lo", result[0]);
}
}
@Test
public void testRealLine() {
String[] result = IniFileReader.split("\tdialog = engineChars,\t\"Base Engine Settings\"");
assertEquals(result.length, 3);
assertEquals("dialog", result[0]);
assertEquals("engineChars", result[1]);
assertEquals("Base Engine Settings", result[2]);
}
}

View File

@ -29,7 +29,7 @@ public class SettingsTab {
}
public void showContent() {
final Map<String, DialogModel> dialogs = IniFileModel.getinstance().getDialogs();
final Map<String, DialogModel> dialogs = IniFileModel.getInstance().getDialogs();
if (dialogs.isEmpty()) {
// todo: show error label
return;
@ -121,7 +121,7 @@ public class SettingsTab {
dialog.setText(name);
dialogBody.removeAll();
DialogModel m = IniFileModel.getinstance().getDialogs().get(name);
DialogModel m = IniFileModel.getInstance().getDialogs().get(name);
dialogBody.setLayout(new GridLayout(m.getFields().size(), 1));

View File

@ -1,6 +1,6 @@
package com.rusefi.ui.config;
import com.rusefi.config.Field;
import com.opensr5.io.IniFileReader;
import java.io.*;
import java.util.*;
@ -67,7 +67,7 @@ public class IniFileModel {
private void handleLine(String line) {
try {
LinkedList<String> list = new LinkedList<>(Arrays.asList(split(line)));
LinkedList<String> list = new LinkedList<>(Arrays.asList(IniFileReader.split(line)));
trim(list);
@ -122,33 +122,8 @@ public class IniFileModel {
DIALOG
}
public static String[] split(String str) {
ArrayList<String> strings = new ArrayList<>();
boolean inQuote = false;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\"' || isWhitespace(c) && !inQuote) {
if (c == '\"')
inQuote = !inQuote;
if (!inQuote && sb.length() > 0) {
strings.add(sb.toString());
sb.delete(0, sb.length());
}
} else
sb.append(c);
}
if (sb.length() > 0)
strings.add(sb.toString());
return strings.toArray(new String[strings.size()]);
}
private static boolean isWhitespace(int c) {
return c == ' ' || c == '\t' || c == '=' || c == ',';
}
public static IniFileModel getinstance() {
public static IniFileModel getInstance() {
return INSTANCE;
}

View File

@ -1,49 +0,0 @@
package com.rusefi.ui.config.test;
import com.rusefi.ui.config.IniFileModel;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/**
* (c) Andrey Belomutskiy
* 12/23/2015.
*/
public class IniFileModelTest {
@Test
public void testSplit() {
{
String[] s = IniFileModel.split("1");
assertEquals(s.length, 1);
}
{
String[] s = IniFileModel.split("hello");
assertEquals(s.length, 1);
}
{
String[] s = IniFileModel.split("\"hello\"");
assertEquals(s.length, 1);
}
{
String[] s = IniFileModel.split("\"hello\",\"w\"");
assertEquals(s.length, 2);
}
}
@Test
public void testQuotedTokens() {
{
String[] s = IniFileModel.split("\"hel lo\"");
assertEquals(s.length, 1);
}
}
@Test
public void testRealLine() {
String[] s = IniFileModel.split("\tdialog = engineChars,\t\"Base Engine Settings\"");
assertEquals(s.length, 3);
System.out.println(Arrays.toString(s));
}
}