4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "NMEA/InputLine.hpp"
31 EndOfLine(const char *line
)
33 return line
+ strlen(line
);
36 CSVLine::CSVLine(const char *line
):
37 data(line
), end(EndOfLine(line
)) {}
42 const char* _seperator
= strchr(data
, ',');
43 if (_seperator
!= NULL
&& _seperator
< end
) {
44 size_t length
= _seperator
- data
;
45 data
= _seperator
+ 1;
48 size_t length
= end
- data
;
55 CSVLine::ReadFirstChar()
58 return Skip() > 0 ? ch
: '\0';
62 CSVLine::ReadOneChar()
65 return Skip() == 1 ? ch
: '\0';
69 CSVLine::Read(char *dest
, size_t size
)
71 const char *src
= data
;
72 size_t length
= Skip();
75 strncpy(dest
, src
, length
);
80 CSVLine::ReadCompare(const char *value
)
82 size_t length
= strlen(value
);
83 char buffer
[length
+ 2];
84 Read(buffer
, length
+ 2);
85 return strcmp(buffer
, value
) == 0;
89 CSVLine::Read(long default_value
)
91 ReadChecked(default_value
);
96 CSVLine::ReadHex(unsigned default_value
)
99 unsigned long value
= strtoul(data
, &endptr
, 16);
100 assert(endptr
>= data
&& endptr
<= end
);
102 /* nothing was parsed */
103 value
= default_value
;
108 } else if (*endptr
== ',') {
114 return default_value
;
119 CSVLine::Read(double default_value
)
121 ReadChecked(default_value
);
122 return default_value
;
126 CSVLine::ReadChecked(double &value_r
)
129 double value
= strtod(data
, &endptr
);
130 assert(endptr
>= data
&& endptr
<= end
);
132 bool success
= endptr
> data
;
135 } else if (*endptr
== ',') {
151 CSVLine::Read(fixed default_value
)
154 return ReadChecked(value
)
160 CSVLine::ReadChecked(fixed
&value_r
)
163 if (ReadChecked(value
)) {
164 value_r
= fixed(value
);
170 #endif /* FIXED_MATH */
173 CSVLine::ReadChecked(int &value_r
)
176 if (!ReadChecked(lvalue
))
184 CSVLine::ReadChecked(long &value_r
)
187 long value
= strtol(data
, &endptr
, 10);
188 assert(endptr
>= data
&& endptr
<= end
);
190 bool success
= endptr
> data
;
193 } else if (*endptr
== ',') {
207 CSVLine::ReadHexChecked(unsigned &value_r
)
210 unsigned long value
= strtoul(data
, &endptr
, 16);
211 assert(endptr
>= data
&& endptr
<= end
);
213 bool success
= endptr
> data
;
216 } else if (*endptr
== ',') {
230 CSVLine::ReadChecked(unsigned long &value_r
)
233 unsigned long value
= strtoul(data
, &endptr
, 10);
234 assert(endptr
>= data
&& endptr
<= end
);
236 bool success
= endptr
> data
;
239 } else if (*endptr
== ',') {
253 CSVLine::ReadChecked(unsigned &value_r
)
255 unsigned long lvalue
;
256 if (!ReadChecked(lvalue
))
264 CSVLine::ReadCheckedCompare(fixed
&value_r
, const char *string
)
267 if (ReadChecked(value
)) {
268 if (ReadCompare(string
)) {