summaryrefslogtreecommitdiff
path: root/dslog/LogSet.java
blob: e284eba37c7c544b557849ede97f809a6b6a95d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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:   (?<matchname>[A-Za-z].+) - (?<matchnumber>[0-9:]+), Field Time: (?<fieldtime>[0-9/]+ [0-9:]+)$");

	public String[] matches() throws IOException {
		ArrayList<String> matches = new ArrayList<String>();
		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));
	}
}