2021-02-10 17:36:56 -08:00
package com.rusefi.output ;
import com.rusefi.* ;
2021-10-23 19:34:08 -07:00
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 ( ) ) {
2022-01-03 21:25:49 -08:00
String comment = " \ t/** " + EOL + packComment ( configField . getCommentContent ( ) , " \ t " ) + " \ toffset " + iterator . currentOffset + " bit " + iterator . bitState . get ( ) + " */ " + EOL ;
2021-11-19 19:19:34 -08:00
return comment + " \ t " + BOOLEAN_TYPE + " " + configField . getName ( ) + " : 1 {}; " + EOL ;
2021-02-10 17:36:56 -08:00
}
2022-01-03 21:25:49 -08:00
String cEntry = getComment ( configField . getCommentContent ( ) , iterator . currentOffset , configField . getUnits ( ) ) ;
2021-02-10 17:36:56 -08:00
2021-11-02 15:59:19 -07: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
2021-11-02 15:59:19 -07:00
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 {
2021-11-02 15:59:19 -07:00
cEntry + = " \ t " + typeName + " " + configField . getName ( ) + " [ " + configField . arraySizeVariableName + " ]; " + EOL ;
2021-02-10 17:36:56 -08:00
}
return cEntry ;
}
2022-01-03 21:25:49 -08:00
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 ) {
2022-01-03 21:25:49 -08:00
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 + + ) {
2021-11-28 10:13:13 -08:00
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 ) ;
2021-11-28 10:13:13 -08:00
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 ;
}
}