Driver/Volkslogger: rename function to ReadAllFlights()
[xcsoar.git] / src / IO / CSVLine.cpp
blob0b123fdbc638a0bb1e62879df3ce9365f2815bc3
1 /*
2 Copyright_License {
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"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdlib.h>
30 static const char *
31 EndOfLine(const char *line)
33 return line + strlen(line);
36 CSVLine::CSVLine(const char *line):
37 data(line), end(EndOfLine(line)) {}
39 size_t
40 CSVLine::Skip()
42 const char* _seperator = strchr(data, ',');
43 if (_seperator != NULL && _seperator < end) {
44 size_t length = _seperator - data;
45 data = _seperator + 1;
46 return length;
47 } else {
48 size_t length = end - data;
49 data = end;
50 return length;
54 char
55 CSVLine::ReadFirstChar()
57 char ch = *data;
58 return Skip() > 0 ? ch : '\0';
61 char
62 CSVLine::ReadOneChar()
64 char ch = *data;
65 return Skip() == 1 ? ch : '\0';
68 void
69 CSVLine::Read(char *dest, size_t size)
71 const char *src = data;
72 size_t length = Skip();
73 if (length >= size)
74 length = size - 1;
75 strncpy(dest, src, length);
76 dest[length] = '\0';
79 bool
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;
88 long
89 CSVLine::Read(long default_value)
91 ReadChecked(default_value);
92 return default_value;
95 unsigned
96 CSVLine::ReadHex(unsigned default_value)
98 char *endptr;
99 unsigned long value = strtoul(data, &endptr, 16);
100 assert(endptr >= data && endptr <= end);
101 if (endptr == data)
102 /* nothing was parsed */
103 value = default_value;
105 if (endptr >= end) {
106 data = end;
107 return value;
108 } else if (*endptr == ',') {
109 data = endptr + 1;
110 return value;
111 } else {
112 data = endptr;
113 Skip();
114 return default_value;
118 double
119 CSVLine::Read(double default_value)
121 ReadChecked(default_value);
122 return default_value;
125 bool
126 CSVLine::ReadChecked(double &value_r)
128 char *endptr;
129 double value = strtod(data, &endptr);
130 assert(endptr >= data && endptr <= end);
132 bool success = endptr > data;
133 if (endptr >= end) {
134 data = end;
135 } else if (*endptr == ',') {
136 data = endptr + 1;
137 } else {
138 data = endptr;
139 Skip();
140 return false;
143 if (success)
144 value_r = value;
145 return success;
148 #ifdef FIXED_MATH
150 fixed
151 CSVLine::Read(fixed default_value)
153 double value;
154 return ReadChecked(value)
155 ? fixed(value)
156 : default_value;
159 bool
160 CSVLine::ReadChecked(fixed &value_r)
162 double value;
163 if (ReadChecked(value)) {
164 value_r = fixed(value);
165 return true;
166 } else
167 return false;
170 #endif /* FIXED_MATH */
172 bool
173 CSVLine::ReadChecked(int &value_r)
175 long lvalue;
176 if (!ReadChecked(lvalue))
177 return false;
179 value_r = lvalue;
180 return true;
183 bool
184 CSVLine::ReadChecked(long &value_r)
186 char *endptr;
187 long value = strtol(data, &endptr, 10);
188 assert(endptr >= data && endptr <= end);
190 bool success = endptr > data;
191 if (endptr >= end) {
192 data = end;
193 } else if (*endptr == ',') {
194 data = endptr + 1;
195 } else {
196 data = endptr;
197 Skip();
198 return false;
201 if (success)
202 value_r = value;
203 return success;
206 bool
207 CSVLine::ReadHexChecked(unsigned &value_r)
209 char *endptr;
210 unsigned long value = strtoul(data, &endptr, 16);
211 assert(endptr >= data && endptr <= end);
213 bool success = endptr > data;
214 if (endptr >= end) {
215 data = end;
216 } else if (*endptr == ',') {
217 data = endptr + 1;
218 } else {
219 data = endptr;
220 Skip();
221 return false;
224 if (success)
225 value_r = value;
226 return success;
229 bool
230 CSVLine::ReadChecked(unsigned long &value_r)
232 char *endptr;
233 unsigned long value = strtoul(data, &endptr, 10);
234 assert(endptr >= data && endptr <= end);
236 bool success = endptr > data;
237 if (endptr >= end) {
238 data = end;
239 } else if (*endptr == ',') {
240 data = endptr + 1;
241 } else {
242 data = endptr;
243 Skip();
244 return false;
247 if (success)
248 value_r = value;
249 return success;
252 bool
253 CSVLine::ReadChecked(unsigned &value_r)
255 unsigned long lvalue;
256 if (!ReadChecked(lvalue))
257 return false;
259 value_r = lvalue;
260 return true;
263 bool
264 CSVLine::ReadCheckedCompare(fixed &value_r, const char *string)
266 fixed value;
267 if (ReadChecked(value)) {
268 if (ReadCompare(string)) {
269 value_r = value;
270 return true;
271 } else
272 return false;
273 } else {
274 Skip();
275 return false;