-
Notifications
You must be signed in to change notification settings - Fork 4
/
ParseDefCsv.cs
108 lines (88 loc) · 2.97 KB
/
ParseDefCsv.cs
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
// ParseDefCsv.cs: Parse parameter definitions file (CSV)
/* Copyright (C) 2015 SubaruDieselCrew
*
* This file is part of ParsePID.
*
* ParsePID is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ParsePID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ParsePID. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace ParsePID
{
public sealed class ParseDefCsv : IDisposable
{
const string Comment = "#";
// for parsing integers, cannot use const
static readonly System.Globalization.NumberFormatInfo NumberFormatInfoInvariant = System.Globalization.NumberFormatInfo.InvariantInfo;
readonly StreamReader sr;
readonly char[] separators;
public ParseDefCsv (string path, char separator)
{
var fs = File.OpenRead (path);
this.sr = new StreamReader (fs);
this.separators = new char[] { separator };
}
public bool Parse (out List<Parameter> parameters)
{
parameters = new List<Parameter> (64);
// skip first line = header row
sr.ReadLine ();
string line;
while ((line = sr.ReadLine ()) != null) {
if (line.StartsWith (Comment) || string.IsNullOrEmpty (line))
continue;
try {
Parameter p = ParseLine (line);
parameters.Add (p);
} catch (Exception ex) {
throw new InvalidDataException ("Error parsing line:\n" + line, ex);
}
}
return true;
}
Parameter ParseLine (string line)
{
var p = new Parameter ();
string[] splitted = line.Split (separators, StringSplitOptions.None);
p.PID = (UInt16)ParseHexInt (splitted [(int)CsvColumn.PID]);
p.NameShort = splitted [(int)CsvColumn.Name_Short];
p.NameFull = splitted [(int)CsvColumn.Name_Full];
p.Units = splitted [(int)CsvColumn.Units];
p.Length = byte.Parse (splitted [(int)CsvColumn.Length]);
return p;
}
/// <summary>
/// Parses hex number e.g."0x123af". Prefix "0x" is required.
/// </summary>
static internal int ParseHexInt (string strToParse)
{
const string HexPrefix = "0x";
// prefix "0x" required
// but not allowed in int.Parse(...) even though using NumberStyles.HexNumber.
int indexPrefix = strToParse.IndexOf (HexPrefix);
if (indexPrefix < 0) {
throw new InvalidDataException ("Prefix '" + HexPrefix + "' missing.");
}
return int.Parse (strToParse.Substring (indexPrefix + HexPrefix.Length),
System.Globalization.NumberStyles.HexNumber, NumberFormatInfoInvariant);
}
#region IDisposable implementation
public void Dispose ()
{
sr.Dispose ();
}
#endregion
}
}