rusefi/java_tools/configuration_definition/src/main/java/com/rusefi/output/BaseCHeaderConsumer.java

89 lines
3.3 KiB
Java
Raw Normal View History

2021-02-10 17:36:56 -08:00
package com.rusefi.output;
import com.rusefi.*;
import static com.rusefi.ToolUtil.EOL;
2021-02-10 17:36:56 -08:00
2022-04-15 10:47:14 -07:00
public class BaseCHeaderConsumer implements ConfigurationConsumer {
2021-02-10 17:36:56 -08:00
private static final String BOOLEAN_TYPE = "bool";
private final StringBuilder content = new StringBuilder();
2021-12-11 01:04:15 -08:00
private static String getHeaderText(FieldIteratorWithOffset iterator) {
ConfigField configField = iterator.cf;
2021-02-10 17:36:56 -08:00
if (configField.isBit()) {
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;
2021-02-10 17:36:56 -08:00
}
String cEntry = getComment(configField.getCommentContent(), iterator.currentOffset, configField.getUnits());
2021-02-10 17:36:56 -08:00
String typeName = configField.getType();
String autoscaleSpec = configField.autoscaleSpec();
if (autoscaleSpec != null) {
typeName = "scaled_channel<" + typeName + ", " + autoscaleSpec + ">";
}
2021-02-10 17:36:56 -08:00
if (!configField.isArray()) {
// not an array
cEntry += "\t" + typeName + " " + configField.getName();
2021-02-10 17:36:56 -08:00
if (ConfigDefinition.needZeroInit && TypesHelper.isPrimitive(configField.getType())) {
// we need this cast in case of enums
cEntry += " = (" + configField.getType() + ")0";
}
cEntry += ";" + EOL;
} else {
cEntry += "\t" + typeName + " " + configField.getName() + "[" + configField.arraySizeVariableName + "];" + EOL;
2021-02-10 17:36:56 -08:00
}
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;
}
2021-02-10 17:36:56 -08:00
@Override
2021-12-12 19:42:46 -08:00
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) {
2021-02-10 17:36:56 -08:00
if (structure.comment != null) {
content.append("/**" + EOL + packComment(structure.comment, "") + EOL + "*/" + EOL);
2021-02-10 17:36:56 -08:00
}
content.append("// start of " + structure.name + EOL);
content.append("struct " + structure.name + " {" + EOL);
2021-12-11 01:04:15 -08:00
FieldIteratorWithOffset iterator = new FieldIteratorWithOffset(structure.cFields);
2021-02-10 17:36:56 -08:00
for (int i = 0; i < structure.cFields.size(); i++) {
iterator.start(i);
2021-12-11 01:04:15 -08:00
content.append(getHeaderText(iterator));
2021-02-10 17:36:56 -08:00
2021-12-11 01:04:15 -08:00
iterator.currentOffset += iterator.cf.getSize(iterator.next);
iterator.end();
2021-02-10 17:36:56 -08:00
}
2021-12-11 01:04:15 -08:00
content.append("\t/** total size " + iterator.currentOffset + "*/" + EOL);
2021-02-10 17:36:56 -08:00
content.append("};" + EOL + EOL);
}
public StringBuilder getContent() {
return content;
}
}