2023-10-14 09:17:23 -07:00
|
|
|
package com.rusefi.can.reader.impl;
|
|
|
|
|
|
|
|
import com.rusefi.can.CANPacket;
|
|
|
|
import com.rusefi.can.reader.CANLineReader;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.List;
|
|
|
|
|
2023-12-30 16:22:37 -08:00
|
|
|
public class AutoFormatReader implements CANLineReader {
|
|
|
|
public static final CANLineReader INSTANCE = new AutoFormatReader();
|
2023-10-14 09:17:23 -07:00
|
|
|
|
|
|
|
private CANLineReader delegate;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CANPacket readLine(String line) {
|
|
|
|
return delegate.readLine(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-12-15 07:38:17 -08:00
|
|
|
public CANPacket readLine(String line, String fileName, int lineIndex) {
|
|
|
|
return delegate.readLine(line, fileName, lineIndex);
|
2023-10-14 09:17:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<CANPacket> readFile(String fileName) throws IOException {
|
|
|
|
String firstLine = Files.lines(Paths.get(fileName)).findFirst().get();
|
2023-12-30 16:22:37 -08:00
|
|
|
if (!firstLine.contains(PcanTrcReader2_0.FILEVERSION) && !firstLine.contains(CanHackerReader.HEADER))
|
2023-10-14 09:17:23 -07:00
|
|
|
throw new IllegalStateException(PcanTrcReader2_0.FILEVERSION + " expected in first line");
|
2023-12-30 16:22:37 -08:00
|
|
|
if (firstLine.contains(CanHackerReader.HEADER)) {
|
|
|
|
delegate = CanHackerReader.INSTANCE;
|
|
|
|
} else if (firstLine.contains("1.1")) {
|
2023-10-14 09:17:23 -07:00
|
|
|
delegate = PcanTrcReader1_1.INSTANCE;
|
|
|
|
} else if (firstLine.contains("2.0")) {
|
|
|
|
delegate = PcanTrcReader2_0.INSTANCE;
|
|
|
|
} else {
|
|
|
|
throw new IllegalStateException("Unsupported version in " + firstLine);
|
|
|
|
}
|
|
|
|
return delegate.readFile(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<CANPacket> skipHeaderAndRead(String fileName, int skipCount) throws IOException {
|
|
|
|
return delegate.skipHeaderAndRead(fileName, skipCount);
|
|
|
|
}
|
|
|
|
}
|