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