package dslog; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.ArrayList; public class LogSet { public final String name; public LogSet(String name) { this.name = name; } public Path dslogPath, dseventsPath, pdplogPath; public String toString() { return name+" "+verstr(this::dslog)+"/"+verstr(this::dsevents)+"/"+verstr(this::pdplog); } private static final Pattern fmsConnected = Pattern.compile("^FMS Connected: (?[A-Za-z].+) - (?[0-9:]+), Field Time: (?[0-9/]+ [0-9:]+)$"); public String[] matches() throws IOException { ArrayList matches = new ArrayList(); try (DseventsReader dsevents = dsevents()) { if (dsevents == null) { return matches.toArray(new String[matches.size()]); } while (dsevents.hasNext()) { DseventsReader.Event event = dsevents.next(); // do a (cheap) `.startsWith` check before doing an (expensive) regex check. if (event.message.startsWith("FMS Connected:")) { Matcher m = fmsConnected.matcher(event.message); if (m.matches()) { matches.add(m.group("matchname") + " - " + m.group("matchnumber")); } } } } return matches.toArray(new String[matches.size()]); } private static String verstr(Versioned.Supplier getter) { try (Versioned thing = getter.get()) { if (thing == null) { return "␕"; } return ""+thing.version(); } catch (IOException e) { return "☠"; } } public DslogReader dslog() throws IOException { if (dslogPath == null) { return null; } return new DslogReader(Files.newInputStream(dslogPath)); } public DseventsReader dsevents() throws IOException { if (dseventsPath == null) { return null; } return new DseventsReader(Files.newInputStream(dseventsPath)); } public PdplogReader pdplog() throws IOException { if (pdplogPath == null) { return null; } return new PdplogReader(Files.newInputStream(pdplogPath)); } }