From 40acea4416159b891cc69655346f24c66a4a285e Mon Sep 17 00:00:00 2001 From: rusEfi Date: Wed, 23 Dec 2015 22:01:26 -0500 Subject: [PATCH] auto-sync --- java_console/.idea/ant.xml | 7 +- java_console/build.xml | 7 ++ .../com/rusefi/ui/config/IniFileModel.java | 100 ++++++++++++++++++ .../ui/config/test/IniFileModelTest.java | 32 ++++++ 4 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 java_console/ui/src/com/rusefi/ui/config/IniFileModel.java create mode 100644 java_console/ui/src/com/rusefi/ui/config/test/IniFileModelTest.java diff --git a/java_console/.idea/ant.xml b/java_console/.idea/ant.xml index 313b0c0cb8..8fae66c82f 100644 --- a/java_console/.idea/ant.xml +++ b/java_console/.idea/ant.xml @@ -1,7 +1,8 @@ - + + + - - + \ No newline at end of file diff --git a/java_console/build.xml b/java_console/build.xml index fbc8ec9a47..e4ef96001c 100644 --- a/java_console/build.xml +++ b/java_console/build.xml @@ -93,4 +93,11 @@ + + + + + + + \ No newline at end of file diff --git a/java_console/ui/src/com/rusefi/ui/config/IniFileModel.java b/java_console/ui/src/com/rusefi/ui/config/IniFileModel.java new file mode 100644 index 0000000000..76c6ea26fa --- /dev/null +++ b/java_console/ui/src/com/rusefi/ui/config/IniFileModel.java @@ -0,0 +1,100 @@ +package com.rusefi.ui.config; + +import java.io.*; +import java.util.*; + +/** + * (c) Andrey Belomutskiy + * 12/23/2015. + */ +public class IniFileModel { + // todo: https://sourceforge.net/p/rusefi/tickets/243/ + private static final String FILENAME = "rusefi.ini"; + + private final static IniFileModel INSTANCE = new IniFileModel(); + + public static void main(String[] args) { + System.out.println(IniFileModel.INSTANCE); + } + + private IniFileModel() { + readIniFile(); + } + + private void readIniFile() { + File input = new File(FILENAME); + BufferedReader d = null; + try { + d = new BufferedReader(new InputStreamReader(new FileInputStream(input))); + } catch (FileNotFoundException e) { + System.out.println("No such file: " + FILENAME); + return; + } + + + try { + String line; + + State state = State.SKIPPING; + + while ((line = d.readLine()) != null) { + LinkedList list = new LinkedList<>(Arrays.asList(line.split("[\t =,]"))); + + trim(list); + + if (list.isEmpty()) + continue; + String first = list.getFirst(); + + if ("dialog".equals(first)) { + list.removeFirst(); + state = State.DIALOG; + trim(list); + String keyword = list.removeFirst(); + trim(list); + String name = list.isEmpty() ? null : list.removeFirst(); + + System.out.println("Dialog " + keyword + ": " + name); + + } + + + } + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private void trim(LinkedList list) { + while (!list.isEmpty() && list.getFirst().isEmpty()) + list.removeFirst(); + } + + enum State { + SKIPPING, + DIALOG + } + + public static String[] split(String str) { + ArrayList 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 == '\"' || 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()]); + } + +} diff --git a/java_console/ui/src/com/rusefi/ui/config/test/IniFileModelTest.java b/java_console/ui/src/com/rusefi/ui/config/test/IniFileModelTest.java new file mode 100644 index 0000000000..3c0a059982 --- /dev/null +++ b/java_console/ui/src/com/rusefi/ui/config/test/IniFileModelTest.java @@ -0,0 +1,32 @@ +package com.rusefi.ui.config.test; + +import com.rusefi.ui.config.IniFileModel; +import org.junit.Test; + +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); + } + } +}