-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
270 lines (253 loc) · 7.56 KB
/
index.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import fs from "fs";
/**
* Validates a CarMaker erg Infofile
* @param infoFile Path to CarMaker erg InfoFile
* @returns true if valid erg infofile, false if invalid
*/
function validateInfoHeader(infoFile: string) {
// read infofile
const info: string[] = fs
.readFileSync(infoFile, "utf8")
.replace(/\r/g, "")
.split("\n");
let validFormat = false;
let validInfofile = false;
// check that first line of infofile is valid
if (info[0].startsWith("#INFOFILE1.1")) {
validInfofile = true;
}
// Check that file format is erg
for (const line of info) {
if (line.startsWith("File.Format") && line.includes("erg")) {
validFormat = true;
break;
}
}
return validFormat && validInfofile;
}
/**
* Read the erg infofile header.
* @param infoFile Path to CarMaker erg InfoFile.
* @returns Object containing erg file metadata.
*/
function readInfoHeader(infoFile: string) {
// read the info file
const info: string[] = fs
.readFileSync(infoFile, "utf8")
.replace(/\r/g, "")
.split("\n");
let headerInfo: {
Date?: string;
Testrun?: string;
CM_Version?: string;
} = {};
// extract header info
info.forEach((line) => {
if (line.startsWith(`File.DateLocal`)) {
// date
headerInfo[`Date`] = line.split("= ")[1];
} else if (line.startsWith(`Testrun =`)) {
// test run
headerInfo[`Testrun`] = line.split("= ")[1];
} else if (line.startsWith(`CarMaker.Version =`)) {
// carmaker version
headerInfo[`CM_Version`] = line.split("= ")[1];
}
});
return headerInfo;
}
/**
* Read list of quantities from erg infofile.
* @param infoFile Path to CarMaker erg InfoFile.
* @returns Array of objects containing erg file quantity names.
*/
function readInfoQuants(infoFile: string) {
// read the info file
const info: string[] = fs
.readFileSync(infoFile, "utf8")
.replace(/\r/g, "")
.split("\n");
// extract quantity info
let quantNumber = 1;
let lastQuantName = "";
let quants: {
name: string;
type: string;
unit: string;
values: (string | number)[];
}[] = [];
info.forEach((line) => {
// name
if (line.startsWith(`File.At.${quantNumber}.Name`)) {
lastQuantName = line.split("= ")[1];
quants.push({ name: lastQuantName, type: "", unit: "", values: [] });
quantNumber++;
return;
}
// type
if (line.startsWith(`File.At.${quantNumber - 1}.Type`)) {
quants[quants.length - 1].type = line.split("= ")[1];
return;
}
// unit
if (line.startsWith(`Quantity.${lastQuantName}.Unit`)) {
quants[quants.length - 1].unit = line.split("= ")[1];
return;
}
});
return quants;
}
/**
* Read erg and info file and return data values for each quantity.
* @param ergFile Path to CarMaker erg file.
* @param infoFile Path to CarMaker erg InfoFile.
* @returns Array of CarMaker quantities
*/
function read(ergFile: string, infoFile: string) {
// read info file
let quants = readInfoQuants(infoFile);
// error if there are no quantities in erg infofile
if (quants.length < 1) {
throw new Error("ERG File does not contain any quantities");
}
// read erg file
let ergBuffer = fs.readFileSync(ergFile);
// process erg header
let headerBuffer = ergBuffer.slice(0, 15);
let ergHeader = {
format: headerBuffer.toString("ascii", 0, 7),
version: headerBuffer.readUInt8(8),
byteOrder: headerBuffer.readUInt8(9),
};
// Check format specified in the ERG header. Error if not ERG
if (!ergHeader.format.startsWith("CM-ERG")) {
throw new Error("Invalid ERG file");
}
// process erg records by reading value for each quantity
let recordBuffer = ergBuffer.slice(16);
let recordIndex = 0;
while (recordIndex < recordBuffer.length) {
quants.forEach((quant) => {
switch (quant.type) {
case "Double": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readDoubleLE(recordIndex)
: recordBuffer.readDoubleBE(recordIndex);
quant.values.push(value);
recordIndex += 8;
break;
}
case "Float": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readFloatLE(recordIndex)
: recordBuffer.readFloatBE(recordIndex);
quant.values.push(value);
recordIndex += 4;
break;
}
case "LongLong": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readIntLE(recordIndex, 8)
: recordBuffer.readIntBE(recordIndex, 8);
quant.values.push(value);
recordIndex += 8;
break;
}
case "ULongLong": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readUIntLE(recordIndex, 8)
: recordBuffer.readUIntBE(recordIndex, 8);
quant.values.push(value);
recordIndex += 8;
break;
}
case "Long": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readIntLE(recordIndex, 4)
: recordBuffer.readIntBE(recordIndex, 4);
quant.values.push(value);
recordIndex += 4;
break;
}
case "ULong": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readUIntLE(recordIndex, 4)
: recordBuffer.readUIntBE(recordIndex, 4);
quant.values.push(value);
recordIndex += 4;
break;
}
case "Int": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readIntLE(recordIndex, 4)
: recordBuffer.readIntBE(recordIndex, 4);
quant.values.push(value);
recordIndex += 4;
break;
}
case "UInt": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readUIntLE(recordIndex, 4)
: recordBuffer.readUIntBE(recordIndex, 4);
quant.values.push(value);
recordIndex += 4;
break;
}
case "Short": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readIntLE(recordIndex, 2)
: recordBuffer.readIntBE(recordIndex, 2);
quant.values.push(value);
recordIndex += 4;
break;
}
case "UShort": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readUIntLE(recordIndex, 2)
: recordBuffer.readUIntBE(recordIndex, 2);
quant.values.push(value);
recordIndex += 4;
break;
}
case "Char": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readIntLE(recordIndex, 1)
: recordBuffer.readIntBE(recordIndex, 1);
quant.values.push(value.toString());
recordIndex += 1;
break;
}
case "UChar": {
let value =
ergHeader.byteOrder == 0
? recordBuffer.readUIntLE(recordIndex, 1)
: recordBuffer.readUIntBE(recordIndex, 1);
quant.values.push(value.toString());
recordIndex += 1;
break;
}
default: {
if (quant.type.endsWith("Bytes")) {
let skipBytes = parseInt(quant.type.split(" Bytes")[0]);
recordIndex += skipBytes;
}
break;
}
}
});
}
return quants;
}
export { read, readInfoHeader, readInfoQuants, validateInfoHeader };
export default { read, readInfoHeader, readInfoQuants, validateInfoHeader };