This repository has been archived by the owner on May 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 185
/
FFTextStream.cpp
212 lines (186 loc) · 5.69 KB
/
FFTextStream.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
//============================================================================
//
// This file is part of GPSTk, the GPS Toolkit.
//
// The GPSTk is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0 of the License, or
// any later version.
//
// The GPSTk 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with GPSTk; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
//
// Copyright 2004, The University of Texas at Austin
//
//============================================================================
//============================================================================
//
//This software developed by Applied Research Laboratories at the University of
//Texas at Austin, under contract to an agency or agencies within the U.S.
//Department of Defense. The U.S. Government retains all rights to use,
//duplicate, distribute, disclose, or release this software.
//
//Pursuant to DoD Directive 523024
//
// DISTRIBUTION STATEMENT A: This software has been approved for public
// release, distribution is unlimited.
//
//=============================================================================
/**
* @file FFTextStream.cpp
* An FFStream for text files
*/
#include "FFTextStream.hpp"
namespace gpstk
{
FFTextStream ::
FFTextStream()
{
init();
}
FFTextStream ::
~FFTextStream()
{
}
FFTextStream ::
FFTextStream( const char* fn,
std::ios::openmode mode )
: FFStream(fn, mode)
{
init();
}
FFTextStream ::
FFTextStream( const std::string& fn,
std::ios::openmode mode )
: FFStream( fn.c_str(), mode )
{
init();
}
void FFTextStream ::
open( const char* fn,
std::ios::openmode mode )
{
FFStream::open(fn, mode);
init();
}
void FFTextStream ::
open( const std::string& fn,
std::ios::openmode mode )
{
open(fn.c_str(), mode);
}
void FFTextStream ::
init()
{
lineNumber = 0;
}
void FFTextStream ::
tryFFStreamGet(FFData& rec)
throw(FFStreamError, gpstk::StringUtils::StringException)
{
unsigned int initialLineNumber = lineNumber;
try
{
FFStream::tryFFStreamGet(rec);
}
catch(gpstk::Exception& e)
{
e.addText( std::string("Near file line ") +
gpstk::StringUtils::asString(lineNumber) );
lineNumber = initialLineNumber;
mostRecentException = e;
conditionalThrow();
}
}
void FFTextStream ::
tryFFStreamPut(const FFData& rec)
throw(FFStreamError, gpstk::StringUtils::StringException)
{
unsigned int initialLineNumber = lineNumber;
try
{
FFStream::tryFFStreamPut(rec);
}
catch(gpstk::Exception& e)
{
e.addText( std::string("Near file line ") +
gpstk::StringUtils::asString(lineNumber) );
lineNumber = initialLineNumber;
mostRecentException = e;
conditionalThrow();
}
}
// the reason for checking ffs.eof() in the try AND catch block is
// because if the user enabled exceptions on the stream with exceptions()
// then eof could throw an exception, in which case we need to catch it
// and rethrow an EOF or FFStream exception. In any event, EndOfFile
// gets thrown whenever there's an EOF and expectEOF is true
void FFTextStream ::
formattedGetLine( std::string& line,
const bool expectEOF )
throw(EndOfFile, FFStreamError, gpstk::StringUtils::StringException)
{
try
{
std::getline(*this, line);
// Remove CR characters left over in the buffer from windows files
while (*line.rbegin() == '\r')
line.erase(line.end()-1);
for (int i=0; i<line.length(); i++)
if (!isprint(line[i]))
{
FFStreamError err("Non-text data in file.");
GPSTK_THROW(err);
}
lineNumber++;
if(fail() && !eof())
{
FFStreamError err("Line too long");
GPSTK_THROW(err);
}
// catch EOF when stream exceptions are disabled
if ((line.size() == 0) && eof())
{
if (expectEOF)
{
EndOfFile err("EOF encountered");
GPSTK_THROW(err);
}
else
{
FFStreamError err("Unexpected EOF encountered");
GPSTK_THROW(err);
}
}
}
catch(std::exception &e)
{
// catch EOF when exceptions are enabled
if ( (line.size() == 0) && eof())
{
if (expectEOF)
{
EndOfFile err("EOF encountered");
GPSTK_THROW(err);
}
else
{
FFStreamError err("Unexpected EOF");
GPSTK_THROW(err);
}
}
else
{
FFStreamError err("Critical file error: " +
std::string(e.what()));
GPSTK_THROW(err);
}
}
} // End of method 'FFTextStream::formattedGetLine()'
} // End of namespace gpstk