refactoring

This commit is contained in:
rusefi 2018-12-20 00:04:06 -05:00
parent 176aa20908
commit 4694292b74
7 changed files with 114 additions and 61 deletions

View File

@ -0,0 +1,38 @@
package com.rusefi;
import java.io.BufferedWriter;
import java.io.IOException;
import static com.rusefi.ConfigDefinition.EOL;
/**
* Configuration consumer which writes C header file
*/
public class CHeaderConsumer implements ConfigurationConsumer {
private final BufferedWriter cHeader;
public CHeaderConsumer(BufferedWriter cHeader) {
this.cHeader = cHeader;
}
@Override
public void startFile() throws IOException {
cHeader.write("// this section " + ConfigDefinition.MESSAGE + EOL);
cHeader.write("// begin" + EOL);
cHeader.write("#ifndef ENGINE_CONFIGURATION_GENERATED_H_" + EOL);
cHeader.write("#define ENGINE_CONFIGURATION_GENERATED_H_" + EOL);
cHeader.write("#include \"rusefi_types.h\"" + EOL);
}
@Override
public void endFile() throws IOException {
cHeader.write("#endif" + EOL);
cHeader.write("// end" + EOL);
cHeader.write("// this section " + ConfigDefinition.MESSAGE + EOL);
}
@Override
public void handleEndStruct(ConfigStructure structure) throws IOException {
ConfigStructure.headerWrite(structure, cHeader);
}
}

View File

@ -2,8 +2,6 @@ package com.rusefi;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* (c) Andrey Belomutskiy
@ -13,7 +11,7 @@ import java.util.Map;
public class ConfigDefinition {
public static final String EOL = "\n";
private static final String INPUT_FILE_NAME = "rusefi_config.txt";
private static final String MESSAGE = "was generated automatically by ConfigDefinition.jar based on " + INPUT_FILE_NAME + " " + new Date();
public static final String MESSAGE = "was generated automatically by ConfigDefinition.jar based on " + INPUT_FILE_NAME + " " + new Date();
private static final String TS_FILE_INPUT_NAME = "rusefi.input";
private static final String TS_FILE_OUTPUT_NAME = "rusefi.ini";
private static final String STRUCT_NO_PREFIX = "struct_no_prefix ";
@ -30,7 +28,6 @@ public class ConfigDefinition {
private static final String FIELDS_JAVA = "models/src/com/rusefi/config/Fields.java";
private static int totalTsSize;
public static Map<String, ConfigStructure> structures = new HashMap<>();
public static StringBuilder settingContextHelp = new StringBuilder();
public static void main(String[] args) throws IOException {
@ -57,8 +54,11 @@ public class ConfigDefinition {
CharArrayWriter javaFieldsWriter = new CharArrayWriter();
ConfigurationConsumer cHeaderConsumer = new CHeaderConsumer(cHeader);
ReaderState state = new ReaderState();
processFile(state, br, cHeader, tsWriter, javaFieldsWriter);
processFile(state, br, cHeaderConsumer, tsWriter, javaFieldsWriter);
BufferedWriter javaFields = new BufferedWriter(new FileWriter(javaConsolePath + File.separator + FIELDS_JAVA));
javaFields.write("package com.rusefi.config;" + EOL + EOL);
@ -154,15 +154,11 @@ public class ConfigDefinition {
return new TsFileContent(prefix.toString(), postfix.toString());
}
private static void processFile(ReaderState state, BufferedReader br, BufferedWriter cHeader, Writer tsHeader, CharArrayWriter javaFieldsWriter) throws IOException {
private static void processFile(ReaderState state, BufferedReader br, ConfigurationConsumer cHeaderConsumer,
Writer tsHeader, CharArrayWriter javaFieldsWriter) throws IOException {
String line;
String message = "// this section " + MESSAGE + EOL;
cHeader.write(message);
cHeader.write("// begin" + EOL);
cHeader.write("#ifndef ENGINE_CONFIGURATION_GENERATED_H_" + EOL);
cHeader.write("#define ENGINE_CONFIGURATION_GENERATED_H_" + EOL);
cHeader.write("#include \"rusefi_types.h\"" + EOL);
cHeaderConsumer.startFile();
while ((line = br.readLine()) != null) {
line = line.trim();
@ -178,28 +174,12 @@ public class ConfigDefinition {
} else if (line.startsWith(STRUCT_NO_PREFIX)) {
handleStartStructure(state, line.substring(STRUCT_NO_PREFIX.length()), false);
} else if (line.startsWith(END_STRUCT)) {
handleEndStruct(state, cHeader, tsHeader, javaFieldsWriter);
handleEndStruct(state, cHeaderConsumer, tsHeader, javaFieldsWriter);
} else if (line.startsWith(BIT)) {
handleBitLine(state, line);
} else if (startsWithToken(line, CUSTOM)) {
line = line.substring(CUSTOM.length() + 1).trim();
int index = line.indexOf(' ');
String name = line.substring(0, index);
line = line.substring(index).trim();
index = line.indexOf(' ');
String customSize = line.substring(0, index);
String tunerStudioLine = line.substring(index).trim();
tunerStudioLine = VariableRegistry.INSTANCE.applyVariables(tunerStudioLine);
int size;
try {
size = Integer.parseInt(customSize);
} catch (NumberFormatException e) {
throw new IllegalStateException("Size in " + line);
}
state.tsCustomSize.put(name, size);
state.tsCustomLine.put(name, tunerStudioLine);
handleCustomLine(state, line);
} else if (startsWithToken(line, DEFINE)) {
/**
@ -211,9 +191,27 @@ public class ConfigDefinition {
processField(state, line);
}
}
cHeader.write("#endif" + EOL);
cHeader.write("// end" + EOL);
cHeader.write(message);
cHeaderConsumer.endFile();
}
private static void handleCustomLine(ReaderState state, String line) {
line = line.substring(CUSTOM.length() + 1).trim();
int index = line.indexOf(' ');
String name = line.substring(0, index);
line = line.substring(index).trim();
index = line.indexOf(' ');
String customSize = line.substring(0, index);
String tunerStudioLine = line.substring(index).trim();
tunerStudioLine = VariableRegistry.INSTANCE.applyVariables(tunerStudioLine);
int size;
try {
size = Integer.parseInt(customSize);
} catch (NumberFormatException e) {
throw new IllegalStateException("Size in " + line);
}
state.tsCustomSize.put(name, size);
state.tsCustomLine.put(name, tunerStudioLine);
}
private static void handleBitLine(ReaderState state, String line) {
@ -254,23 +252,24 @@ public class ConfigDefinition {
System.out.println("Starting structure " + structure.getName());
}
private static void handleEndStruct(ReaderState state, Writer cHeader, Writer tsHeader, CharArrayWriter javaFieldsWriter) throws IOException {
private static void handleEndStruct(ReaderState state, ConfigurationConsumer cHeaderConsumer,
Writer tsHeader, CharArrayWriter javaFieldsWriter) throws IOException {
if (state.stack.isEmpty())
throw new IllegalStateException("Unexpected end_struct");
ConfigStructure structure = state.stack.pop();
System.out.println("Ending structure " + structure.getName());
structure.addAlignmentFill(state);
ConfigDefinition.structures.put(structure.getName(), structure);
state.structures.put(structure.getName(), structure);
structure.headerWrite(cHeader);
cHeaderConsumer.handleEndStruct(structure);
if (state.stack.isEmpty()) {
totalTsSize = structure.writeTunerStudio(state,"", tsHeader, 0);
totalTsSize = structure.writeTunerStudio("", tsHeader, 0);
tsHeader.write("; total TS size = " + totalTsSize + EOL);
VariableRegistry.INSTANCE.register("TOTAL_CONFIG_SIZE", totalTsSize);
structure.writeJavaFields("", javaFieldsWriter, 0);
structure.writeJavaFields(state,"", javaFieldsWriter, 0);
}
}

View File

@ -48,9 +48,11 @@ public class ConfigField {
* this property of array expands field into a bunch of variables like field1 field2 field3 etc
*/
public final boolean isIterate;
private final ReaderState state;
public ConfigField(ReaderState state, String name, String comment, boolean isBit, String arraySizeAsText, String type,
int arraySize, String tsInfo, boolean isIterate) {
this.state = state;
if (name == null)
throw new NullPointerException(comment + " " + isBit + " " + type);
assertNoWhitespaces(name);
@ -137,15 +139,15 @@ public class ConfigField {
'}';
}
public int writeTunerStudio(ReaderState state, String prefix, Writer tsHeader, int tsPosition, ConfigField next, int bitIndex) throws IOException {
public int writeTunerStudio(String prefix, Writer tsHeader, int tsPosition, ConfigField next, int bitIndex) throws IOException {
String nameWithPrefix = prefix + name;
VariableRegistry.INSTANCE.register(nameWithPrefix + "_offset", tsPosition);
ConfigStructure cs = ConfigDefinition.structures.get(type);
ConfigStructure cs = state.structures.get(type);
if (cs != null) {
String extraPrefix = cs.withPrefix ? name + "_" : "";
return cs.writeTunerStudio(state, prefix + extraPrefix, tsHeader, tsPosition);
return cs.writeTunerStudio(prefix + extraPrefix, tsHeader, tsPosition);
}
if (isBit) {
@ -203,10 +205,10 @@ public class ConfigField {
}
public int writeJavaFields(String prefix, Writer javaFieldsWriter, int tsPosition, ConfigField next, int bitIndex) throws IOException {
ConfigStructure cs = ConfigDefinition.structures.get(type);
ConfigStructure cs = state.structures.get(type);
if (cs != null) {
String extraPrefix = cs.withPrefix ? name + "_" : "";
return cs.writeJavaFields(prefix + extraPrefix, javaFieldsWriter, tsPosition);
return cs.writeJavaFields(state, prefix + extraPrefix, javaFieldsWriter, tsPosition);
}
String nameWithPrefix = prefix + name;

View File

@ -64,41 +64,41 @@ public class ConfigStructure {
/**
* This method writes a C header version of a data structure
*/
public void headerWrite(Writer cHeader) throws IOException {
if (comment != null) {
cHeader.write("/**" + ConfigDefinition.EOL + ConfigDefinition.packComment(comment, "") + ConfigDefinition.EOL + "*/" + ConfigDefinition.EOL);
public static void headerWrite(ConfigStructure configStructure, Writer cHeader) throws IOException {
if (configStructure.comment != null) {
cHeader.write("/**" + ConfigDefinition.EOL + ConfigDefinition.packComment(configStructure.comment, "") + ConfigDefinition.EOL + "*/" + ConfigDefinition.EOL);
}
cHeader.write("// start of " + name + ConfigDefinition.EOL);
cHeader.write("// start of " + configStructure.name + ConfigDefinition.EOL);
cHeader.write("typedef struct {" + ConfigDefinition.EOL);
bitState.reset();
for (int i = 0; i < cFields.size(); i++) {
ConfigField cf = cFields.get(i);
cHeader.write(cf.getHeaderText(currentOffset, bitState.get()));
ConfigField next = i == cFields.size() - 1 ? ConfigField.VOID : cFields.get(i + 1);
configStructure.bitState.reset();
for (int i = 0; i < configStructure.cFields.size(); i++) {
ConfigField cf = configStructure.cFields.get(i);
cHeader.write(cf.getHeaderText(configStructure.currentOffset, configStructure.bitState.get()));
ConfigField next = i == configStructure.cFields.size() - 1 ? ConfigField.VOID : configStructure.cFields.get(i + 1);
bitState.incrementBitIndex(cf, next);
currentOffset += cf.getSize(next);
configStructure.bitState.incrementBitIndex(cf, next);
configStructure.currentOffset += cf.getSize(next);
}
cHeader.write("\t/** total size " + currentOffset + "*/" + ConfigDefinition.EOL);
cHeader.write("} " + name + ";" + ConfigDefinition.EOL + ConfigDefinition.EOL);
cHeader.write("\t/** total size " + configStructure.currentOffset + "*/" + ConfigDefinition.EOL);
cHeader.write("} " + configStructure.name + ";" + ConfigDefinition.EOL + ConfigDefinition.EOL);
}
public int writeTunerStudio(ReaderState state, String prefix, Writer tsHeader, int tsPosition) throws IOException {
public int writeTunerStudio(String prefix, Writer tsHeader, int tsPosition) throws IOException {
FieldIterator fieldIterator = new FieldIterator();
for (int i = 0; i < tsFields.size(); i++) {
ConfigField next = i == tsFields.size() - 1 ? ConfigField.VOID : tsFields.get(i + 1);
ConfigField cf = tsFields.get(i);
tsPosition = cf.writeTunerStudio(state, prefix, tsHeader, tsPosition, next, fieldIterator.bitState.get());
tsPosition = cf.writeTunerStudio(prefix, tsHeader, tsPosition, next, fieldIterator.bitState.get());
fieldIterator.bitState.incrementBitIndex(cf, next);
}
return tsPosition;
}
public int writeJavaFields(String prefix, Writer javaFieldsWriter, int tsPosition) throws IOException {
public int writeJavaFields(ReaderState state, String prefix, Writer javaFieldsWriter, int tsPosition) throws IOException {
FieldIterator fieldIterator = new FieldIterator();
for (int i = 0; i < tsFields.size(); i++) {
ConfigField next = i == tsFields.size() - 1 ? ConfigField.VOID : tsFields.get(i + 1);

View File

@ -0,0 +1,11 @@
package com.rusefi;
import java.io.IOException;
public interface ConfigurationConsumer {
void startFile() throws IOException;
void endFile() throws IOException;
void handleEndStruct(ConfigStructure structure) throws IOException;
}

View File

@ -5,6 +5,8 @@ import java.util.Map;
import java.util.Stack;
/**
* We keep state here as we read configuration definition
*
* (c) Andrey Belomutskiy
* 12/19/18
*/
@ -12,6 +14,7 @@ public class ReaderState {
Stack<ConfigStructure> stack = new Stack<>();
public Map<String, Integer> tsCustomSize = new HashMap<>();
public Map<String, String> tsCustomLine = new HashMap<>();
public Map<String, ConfigStructure> structures = new HashMap<>();
public void ensureEmptyAfterProcessing() {
if (!this.stack.isEmpty())

View File

@ -12,8 +12,8 @@ public class TypesHelper {
public static int getElementSize(ReaderState state, String type) {
if (type == null)
return 0;
if (ConfigDefinition.structures.containsKey(type))
return ConfigDefinition.structures.get(type).totalSize;
if (state != null && state.structures.containsKey(type))
return state.structures.get(type).totalSize;
if (state != null && state.tsCustomSize.containsKey(type))
return state.tsCustomSize.get(type);
if (type.equals(UINT8_T))