Repository navigation
Expand file tree
/
Copy pathInputFile.cpp
More file actions
391 lines (350 loc) · 8.16 KB
/
Copy pathInputFile.cpp
File metadata and controls
391 lines (350 loc) · 8.16 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
//////////////////////////////////////////////////////////////////////
// InputFile.cpp: implementation for the CInputFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdio.h"
#include "InputFile.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CInputFile::CInputFile()
{
m_nBufferLen = 0;
m_ProcessRecord = 0 ;
m_fp = NULL;
m_nColNum = 0;
m_nCurCol = 0;
m_bFixLen = false;
}
CInputFile::~CInputFile()
{
Close() ;
}
CInputFile& CInputFile::operator>>(string& rStrData)
{
char str[1024];
memset( str , 0 , sizeof(str));
GetData( m_nCurCol ,str ) ;
rStrData = str ;
m_nCurCol ++;
return *this;
}
CInputFile& CInputFile::operator>>(int& rIntData)
{
char str[1024];
memset( str , 0 , sizeof(str));
GetData( m_nCurCol ,str );
rIntData = atoi( str );
m_nCurCol ++;
return *this;
}
CInputFile& CInputFile::operator>>(long long& rIntData)
{
char str[1024];
memset( str , 0 , sizeof(str));
GetData( m_nCurCol ,str );
rIntData = atoll( str );
m_nCurCol ++;
return *this;
}
CInputFile& CInputFile::operator>>(long& rLongData)
{
char str[1024];
memset( str , 0 , sizeof(str));
GetData( m_nCurCol ,str );
rLongData = atol( str);
m_nCurCol ++;
return *this;
}
CInputFile& CInputFile::operator>>(double& rDoubleData)
{
char str[1024];
memset( str , 0 , sizeof(str));
GetData( m_nCurCol ,str );
rDoubleData = atof(str);
m_nCurCol ++;
return *this;
}
CInputFile& CInputFile::operator>>(char* CharData)
{
char str[1024];
memset( str , 0 , sizeof(str));
GetData( m_nCurCol ,str );
strcpy( CharData , str );
m_nCurCol ++;
return *this;
}
/*
int CInputFile::Open(const string & str , int* nCols , int nColNum)
{
return Open(str.c_str() , nCols , nColNum);
}
*/
//打开固定长度文件
int CInputFile::Open(const char *filename,int *nCols , int nColNum )
{
if ( (m_fp = fopen(filename,"r")) == NULL )
{
//theLog<< " open file failed!\n" << ende;
//theLog<< "open file failed!" << __FILE__ << __LINE__ << errno << ende;
printf("open file failed,errno:[%d]!",errno) ;
return -1;
}
m_bFixLen = true;//为固定长度
m_nColNum = nColNum;
for(int i = 0 ; i < nColNum ; i++)
{
m_nCols[ i ] = nCols[ i ];
}
m_ProcessRecord = 0;
m_nCurCol = 0;
return 0;
}
//打开非固定长度文件
int CInputFile::Open(char *filename,char chSplit, bool bIgnore, bool bSavespace )
{
const char* filename1 = filename;
return Open(filename1 , chSplit, bIgnore);
}
int CInputFile::Open(const char *filename,char chSplit, bool bIgnore, bool bSavespace )
{
if ( (m_fp = fopen(filename,"r")) == NULL )
{
//theLog<< " open file failed!\n" << ende;
//theLog<< "open file failed!" << __FILE__ << __LINE__ << errno << ende;
printf("open file failed,errno:[%d]!",errno) ;
return -1;
}
m_bFixLen = false;//为以分隔符分隔文件
m_chSplit = chSplit;
m_ProcessRecord = 0;
m_nCurCol = 0;
m_bIgnore = bIgnore;
m_bSavespace = bSavespace;
return 0;
}
int CInputFile::Close()
{
if ( m_fp )
fclose(m_fp);
m_fp = NULL ;
return 0 ;
}
int CInputFile::operator ++(int)
{
return FetchData();
}
string CInputFile::operator [](int nColNo)
{
char str[1024];
memset(str,0,sizeof(str));
GetData(nColNo , str);
return str;
}
void CInputFile::GetIntColumn(int nColNo , int &nValue)
{
char str[1024];
int n;
memset(str,0,sizeof(str));
GetData(nColNo , str);
nValue = atoi( str );
}
void CInputFile::GetDoubleColumn(int nColNo , double &dValue)
{
char str[1024];
memset(str,0,sizeof(str));
GetData(nColNo , str);
dValue = atof( str );
}
int CInputFile::FetchData()
{
int m_nCurColumn = 0;
memset(m_buffer , 0 , sizeof(m_buffer));
int n = util_getline( m_fp , 1024 ,m_buffer);
if(n >= 0)
{
m_nBufferLen = n;
m_ProcessRecord ++;
m_nCurCol = 0;
return n;
}
else
return 0;
}
// 取文件的一行 hm
int CInputFile::FetchData2(string & str)
{
int m_nCurColumn = 0;
memset(m_buffer , 0 , sizeof(m_buffer));
int n = util_getline( m_fp , 1024 ,m_buffer);
if(n >= 0)
{
m_nBufferLen = n;
m_ProcessRecord ++;
m_nCurCol = 0;
str.assign(m_buffer);
return n;
}
else
return 0;
}
bool CInputFile::GetData(int nCurCol ,char *str )
{
//定长文件,计算在文件行中的开始位置
if(m_bFixLen)
{
int nBeginPos = 0;
for(int i = 0 ; i < nCurCol ; i++)
nBeginPos += m_nCols[ i ];
int nEndPos = nBeginPos + m_nCols[nCurCol];
if(nEndPos > m_nBufferLen)
{
//theLog<< "get column failed!" << __FILE__ << __LINE__ << errno << ende;
printf("get column failed!") ;
return false;
}
strncpy(str , m_buffer + nBeginPos , nEndPos - nBeginPos);
str[ nEndPos - nBeginPos ] = '\0';
if(m_bSavespace == false) util_trim( str );
}
else if(!m_bIgnore)
{
char *szBeginPos = m_buffer;
for(int i = 0 ; i < nCurCol ; i ++)
{
char *szTemp = szBeginPos ;
szBeginPos = strchr( szTemp , m_chSplit );
if (i != (nCurCol-1)) szBeginPos++;
if( szBeginPos >= m_nBufferLen + m_buffer )
{
//theLog<< "get column failed!" << __FILE__ << __LINE__ << errno << ende;
printf("get column failed!") ;
return false;
}
}
if (szBeginPos == NULL) return false;
if (nCurCol != 0) szBeginPos++;
char *szEndPos = strchr( szBeginPos , m_chSplit );
if(szEndPos == NULL )
szEndPos = m_buffer + m_nBufferLen;
strncpy(str , szBeginPos, szEndPos - szBeginPos);
str[szEndPos - szBeginPos] = '\0';
if(m_bSavespace == false) util_trim( str );
}
else
{
char *szBeginPos = m_buffer;
for(int i = 0 ; i < nCurCol ; i ++)
{
char *szTemp = szBeginPos ;
szBeginPos = strchr( szTemp , m_chSplit );
while (*(szBeginPos) == m_chSplit) szBeginPos++;
if( szBeginPos >= m_nBufferLen + m_buffer )
{
//theLog<< "get column failed!" << __FILE__ << __LINE__ << errno << ende;
printf("get column failed!") ;
return false;
}
}
if (szBeginPos == NULL) return false;
char *szEndPos = strchr( szBeginPos , m_chSplit );
if(szEndPos == NULL )
szEndPos = m_buffer + m_nBufferLen;
strncpy(str , szBeginPos, szEndPos - szBeginPos);
str[szEndPos - szBeginPos] = '\0';
if(m_bSavespace == false) util_trim( str );
}
return true ;
// bIgnore=true 忽略连续的分隔符,即多个连续的分隔符看作是一个分隔符
}
int CInputFile::getColumnSize()
{
string strtmp(m_buffer);
int nCount = count(strtmp.begin(), strtmp.end(), m_chSplit);
return nCount;
}
/////////////////////////////////////////////////////////////////////////////////////////////
/*enforced getline function , you can specify the type of file*/
static const char *crlfs[] =
{
"\x0d\x0a", /* DOS/Windows style*/
"\x0a\x0d", /* UNIX style*/
"\x0d\x0d\x0a", /*UNIX style2*/
"\x0a" /*Macintosh style*/
};
int util_getlineex( FILE *fd , int nLineLen , char *line ,int nstype)
{
int c;
int i = 0;
int nCrlfPtr = 0;
char *crlf;
memset( line , 0 , sizeof( line) );
c = fgetc( fd );
if( c == EOF )
return -1;
if( nstype < 0 || nstype > 3 )
{
//theLog<< "util_getlineex nstype error!\n"<<__FILE__<<__LINE__<<ende ;
printf("util_getlineex nstype error!\n") ;
return -1;
}
crlf = (char *)crlfs[ nstype ];
while( c != EOF && i < nLineLen )
{
if ((char) c == crlf[nCrlfPtr])
{
nCrlfPtr ++;
//到达了换行符的中止位置
if (crlf[nCrlfPtr] == 0)
{
nCrlfPtr = 0;
break;
}
}
else
nCrlfPtr = 0;
line[ i++ ] = c;
c = fgetc(fd );
}
line[ i ] = '\0';
return i;
}
int util_getline( FILE *fd , int nLineLen , char *line)
{
int c;
int i = 0;
memset( line , 0 , sizeof( line) );
c = fgetc( fd );
if( c == EOF )
return -1;
while( c == '\r' || c == '\n' )
{
c = fgetc( fd );
if( c == EOF )
return -1;
}
while( c != '\r' && c != '\n' && c != EOF && i < nLineLen )
{
line[ i++ ] = char(c);
c = fgetc(fd );
}
line[ i ] = '\0';
return i;
}
/*delete control characters*/
void util_trim(char *buf )
{
char tmp[1024];
char *pbegin = buf;
int nlen = strlen(buf);
char *pend = buf + nlen - 1;
memset( tmp , 0 , sizeof( tmp ));
while( pbegin < buf + nlen && strchr( "\r\n\t " , *pbegin))
pbegin ++;
while( pend >= buf && strchr( "\r\n\t " , *pend))
pend --;
if( pend >= pbegin )
strncpy( tmp , pbegin , pend - pbegin + 1);
memset( buf , 0 , sizeof( buf ));
strcpy( buf , tmp );
}