1 // tabbed_values.cxx -- parse tab separated strings into fields
3 // Written by James Turner, started February 2003.
5 // Copyright (C) 2003 James Turner
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "tabbed_values.hxx"
29 SGTabbedValues::SGTabbedValues(const char *line)
32 _fields.push_back(const_cast<char*>(line));
35 const char* SGTabbedValues::fieldAt(const unsigned int index) const
37 // we already computed that offset, cool
38 if (_fields.size() > index)
39 return _fields[index];
41 while (_fields.size() <= index) {
42 char* nextField = _fields.back();
43 if (*nextField=='\0') return NULL; // we went off the end
45 while (*nextField != '\t') {
46 if (*nextField=='\0') return NULL; // we went off the end
49 _fields.push_back(++nextField);
52 return _fields.back();
55 string SGTabbedValues::operator[](const unsigned int offset) const
57 const char *data = fieldAt(offset);
58 char* endPtr = const_cast<char*>(data);
60 while ((*endPtr != '\0') && (*endPtr != '\t')) {
64 return string(fieldAt(offset), len);
67 bool SGTabbedValues::isValueAt(const unsigned int offset) const
69 const char *data = fieldAt(offset);
70 return data && (*data != '\t'); // must be non-NULL and non-tab
73 char SGTabbedValues::getCharAt(const unsigned int offset) const
75 const char *data = fieldAt(offset);
76 if (!data || (*data == '\t'))
82 double SGTabbedValues::getDoubleAt(const unsigned int offset) const
84 const char *data = fieldAt(offset);
85 if (!data || (*data == '\t'))
88 /* this is safe because strtod will stop parsing when it sees an unrecogznied
89 character, which includes tab. */
90 return std::strtod(data, NULL);
93 long SGTabbedValues::getLongAt(const unsigned int offset) const
95 const char *data = fieldAt(offset);
96 if (!data || (*data == '\t'))
99 return std::strtol(data, NULL, 0);