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.
23 #include "tabbed_values.hxx"
27 SGTabbedValues::SGTabbedValues(const char *line) :
31 _fields.push_back(const_cast<char*>(line));
34 const char* SGTabbedValues::fieldAt(const unsigned int index) const
36 // we already computed that offset, cool
37 if (_fields.size() > index)
38 return _fields[index];
40 while (_fields.size() <= index) {
41 char* nextField = _fields.back();
42 if (*nextField=='\0') return NULL; // we went off the end
44 while (*nextField != '\t') {
45 if (*nextField=='\0') return NULL; // we went off the end
48 _fields.push_back(++nextField);
51 return _fields.back();
54 string SGTabbedValues::operator[](const unsigned int offset) const
56 const char *data = fieldAt(offset);
57 char* endPtr = const_cast<char*>(data);
59 while ((*endPtr != '\0') && (*endPtr != '\t')) {
63 return string(fieldAt(offset), len);
66 bool SGTabbedValues::isValueAt(const unsigned int offset) const
68 const char *data = fieldAt(offset);
69 return data && (*data != '\t'); // must be non-NULL and non-tab
72 char SGTabbedValues::getCharAt(const unsigned int offset) const
74 const char *data = fieldAt(offset);
75 if (!data || (*data == '\t'))
81 double SGTabbedValues::getDoubleAt(const unsigned int offset) const
83 const char *data = fieldAt(offset);
84 if (!data || (*data == '\t'))
87 /* this is safe because strtod will stop parsing when it sees an unrecogznied
88 character, which includes tab. */
89 return strtod(data, NULL);
92 long SGTabbedValues::getLongAt(const unsigned int offset) const
94 const char *data = fieldAt(offset);
95 if (!data || (*data == '\t'))
98 return strtol(data, NULL, 0);