]> git.mxchange.org Git - simgear.git/blob - simgear/misc/tabbed_values.cxx
Modified Files:
[simgear.git] / simgear / misc / tabbed_values.cxx
1 // tabbed_values.cxx -- parse tab separated strings into fields
2 //
3 // Written by James Turner, started February 2003.
4 //
5 // Copyright (C) 2003 James Turner
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #include "tabbed_values.hxx"
24
25 #include "assert.h"
26
27 SGTabbedValues::SGTabbedValues(const char *line) :
28         _line(line)
29 {
30         assert(line);
31         _fields.push_back(const_cast<char*>(line));
32 }
33
34 const char* SGTabbedValues::fieldAt(const unsigned int index) const
35 {
36         // we already computed that offset, cool
37         if (_fields.size() > index)
38                 return _fields[index];
39
40         while (_fields.size() <= index) {
41                 char* nextField = _fields.back();
42                 if (*nextField=='\0') return NULL; // we went off the end
43                         
44                 while (*nextField != '\t') {
45                         if (*nextField=='\0') return NULL; // we went off the end
46                         ++nextField;
47                 }
48                 _fields.push_back(++nextField);
49         }
50         
51         return _fields.back();
52 }
53
54 string SGTabbedValues::operator[](const unsigned int offset) const
55 {
56         const char *data = fieldAt(offset);
57         char* endPtr = const_cast<char*>(data);
58         int len = 0;
59         while ((*endPtr != '\0') && (*endPtr != '\t')) {
60                 ++len;
61                 ++endPtr;
62         }
63         return string(fieldAt(offset), len);
64 }
65
66 bool SGTabbedValues::isValueAt(const unsigned int offset) const
67 {
68         const char *data = fieldAt(offset);
69         return data && (*data != '\t'); // must be non-NULL and non-tab
70 }
71
72 char SGTabbedValues::getCharAt(const unsigned int offset) const
73 {
74         const char *data = fieldAt(offset);
75         if (!data || (*data == '\t'))
76                 return 0;
77         
78         return *data;
79 }
80
81 double SGTabbedValues::getDoubleAt(const unsigned int offset) const
82 {
83         const char *data = fieldAt(offset);
84         if (!data || (*data == '\t'))
85                 return 0;
86                 
87         /* this is safe because strtod will stop parsing when it sees an unrecogznied
88         character, which includes tab. */       
89         return strtod(data, NULL);
90 }
91
92 long SGTabbedValues::getLongAt(const unsigned int offset) const
93 {
94         const char *data = fieldAt(offset);
95         if (!data || (*data == '\t'))
96                 return 0;
97
98         return strtol(data, NULL, 0);
99 }