From 34ce9f529cbdcc0fa2b45e6056d38b0eeb214217 Mon Sep 17 00:00:00 2001 From: rusefi Date: Wed, 29 May 2019 23:07:46 -0400 Subject: [PATCH] board-specific usability #808 --- .../src/com/rusefi/EnumToString.java | 15 +++++++++------ .../src/com/rusefi/EnumToStringTest.java | 5 +++++ .../src/com/rusefi/EnumsReader.java | 10 ++++++---- .../src/com/rusefi/enum_reader/Value.java | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 java_tools/enum_to_string/src/com/rusefi/enum_reader/Value.java diff --git a/java_tools/enum_to_string/src/com/rusefi/EnumToString.java b/java_tools/enum_to_string/src/com/rusefi/EnumToString.java index eaa8719e21..24433b98a0 100644 --- a/java_tools/enum_to_string/src/com/rusefi/EnumToString.java +++ b/java_tools/enum_to_string/src/com/rusefi/EnumToString.java @@ -1,5 +1,7 @@ package com.rusefi; +import com.rusefi.enum_reader.Value; + import java.io.*; import java.util.Date; import java.util.Map; @@ -14,6 +16,7 @@ import java.util.Set; public class EnumToString { public final static StringBuilder cppFileContent = new StringBuilder(); private final static StringBuilder headerFileContent = new StringBuilder(); + public static final String RELATIVE_PATH = "controllers/algo/rusefi_enums.h"; public static void main(String[] args) throws IOException { if (args.length != 2) { @@ -26,7 +29,7 @@ public class EnumToString { headerFileContent.append("#ifndef _A_H_HEADER_\r\n"); headerFileContent.append("#define _A_H_HEADER_\r\n"); - processFile(inputPath + File.separator + "controllers/algo/rusefi_enums.h"); + processFile(inputPath + File.separator + RELATIVE_PATH); headerFileContent.append("#endif /*_A_H_HEADER_ */\r\n"); @@ -69,7 +72,7 @@ public class EnumToString { public static void process(Reader reader) throws IOException { EnumsReader.process(reader); - for (Map.Entry> e : EnumsReader.enums.entrySet()) { + for (Map.Entry> e : EnumsReader.enums.entrySet()) { String enumName = e.getKey(); cppFileContent.append(makeCode(enumName, e.getValue())); EnumToString.headerFileContent.append(getMethodSignature(enumName) + ";\r\n"); @@ -80,15 +83,15 @@ public class EnumToString { cppFileContent.setLength(0); } - private static String makeCode(String enumName, Set values) { + private static String makeCode(String enumName, Set values) { StringBuilder sb = new StringBuilder(); sb.append(getMethodSignature(enumName) + "{\r\n"); sb.append("switch(value) {\r\n"); - for (String e : values) { - sb.append("case " + e + ":\r\n"); - sb.append(" return \"" + e + "\";\r\n"); + for (Value e : values) { + sb.append("case " + e.getName() + ":\r\n"); + sb.append(" return \"" + e.getName() + "\";\r\n"); } sb.append(" }\r\n"); diff --git a/java_tools/enum_to_string/src/com/rusefi/EnumToStringTest.java b/java_tools/enum_to_string/src/com/rusefi/EnumToStringTest.java index b1e350d9eb..db31477e51 100644 --- a/java_tools/enum_to_string/src/com/rusefi/EnumToStringTest.java +++ b/java_tools/enum_to_string/src/com/rusefi/EnumToStringTest.java @@ -1,9 +1,11 @@ package com.rusefi; +import com.rusefi.enum_reader.Value; import org.junit.Test; import java.io.IOException; import java.io.StringReader; +import java.util.Set; import static com.rusefi.EnumsReader.isKeyValueLine; import static org.junit.Assert.assertEquals; @@ -25,6 +27,9 @@ public class EnumToStringTest { "\tGPIO_UNASSIGNED = 0,\n" + "\tGPIO_INVALID = 1,\n" + "}brain_pin_e;")); + + Set values = EnumsReader.enums.get("brain_pin_e"); + assertEquals("const char *getBrain_pin_e(brain_pin_e value){\r\n" + "switch(value) {\r\n" + "case GPIO_INVALID:\r\n" + diff --git a/java_tools/enum_to_string/src/com/rusefi/EnumsReader.java b/java_tools/enum_to_string/src/com/rusefi/EnumsReader.java index 5f5215430a..9efbdaad20 100644 --- a/java_tools/enum_to_string/src/com/rusefi/EnumsReader.java +++ b/java_tools/enum_to_string/src/com/rusefi/EnumsReader.java @@ -1,5 +1,7 @@ package com.rusefi; +import com.rusefi.enum_reader.Value; + import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; @@ -9,9 +11,9 @@ import java.util.TreeMap; import java.util.TreeSet; public class EnumsReader { - private final static Set currentValues = new TreeSet<>(); + private final static Set currentValues = new TreeSet<>(); - public final static Map> enums = new TreeMap<>(); + public final static Map> enums = new TreeMap<>(); public static void process(Reader in) throws IOException { boolean isInsideEnum = false; @@ -28,7 +30,7 @@ public class EnumsReader { isInsideEnum = false; line = line.substring(1, line.length() - 1); System.out.println("Ending enum " + line); - enums.put(line, new TreeSet<>(currentValues)); + enums.put(line, new TreeSet(currentValues)); } else { line = line.replaceAll("//.+", ""); if (isInsideEnum) { @@ -38,7 +40,7 @@ public class EnumsReader { if (index != -1) line = line.substring(0, index); System.out.println("Line " + line); - currentValues.add(line); + currentValues.add(new Value(line)); } } } diff --git a/java_tools/enum_to_string/src/com/rusefi/enum_reader/Value.java b/java_tools/enum_to_string/src/com/rusefi/enum_reader/Value.java new file mode 100644 index 0000000000..c306d65776 --- /dev/null +++ b/java_tools/enum_to_string/src/com/rusefi/enum_reader/Value.java @@ -0,0 +1,18 @@ +package com.rusefi.enum_reader; + +public class Value implements Comparable { + private final String name; + + public Value(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public int compareTo(Value o) { + return name.compareTo(o.name); + } +}