~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/terralib/kernel/TeAsciiFile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
ImportĀ upstreamĀ versionĀ 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright ļæ½ 2001-2004 INPE and Tecgraf/PUC-Rio.
 
4
 
 
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.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
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
*************************************************************************************/
 
23
 
 
24
#ifdef WIN32
 
25
#pragma warning ( disable: 4786 )
 
26
#endif
 
27
 
 
28
#include "TeAsciiFile.h"
 
29
#include "TeErrorLog.h"
 
30
#include "TeException.h"
 
31
#include "TeAssertions.h"
 
32
#include "TeDefines.h"
 
33
 
 
34
TeAsciiFile::TeAsciiFile ( const string& name, const char* mode ):      
 
35
        TeStdFile ( name, mode  ) 
 
36
{
 
37
}  
 
38
 
 
39
TeAsciiFile::~TeAsciiFile() 
 
40
{       
 
41
}  
 
42
 
 
43
void
 
44
TeAsciiFile::findNewLine ()
 
45
{
 
46
        if ( feof ( file_ ) != 0 )
 
47
                return;
 
48
        char ch = '0';
 
49
 
 
50
        while ( ch != '\n' )
 
51
        {
 
52
                fscanf ( file_, "%c", &ch );
 
53
                if ( feof ( file_ ) != 0 )
 
54
                        return;
 
55
        }
 
56
}
 
57
 
 
58
char 
 
59
TeAsciiFile::readQuotedChar()
 
60
{
 
61
        require ( feof ( file_ ) == 0 );
 
62
 
 
63
        char ch = ',', ch2;
 
64
 
 
65
        while ( ch != '"' )
 
66
        {
 
67
                fscanf ( file_, "%c", &ch );
 
68
        }
 
69
        fscanf ( file_, "%c", &ch );
 
70
        fscanf ( file_, "%c", &ch2 );
 
71
 
 
72
        ensure ( ch2 == '"' ); // just to make sure
 
73
return ch;
 
74
}
 
75
 
 
76
char 
 
77
TeAsciiFile::readChar()
 
78
{
 
79
        char ch=' ';
 
80
        require ( feof ( file_ ) == 0 );
 
81
        while ( ch == ' ' )
 
82
        {
 
83
                fscanf ( file_, "%c", &ch );
 
84
        }
 
85
        return ch;
 
86
}
 
87
 
 
88
 
 
89
string
 
90
TeAsciiFile::readString ()
 
91
{
 
92
        if ( feof ( file_ ) != 0 )
 
93
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
94
 
 
95
        char input [TeNAME_LENGTH];
 
96
        fscanf ( file_, "%s", input );
 
97
 
 
98
return input;
 
99
}
 
100
 
 
101
void
 
102
TeAsciiFile::writeString (const string& s)
 
103
{
 
104
        fprintf ( file_, "%s", s.c_str() );
 
105
}
 
106
 
 
107
string
 
108
TeAsciiFile::readLine ()
 
109
{
 
110
        if ( feof ( file_ ) != 0 )
 
111
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
112
 
 
113
        char input [TeNAME_LENGTH];
 
114
        fgets ( input, TeNAME_LENGTH, file_ );
 
115
 
 
116
return input;
 
117
}
 
118
 
 
119
string 
 
120
TeAsciiFile::readQuotedString()
 
121
{
 
122
        require ( feof ( file_ ) == 0 );
 
123
 
 
124
        char ch = ' ';
 
125
 
 
126
        string quote;
 
127
 
 
128
        while ( ch != '"' )
 
129
        {
 
130
                fscanf ( file_, "%c", &ch );
 
131
        }
 
132
 
 
133
        while ( true )
 
134
        {
 
135
                fscanf ( file_, "%c", &ch );
 
136
                if ( ch == '\n' || ch == '\r') 
 
137
                {
 
138
                        ungetc ( ch, file_ );
 
139
                        break;
 
140
                }
 
141
                if ( ch == '"'  ) break;
 
142
 
 
143
                quote = quote + ch;
 
144
        }
 
145
return quote;
 
146
}
 
147
 
 
148
void
 
149
TeAsciiFile::readStringList ( vector<string>& strlist )
 
150
{
 
151
        require ( file_ != 0 );
 
152
 
 
153
        this->readStringListCSV ( strlist, ' ');
 
154
}
 
155
 
 
156
void
 
157
TeAsciiFile::readStringListCSV ( vector<string>& strlist, const char sep)
 
158
{
 
159
 
 
160
        if ( feof ( file_ ) != 0 )
 
161
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
162
 
 
163
        char ch, lastChr = 0;
 
164
        while (fscanf ( file_, "%c", &ch ) !=  EOF )
 
165
        {
 
166
                if ( ch == '\n' || ch == '\r') 
 
167
                { 
 
168
                        if(lastChr==sep)
 
169
                                strlist.push_back ( "" );
 
170
 
 
171
                        ungetc ( ch, file_ );
 
172
                        return;
 
173
                }
 
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 );
 
179
                
 
180
                lastChr = ch;
 
181
        }
 
182
}
 
183
 
 
184
void
 
185
TeAsciiFile::readNStringCSV ( vector<string>& strlist, unsigned int n, const char sep)
 
186
{
 
187
 
 
188
        if ( feof ( file_ ) != 0 )
 
189
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
190
 
 
191
        strlist.clear();
 
192
        char ch, lastChr = 0;
 
193
        while (fscanf ( file_, "%c", &ch ) !=  EOF )
 
194
        {
 
195
                if ( ch == '\n' || ch == '\r') 
 
196
                { 
 
197
                        if(lastChr==sep)
 
198
                                strlist.push_back ( "" );
 
199
 
 
200
                        ungetc ( ch, file_ );
 
201
                        return;
 
202
                }
 
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)
 
209
                        break;
 
210
                
 
211
                lastChr = ch;
 
212
        }
 
213
}
 
214
 
 
215
string
 
216
TeAsciiFile::readStringCSV( const char del, bool skip, const char skip_char )
 
217
{
 
218
        require ( file_ != 0 );
 
219
 
 
220
        if ( feof ( file_ ) != 0 )
 
221
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
222
 
 
223
        char ch = '0';
 
224
        string line;
 
225
        bool inQuotes = false;
 
226
 
 
227
        int ret;
 
228
        while ( (ret=fscanf ( file_, "%c", &ch )) != EOF)
 
229
        {
 
230
                if ( ch == '\n' || ch == '\r') 
 
231
                {
 
232
                        ungetc ( ch, file_ );
 
233
                        break;
 
234
                }
 
235
                if ( ch == del  && !inQuotes ) break;
 
236
                if (skip)
 
237
                        if (ch == skip_char) 
 
238
                        {
 
239
                                if (skip_char == '"')
 
240
                                        inQuotes = !inQuotes;
 
241
                                continue;
 
242
                        }
 
243
                line = line + ch;
 
244
        }
 
245
        return line;
 
246
}
 
247
 
 
248
string
 
249
TeAsciiFile::readStringCSVNoSpace( const char del )
 
250
{
 
251
        char blank = ' ';
 
252
        string line = readStringCSV ( del, true, blank );
 
253
return line;
 
254
}
 
255
 
 
256
string
 
257
TeAsciiFile::readStringCSVNoQuote( const char del )
 
258
{
 
259
        char quote = '"';
 
260
        string line = readStringCSV ( del, true, quote );
 
261
return line;
 
262
}
 
263
 
 
264
int
 
265
TeAsciiFile::readInt ()
 
266
{
 
267
        require ( file_ != 0 );
 
268
 
 
269
        if ( feof ( file_ ) != 0 )
 
270
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
271
        
 
272
        int value;
 
273
        fscanf ( file_, "%d", &value );
 
274
 
 
275
return value;
 
276
}
 
277
 
 
278
int
 
279
TeAsciiFile::readIntCSV ( const char del )
 
280
{
 
281
        char blank  = ' ';
 
282
        string line = readStringCSV ( del, true, blank );
 
283
 
 
284
return atoi(line.c_str());
 
285
}
 
286
 
 
287
double
 
288
TeAsciiFile::readFloatCSV ( const char del )
 
289
{
 
290
        char blank = ' ';
 
291
        string line = readStringCSV ( del, true, blank );
 
292
 
 
293
return atof(line.c_str());
 
294
}
 
295
 
 
296
 
 
297
double
 
298
TeAsciiFile::readFloat ()
 
299
{
 
300
        require ( file_ != 0 );
 
301
 
 
302
        if ( feof ( file_ ) != 0 )
 
303
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
304
        
 
305
        double value;
 
306
        fscanf ( file_, "%lf", &value );
 
307
 
 
308
return value;
 
309
}
 
310
 
 
311
TeCoord2D
 
312
TeAsciiFile::readCoord2D ()
 
313
{
 
314
        require ( file_ != 0 );
 
315
 
 
316
        if ( feof ( file_ ) != 0 )
 
317
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
318
 
 
319
        double x, y;
 
320
        fscanf ( file_, "%lf %lf", &x, &y);
 
321
 
 
322
return TeCoord2D( x,  y );
 
323
}
 
324
 
 
325
TeBox
 
326
TeAsciiFile::readBox() 
 
327
{
 
328
        require ( file_ != 0 );
 
329
 
 
330
        if ( feof ( file_ ) != 0 )
 
331
        {
 
332
                throw TeException ( END_OF_FILE_REACHED, name(), true );
 
333
        }
 
334
        double x1, y1, x2, y2;
 
335
 
 
336
 
 
337
        fscanf ( file_, "%lf %lf %lf %lf", &x1, &y1, &x2, &y2 );
 
338
 
 
339
return TeBox ( x1, y1, x2, y2 );
 
340
 
 
341
}
 
342