-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFileTable.cpp
More file actions
268 lines (244 loc) · 8.08 KB
/
Copy pathTextFileTable.cpp
File metadata and controls
268 lines (244 loc) · 8.08 KB
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
#include "TextFileTable.h"
#include <fcntl.h>
#include <io.h>
#include <Windows.h>
std::string TextFileTable::Unquoted(std::string const &str) {
if (str.size() > 1 && str[0] == '"' && str[str.size() - 1] == '"') {
std::string unquoted = str.substr(1, str.size() - 2);
std::string result;
for (size_t i = 0; i < unquoted.size(); i++) {
if (unquoted[i] == '"' && (i + 1) < unquoted.size() && unquoted[i + 1] == '"')
i++;
result += unquoted[i];
}
return result;
}
return str;
}
std::string TextFileTable::Quoted(std::string const &str, char separator) {
bool addQuotes = false;
for (char c : str) {
if (c == '\r' || c == '\n' || c == '"' || c == separator) {
addQuotes = true;
break;
}
}
if (!addQuotes)
return str;
std::string result;
result += '"';
for (char c : str) {
result += c;
if (c == '"')
result += c;
}
result += '"';
return result;
}
size_t TextFileTable::NumRowsToWrite() const {
size_t result = 0;
for (size_t r = 0; r < mCells.size(); r++) {
bool validRow = false;
if (!mCells[r].empty()) {
for (size_t c = 0; c < mCells[r].size(); c++) {
if (!mCells[r][c].empty()) {
validRow = true;
break;
}
}
}
if (validRow)
result = r + 1;
}
return result;
}
size_t TextFileTable::NumRows() const {
return mCells.size();
}
size_t TextFileTable::NumColumns(size_t row) const {
if (row < mCells.size())
return mCells[row].size();
return 0;
}
bool TextFileTable::IsConsistent() const {
if (mCells.size() > 1) {
for (size_t i = 1; i < mCells.size(); i++) {
if (mCells[i - 1].size() != mCells[i].size())
return false;
}
}
return true;
}
size_t TextFileTable::MaxColumns() const {
size_t maxColumns = 0;
for (auto const &row : mCells) {
if (row.size() > maxColumns)
maxColumns = row.size();
}
return maxColumns;
}
std::vector<std::vector<std::string>> const &TextFileTable::Rows() const {
return mCells;
}
std::vector<std::string> const &TextFileTable::Row(size_t row) const {
static std::vector<std::string> emptyRow;
if (row < mCells.size())
return mCells[row];
return emptyRow;
}
std::string const &TextFileTable::Cell(size_t column, size_t row) const {
static std::string emptyCell;
if (row < mCells.size() && column < mCells[row].size())
return mCells[row][column];
return emptyCell;
}
void TextFileTable::AddRow(std::vector<std::string> const &row) {
mCells.push_back(row);
}
void TextFileTable::Clear() {
mCells.clear();
}
bool TextFileTable::Read(std::filesystem::path const &filename, char separator) {
Clear();
FILE *file = _wfopen(filename.c_str(), L"rb");
if (file) {
fseek(file, 0, SEEK_END);
auto fileSizeWithBom = ftell(file);
if (fileSizeWithBom == 0) {
fclose(file);
return false;
}
fseek(file, 0, SEEK_SET);
eEncoding enc = ENCODING_UTF8;
long numBytesToSkip = 0;
if (fileSizeWithBom >= 2) {
unsigned char bom[3];
bom[0] = 0;
fread(&bom, 1, 2, file);
fseek(file, 0, SEEK_SET);
if (bom[0] == 0xFE && bom[1] == 0xFF) {
enc = ENCODING_UTF16BE_BOM;
numBytesToSkip = 2;
}
else if (bom[0] == 0xFF && bom[1] == 0xFE) {
enc = ENCODING_UTF16LE_BOM;
numBytesToSkip = 2;
}
else if (fileSizeWithBom >= 3) {
bom[0] = 0;
fread(&bom, 1, 3, file);
fseek(file, 0, SEEK_SET);
if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF) {
enc = ENCODING_UTF8_BOM;
numBytesToSkip = 3;
}
}
}
long totalSize = fileSizeWithBom - numBytesToSkip;
char *fileData = new char[totalSize];
fseek(file, numBytesToSkip, SEEK_SET);
auto numRead = fread(fileData, 1, totalSize, file);
fclose(file);
if (numRead != totalSize) {
delete[] fileData;
return false;
}
long numChars = totalSize;
if (enc == ENCODING_UTF16LE_BOM || enc == ENCODING_UTF16BE_BOM)
numChars = WideCharToMultiByte(CP_UTF8, 0, (wchar_t const *)fileData, totalSize, NULL, 0, NULL, NULL);
if (numChars == 0) {
delete[] fileData;
return true;
}
char *data = new char[numChars];
memset(data, 0, numChars * sizeof(char));
if (enc == ENCODING_UTF16LE_BOM || enc == ENCODING_UTF16BE_BOM) {
if (enc == ENCODING_UTF16BE_BOM) {
auto numWideChars = totalSize / 2;
wchar_t* wideData = (wchar_t *)fileData;
for (long i = 0; i < numWideChars; i++)
wideData[i] = (wideData[i] >> 8) | (wideData[i] << 8);
}
WideCharToMultiByte(CP_UTF8, 0, (wchar_t const*)fileData, totalSize, data, numChars, NULL, NULL);
}
else
memcpy(data, fileData, totalSize);
delete[] fileData;
std::vector<std::string> mLines;
std::string currentLine;
unsigned int numQuotes = 0;
for (long i = 0; i < numChars; i++) {
if (data[i] == '\n') {
if (numQuotes % 2)
currentLine += data[i];
else {
mLines.push_back(currentLine);
currentLine.clear();
}
}
else if (data[i] == '\r') {
if (numQuotes % 2)
currentLine += data[i];
if ((i + 1) < numChars && data[i + 1] == '\n') {
i++;
if (numQuotes % 2)
currentLine += data[i];
}
if ((numQuotes % 2) == 0) {
mLines.push_back(currentLine);
currentLine.clear();
}
}
else {
if (data[i] == '"')
numQuotes++;
currentLine += data[i];
}
}
if (!currentLine.empty() || !mLines.empty())
mLines.push_back(currentLine);
delete[] data;
mCells.resize(mLines.size());
for (size_t l = 0; l < mLines.size(); l++) {
numQuotes = 0;
auto const &line = mLines[l];
std::string value;
for (size_t i = 0; i < line.size(); i++) {
if (line[i] == '"')
numQuotes++;
else if (line[i] == separator && ((numQuotes % 2) == 0)) {
mCells[l].push_back(Unquoted(value));
value.clear();
continue;
}
value += line[i];
}
mCells[l].push_back(Unquoted(value));
value.clear();
}
mCells.resize(NumRowsToWrite());
}
return true;
}
bool TextFileTable::ReadCSV(std::filesystem::path const &filename, char separator) {
return Read(filename, separator);
}
bool TextFileTable::ReadTSV(std::filesystem::path const &filename) {
return Read(filename, L'\t');
}
bool TextFileTable::ReadUnicodeText(std::filesystem::path const &filename) {
return Read(filename, L'\t');
}
bool TextFileTable::Write(std::filesystem::path const &filename, char separator, eEncoding encoding) {
// not implemented
return false;
}
bool TextFileTable::WriteCSV(std::filesystem::path const &filename, char separator) {
return Write(filename, separator, ENCODING_UTF8_BOM);
}
bool TextFileTable::WriteTSV(std::filesystem::path const &filename) {
return Write(filename, L'\t', ENCODING_UTF8_BOM);
}
bool TextFileTable::WriteUnicodeText(std::filesystem::path const &filename) {
return Write(filename, L'\t', ENCODING_UTF16LE_BOM);
}