extracting opensr5
This commit is contained in:
parent
3d1f560ed7
commit
1e81281db0
|
@ -0,0 +1,33 @@
|
|||
package com.opensr5;
|
||||
|
||||
import com.opensr5.io.IniFileMetaInfo;
|
||||
import com.opensr5.io.IniFileReader;
|
||||
import com.opensr5.io.RawIniFile;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("OpenSR5 - load/program tune via serial port utility");
|
||||
System.out.println(" (c) 2017 Andrey Belomutskiy");
|
||||
System.out.println(" https://github.com/rusefi/opensr5_flash");
|
||||
System.out.flush();
|
||||
|
||||
if (args.length < 1) {
|
||||
System.err.println("Usage:");
|
||||
System.err.println("opensr5 DEFINITION_FILE.INI");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
String projectIniFileName = args[0];
|
||||
|
||||
RawIniFile content = IniFileReader.read(new File(projectIniFileName));
|
||||
|
||||
IniFileMetaInfo meta = new IniFileMetaInfo(content);
|
||||
|
||||
System.out.println("nPages = " + meta.getnPages());
|
||||
System.out.println("blockingFactor = " + meta.getBlockingFactor());
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.opensr5.io;
|
||||
|
||||
/**
|
||||
* (c) Andrey Belomutskiy
|
||||
* 3/1/2017
|
||||
*/
|
||||
public class BasicBinaryProtocol {
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.opensr5.io;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* (c) Andrey Belomutskiy
|
||||
* 3/1/2017
|
||||
*/
|
||||
public class IniFileMetaInfo {
|
||||
public static final int DEFAULT_BLOCKING_FACTOR = 16000;
|
||||
private final int nPages;
|
||||
|
||||
private int totalSize;
|
||||
private final List<Integer> pageSizes = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* read maximum chunk size
|
||||
*/
|
||||
private final int blockingFactor; // todo: this is probably an optional propery, come up with some default
|
||||
|
||||
public IniFileMetaInfo(RawIniFile file) {
|
||||
|
||||
nPages = file.getSimpleIntegerProperty("nPages");
|
||||
|
||||
blockingFactor = file.getSimpleIntegerProperty("blockingFactor", DEFAULT_BLOCKING_FACTOR);
|
||||
|
||||
|
||||
RawIniFile.Line pageSize = file.getMandatoryLine("pageSize");
|
||||
|
||||
List<String> individualPageSizes = Arrays.asList(pageSize.getTokens()).subList(1, pageSize.getTokens().length);
|
||||
|
||||
if (individualPageSizes.size() != nPages)
|
||||
throw new IllegalStateException("Unexpected individual sizes: " + individualPageSizes);
|
||||
|
||||
for (String value : individualPageSizes) {
|
||||
int size = Integer.parseInt(value);
|
||||
pageSizes.add(size);
|
||||
totalSize += size;
|
||||
}
|
||||
}
|
||||
|
||||
public int getnPages() {
|
||||
return nPages;
|
||||
}
|
||||
|
||||
public int getBlockingFactor() {
|
||||
return blockingFactor;
|
||||
}
|
||||
|
||||
public int getTotalSize() {
|
||||
return totalSize;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package com.opensr5.io;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* (c) Andrey Belomutskiy
|
||||
|
@ -26,7 +28,7 @@ public class IniFileReader {
|
|||
*
|
||||
* </pre>
|
||||
*/
|
||||
public static String[] split(String str) {
|
||||
public static String[] splitTokens(String str) {
|
||||
ArrayList<String> strings = new ArrayList<>();
|
||||
boolean inQuote = false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
@ -51,4 +53,32 @@ public class IniFileReader {
|
|||
private static boolean isTokenSeparator(int c) {
|
||||
return c == ' ' || c == '\t' || c == '=' || c == ',';
|
||||
}
|
||||
|
||||
public static RawIniFile read(InputStream in) {
|
||||
List<RawIniFile.Line> lines = new ArrayList<>();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||
|
||||
try {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (line.trim().isEmpty() || RawIniFile.Line.isCommentLine(line)) {
|
||||
// let's skip comments right here
|
||||
continue;
|
||||
}
|
||||
lines.add(new RawIniFile.Line(line));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return new RawIniFile(lines);
|
||||
}
|
||||
|
||||
public static RawIniFile read(File input) {
|
||||
try {
|
||||
InputStream in = new FileInputStream(input);
|
||||
return read(in);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
package com.opensr5.io;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* (c) Andrey Belomutskiy
|
||||
* 3/1/2017
|
||||
*/
|
||||
public class RawIniFile {
|
||||
/**
|
||||
* A list of lines. Lines which are only a comment were filtered out already.
|
||||
*/
|
||||
private final List<Line> lines;
|
||||
|
||||
/**
|
||||
* Often we want to look-up line by first token.
|
||||
* Each line in this map has at least two tokens
|
||||
*/
|
||||
private final Map<String, Line> asSet = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
public RawIniFile(List<Line> lines) {
|
||||
this.lines = lines;
|
||||
|
||||
for (Line line : lines) {
|
||||
if (line.tokens.length > 1)
|
||||
asSet.put(line.tokens[0], line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public Line getMandatoryLine(String key) {
|
||||
Line result = getByKey(key);
|
||||
if (result == null)
|
||||
throw new IllegalStateException("Line not found: " + key);
|
||||
assert result.tokens.length > 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Line getByKey(String key) {
|
||||
return asSet.get(key);
|
||||
}
|
||||
|
||||
public List<Line> getLines() {
|
||||
return lines;
|
||||
}
|
||||
|
||||
public int getSimpleIntegerProperty(String key) {
|
||||
Line line = asSet.get(key);
|
||||
if (line == null)
|
||||
throw new IllegalStateException("Line not found: " + key);
|
||||
String value = line.getTokens()[1];
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
public int getSimpleIntegerProperty(String key, int defaultValue) {
|
||||
if (!asSet.containsKey(key))
|
||||
return defaultValue;
|
||||
return getSimpleIntegerProperty(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Immutable representation of since ini file line
|
||||
*/
|
||||
public static class Line {
|
||||
private String rawText;
|
||||
private String[] tokens;
|
||||
|
||||
public Line(String rawText) {
|
||||
this.rawText = rawText;
|
||||
tokens = IniFileReader.splitTokens(rawText);
|
||||
}
|
||||
|
||||
public String[] getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
public static boolean isCommentLine(String rawText) {
|
||||
return rawText.trim().startsWith(";");
|
||||
}
|
||||
|
||||
public String getRawText() {
|
||||
return rawText;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +1,12 @@
|
|||
package com.opensr5.io.test;
|
||||
|
||||
import com.opensr5.io.IniFileMetaInfo;
|
||||
import com.opensr5.io.IniFileReader;
|
||||
import com.opensr5.io.RawIniFile;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
|
@ -13,31 +17,32 @@ public class IniFileReaderTest {
|
|||
@Test
|
||||
public void testSplit() {
|
||||
{
|
||||
String[] s = IniFileReader.split("1");
|
||||
String[] s = IniFileReader.splitTokens("1");
|
||||
assertEquals(s.length, 1);
|
||||
assertEquals("1", s[0]);
|
||||
}
|
||||
{
|
||||
String[] s = IniFileReader.split("hello");
|
||||
String[] s = IniFileReader.splitTokens("hello");
|
||||
assertEquals(s.length, 1);
|
||||
assertEquals("hello", s[0]);
|
||||
}
|
||||
{
|
||||
String[] s = IniFileReader.split("\"hello\"");
|
||||
String[] s = IniFileReader.splitTokens("\"hello\"");
|
||||
assertEquals(s.length, 1);
|
||||
assertEquals("hello", s[0]);
|
||||
}
|
||||
{
|
||||
String[] s = IniFileReader.split("\"hello\",\"w\"");
|
||||
String[] s = IniFileReader.splitTokens("\"hello\",\"w\"");
|
||||
assertEquals(s.length, 2);
|
||||
assertEquals("hello", s[0]);
|
||||
assertEquals("w", s[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuotedTokens() {
|
||||
{
|
||||
String[] result = IniFileReader.split("\"hel lo\"");
|
||||
String[] result = IniFileReader.splitTokens("\"hel lo\"");
|
||||
assertEquals(result.length, 1);
|
||||
assertEquals("hel lo", result[0]);
|
||||
}
|
||||
|
@ -45,11 +50,26 @@ public class IniFileReaderTest {
|
|||
|
||||
@Test
|
||||
public void testRealLine() {
|
||||
String[] result = IniFileReader.split("\tdialog = engineChars,\t\"Base Engine Settings\"");
|
||||
String[] result = IniFileReader.splitTokens("\tdialog = engineChars,\t\"Base Engine Settings\"");
|
||||
assertEquals(result.length, 3);
|
||||
|
||||
assertEquals("dialog", result[0]);
|
||||
assertEquals("engineChars", result[1]);
|
||||
assertEquals("Base Engine Settings", result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTotalPagesSize() {
|
||||
String string = " nPages = 3\n" +
|
||||
" pageSize = 288, 64, 288\n";
|
||||
|
||||
|
||||
RawIniFile content = IniFileReader.read(new ByteArrayInputStream(string.getBytes()));
|
||||
|
||||
IniFileMetaInfo meta = new IniFileMetaInfo(content);
|
||||
|
||||
assertEquals(3, meta.getnPages());
|
||||
assertEquals(IniFileMetaInfo.DEFAULT_BLOCKING_FACTOR, meta.getBlockingFactor());
|
||||
assertEquals(640, meta.getTotalSize());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.rusefi.ui.config;
|
||||
|
||||
import com.opensr5.io.IniFileReader;
|
||||
import com.opensr5.io.RawIniFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
@ -29,29 +30,19 @@ public class IniFileModel {
|
|||
|
||||
private void readIniFile() {
|
||||
File input = new File(FILENAME);
|
||||
BufferedReader d = null;
|
||||
try {
|
||||
d = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
|
||||
} catch (FileNotFoundException e) {
|
||||
if (!input.exists()) {
|
||||
System.out.println("No such file: " + FILENAME);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
String line;
|
||||
|
||||
State state = State.SKIPPING;
|
||||
|
||||
while ((line = d.readLine()) != null) {
|
||||
handleLine(line);
|
||||
RawIniFile content = IniFileReader.read(input);
|
||||
|
||||
|
||||
}
|
||||
finishDialog();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
for (RawIniFile.Line line : content.getLines()) {
|
||||
handleLine(line);
|
||||
}
|
||||
|
||||
finishDialog();
|
||||
}
|
||||
|
||||
private void finishDialog() {
|
||||
|
@ -65,9 +56,9 @@ public class IniFileModel {
|
|||
fields.clear();
|
||||
}
|
||||
|
||||
private void handleLine(String line) {
|
||||
private void handleLine(RawIniFile.Line line) {
|
||||
try {
|
||||
LinkedList<String> list = new LinkedList<>(Arrays.asList(IniFileReader.split(line)));
|
||||
LinkedList<String> list = new LinkedList<>(Arrays.asList(line.getTokens()));
|
||||
|
||||
trim(list);
|
||||
|
||||
|
@ -82,7 +73,7 @@ public class IniFileModel {
|
|||
handleField(list);
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
throw new IllegalStateException("While [" + line + "]", e);
|
||||
throw new IllegalStateException("While [" + line.getRawText() + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue