1
/************************************************************************************
2
TerraLib - a library for developing GIS applications.
3
Copyright ļæ½ 2001-2004 INPE and Tecgraf/PUC-Rio.
5
This code is part of the TerraLib library.
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) any later version.
11
You should have received a copy of the GNU Lesser General Public
12
License along with this library.
14
The authors reassure the license terms regarding the warranties.
15
They specifically disclaim any warranties, including, but not limited to,
16
the implied warranties of merchantability and fitness for a particular purpose.
17
The library provided hereunder is on an "as is" basis, and the authors have no
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
20
indirect, special, incidental, or consequential damages arising out of the use
21
of this library and its documentation.
22
*************************************************************************************/
25
#pragma warning ( disable: 4786 )
28
#include "TeAsciiFile.h"
29
#include "TeErrorLog.h"
30
#include "TeException.h"
31
#include "TeAssertions.h"
32
#include "TeDefines.h"
34
TeAsciiFile::TeAsciiFile ( const string& name, const char* mode ):
35
TeStdFile ( name, mode )
39
TeAsciiFile::~TeAsciiFile()
44
TeAsciiFile::findNewLine ()
46
if ( feof ( file_ ) != 0 )
52
fscanf ( file_, "%c", &ch );
53
if ( feof ( file_ ) != 0 )
59
TeAsciiFile::readQuotedChar()
61
require ( feof ( file_ ) == 0 );
67
fscanf ( file_, "%c", &ch );
69
fscanf ( file_, "%c", &ch );
70
fscanf ( file_, "%c", &ch2 );
72
ensure ( ch2 == '"' ); // just to make sure
77
TeAsciiFile::readChar()
80
require ( feof ( file_ ) == 0 );
83
fscanf ( file_, "%c", &ch );
90
TeAsciiFile::readString ()
92
if ( feof ( file_ ) != 0 )
93
throw TeException ( END_OF_FILE_REACHED, name(), true );
95
char input [TeNAME_LENGTH];
96
fscanf ( file_, "%s", input );
102
TeAsciiFile::writeString (const string& s)
104
fprintf ( file_, "%s", s.c_str() );
108
TeAsciiFile::readLine ()
110
if ( feof ( file_ ) != 0 )
111
throw TeException ( END_OF_FILE_REACHED, name(), true );
113
char input [TeNAME_LENGTH];
114
fgets ( input, TeNAME_LENGTH, file_ );
120
TeAsciiFile::readQuotedString()
122
require ( feof ( file_ ) == 0 );
130
fscanf ( file_, "%c", &ch );
135
fscanf ( file_, "%c", &ch );
136
if ( ch == '\n' || ch == '\r')
138
ungetc ( ch, file_ );
141
if ( ch == '"' ) break;
149
TeAsciiFile::readStringList ( vector<string>& strlist )
151
require ( file_ != 0 );
153
this->readStringListCSV ( strlist, ' ');
157
TeAsciiFile::readStringListCSV ( vector<string>& strlist, const char sep)
160
if ( feof ( file_ ) != 0 )
161
throw TeException ( END_OF_FILE_REACHED, name(), true );
163
char ch, lastChr = 0;
164
while (fscanf ( file_, "%c", &ch ) != EOF )
166
if ( ch == '\n' || ch == '\r')
169
strlist.push_back ( "" );
171
ungetc ( ch, file_ );
174
ungetc ( ch, file_ );
175
// there are still more values to be read
176
string name = readStringCSV (sep);
177
if ( name.size() != 0 || sep != ' ')
178
strlist.push_back ( name );
185
TeAsciiFile::readNStringCSV ( vector<string>& strlist, unsigned int n, const char sep)
188
if ( feof ( file_ ) != 0 )
189
throw TeException ( END_OF_FILE_REACHED, name(), true );
192
char ch, lastChr = 0;
193
while (fscanf ( file_, "%c", &ch ) != EOF )
195
if ( ch == '\n' || ch == '\r')
198
strlist.push_back ( "" );
200
ungetc ( ch, file_ );
203
ungetc ( ch, file_ );
204
// there are still more values to be read
205
string name = readStringCSV (sep);
206
if ( name.size() != 0 || sep != ' ')
207
strlist.push_back ( name );
208
if (strlist.size() == n)
216
TeAsciiFile::readStringCSV( const char del, bool skip, const char skip_char )
218
require ( file_ != 0 );
220
if ( feof ( file_ ) != 0 )
221
throw TeException ( END_OF_FILE_REACHED, name(), true );
225
bool inQuotes = false;
228
while ( (ret=fscanf ( file_, "%c", &ch )) != EOF)
230
if ( ch == '\n' || ch == '\r')
232
ungetc ( ch, file_ );
235
if ( ch == del && !inQuotes ) break;
239
if (skip_char == '"')
240
inQuotes = !inQuotes;
249
TeAsciiFile::readStringCSVNoSpace( const char del )
252
string line = readStringCSV ( del, true, blank );
257
TeAsciiFile::readStringCSVNoQuote( const char del )
260
string line = readStringCSV ( del, true, quote );
265
TeAsciiFile::readInt ()
267
require ( file_ != 0 );
269
if ( feof ( file_ ) != 0 )
270
throw TeException ( END_OF_FILE_REACHED, name(), true );
273
fscanf ( file_, "%d", &value );
279
TeAsciiFile::readIntCSV ( const char del )
282
string line = readStringCSV ( del, true, blank );
284
return atoi(line.c_str());
288
TeAsciiFile::readFloatCSV ( const char del )
291
string line = readStringCSV ( del, true, blank );
293
return atof(line.c_str());
298
TeAsciiFile::readFloat ()
300
require ( file_ != 0 );
302
if ( feof ( file_ ) != 0 )
303
throw TeException ( END_OF_FILE_REACHED, name(), true );
306
fscanf ( file_, "%lf", &value );
312
TeAsciiFile::readCoord2D ()
314
require ( file_ != 0 );
316
if ( feof ( file_ ) != 0 )
317
throw TeException ( END_OF_FILE_REACHED, name(), true );
320
fscanf ( file_, "%lf %lf", &x, &y);
322
return TeCoord2D( x, y );
326
TeAsciiFile::readBox()
328
require ( file_ != 0 );
330
if ( feof ( file_ ) != 0 )
332
throw TeException ( END_OF_FILE_REACHED, name(), true );
334
double x1, y1, x2, y2;
337
fscanf ( file_, "%lf %lf %lf %lf", &x1, &y1, &x2, &y2 );
339
return TeBox ( x1, y1, x2, y2 );