1 #include "tabbed_values.hxx"
3 SGTabbedValues::SGTabbedValues(const char *line) :
7 _fields.push_back(const_cast<char*>(line));
10 const char* SGTabbedValues::fieldAt(const unsigned int index) const
12 // we already computed that offset, cool
13 if (_fields.size() > index)
14 return _fields[index];
16 while (_fields.size() <= index) {
17 char* nextField = _fields.back();
18 if (*nextField=='\0') return NULL; // we went off the end
20 while (*nextField != '\t') {
21 if (*nextField=='\0') return NULL; // we went off the end
24 _fields.push_back(++nextField);
27 return _fields.back();
30 string SGTabbedValues::operator[](const unsigned int offset) const
32 const char *data = fieldAt(offset);
33 char* endPtr = const_cast<char*>(data);
35 while ((*endPtr != '\0') && (*endPtr != '\t')) {
39 return string(fieldAt(offset), len);
42 bool SGTabbedValues::isValueAt(const unsigned int offset) const
44 const char *data = fieldAt(offset);
45 return data && (*data != '\t'); // must be non-NULL and non-tab
48 char SGTabbedValues::getCharAt(const unsigned int offset) const
50 const char *data = fieldAt(offset);
51 if (!data || (*data == '\t'))
57 double SGTabbedValues::getDoubleAt(const unsigned int offset) const
59 const char *data = fieldAt(offset);
60 if (!data || (*data == '\t'))
63 /* this is safe because strtod will stop parsing when it sees an unrecogznied
64 character, which includes tab. */
65 return strtod(data, NULL);
68 long SGTabbedValues::getLongAt(const unsigned int offset) const
70 const char *data = fieldAt(offset);
71 if (!data || (*data == '\t'))
74 return strtol(data, NULL, 0);