refactoring

This commit is contained in:
rusefillc 2021-10-14 16:40:19 -04:00
parent e1b5fdc60b
commit 636f7dec71
4 changed files with 47 additions and 36 deletions

Binary file not shown.

View File

@ -14,8 +14,8 @@ import java.util.*;
*/
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
public class EnumToString {
private final static StringBuilder cppFileContent = new StringBuilder();
private final static StringBuilder includesSection = new StringBuilder();
private final StringBuilder cppFileContent = new StringBuilder();
private final StringBuilder includesSection = new StringBuilder();
private final static StringBuilder bothFilesHeader = new StringBuilder("// by enum2string.jar tool " +
"on " + new Date() + "\n" +
@ -44,13 +44,14 @@ public class EnumToString {
String inputPath = ".";
String outputPath = null;
EnumsReader enumsReader = new EnumsReader();
EnumToString state = new EnumToString();
for (int i = 0; i < args.length - 1; i += 2) {
String key = args[i];
if (key.equals(KEY_INPUT_PATH)) {
inputPath = Objects.requireNonNull(args[i + 1], KEY_INPUT_PATH);
} else if (key.equals(KEY_ENUM_INPUT_FILE)) {
String headerInputFile = args[i + 1];
consumeFile(enumsReader, inputPath, headerInputFile);
state.consumeFile(enumsReader, inputPath, headerInputFile);
} else if (key.equals(KEY_OUTPUT_FILE)) {
fileSuffix = args[i + 1];
} else if (key.equals(KEY_OUTPUT)) {
@ -60,25 +61,25 @@ public class EnumToString {
headerFileContent.append("#pragma once\n");
outputData(enumsReader);
state.outputData(enumsReader);
cppFileContent.insert(0, bothFilesHeader.toString());
state.cppFileContent.insert(0, bothFilesHeader.toString());
cppFileContent.insert(0, includesSection);
headerFileContent.insert(0, includesSection);
state.cppFileContent.insert(0, state.includesSection);
headerFileContent.insert(0, state.includesSection);
SystemOut.println("includesSection:\n" + includesSection + "end of includesSection\n");
SystemOut.println("includesSection:\n" + state.includesSection + "end of includesSection\n");
cppFileContent.insert(0, "#include \"global.h\"\n");
state.cppFileContent.insert(0, "#include \"global.h\"\n");
headerFileContent.insert(0, bothFilesHeader.toString());
new File(outputPath).mkdirs();
writeCppAndHeaderFiles(outputPath + File.separator + "auto_generated_" +
state.writeCppAndHeaderFiles(outputPath + File.separator + "auto_generated_" +
fileSuffix);
SystemOut.close();
}
private static void writeCppAndHeaderFiles(String outFileName) throws IOException {
private void writeCppAndHeaderFiles(String outFileName) throws IOException {
LazyFile bw = new LazyFile(outFileName + ".cpp");
bw.write(cppFileContent.toString());
bw.close();
@ -88,7 +89,7 @@ public class EnumToString {
bw.close();
}
private static void consumeFile(EnumsReader enumsReader, String inputPath, String headerInputFileName) throws IOException {
private void consumeFile(EnumsReader enumsReader, String inputPath, String headerInputFileName) throws IOException {
Objects.requireNonNull(inputPath, "inputPath");
File f = new File(inputPath + File.separator + headerInputFileName);
SystemOut.println("Reading enums from " + headerInputFileName);
@ -97,10 +98,10 @@ public class EnumToString {
bothFilesHeader.insert(0, "// " + LazyFile.LAZY_FILE_TAG + " from " + simpleFileName + " ");
includesSection.append("#include \"" + simpleFileName + "\"\n");
enumsReader.process(new FileReader(f));
enumsReader.read(new FileReader(f));
}
public static void outputData(EnumsReader enumsReader) {
public EnumToString outputData(EnumsReader enumsReader) {
SystemOut.println("Preparing output for " + enumsReader.getEnums().size() + " enums\n");
for (Map.Entry<String, Map<String, Value>> e : enumsReader.getEnums().entrySet()) {
@ -109,10 +110,7 @@ public class EnumToString {
headerFileContent.append(getMethodSignature(enumName) + ";\n");
}
SystemOut.println("EnumToString: " + headerFileContent.length() + " bytes of content\n");
}
public static void clear() {
cppFileContent.setLength(0);
return this;
}
private static String makeCode(String enumName, Collection<Value> values) {
@ -141,7 +139,7 @@ public class EnumToString {
return Character.toUpperCase(enumName.charAt(0)) + enumName.substring(1);
}
public static String getCppFileContent() {
public String getCppFileContent() {
return cppFileContent.toString();
}
}

View File

@ -16,11 +16,11 @@ public class EnumsReader {
return enums;
}
public void process(String path, String fileName) throws IOException {
process(new FileReader(path + File.separator + fileName));
public EnumsReader read(String path, String fileName) throws IOException {
return read(new FileReader(path + File.separator + fileName));
}
public void process(Reader in) throws IOException {
public EnumsReader read(Reader in) throws IOException {
boolean isInsideEnum = false;
BufferedReader reader = new BufferedReader(in);
String line;
@ -55,6 +55,7 @@ public class EnumsReader {
}
}
}
return this;
}
private static String removeSpaces(String line) {

View File

@ -1,10 +1,10 @@
package com.rusefi;
import com.rusefi.enum_reader.Value;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
@ -14,11 +14,10 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class EnumToStringTest {
public static EnumsReader process(Reader reader) throws IOException {
EnumsReader enumsReader = new EnumsReader();
enumsReader.process(reader);
EnumToString.outputData(enumsReader);
return enumsReader;
public static EnumToString process(EnumsReader enumsReader) {
EnumToString enumToString = new EnumToString();
enumToString.outputData(enumsReader);
return enumToString;
}
@Test
@ -34,13 +33,14 @@ public class EnumToStringTest {
@Test
public void parseEnum() throws IOException {
EnumToString.clear();
EnumsReader enumsReader = process(new StringReader(
final StringReader reader = new StringReader(
"typedef enum {\n" +
"\tGPIO_UNASSIGNED = 0,\n" +
"\tGPIO_INVALID = 1,\n" +
"\tGPIO_HEX = 0xA1,\n" +
"}brain_pin_e; // hello"));
"}brain_pin_e; // hello");
EnumsReader enumsReader = new EnumsReader().read(reader);
EnumToString enumToString = process(enumsReader);
List<Value> values = new ArrayList<>(enumsReader.getEnums().get("brain_pin_e").values());
assertEquals(3, values.size());
@ -62,17 +62,18 @@ public class EnumToStringTest {
" return \"GPIO_UNASSIGNED\";\n" +
" }\n" +
" return NULL;\n" +
"}\n", EnumToString.getCppFileContent());
"}\n", enumToString.getCppFileContent());
}
@Test
public void parsePackedEnum() throws IOException {
EnumToString.clear();
process(new StringReader(
final StringReader reader = new StringReader(
"typedef enum __attribute__ ((__packed__)) {\n" +
"\tGPIO_UNASSIGNED = 0,\n" +
"\tGPIO_INVALID = 1,\n" +
"} brain_pin_e ;"));
"} brain_pin_e ;");
EnumsReader enumsReader = new EnumsReader().read(reader);
EnumToString enumToString = process(enumsReader);
assertEquals("const char *getBrain_pin_e(brain_pin_e value){\n" +
"switch(value) {\n" +
"case GPIO_INVALID:\n" +
@ -81,6 +82,17 @@ public class EnumToStringTest {
" return \"GPIO_UNASSIGNED\";\n" +
" }\n" +
" return NULL;\n" +
"}\n", EnumToString.getCppFileContent());
"}\n", enumToString.getCppFileContent());
}
@Test
public void parseEnumWithoutExplicitValues() throws IOException {
final StringReader reader = new StringReader(
"typedef enum {\n" +
"\tGPIO_UNASSIGNED,\n" +
"\tGPIO_INVALID,\n" +
"\tGPIO_HEX,\n" +
"}brain_pin_e; // hello");
EnumsReader enumsReader = new EnumsReader().read(reader);
}
}