refactoring - splitting class a bit further

This commit is contained in:
rusefillc 2022-01-04 00:25:49 -05:00
parent c95948c143
commit 26a70b8107
7 changed files with 95 additions and 83 deletions

Binary file not shown.

View File

@ -16,7 +16,6 @@ import java.util.*;
* *
* @see ConfigurationConsumer * @see ConfigurationConsumer
*/ */
@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
public class ConfigDefinition { public class ConfigDefinition {
static final String SIGNATURE_HASH = "SIGNATURE_HASH"; static final String SIGNATURE_HASH = "SIGNATURE_HASH";
private static String TS_OUTPUTS_DESTINATION = null; private static String TS_OUTPUTS_DESTINATION = null;
@ -201,7 +200,7 @@ public class ConfigDefinition {
// Add the variable for the config signature // Add the variable for the config signature
long crc32 = IoUtil.signatureHash(state, parseState, tsInputFileFolder, inputFiles); long crc32 = IoUtil.signatureHash(state, parseState, tsInputFileFolder, inputFiles);
handleFiringOrder(firingEnumFileName, state.variableRegistry, parseState); ExtraUtil.handleFiringOrder(firingEnumFileName, state.variableRegistry, parseState);
MESSAGE = ToolUtil.getGeneratedAutomaticallyTag() + definitionInputFile + " " + new Date(); MESSAGE = ToolUtil.getGeneratedAutomaticallyTag() + definitionInputFile + " " + new Date();
@ -279,79 +278,11 @@ public class ConfigDefinition {
state.readBufferedReader(definitionReader, destinations); state.readBufferedReader(definitionReader, destinations);
if (destCDefinesFileName != null) { if (destCDefinesFileName != null) {
writeDefinesToFile(state.variableRegistry, destCDefinesFileName); ExtraUtil.writeDefinesToFile(state.variableRegistry, destCDefinesFileName);
} }
if (romRaiderDestination != null && romRaiderInputFile != null) { if (romRaiderDestination != null && romRaiderInputFile != null) {
processTextTemplate(state, romRaiderInputFile, romRaiderDestination); ExtraUtil.processTextTemplate(state, romRaiderInputFile, romRaiderDestination);
} }
} }
private static void handleFiringOrder(String firingEnumFileName, VariableRegistry variableRegistry, ParseState parseState) throws IOException {
if (firingEnumFileName != null) {
SystemOut.println("Reading firing from " + firingEnumFileName);
String result = FiringOrderTSLogic.invoke(firingEnumFileName);
variableRegistry.register("FIRINGORDER", result);
parseState.addDefinition("FIRINGORDER", result, Definition.OverwritePolicy.NotAllowed);
}
}
private static void processTextTemplate(ReaderState state, String inputFileName, String outputFileName) throws IOException {
SystemOut.println("Reading from " + inputFileName);
SystemOut.println("Writing to " + outputFileName);
state.variableRegistry.put("generator_message", ToolUtil.getGeneratedAutomaticallyTag() + new Date());
File inputFile = new File(inputFileName);
BufferedReader fr = new BufferedReader(new FileReader(inputFile));
LazyFile fw = new LazyFile(outputFileName);
String line;
while ((line = fr.readLine()) != null) {
line = state.variableRegistry.applyVariables(line);
fw.write(line + ToolUtil.EOL);
}
fw.close();
}
public static String getComment(String comment, int currentOffset, String units) {
String start = "\t/**";
String packedComment = packComment(comment, "\t");
String unitsComment = units.isEmpty() ? "" : "\t" + units + ToolUtil.EOL;
return start + ToolUtil.EOL +
packedComment +
unitsComment +
"\t * offset " + currentOffset + ToolUtil.EOL + "\t */" + ToolUtil.EOL;
}
public static String packComment(String comment, String linePrefix) {
if (comment == null)
return "";
if (comment.trim().isEmpty())
return "";
String result = "";
for (String line : comment.split("\\\\n")) {
result += linePrefix + " * " + line + ToolUtil.EOL;
}
return result;
}
public static int getSize(VariableRegistry variableRegistry, String s) {
if (variableRegistry.intValues.containsKey(s)) {
return variableRegistry.intValues.get(s);
}
return Integer.parseInt(s);
}
public static void writeDefinesToFile(VariableRegistry variableRegistry, String fileName) throws IOException {
SystemOut.println("Writing to " + fileName);
LazyFile cHeader = new LazyFile(fileName);
cHeader.write("//\n// " + ToolUtil.getGeneratedAutomaticallyTag() + definitionInputFile + "\n//\n\n");
cHeader.write(variableRegistry.getDefinesSection());
cHeader.close();
}
} }

View File

@ -85,6 +85,13 @@ public class ConfigField {
this.isIterate = isIterate; this.isIterate = isIterate;
} }
private static int getSize(VariableRegistry variableRegistry, String s) {
if (variableRegistry.intValues.containsKey(s)) {
return variableRegistry.intValues.get(s);
}
return Integer.parseInt(s);
}
public boolean isArray() { public boolean isArray() {
return arraySizeVariableName != null || arraySizes.length != 0; return arraySizeVariableName != null || arraySizes.length != 0;
} }
@ -143,12 +150,12 @@ public class ConfigField {
if (matcher.group(5) != null) { if (matcher.group(5) != null) {
arraySizeAsText = matcher.group(3) + "][" + matcher.group(5); arraySizeAsText = matcher.group(3) + "][" + matcher.group(5);
arraySizes = new int[2]; arraySizes = new int[2];
arraySizes[0] = ConfigDefinition.getSize(state.variableRegistry, matcher.group(3)); arraySizes[0] = getSize(state.variableRegistry, matcher.group(3));
arraySizes[1] = ConfigDefinition.getSize(state.variableRegistry, matcher.group(5)); arraySizes[1] = getSize(state.variableRegistry, matcher.group(5));
} else if (matcher.group(3) != null) { } else if (matcher.group(3) != null) {
arraySizeAsText = matcher.group(3); arraySizeAsText = matcher.group(3);
arraySizes = new int[1]; arraySizes = new int[1];
arraySizes[0] = ConfigDefinition.getSize(state.variableRegistry, arraySizeAsText); arraySizes[0] = getSize(state.variableRegistry, arraySizeAsText);
} else { } else {
arraySizes = new int[0]; arraySizes = new int[0];
arraySizeAsText = null; arraySizeAsText = null;

View File

@ -0,0 +1,52 @@
package com.rusefi;
import com.rusefi.newparse.ParseState;
import com.rusefi.newparse.parsing.Definition;
import com.rusefi.util.LazyFile;
import com.rusefi.util.SystemOut;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
public class ExtraUtil {
static void handleFiringOrder(String firingEnumFileName, VariableRegistry variableRegistry, ParseState parseState) throws IOException {
if (firingEnumFileName != null) {
SystemOut.println("Reading firing from " + firingEnumFileName);
String result = FiringOrderTSLogic.invoke(firingEnumFileName);
variableRegistry.register("FIRINGORDER", result);
parseState.addDefinition("FIRINGORDER", result, Definition.OverwritePolicy.NotAllowed);
}
}
static void processTextTemplate(ReaderState state, String inputFileName, String outputFileName) throws IOException {
SystemOut.println("Reading from " + inputFileName);
SystemOut.println("Writing to " + outputFileName);
state.variableRegistry.put("generator_message", ToolUtil.getGeneratedAutomaticallyTag() + new Date());
File inputFile = new File(inputFileName);
BufferedReader fr = new BufferedReader(new FileReader(inputFile));
LazyFile fw = new LazyFile(outputFileName);
String line;
while ((line = fr.readLine()) != null) {
line = state.variableRegistry.applyVariables(line);
fw.write(line + ToolUtil.EOL);
}
fw.close();
}
public static void writeDefinesToFile(VariableRegistry variableRegistry, String fileName) throws IOException {
SystemOut.println("Writing to " + fileName);
LazyFile cHeader = new LazyFile(fileName);
cHeader.write("//\n// " + ToolUtil.getGeneratedAutomaticallyTag() + ConfigDefinition.definitionInputFile + "\n//\n\n");
cHeader.write(variableRegistry.getDefinesSection());
cHeader.close();
}
}

View File

@ -11,11 +11,11 @@ public abstract class BaseCHeaderConsumer extends AbstractConfigurationConsumer
private static String getHeaderText(FieldIteratorWithOffset iterator) { private static String getHeaderText(FieldIteratorWithOffset iterator) {
ConfigField configField = iterator.cf; ConfigField configField = iterator.cf;
if (configField.isBit()) { if (configField.isBit()) {
String comment = "\t/**" + EOL + ConfigDefinition.packComment(configField.getCommentContent(), "\t") + "\toffset " + iterator.currentOffset + " bit " + iterator.bitState.get() + " */" + EOL; String comment = "\t/**" + EOL + packComment(configField.getCommentContent(), "\t") + "\toffset " + iterator.currentOffset + " bit " + iterator.bitState.get() + " */" + EOL;
return comment + "\t" + BOOLEAN_TYPE + " " + configField.getName() + " : 1 {};" + EOL; return comment + "\t" + BOOLEAN_TYPE + " " + configField.getName() + " : 1 {};" + EOL;
} }
String cEntry = ConfigDefinition.getComment(configField.getCommentContent(), iterator.currentOffset, configField.getUnits()); String cEntry = getComment(configField.getCommentContent(), iterator.currentOffset, configField.getUnits());
String typeName = configField.getType(); String typeName = configField.getType();
@ -38,10 +38,32 @@ public abstract class BaseCHeaderConsumer extends AbstractConfigurationConsumer
return cEntry; return cEntry;
} }
private static String getComment(String comment, int currentOffset, String units) {
String start = "\t/**";
String packedComment = packComment(comment, "\t");
String unitsComment = units.isEmpty() ? "" : "\t" + units + EOL;
return start + EOL +
packedComment +
unitsComment +
"\t * offset " + currentOffset + EOL + "\t */" + EOL;
}
public static String packComment(String comment, String linePrefix) {
if (comment == null)
return "";
if (comment.trim().isEmpty())
return "";
String result = "";
for (String line : comment.split("\\\\n")) {
result += linePrefix + " * " + line + EOL;
}
return result;
}
@Override @Override
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) { public void handleEndStruct(ReaderState readerState, ConfigStructure structure) {
if (structure.comment != null) { if (structure.comment != null) {
content.append("/**" + EOL + ConfigDefinition.packComment(structure.comment, "") + EOL + "*/" + EOL); content.append("/**" + EOL + packComment(structure.comment, "") + EOL + "*/" + EOL);
} }
content.append("// start of " + structure.name + EOL); content.append("// start of " + structure.name + EOL);

View File

@ -20,6 +20,6 @@ public class SignatureConsumer extends AbstractConfigurationConsumer {
@Override @Override
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException { public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
ConfigDefinition.writeDefinesToFile(registry, destHeader); ExtraUtil.writeDefinesToFile(registry, destHeader);
} }
} }

View File

@ -1,6 +1,6 @@
package com.rusefi.test; package com.rusefi.test;
import com.rusefi.ConfigDefinition; import com.rusefi.output.BaseCHeaderConsumer;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -8,9 +8,9 @@ import static org.junit.Assert.assertEquals;
public class ConfigDefinitionOutputTest { public class ConfigDefinitionOutputTest {
@Test @Test
public void testComment() { public void testComment() {
assertEquals("", ConfigDefinition.packComment("", "\t")); assertEquals("", BaseCHeaderConsumer.packComment("", "\t"));
assertEquals("\t * abc\n", ConfigDefinition.packComment("abc", "\t")); assertEquals("\t * abc\n", BaseCHeaderConsumer.packComment("abc", "\t"));
assertEquals("\t * abc\n" + assertEquals("\t * abc\n" +
"\t * vbn\n", ConfigDefinition.packComment("abc\\nvbn", "\t")); "\t * vbn\n", BaseCHeaderConsumer.packComment("abc\\nvbn", "\t"));
} }
} }