-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSVFile.cpp
95 lines (83 loc) · 2.05 KB
/
CSVFile.cpp
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
/*//////////////////////////////////////////////////////////////////////////////
// Name: cvsfile.cpp
// Purpose: Implementation of CCSVFile, a CFile descentand with CSV-parsing
// Author: Ruediger Herrmann
// Copyright: (c) Ruediger Herrmann
//////////////////////////////////////////////////////////////////////////////*/
#include "stdafx.h"
#include "CSVFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CCSVFile::CCSVFile() {
}
CCSVFile::~CCSVFile() {
}
void CCSVFile::ParseString ( CString& Line, CStringArray& Items, const CString& ExternalSeparatorChar ) {
// clear
Items.RemoveAll();
// declare helper variables
int i;
BOOL InQuotation = FALSE;
CString Part = _T("");
CString SeparatorChar;
// decode separator char
if ( ExternalSeparatorChar.CompareNoCase ( _T("<Tab>") ) == 0 )
SeparatorChar = _T("\t");
else
SeparatorChar = ExternalSeparatorChar;
/*
//
for ( i = 0; i <= Line.GetLength() - 1; i++ ) {
if ( Line[i] == QuotationMark && ( ( i <= Line.GetLength() - 1 && Line[i+1] != QuotationMark ) || i+1 > Line.GetLength() - 1 ) )
InQuotation = !InQuotation;
else
if ( !InQuotation && Line[i] == SeparatorChar ) {
Items.Add ( Part );
Part.Empty();
}
else
Part += Line[i];
}
if ( Part != _T("") )
Items.Add ( Part );
*/
//
i = 0;
while ( i <= Line.GetLength() -1 )
{
if ( InQuotation )
{
if ( Line.GetAt ( i ) == _T("\"")[0] )
{
if ( i < Line.GetLength() - 1 && Line[i+1] == _T("\"")[0] )
{
Part += Line[i];
i++;
}
else
InQuotation = !InQuotation;
}
else
Part += Line[i];
}
else
{
if ( Line[i] == SeparatorChar )
{
Items.Add ( Part );
Part.Empty();
}
else
if ( Line[i] == _T("\"")[0] )
InQuotation = TRUE;
else
Part += Line[i];
}
i++;
}
if ( !Part.IsEmpty() )
Items.Add ( Part );
}