FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
keepassxc/src/core/CsvParser.cpp at develop · sourcecodes2/keepassxc · GitHub
sourcecodes2
/
keepassxc
Public
forked from
keepassxreboot/keepassxc
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
keepassxc
/
src
/
core
/
CsvParser.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
439 lines (389 loc) · 8.63 KB
Breadcrumbs
keepassxc
/
src
/
core
/
CsvParser.cpp
Copy path
File metadata and controls
439 lines (389 loc) · 8.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/
*
* Copyright (C) 2016 Enrico Mariotti <enricomariotti@yahoo.it>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program 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 2 or (at your option)
* version 3 of the License.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#
include
"
CsvParser.h
"
#
include
<
QObject
>
#
include
<
QTextCodec
>
#
include
"
core/Tools.h
"
CsvParser::CsvParser
()
: m_ch(
0
)
, m_comment(
'
#
'
)
, m_currCol(
1
)
, m_currRow(
1
)
, m_isBackslashSyntax(
false
)
, m_isEof(
false
)
, m_isFileLoaded(
false
)
, m_isGood(
true
)
, m_lastPos(-
1
)
, m_maxCols(
0
)
, m_qualifier(
'
"
'
)
, m_separator(
'
,
'
)
, m_statusMsg(
"
"
)
{
m_csv.
setBuffer
(&m_array);
m_ts.
setDevice
(&m_csv);
m_csv.
open
(QIODevice::ReadOnly);
m_ts.
setCodec
(
"
UTF-8
"
);
}
CsvParser::~CsvParser
()
{
m_csv.
close
();
}
bool
CsvParser::isFileLoaded
()
{
return
m_isFileLoaded;
}
bool
CsvParser::reparse
()
{
reset
();
return
parseFile
();
}
bool
CsvParser::parse
(QFile* device)
{
clear
();
if
(
nullptr
== device) {
appendStatusMsg
(
QObject::tr
(
"
NULL device
"
),
true
);
return
false
;
}
if
(!
readFile
(device)) {
return
false
;
}
return
parseFile
();
}
bool
CsvParser::readFile
(QFile* device)
{
if
(device->
isOpen
()) {
device->
close
();
}
device->
open
(QIODevice::ReadOnly);
if
(!
Tools::readAllFromDevice
(device, m_array)) {
appendStatusMsg
(
QObject::tr
(
"
error reading from device
"
),
true
);
m_isFileLoaded =
false
;
}
else
{
device->
close
();
m_array.
replace
(
"
\r\n
"
,
"
\n
"
);
m_array.
replace
(
"
\r
"
,
"
\n
"
);
if
(
0
== m_array.
size
()) {
appendStatusMsg
(
QObject::tr
(
"
file empty
"
).
append
(
"
\n
"
));
}
m_isFileLoaded =
true
;
}
return
m_isFileLoaded;
}
void
CsvParser::reset
()
{
m_ch =
0
;
m_currCol =
1
;
m_currRow =
1
;
m_isEof =
false
;
m_isGood =
true
;
m_lastPos = -
1
;
m_maxCols =
0
;
m_statusMsg =
"
"
;
m_ts.
seek
(
0
);
m_table.
clear
();
//
the following are users' concern :)
//
m_comment = '#';
//
m_backslashSyntax = false;
//
m_comment = '#';
//
m_qualifier = '"';
//
m_separator = ',';
}
void
CsvParser::clear
()
{
reset
();
m_isFileLoaded =
false
;
m_array.
clear
();
}
bool
CsvParser::parseFile
()
{
parseRecord
();
while
(!m_isEof) {
if
(!
skipEndline
()) {
appendStatusMsg
(
QObject::tr
(
"
malformed string
"
),
true
);
}
m_currRow++;
m_currCol =
1
;
parseRecord
();
}
fillColumns
();
return
m_isGood;
}
void
CsvParser::parseRecord
()
{
CsvRow row;
if
(
isComment
()) {
skipLine
();
return
;
}
do
{
parseField
(row);
getChar
(m_ch);
}
while
(
isSeparator
(m_ch) && !m_isEof);
if
(!m_isEof) {
ungetChar
();
}
if
(
isEmptyRow
(row)) {
row.
clear
();
return
;
}
m_table.
push_back
(row);
if
(m_maxCols < row.
size
()) {
m_maxCols = row.
size
();
}
m_currCol++;
}
void
CsvParser::parseField
(CsvRow& row)
{
QString field;
peek
(m_ch);
if
(!
isTerminator
(m_ch)) {
if
(
isQualifier
(m_ch)) {
parseQuoted
(field);
}
else
{
parseSimple
(field);
}
}
row.
push_back
(field);
}
void
CsvParser::parseSimple
(QString& s)
{
QChar c;
getChar
(c);
while
((
isText
(c)) && (!m_isEof)) {
s.
append
(c);
getChar
(c);
}
if
(!m_isEof) {
ungetChar
();
}
}
void
CsvParser::parseQuoted
(QString& s)
{
//
read and discard initial qualifier (e.g. quote)
getChar
(m_ch);
parseEscaped
(s);
//
getChar(m_ch);
if
(!
isQualifier
(m_ch)) {
appendStatusMsg
(
QObject::tr
(
"
missing closing quote
"
),
true
);
}
}
void
CsvParser::parseEscaped
(QString& s)
{
parseEscapedText
(s);
while
(
processEscapeMark
(s, m_ch)) {
parseEscapedText
(s);
}
if
(!m_isEof) {
ungetChar
();
}
}
void
CsvParser::parseEscapedText
(QString& s)
{
getChar
(m_ch);
while
((!
isQualifier
(m_ch)) && !m_isEof) {
s.
append
(m_ch);
getChar
(m_ch);
}
}
bool
CsvParser::processEscapeMark
(QString& s, QChar c)
{
QChar buf;
peek
(buf);
QChar c2;
if
(
true
== m_isBackslashSyntax) {
//
escape-character syntax, e.g. \"
if
(c !=
'
\\
'
) {
return
false
;
}
//
consume (and append) second qualifier
getChar
(c2);
if
(m_isEof) {
c2 =
'
\\
'
;
s.
append
(
'
\\
'
);
return
false
;
}
else
{
s.
append
(c2);
return
true
;
}
}
else
{
//
double quote syntax, e.g. ""
if
(!
isQualifier
(c)) {
return
false
;
}
peek
(c2);
if
(!m_isEof) {
//
not EOF, can read one char
if
(
isQualifier
(c2)) {
s.
append
(c2);
getChar
(c2);
return
true
;
}
}
return
false
;
}
}
void
CsvParser::fillColumns
()
{
//
fill shorter rows with empty placeholder columns
for
(
int
i =
0
; i < m_table.
size
(); ++i) {
int
gap = m_maxCols - m_table.
at
(i).
size
();
if
(gap >
0
) {
CsvRow r = m_table.
at
(i);
for
(
int
j =
0
; j < gap; ++j) {
r.
append
(
QString
(
"
"
));
}
m_table.
replace
(i, r);
}
}
}
void
CsvParser::skipLine
()
{
m_ts.
readLine
();
m_ts.
seek
(m_ts.
pos
() -
1
);
}
bool
CsvParser::skipEndline
()
{
getChar
(m_ch);
return
(m_ch ==
'
\n
'
);
}
void
CsvParser::getChar
(QChar& c)
{
m_isEof = m_ts.
atEnd
();
if
(!m_isEof) {
m_lastPos = m_ts.
pos
();
m_ts >> c;
}
}
void
CsvParser::ungetChar
()
{
if
(!m_ts.
seek
(m_lastPos)) {
qWarning
(
"
CSV Parser: unget lower bound exceeded
"
);
m_isGood =
false
;
}
}
void
CsvParser::peek
(QChar& c)
{
getChar
(c);
if
(!m_isEof) {
ungetChar
();
}
}
bool
CsvParser::isQualifier
(
const
QChar& c)
const
{
if
(
true
== m_isBackslashSyntax && (c != m_qualifier)) {
return
(c ==
'
\\
'
);
}
else
{
return
(c == m_qualifier);
}
}
bool
CsvParser::isComment
()
{
bool
result =
false
;
QChar c2;
qint64 pos = m_ts.
pos
();
do
{
getChar
(c2);
}
while
((
isSpace
(c2) ||
isTab
(c2)) && (!m_isEof));
if
(c2 == m_comment) {
result =
true
;
}
m_ts.
seek
(pos);
return
result;
}
bool
CsvParser::isText
(QChar c)
const
{
return
!((
isCRLF
(c)) || (
isSeparator
(c)));
}
bool
CsvParser::isEmptyRow
(
const
CsvRow& row)
const
{
CsvRow::const_iterator it = row.
constBegin
();
for
(; it != row.
constEnd
(); ++it) {
if
(((*it) !=
"
\n
"
) && ((*it) !=
"
"
)) {
return
false
;
}
}
return
true
;
}
bool
CsvParser::isCRLF
(
const
QChar& c)
const
{
return
(c ==
'
\n
'
);
}
bool
CsvParser::isSpace
(
const
QChar& c)
const
{
return
(c ==
'
'
);
}
bool
CsvParser::isTab
(
const
QChar& c)
const
{
return
(c ==
'
\t
'
);
}
bool
CsvParser::isSeparator
(
const
QChar& c)
const
{
return
(c == m_separator);
}
bool
CsvParser::isTerminator
(
const
QChar& c)
const
{
return
(
isSeparator
(c) || (c ==
'
\n
'
) || (c ==
'
\r
'
));
}
void
CsvParser::setBackslashSyntax
(
bool
set)
{
m_isBackslashSyntax = set;
}
void
CsvParser::setComment
(
const
QChar& c)
{
m_comment = c.
unicode
();
}
void
CsvParser::setCodec
(
const
QString& s)
{
m_ts.
setCodec
(
QTextCodec::codecForName
(s.
toLocal8Bit
()));
}
void
CsvParser::setFieldSeparator
(
const
QChar& c)
{
m_separator = c.
unicode
();
}
void
CsvParser::setTextQualifier
(
const
QChar& c)
{
m_qualifier = c.
unicode
();
}
int
CsvParser::getFileSize
()
const
{
return
m_csv.
size
();
}
const
CsvTable
CsvParser::getCsvTable
()
const
{
return
m_table;
}
QString
CsvParser::getStatus
()
const
{
return
m_statusMsg;
}
int
CsvParser::getCsvCols
()
const
{
if
(!m_table.
isEmpty
() && !m_table.
at
(
0
).
isEmpty
()) {
return
m_table.
at
(
0
).
size
();
}
else
{
return
0
;
}
}
int
CsvParser::getCsvRows
()
const
{
return
m_table.
size
();
}
void
CsvParser::appendStatusMsg
(
const
QString& s,
bool
isCritical)
{
m_statusMsg +=
QObject::tr
(
"
%1: (row, col) %2,%3
"
).
arg
(s, m_currRow, m_currCol).
append
(
"
\n
"
);
m_isGood = !isCritical;
}
Back
|
FazBrowse Home
|
New Git URL