-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCsvFile.cpp
More file actions
681 lines (518 loc) · 14.4 KB
/
CsvFile.cpp
File metadata and controls
681 lines (518 loc) · 14.4 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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*******************************************************************
// Copyright (c) 2002, Robert Umbehant ( www.wheresjames.com )
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// CsvFile.cpp: implementation of the CCsvFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCsvFile::CCsvFile()
{_STTEX();
m_col = 0;
m_row = 0;
m_rowptr = 0;
m_pTable = NULL;
m_pDesc = NULL;
m_pTypes = NULL;
m_dwErrors = 0;
m_dwRecordSize = 4096;
m_dwRowBlockSize = 16;
m_ucSep = ',';
m_dwSkip = 0;
}
CCsvFile::~CCsvFile()
{_STTEX();
Destroy();
}
void CCsvFile::Destroy()
{_STTEX();
// Delete table memory
if ( m_pTable != NULL )
{
// Delete column memory
for ( DWORD i = 0; i < m_row; i++ )
if ( m_pTable[ i ] != NULL )
{
delete[] m_pTable[ i ];
m_pTable[ i ] = NULL;
} // end if
// Delete table
delete[] m_pTable;
m_pTable = NULL;
} // end if
// Delete descriptions memory
if ( m_pDesc != NULL )
{
delete[] m_pDesc;
m_pDesc = NULL;
} // end if
// Delete type memory
if ( m_pTypes != NULL )
{
delete[] m_pTypes;
m_pTypes = NULL;
} // end if
m_col = 0;
m_row = 0;
m_rowptr = 0;
m_dwErrors = 0;
}
BOOL CCsvFile::AllocateColumns(DWORD col)
{_STTEX();
Destroy();
// Do we want any columns?
if ( col == 0 ) return TRUE;
// Save number of columns
m_col = col;
// Allocate description memory
m_pDesc = new BYTE[ m_col * m_dwRecordSize ];
if ( m_pDesc == NULL ) { Destroy(); return FALSE; }
ZeroMemory( m_pDesc, m_col * m_dwRecordSize );
// Allocate types memory
m_pTypes = new BYTE[ m_col ];
ZeroMemory( m_pTypes, m_col );
return TRUE;
}
BOOL CCsvFile::AllocateRows(DWORD size)
{_STTEX();
// Do we already have enough rows?
if ( size <= m_row ) return TRUE;
// Sanity check
if ( m_dwRecordSize == 0 ) return FALSE;
LPBYTE *oldtable = m_pTable;
// Allocate new table memory
m_pTable = new LPBYTE[ size ];
if ( m_pTable == NULL )
{ m_pTable = oldtable;
return FALSE;
} // end if
// Initialize memory in case of failure
ZeroMemory( m_pTable, sizeof( LPBYTE ) * size );
// Copy old table pointers
if ( oldtable != NULL )
{ memcpy( m_pTable, oldtable, sizeof( LPBYTE ) * m_row );
delete[] oldtable;
} // end if
// allocate new rows
for ( DWORD i = m_row; i < size; i++ )
{
// Allocate row memory
m_pTable[ i ] = new BYTE[ m_col * m_dwRecordSize ];
if ( m_pTable[ i ] == NULL )
{ m_row = i; return FALSE; }
// Init the memory
ZeroMemory( m_pTable[ i ], m_col * m_dwRecordSize );
} // end for
// Set new row size
m_row = size;
return TRUE;
}
BOOL CCsvFile::LoadFile(LPCTSTR pFile, BOOL bAdd )
{_STTEX();
// Lose old record
if ( !bAdd ) Destroy();
CWinFile file;
// Open the file
if ( !file.OpenExisting( pFile, GENERIC_READ ) )
return FALSE;
// Get file size
DWORD size = file.Size();
if ( size == 0 ) return FALSE;
// Allocate memory
LPBYTE buf = new BYTE[ size + 1 ];
if ( buf == NULL ) return FALSE;
// Read in the data into ram
DWORD read;
if ( !file.Read( buf, size, &read ) || read != size )
{ delete[] buf; return FALSE; }
buf[ size ] = 0;
// Load the file
if ( !LoadFromMem( buf, size, bAdd ) )
{ delete[] buf; return FALSE; }
delete buf;
return TRUE;
}
BOOL CCsvFile::LoadFromMem(LPBYTE buf, DWORD size, BOOL bAdd )
{_STTEX();
// Lose old record
if ( !bAdd ) Destroy();
// Sanity check
if ( buf == NULL || size == 0 ) return FALSE;
// Try to make sense out of the buffer
for ( DWORD i = 0; i < size; )
{
if ( m_dwSkip ) m_dwSkip--;
else
{
// Do we need to load the header?
if ( !IsTable() )
{
if ( !LoadColumns( (LPCTSTR)&buf[ i ] ) ) m_dwErrors++;
} // end if
// Skip header if adding
else if ( i == 0 && bAdd );
// Add this line as a record
else if ( !AddLine( (LPCTSTR)&buf[ i ] ) ) m_dwErrors++;
} // end else
// Find the next line
while ( i < size && buf[ i ] >= ' ' && buf[ i ] <= '~' ) i++;
while ( i < size && ( buf[ i ] < ' ' || buf[ i ] > '~' ) ) i++;
} // end for
return TRUE;
}
CSV_HROW CCsvFile::AppendNewRow()
{_STTEX();
// Allocate more rows if needed
if ( m_rowptr >= m_row )
{
if ( !AllocateRows( m_row + m_dwRowBlockSize ) ) return FALSE;
if ( m_rowptr >= m_row ) return FALSE;
} // end if
// Return this row
DWORD i = m_rowptr;
// Count a row
m_rowptr++;
// Return new row
return m_pTable[ i ];
}
BOOL CCsvFile::AddLine(LPCTSTR pLine)
{_STTEX();
BOOL bQuoted = FALSE;
// Get new row pointer
CSV_HROW hrow = AppendNewRow();
if ( hrow == NULL ) return FALSE;
// Read in each column
DWORD b = 0, o = 0, c = 0;
for ( DWORD i = 0; pLine[ i ] >= ' ' && pLine[ i ] <= '~'; i++ )
{
// Handle separator
if ( !bQuoted && pLine[ i ] == m_ucSep )
{
// Terminate string
hrow[ b + o ] = 0;
// Next element
o = 0; b += m_dwRecordSize; c++;
// Skip multiple separators
while ( pLine[ i + 1 ] == m_ucSep ) i++;
// Are we done?
if ( c >= m_col ) return TRUE;
} // end if
// Store the byte if room
else if ( o < ( m_dwRecordSize - 1 ) )
{
// Turn quoted mode on
if ( !bQuoted && pLine[ i ] == '\"' ) bQuoted = TRUE;
// Toggle quoted mode
else if ( pLine[ i ] == '\"' && pLine[ i + 1 ] != '\"' )
bQuoted = !bQuoted;
else
{
// Skip escape character
if ( pLine[ i ] == '\"' ) i++;
// Save character
hrow[ b + o++ ] = pLine[ i ];
} // end else
} // end else if
} // end for
// Null terminate string
hrow[ b + o ] = 0;
return TRUE;
}
BOOL CCsvFile::LoadColumns(LPCTSTR pLine)
{_STTEX();
BOOL bQuoted = FALSE;
DWORD col = 0;
// How many columns will there be?
DWORD i;
for ( i = 0; pLine[ i ] >= ' ' && pLine[ i ] <= '~'; i++ )
{
// Don't count multiple sep
if ( pLine[ i ] == m_ucSep ) col++;
while ( pLine[ i ] == m_ucSep ) i++;
} // end for
// Quit if no columns
if ( col == 0 ) return FALSE;
col += 1;
// Allocate the columns
if ( !AllocateColumns( col ) ) return FALSE;
// Read in each column
DWORD b = 0, o = 0, c = 0;
for ( i = 0; pLine[ i ] >= ' ' && pLine[ i ] <= '~'; i++ )
{
// Handle separator
if ( !bQuoted && pLine[ i ] == m_ucSep )
{
// Terminate string
m_pDesc[ b + o ] = 0;
// Next element
o = 0; b += m_dwRecordSize; c++;
// Skip multiple separators
while ( pLine[ i + 1 ] == m_ucSep ) i++;
// Are we done?
if ( c >= m_col ) return TRUE;
} // end if
// Store the byte if room
else if ( o < ( m_dwRecordSize - 1 ) )
{
// Turn quoted mode on
if ( !bQuoted && pLine[ i ] == '\"' ) bQuoted = TRUE;
// Toggle quoted mode
else if ( pLine[ i ] == '\"' && pLine[ i + 1 ] != '\"' )
bQuoted = !bQuoted;
else
{
// Skip escape character
if ( pLine[ i ] == '\"' ) i++;
// Save character
m_pDesc[ b + o++ ] = pLine[ i ];
} // end else
} // end else if
} // end for
// Null terminate string
m_pDesc[ b + o ] = 0;
return TRUE;
}
BOOL CCsvFile::SaveFile(LPCTSTR pFile)
{_STTEX();
CWinFile file;
// Is there a table to save?
if ( !IsTable() ) return FALSE;
// Open the file
if ( !file.OpenNew( pFile, GENERIC_WRITE ) )
return FALSE;
// Is there anything to save?
DWORD size = SaveToMem( NULL, 0 );
if ( size == 0 ) return FALSE;
// Allocate memory for table data
LPBYTE buf = new BYTE[ size + 1 ];
if ( buf == NULL ) return FALSE;
// Write the data to a buffer
if ( SaveToMem( buf, size ) == 0 )
{ delete[] buf; return FALSE; }
// Write the data to a file
if ( !file.Write( buf, size ) )
{ delete[] buf; return FALSE; }
// Lose the memory
delete[] buf;
return TRUE;
}
DWORD CCsvFile::SaveToMem(LPBYTE buf, DWORD size)
{_STTEX();
DWORD ptr = 0;
DWORD i = 0;
DWORD rows = GetNumRows();
DWORD cols = GetNumColumns();
// Separator string
char sep[ 2 ] = { m_ucSep, 0 };
// Anything to save?
if ( rows == 0 || cols == 0 ) return 0;
// Write out the header
for ( i = 0; i < cols; i++ )
{ LPCTSTR pCol = GetColDesc( i );
if ( i != 0 ) Copy( buf, &ptr, size, sep );
if ( pCol != NULL ) Copy( buf, &ptr, size, pCol );
} // end for
// CRLF
Copy( buf, &ptr, size, "\r\n" );
// For each row
for ( i = 0; i < rows; i++ )
{
CSV_HROW hRow = GetRow( i );
if ( hRow != NULL )
{
// Write out each column element and separator
for ( DWORD c = 0; c < cols; c++ )
{ LPCTSTR pStr = (LPCTSTR)GetRowElement( hRow, c );
if ( c != 0 ) Copy( buf, &ptr, size, sep );
if ( pStr != NULL ) Copy( buf, &ptr, size, pStr );
} // end for
// CRLF
Copy( buf, &ptr, size, "\r\n" );
} // end if
} // end for
// Add for NULL character
ptr += 1;
return ptr;
}
BOOL CCsvFile::Copy(LPVOID buf, LPDWORD ptr, DWORD size, LPCTSTR str)
{_STTEX();
DWORD p = 0;
DWORD l = strlen( str );
// Get size from caller
if ( ptr != NULL ) p = *ptr;
// Sanity checks
if ( l == 0 ) return TRUE;
if ( str == NULL ) return FALSE;
// Copy the string
if ( buf != NULL )
{
DWORD i;
for ( i = 0; i < l && ( p + i ) < size; i++ )
( (char*)buf )[ p + i ] = str[ i ];
// Check for buffer overflow
if ( p + i >= size ) return FALSE;
// NULL terminate
( (char*)buf )[ p + i ] = NULL;
} // end if
// Add str length
if ( ptr != NULL ) *ptr += l;
return TRUE;
}
DWORD CCsvFile::GetRowString(DWORD row, LPSTR pRow, DWORD size)
{_STTEX();
// Get row handle
CSV_HROW hRow = GetRow( row );
if ( hRow == NULL ) return 0;
DWORD cols = GetNumColumns();
// Separator string
char sep[ 2 ] = { m_ucSep, 0 };
// NULL row
if ( pRow != NULL ) *pRow = 0;
DWORD ptr = 0;
// Write out each column element and separator
for ( DWORD c = 0; c < cols; c++ )
{
LPCTSTR pStr = (LPCTSTR)GetRowElement( hRow, c );
if ( pStr != NULL )
{ if ( c != 0 ) Copy( pRow, &ptr, size, sep );
Copy( pRow, &ptr, size, pStr );
} // end if
} // end for
// Add NULL character
ptr += 1;
return ptr;
}
DWORD CCsvFile::GetHeaderString(LPSTR buf, DWORD size)
{_STTEX();
DWORD cols = GetNumColumns();
// Separator string
char sep[ 2 ] = { m_ucSep, 0 };
// NULL row
if ( buf != NULL ) *buf = 0;
DWORD ptr = 0;
// Write out each column element and separator
for ( DWORD c = 0; c < cols; c++ )
{
LPCTSTR pCol = GetColDesc( c );
if ( pCol != NULL )
{ if ( c != 0 ) Copy( buf, &ptr, size, sep );
Copy( buf, &ptr, size, pCol );
} // end if
} // end for
// Add NULL character
ptr += 1;
return ptr;
}
BOOL CCsvFile::AddColumn(LPCTSTR pDescription, DWORD insert)
{_STTEX();
// Sanity check
if ( m_dwRecordSize == 0 ) return FALSE;
// Add one more column
DWORD col = m_col + 1;
// Allocate description memory
LPBYTE pDesc = new BYTE[ col * m_dwRecordSize ];
if ( pDesc == NULL ) return FALSE;
ZeroMemory( pDesc, col * m_dwRecordSize );
// Allocate types memory
LPBYTE pTypes = new BYTE[ col ];
if ( pTypes == NULL ) { delete [] pDesc; return FALSE; }
ZeroMemory( pTypes, col );
// How many rows do we need
DWORD row = m_row;
DWORD rowptr = m_rowptr;
// Allocate new table memory
LPBYTE *pTable = new LPBYTE[ row ];
if ( pTable == NULL )
{ delete [] pDesc; delete [] pTypes; return FALSE; }
// Initialize memory in case of failure
ZeroMemory( pTable, sizeof( LPBYTE ) * row );
// allocate new rows
DWORD i;
for ( i = 0; i < row; i++ )
{
// Allocate row memory
pTable[ i ] = new BYTE[ col * m_dwRecordSize ];
if ( pTable[ i ] == NULL )
{ long k = i;
while ( k >= 0 ) { delete [] pTable[ k-- ]; }
delete [] pTable;
delete [] pTypes;
delete [] pDesc;
return FALSE;
}
// Init the memory
ZeroMemory( pTable[ i ], col * m_dwRecordSize );
} // end for
// Copy user description
if ( insert >= col ) insert = col - 1;
strncpy( (char*)&pDesc[ insert * m_dwRecordSize ], pDescription, m_dwRecordSize );
// Copy types and descriptions
DWORD x = 0;
for ( i = 0; i < m_col; i++ )
{
// Skip insert point
if ( x == insert ) x++;
// Copy description
memcpy( &pDesc[ x * m_dwRecordSize ], &m_pDesc[ i * m_dwRecordSize ], m_dwRecordSize );
// Copy type
pTypes[ x ] = m_pTypes[ i ];
x++;
} // end for
// Copy rows
for ( DWORD r = 0; r < m_rowptr; r++ )
{ x = 0;
for ( i = 0; i < m_col; i++ )
{
// Skip insert point
if ( x == insert ) x++;
// Copy row information
memcpy( &pTable[ r ][ x * m_dwRecordSize ], &m_pTable[ r ][ i * m_dwRecordSize ], m_dwRecordSize );
x++;
} // end for
} // end for
// Lose the old csv
Destroy();
// Set the new table into variables and pray
m_pDesc = pDesc;
m_pTypes = pTypes;
m_pTable = pTable;
m_col = col;
m_row = row;
m_rowptr = rowptr;
return TRUE;
}
LPCTSTR CCsvFile::GetElementPtr(CSV_HROW hRow, LPCTSTR pCol, LPCTSTR pDef)
{_STTEX();
// Sanity checks
if ( hRow == NULL || pCol == NULL ) return pDef;
// Find matching column
DWORD dwCol = FindCol( pCol );
if ( dwCol == MAXDWORD ) return pDef;
// Return a pointer to the element value
return (LPSTR)GetRowElement( hRow, dwCol );
}