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