/
csv.cpp
284 lines (251 loc) · 6.35 KB
/
csv.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
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
// final.utils - Utility functions used by final projects.
// Copyright (c) 2004 - 2012 by Marek Sestak, marek.sestak@gmail.com
//
// 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 3 of the License, or
// (at your option) any later version.
//
// 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 "csv.h"
#include <stdio.h>
namespace utils {
void ReplaceSub32OrdinalsWithHexForm( TString& astring )
{
int sl = astring.Length();
for( int i=1; i<=sl; i++ )
{
if( astring[i] == '\\' )
{
astring.Insert( "\\", i );
i++;
sl++;
}
else
{
if( ((unsigned char)astring[i]) < 32 )
{
astring.Insert( "\\x" + utils::IntToHex((unsigned char)astring[i],2), i );
astring.Delete( i+4,1 );
sl += 3;
i += 3;
}
}
}
}
void ReplaceHexFormWithOrdinals( TString& astring )
{
int sl = astring.Length();
for( int i=1; i<=sl; i++ )
{
if( astring[i]=='\\' )
if( i<sl )
if( astring[i+1]=='\\' )
{
astring.Delete( i, 1 );
sl--;
}
else
{
if( astring[i+1]!='x' || i+3>sl )
throw TException( "String '"+astring+"' not in CSV format." );
char c = HexToInt( astring.c_str() + (i+1), 2 );
astring[i] = c;
astring.Delete( i+1, 3 );
sl -= 3;
}
}
}
int HexToInt( const char* ahex, int n )
{
int ret = 0;
for( int i=0; i<n; i++ )
{
ret *= 16;
if( *ahex>='0' && *ahex<='9' )
ret += *ahex-'0';
else if( *ahex>='A' && *ahex<='F' )
ret += *ahex-'A'+10;
else
throw TException( "Error while converting a hexadecimal number into a character." );
}
return ret;
}
TStringVector DecodeCSV( const TString& astring, char adelimiter )
{
TStringVector sv;
DecodeCSVString( sv, astring, adelimiter );
return sv;
}
TStringVector* DecodeCSVString( const TString& astring, char adelimiter )
{
TStringVector* strings = new TStringVector;
DecodeCSVString( *strings, astring, adelimiter );
return strings;
}
void DecodeCSVString( TStringVector &strings, const TString& astring, char adelimiter )
{
TString str;
str.SetLength( astring.Length() );
strings.clear();
if( astring.Length()==0 )
return;
enum { normal, parenthesis } state;
state = normal;
const char *beg = astring.c_str();
const char *c = beg;
char *destbeg = str.c_str();
char *dest = destbeg;
while( *c )
{
switch( state )
{
case normal:
switch( *c )
{
case '"':
state = parenthesis;
c++;
break;
default:
if( *c == adelimiter )
{
strings.push_back( TString( destbeg, dest-destbeg ) );
dest = destbeg;
}
else {
*dest = *c;
dest++;
}
c++;
}
break;
case parenthesis:
if( *c == '"' )
{
c++;
state = normal;
}
else {
*dest = *c;
c++;
dest++;
}
break;
default:
strings.clear();
/* TODO : Nen� upln� jasn�, zda v p��pad� failu smazat to, co se dosud ud�lalo... */
throw TException( "Unknown error in DecodeCSVString." );
}
}
strings.push_back( TString( destbeg, dest-destbeg ) );
unsigned int nstrings = strings.size();
unsigned int ui;
for( ui=0; ui<nstrings; ui++ )
ReplaceHexFormWithOrdinals( strings[ui] );
}
TString CSVString( const TString &astring, char adelimiter )
{
bool changed = false;
int pos;
TString word = astring;
ReplaceSub32OrdinalsWithHexForm( word );
changed = 0;
pos = 1;
while( pos<=word.Length() )
{
if( word[pos]==adelimiter )
{
changed = 1;
}
else if( word[pos]=='"' )
{
word.Insert( "\"", pos );
pos++;
changed = 1;
}
pos++;
}
if( changed )
word = "\"" + word + "\"";
return word;
}
void CSVString( const TStringVector &sv, TString &string, char adelimiter )
{
TString word;
int i;
int n = sv.size();
int totallength = 0;
// int pos = 0;
char *pos;
if( sv.size()==0 ) {
string = "\"\"";
return;
}
for( unsigned int i=0; i<n; i++ )
{
word = CSVString( sv[i], adelimiter );
totallength += word.Length();
}
if( n>0 )
totallength += n-1;
string.SetLength( totallength );
pos = string.c_str();
for( unsigned int i=0; i<n; i++ )
{
word = CSVString( sv[i], adelimiter );
strcpy( pos, word.c_str() );
pos += word.Length();
if( i<(n-1) ) {
*pos = adelimiter;
pos++;
}
}
}
TString CSVString( const TStringVector &strings, char adelimiter )
{
TString ret;
CSVString( strings, ret, adelimiter );
return ret;
}
TString CSVString( TStringVector* strings, char adelimiter )
{
if( !strings )
return "\"\"";
TString ret;
CSVString( *strings, ret, adelimiter );
return ret;
}
/*
TString StringVectorToString( const TStringVector &sv, TString &string, char adelimiter )
{
int totallength = 0;
int n = sv.size();
int i;
for( i=0; i<n; i++ )
totallength += sv[i].Length();
totallength += n-1;
// will add n-1 delimiters
string.SetLength( totallength );
char *c = string.c_str();
TString *s;
nrecords = sv.size();
for( i=0; i<nrecords; i++ ) {
s = &( sv[i] );
strcpy( c, s->c_str() );
c += s->Length();
if( i<(nrecords-1) ) {
*c = DefDelimiter;
c++;
}
}
}
*/
} // end of namespace utils
//---------------------------------------------------------------------------