-
Notifications
You must be signed in to change notification settings - Fork 41
/
PicoGK_Csv.cs
259 lines (215 loc) · 7.71 KB
/
PicoGK_Csv.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
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
//
// SPDX-License-Identifier: Apache-2.0
//
// PicoGK ("peacock") is a compact software kernel for computational geometry,
// specifically for use in Computational Engineering Models (CEM).
//
// For more information, please visit https://picogk.org
//
// PicoGK is developed and maintained by LEAP 71 - © 2023-2024 by LEAP 71
// https://leap71.com
//
// Computational Engineering will profoundly change our physical world in the
// years ahead. Thank you for being part of the journey.
//
// We have developed this library to be used widely, for both commercial and
// non-commercial projects alike. Therefore, we have released it under a
// permissive open-source license.
//
// The foundation of PicoGK is a thin layer on top of the powerful open-source
// OpenVDB project, which in turn uses many other Free and Open Source Software
// libraries. We are grateful to be able to stand on the shoulders of giants.
//
// LEAP 71 licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, THE SOFTWARE IS
// PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace PicoGK
{
public interface IDataTable
{
int nMaxColumnCount();
string strColumnId(int nColumn);
bool bFindColumn( string strColumnName,
out int nColumn);
int nRowCount();
string strGetAt( int nRow,
int nColumn);
void SetColumnIds(IEnumerable<string> astrIds);
void AddRow(IEnumerable<string> astrData);
}
public class CsvTable : IDataTable
{
public CsvTable(IEnumerable<string>? astrColumnIDs = null)
{
m_oColumnIDs = new(astrColumnIDs ?? new List<string>());
}
public CsvTable( string strFilePath,
string strDelimiters = ",")
{
using (StreamReader oReader = new StreamReader(strFilePath))
{
string? strLine = null;
bool bFirst = true;
// Read and process each line in the file
while ((strLine = oReader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(strLine))
continue;
string[] astrParts = strLine.Split(strDelimiters);
List<string> oColumns = new List<string>();
foreach (string str in astrParts)
{
oColumns.Add(str.Trim());
}
if (bFirst)
{
m_oColumnIDs = oColumns;
bFirst = false;
}
else
{
m_oRows.Add(oColumns);
}
m_nMaxColumnCount = Math.Max(m_nMaxColumnCount, oColumns.Count());
}
if (m_oColumnIDs is null)
throw new FileLoadException($"No content in CSV file {strFilePath}");
}
}
public void Save( string strFilePath,
string strDelimiter = ",")
{
using (StreamWriter oWriter = new StreamWriter(strFilePath))
{
// Write column headers
{
string strLine = "";
foreach (string str in m_oColumnIDs)
{
strLine += str + strDelimiter;
}
strLine = strLine.Substring(0, strLine.Length - 1); // trim off trailing delimiter
oWriter.WriteLine(strLine);
}
foreach (List<string> oColumns in m_oRows)
{
string strLine = "";
foreach (string str in oColumns)
{
strLine += str + strDelimiter;
}
strLine = strLine.Substring(0, strLine.Length - 1); // trim off trailing delimiter
oWriter.WriteLine(strLine);
}
}
}
public int nRowCount()
{
return m_oRows.Count;
}
public int nMaxColumnCount()
{
return m_nMaxColumnCount;
}
public string strGetAt( int nRow,
int nColumn)
{
if (nRow >= m_oRows.Count)
return "";
List<string> oColumns = m_oRows[nRow];
if (nColumn >= oColumns.Count)
return "";
return oColumns[nColumn];
}
public void SetKeyColumn(int nColumn)
{
m_nKeyColumn = nColumn;
}
public bool bGetAt(in string strKey,
ref float fVal)
{
string str = "";
if (!bGetAt(strKey, ref str))
return false;
float.TryParse(str, out fVal);
return true;
}
public bool bGetAt( in string strKey,
ref string strVal)
{
string[] astr = strKey.Split(".");
if (astr.Count() != 2)
{
// Expecting "RowName.ColumnName"
return false;
}
int nColumn;
if (!bFindColumn( astr[1],
out nColumn))
{
return false;
}
foreach (List<string> oColumns in m_oRows)
{
if (oColumns.Count <= m_nKeyColumn)
continue; // no value in KeyColumn
if (oColumns[m_nKeyColumn].Equals(astr[0], StringComparison.OrdinalIgnoreCase))
{
if (nColumn < oColumns.Count)
{
strVal = oColumns[nColumn];
}
return true;
}
}
return false;
}
public bool bFindColumn( string strColumnName,
out int nColumn)
{
// find column
int i = 0;
foreach (string str in m_oColumnIDs)
{
if (str.Equals(strColumnName, StringComparison.OrdinalIgnoreCase))
{
nColumn = i;
return true;
}
i++;
}
nColumn = -1;
return false;
}
public string strColumnId(int nColumn)
{
if (nColumn > nMaxColumnCount()-1)
return "";
return m_oColumnIDs[nColumn];
}
public void SetColumnIds(IEnumerable<string> astrIds)
{
m_oColumnIDs = new(astrIds);
m_nMaxColumnCount = int.Max(m_nMaxColumnCount, m_oColumnIDs.Count);
}
public void AddRow(IEnumerable<string> astrData)
{
List<string> oRow = new(astrData);
m_oRows.Add(oRow);
m_nMaxColumnCount = int.Max(m_nMaxColumnCount, oRow.Count);
}
List<string> m_oColumnIDs;
List<List<string>> m_oRows = new List<List<string>>();
int m_nKeyColumn = 0;
int m_nMaxColumnCount = 0;
} // class PicoGKCsv
} // namespace