1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.diyefi.openlogviewer.decoder;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.util.Scanner;
30
31 import org.diyefi.openlogviewer.OpenLogViewer;
32 import org.diyefi.openlogviewer.genericlog.GenericLog;
33
34 public class CSVTypeLog extends AbstractDecoder {
35 private static final int initialLength = 75000;
36 private static final int loadFactor = 2;
37 private int fieldCount = -1;
38
39
40
41
42
43 public CSVTypeLog(final File f) {
44 this.setLogFile(f);
45 this.setT(new Thread(this, "CSV Type Log Loading"));
46 this.getT().setPriority(Thread.MAX_PRIORITY);
47 this.getT().start();
48 }
49
50 @Override
51 public final void run() {
52 try {
53 final long startTime = System.currentTimeMillis();
54 decodeLog();
55 OpenLogViewer.getInstance().getEntireGraphingPanel().setGraphSize(this.getDecodedLog().getRecordCount());
56 this.getDecodedLog().setLogStatus(GenericLog.LogState.LOG_LOADED);
57 System.out.println("Loaded " + (this.getDecodedLog().getRecordCount() + 1) + " records in " + (System.currentTimeMillis() - startTime) + " millis!");
58 } catch (Exception e) {
59 e.printStackTrace();
60 if (this.getDecodedLog() != null) {
61 this.getDecodedLog().setLogStatusMessage(e.getMessage());
62 } else {
63 System.out.println("FIXME!!! CSVTypeLog");
64 }
65 }
66 }
67
68
69
70
71
72
73
74
75
76
77 protected final void decodeLog() throws IOException {
78 final Scanner scan = new Scanner(new BufferedReader(new FileReader(getLogFile())));
79 final String delimiter = scanForDelimiter();
80
81 String[] splitLine = new String[1];
82 String[] headers = new String[1];
83
84 String line = "";
85 boolean headerSet = false;
86 while (scan.hasNextLine() && !headerSet) {
87 line = scan.nextLine();
88 splitLine = line.split(delimiter);
89
90 if (splitLine.length == fieldCount) {
91 headers = splitLine;
92 this.setDecodedLog(new GenericLog(splitLine, initialLength, loadFactor));
93 headerSet = true;
94 }
95 }
96
97 while (scan.hasNextLine()) {
98 line = scan.nextLine();
99 splitLine = line.split(delimiter);
100 this.getDecodedLog().incrementPosition();
101 if (splitLine.length == fieldCount) {
102 for (int x = 0; x < splitLine.length; x++) {
103 this.getDecodedLog().addValue(headers[x], Double.parseDouble(splitLine[x]));
104 }
105 }
106 }
107 }
108
109
110
111
112
113
114 private String scanForDelimiter() throws IOException {
115 final String[] delim = {"\t", ",", ":", "/", "\\\\"};
116 final FileReader reader = new FileReader(getLogFile());
117 final Scanner scan = new Scanner(reader);
118
119 final int magicNumber = 10;
120 String delimiterFind = "";
121 String[] split = new String[1];
122 int delimNum = -1;
123
124 for (int i = 0; i < magicNumber && scan.hasNext(); i++) {
125 delimiterFind = scan.nextLine();
126 for (int j = 0; j < delim.length; j++) {
127 split = delimiterFind.split(delim[j]);
128 if (split.length > fieldCount) {
129 fieldCount = split.length;
130 delimNum = j;
131 }
132 }
133 }
134
135 scan.close();
136 reader.close();
137 return delim[delimNum];
138 }
139 }