refactoring
This commit is contained in:
parent
5e96883b15
commit
78c7cfe896
|
@ -0,0 +1,71 @@
|
||||||
|
package com.rusefi.output;
|
||||||
|
|
||||||
|
import com.rusefi.*;
|
||||||
|
|
||||||
|
import static com.rusefi.ConfigDefinition.EOL;
|
||||||
|
|
||||||
|
public abstract class BaseCHeaderConsumer implements ConfigurationConsumer {
|
||||||
|
private static final String BOOLEAN_TYPE = "bool";
|
||||||
|
private final StringBuilder content = new StringBuilder();
|
||||||
|
|
||||||
|
public static String getHeaderText(ConfigField configField, int currentOffset, int bitIndex) {
|
||||||
|
if (configField.isBit()) {
|
||||||
|
String comment = "\t/**" + EOL + ConfigDefinition.packComment(configField.getCommentContent(), "\t") + "\toffset " + currentOffset + " bit " + bitIndex + " */" + EOL;
|
||||||
|
return comment + "\t" + BOOLEAN_TYPE + " " + configField.getName() + " : 1;" + EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
String cEntry = ConfigDefinition.getComment(configField.getCommentContent(), currentOffset);
|
||||||
|
|
||||||
|
if (!configField.isArray()) {
|
||||||
|
// not an array
|
||||||
|
cEntry += "\t" + configField.getType() + " " + configField.getName();
|
||||||
|
if (ConfigDefinition.needZeroInit && TypesHelper.isPrimitive(configField.getType())) {
|
||||||
|
// we need this cast in case of enums
|
||||||
|
cEntry += " = (" + configField.getType() + ")0";
|
||||||
|
}
|
||||||
|
cEntry += ";" + EOL;
|
||||||
|
} else {
|
||||||
|
cEntry += "\t" + configField.getType() + " " + configField.getName() + "[" + configField.arraySizeVariableName + "];" + EOL;
|
||||||
|
}
|
||||||
|
return cEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleEndStruct(ConfigStructure structure) {
|
||||||
|
if (structure.comment != null) {
|
||||||
|
content.append("/**" + EOL + ConfigDefinition.packComment(structure.comment, "") + EOL + "*/" + EOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
content.append("// start of " + structure.name + EOL);
|
||||||
|
content.append("struct " + structure.name + " {" + EOL);
|
||||||
|
if (structure.isWithConstructor()) {
|
||||||
|
content.append("\t" + structure.name + "();" + EOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int currentOffset = 0;
|
||||||
|
|
||||||
|
BitState bitState = new BitState();
|
||||||
|
for (int i = 0; i < structure.cFields.size(); i++) {
|
||||||
|
ConfigField cf = structure.cFields.get(i);
|
||||||
|
content.append(BaseCHeaderConsumer.getHeaderText(cf, currentOffset, bitState.get()));
|
||||||
|
ConfigField next = i == structure.cFields.size() - 1 ? ConfigField.VOID : structure.cFields.get(i + 1);
|
||||||
|
|
||||||
|
bitState.incrementBitIndex(cf, next);
|
||||||
|
currentOffset += cf.getSize(next);
|
||||||
|
}
|
||||||
|
|
||||||
|
content.append("\t/** total size " + currentOffset + "*/" + EOL);
|
||||||
|
content.append("};" + EOL + EOL);
|
||||||
|
|
||||||
|
// https://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions
|
||||||
|
content.append("typedef struct " + structure.name + " " + structure.name + ";" + EOL + EOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringBuilder getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startFile() {
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,11 +11,9 @@ import static com.rusefi.ConfigDefinition.EOL;
|
||||||
/**
|
/**
|
||||||
* Configuration consumer which writes C header file
|
* Configuration consumer which writes C header file
|
||||||
*/
|
*/
|
||||||
public class CHeaderConsumer implements ConfigurationConsumer {
|
public class CHeaderConsumer extends BaseCHeaderConsumer {
|
||||||
public static final String BOOLEAN_TYPE = "bool";
|
|
||||||
public static boolean withC_Defines;
|
public static boolean withC_Defines;
|
||||||
private final LazyFile cHeader;
|
private final LazyFile cHeader;
|
||||||
private final StringBuilder content = new StringBuilder();
|
|
||||||
|
|
||||||
public CHeaderConsumer(String destCHeader) {
|
public CHeaderConsumer(String destCHeader) {
|
||||||
SystemOut.println("Writing C header to " + destCHeader);
|
SystemOut.println("Writing C header to " + destCHeader);
|
||||||
|
@ -23,79 +21,17 @@ public class CHeaderConsumer implements ConfigurationConsumer {
|
||||||
cHeader.write("// this section " + ConfigDefinition.MESSAGE + EOL);
|
cHeader.write("// this section " + ConfigDefinition.MESSAGE + EOL);
|
||||||
cHeader.write("// by " + getClass() + EOL);
|
cHeader.write("// by " + getClass() + EOL);
|
||||||
cHeader.write("// begin" + EOL);
|
cHeader.write("// begin" + EOL);
|
||||||
String id = destCHeader.replaceAll("[\\\\\\.\\/]", "_").toUpperCase();
|
|
||||||
cHeader.write("#pragma once" + EOL);
|
cHeader.write("#pragma once" + EOL);
|
||||||
cHeader.write("#include \"rusefi_types.h\"" + EOL);
|
cHeader.write("#include \"rusefi_types.h\"" + EOL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getHeaderText(ConfigField configField, int currentOffset, int bitIndex) {
|
|
||||||
if (configField.isBit()) {
|
|
||||||
String comment = "\t/**" + EOL + ConfigDefinition.packComment(configField.getCommentContent(), "\t") + "\toffset " + currentOffset + " bit " + bitIndex + " */" + EOL;
|
|
||||||
return comment + "\t" + BOOLEAN_TYPE + " " + configField.getName() + " : 1;" + EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
String cEntry = ConfigDefinition.getComment(configField.getCommentContent(), currentOffset);
|
|
||||||
|
|
||||||
if (!configField.isArray()) {
|
|
||||||
// not an array
|
|
||||||
cEntry += "\t" + configField.getType() + " " + configField.getName();
|
|
||||||
if (ConfigDefinition.needZeroInit && TypesHelper.isPrimitive(configField.getType())) {
|
|
||||||
// we need this cast in case of enums
|
|
||||||
cEntry += " = (" + configField.getType() + ")0";
|
|
||||||
}
|
|
||||||
cEntry += ";" + EOL;
|
|
||||||
} else {
|
|
||||||
cEntry += "\t" + configField.getType() + " " + configField.getName() + "[" + configField.arraySizeVariableName + "];" + EOL;
|
|
||||||
}
|
|
||||||
return cEntry;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startFile() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handleEndStruct(ConfigStructure structure) throws IOException {
|
|
||||||
if (structure.comment != null) {
|
|
||||||
content.append("/**" + EOL + ConfigDefinition.packComment(structure.comment, "") + EOL + "*/" + EOL);
|
|
||||||
}
|
|
||||||
|
|
||||||
content.append("// start of " + structure.name + EOL);
|
|
||||||
content.append("struct " + structure.name + " {" + EOL);
|
|
||||||
if (structure.isWithConstructor()) {
|
|
||||||
content.append("\t" + structure.name + "();" + EOL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int currentOffset = 0;
|
|
||||||
|
|
||||||
BitState bitState = new BitState();
|
|
||||||
for (int i = 0; i < structure.cFields.size(); i++) {
|
|
||||||
ConfigField cf = structure.cFields.get(i);
|
|
||||||
content.append(getHeaderText(cf, currentOffset, bitState.get()));
|
|
||||||
ConfigField next = i == structure.cFields.size() - 1 ? ConfigField.VOID : structure.cFields.get(i + 1);
|
|
||||||
|
|
||||||
bitState.incrementBitIndex(cf, next);
|
|
||||||
currentOffset += cf.getSize(next);
|
|
||||||
}
|
|
||||||
|
|
||||||
content.append("\t/** total size " + currentOffset + "*/" + EOL);
|
|
||||||
content.append("};" + EOL + EOL);
|
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions
|
|
||||||
content.append("typedef struct " + structure.name + " " + structure.name + ";" + EOL + EOL);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endFile() throws IOException {
|
public void endFile() throws IOException {
|
||||||
if (withC_Defines)
|
if (withC_Defines)
|
||||||
cHeader.write(VariableRegistry.INSTANCE.getDefinesSection());
|
cHeader.write(VariableRegistry.INSTANCE.getDefinesSection());
|
||||||
cHeader.write(content.toString());
|
cHeader.write(getContent().toString());
|
||||||
cHeader.write("// end" + EOL);
|
cHeader.write("// end" + EOL);
|
||||||
cHeader.write("// this section " + ConfigDefinition.MESSAGE + EOL);
|
cHeader.write("// this section " + ConfigDefinition.MESSAGE + EOL);
|
||||||
cHeader.close();
|
cHeader.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringBuilder getContent() {
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,7 @@ import com.rusefi.ConfigField;
|
||||||
import com.rusefi.ReaderState;
|
import com.rusefi.ReaderState;
|
||||||
import com.rusefi.TypesHelper;
|
import com.rusefi.TypesHelper;
|
||||||
import com.rusefi.VariableRegistry;
|
import com.rusefi.VariableRegistry;
|
||||||
import com.rusefi.output.CHeaderConsumer;
|
import com.rusefi.output.*;
|
||||||
import com.rusefi.output.FsioSettingsConsumer;
|
|
||||||
import com.rusefi.output.JavaFieldsConsumer;
|
|
||||||
import com.rusefi.output.TSProjectConsumer;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -303,7 +300,11 @@ public class ConfigFieldParserTest {
|
||||||
"";
|
"";
|
||||||
VariableRegistry.INSTANCE.clear();
|
VariableRegistry.INSTANCE.clear();
|
||||||
BufferedReader reader = new BufferedReader(new StringReader(test));
|
BufferedReader reader = new BufferedReader(new StringReader(test));
|
||||||
CHeaderConsumer consumer = new CHeaderConsumer("d");
|
BaseCHeaderConsumer consumer = new BaseCHeaderConsumer() {
|
||||||
|
@Override
|
||||||
|
public void endFile() {
|
||||||
|
}
|
||||||
|
};
|
||||||
new ReaderState().readBufferedReader(reader, Collections.singletonList(consumer));
|
new ReaderState().readBufferedReader(reader, Collections.singletonList(consumer));
|
||||||
assertEquals("// start of pid_s\n" +
|
assertEquals("// start of pid_s\n" +
|
||||||
"struct pid_s {\n" +
|
"struct pid_s {\n" +
|
||||||
|
|
Loading…
Reference in New Issue