1 #include "tabbed_values.hxx"
5 SGTabbedValues::SGTabbedValues(const char *line) :
9 _fields.push_back(const_cast<char*>(line));
12 const char* SGTabbedValues::fieldAt(const unsigned int index) const
14 // we already computed that offset, cool
15 if (_fields.size() > index)
16 return _fields[index];
18 while (_fields.size() <= index) {
19 char* nextField = _fields.back();
20 if (*nextField=='\0') return NULL; // we went off the end
22 while (*nextField != '\t') {
23 if (*nextField=='\0') return NULL; // we went off the end
26 _fields.push_back(++nextField);
29 return _fields.back();
32 string SGTabbedValues::operator[](const unsigned int offset) const
34 const char *data = fieldAt(offset);
35 char* endPtr = const_cast<char*>(data);
37 while ((*endPtr != '\0') && (*endPtr != '\t')) {
41 return string(fieldAt(offset), len);
44 bool SGTabbedValues::isValueAt(const unsigned int offset) const
46 const char *data = fieldAt(offset);
47 return data && (*data != '\t'); // must be non-NULL and non-tab
50 char SGTabbedValues::getCharAt(const unsigned int offset) const
52 const char *data = fieldAt(offset);
53 if (!data || (*data == '\t'))
59 double SGTabbedValues::getDoubleAt(const unsigned int offset) const
61 const char *data = fieldAt(offset);
62 if (!data || (*data == '\t'))
65 /* this is safe because strtod will stop parsing when it sees an unrecogznied
66 character, which includes tab. */
67 return strtod(data, NULL);
70 long SGTabbedValues::getLongAt(const unsigned int offset) const
72 const char *data = fieldAt(offset);
73 if (!data || (*data == '\t'))
76 return strtol(data, NULL, 0);