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) :
33 _fields.push_back(const_cast<char*>(line));
36 const char* SGTabbedValues::fieldAt(const unsigned int index) const
38 // we already computed that offset, cool
39 if (_fields.size() > index)
40 return _fields[index];
42 while (_fields.size() <= index) {
43 char* nextField = _fields.back();
44 if (*nextField=='\0') return NULL; // we went off the end
46 while (*nextField != '\t') {
47 if (*nextField=='\0') return NULL; // we went off the end
50 _fields.push_back(++nextField);
53 return _fields.back();
56 string SGTabbedValues::operator[](const unsigned int offset) const
58 const char *data = fieldAt(offset);
59 char* endPtr = const_cast<char*>(data);
61 while ((*endPtr != '\0') && (*endPtr != '\t')) {
65 return string(fieldAt(offset), len);
68 bool SGTabbedValues::isValueAt(const unsigned int offset) const
70 const char *data = fieldAt(offset);
71 return data && (*data != '\t'); // must be non-NULL and non-tab
74 char SGTabbedValues::getCharAt(const unsigned int offset) const
76 const char *data = fieldAt(offset);
77 if (!data || (*data == '\t'))
83 double SGTabbedValues::getDoubleAt(const unsigned int offset) const
85 const char *data = fieldAt(offset);
86 if (!data || (*data == '\t'))
89 /* this is safe because strtod will stop parsing when it sees an unrecogznied
90 character, which includes tab. */
91 return std::strtod(data, NULL);
94 long SGTabbedValues::getLongAt(const unsigned int offset) const
96 const char *data = fieldAt(offset);
97 if (!data || (*data == '\t'))
100 return std::strtol(data, NULL, 0);