From 7e02210cb870c1d0fd1674ad0e75242a3f465db3 Mon Sep 17 00:00:00 2001 From: rusefi Date: Fri, 14 Feb 2020 22:44:59 -0500 Subject: [PATCH] adding flexibility --- .../com/rusefi/output/TSProjectConsumer.java | 12 +++-- .../src/com/rusefi/test/BitParsingTest.java | 50 +++++++++++++++++++ .../src/com/rusefi/util/LazyFile.java | 4 +- .../src/com/rusefi/util/Output.java | 9 ++++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 java_tools/configuration_definition/src/com/rusefi/test/BitParsingTest.java create mode 100644 java_tools/enum_to_string/src/com/rusefi/util/Output.java diff --git a/java_tools/configuration_definition/src/com/rusefi/output/TSProjectConsumer.java b/java_tools/configuration_definition/src/com/rusefi/output/TSProjectConsumer.java index ada71f1d87..0a61b7897a 100644 --- a/java_tools/configuration_definition/src/com/rusefi/output/TSProjectConsumer.java +++ b/java_tools/configuration_definition/src/com/rusefi/output/TSProjectConsumer.java @@ -2,6 +2,7 @@ package com.rusefi.output; import com.rusefi.*; import com.rusefi.util.LazyFile; +import com.rusefi.util.Output; import com.rusefi.util.SystemOut; import java.io.*; @@ -99,13 +100,17 @@ public class TSProjectConsumer implements ConfigurationConsumer { return tsPosition; } - private void writeTunerStudioFile(String tsPath, String fieldsSection) throws IOException { + protected void writeTunerStudioFile(String tsPath, String fieldsSection) throws IOException { TsFileContent tsContent = readTsFile(tsPath); SystemOut.println("Got " + tsContent.getPrefix().length() + "/" + tsContent.getPostfix().length() + " of " + TS_FILE_INPUT_NAME); // File.getPath() would eliminate potential separator at the end of the path String fileName = new File(tsPath).getPath() + File.separator + TS_FILE_OUTPUT_NAME; - LazyFile tsHeader = new LazyFile(fileName); + Output tsHeader = new LazyFile(fileName); + writeContent(fieldsSection, tsContent, tsHeader); + } + + protected void writeContent(String fieldsSection, TsFileContent tsContent, Output tsHeader) throws IOException { tsHeader.write(tsContent.getPrefix()); tsHeader.write("; " + CONFIG_DEFINITION_START + ConfigDefinition.EOL); @@ -184,8 +189,7 @@ public class TSProjectConsumer implements ConfigurationConsumer { } @Override - public void startFile() throws IOException { - + public void startFile() { } @Override diff --git a/java_tools/configuration_definition/src/com/rusefi/test/BitParsingTest.java b/java_tools/configuration_definition/src/com/rusefi/test/BitParsingTest.java new file mode 100644 index 0000000000..0662dc3508 --- /dev/null +++ b/java_tools/configuration_definition/src/com/rusefi/test/BitParsingTest.java @@ -0,0 +1,50 @@ +package com.rusefi.test; + +import com.rusefi.ReaderState; +import com.rusefi.TsFileContent; +import com.rusefi.output.JavaFieldsConsumer; +import com.rusefi.output.TSProjectConsumer; +import com.rusefi.util.Output; +import org.junit.Test; + +import java.io.*; +import java.util.Arrays; + +public class BitParsingTest { + @Test + public void testBitParser() throws IOException { + ReaderState state = new ReaderState(); + + String inputString = "struct pid_s\nbit activateAuxPid1;\n" + + "bit fieldName;\n" + + "end_struct\n"; + BufferedReader reader = new BufferedReader(new StringReader(inputString)); + + StringWriter sw = new StringWriter(); + + TSProjectConsumer javaFieldsConsumer = new TSProjectConsumer(new CharArrayWriter(), "", state) { + @Override + protected void writeTunerStudioFile(String tsPath, String fieldsSection) throws IOException { + writeContent(fieldsSection, new TsFileContent("", ""), createOutput(sw)); + } + }; + state.readBufferedReader(reader, Arrays.asList(javaFieldsConsumer)); + + System.out.printf("start[" + sw.toString() + "]end"); + + } + + private Output createOutput(StringWriter sw) { + return new Output() { + @Override + public void write(String line) { + sw.write(line); + } + + @Override + public void close() { + + } + }; + } +} diff --git a/java_tools/enum_to_string/src/com/rusefi/util/LazyFile.java b/java_tools/enum_to_string/src/com/rusefi/util/LazyFile.java index 2265c65cb7..88d6162385 100644 --- a/java_tools/enum_to_string/src/com/rusefi/util/LazyFile.java +++ b/java_tools/enum_to_string/src/com/rusefi/util/LazyFile.java @@ -8,7 +8,7 @@ import java.util.regex.Pattern; /** * This file would override file content only of content has changed, disregarding the magic tag line. */ -public class LazyFile { +public class LazyFile implements Output { public static final String LAZY_FILE_TAG = "was generated automatically by rusEfi tool "; private static final String PROPERTY_NAME = "rusefi.generator.lazyfile.enabled"; private static boolean ENABLED = Boolean.getBoolean(PROPERTY_NAME); @@ -26,12 +26,14 @@ public class LazyFile { this.filename = filename; } + @Override public void write(String line) { content.append(line); if (!line.contains(LAZY_FILE_TAG)) contentWithoutTag.append(line); } + @Override public void close() throws IOException { String fileContent = unifySpaces(readCurrentContent(filename)); String newContent = unifySpaces(contentWithoutTag.toString().trim()); diff --git a/java_tools/enum_to_string/src/com/rusefi/util/Output.java b/java_tools/enum_to_string/src/com/rusefi/util/Output.java new file mode 100644 index 0000000000..10b0a4f3a2 --- /dev/null +++ b/java_tools/enum_to_string/src/com/rusefi/util/Output.java @@ -0,0 +1,9 @@ +package com.rusefi.util; + +import java.io.IOException; + +public interface Output { + void write(String line); + + void close() throws IOException; +}