1 // Copyright (C) 2009 - 2010 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #include "HLADataElement.hxx"
20 #include <simgear/debug/logstream.hxx>
24 HLADataElement::PathElement::Data::~Data()
28 const HLADataElement::PathElement::FieldData*
29 HLADataElement::PathElement::Data::toFieldData() const
34 const HLADataElement::PathElement::IndexData*
35 HLADataElement::PathElement::Data::toIndexData() const
40 HLADataElement::PathElement::FieldData::FieldData(const std::string& name) :
45 HLADataElement::PathElement::FieldData::~FieldData()
49 const HLADataElement::PathElement::FieldData*
50 HLADataElement::PathElement::FieldData::toFieldData() const
56 HLADataElement::PathElement::FieldData::less(const Data* data) const
58 const FieldData* fieldData = data->toFieldData();
59 // IndexData is allways smaller than FieldData
62 return _name < fieldData->_name;
66 HLADataElement::PathElement::FieldData::equal(const Data* data) const
68 const FieldData* fieldData = data->toFieldData();
69 // IndexData is allways smaller than FieldData
72 return _name == fieldData->_name;
76 HLADataElement::PathElement::FieldData::append(std::string& s) const
78 s.append(1, std::string::value_type('.'));
82 HLADataElement::PathElement::IndexData::IndexData(unsigned index) :
87 HLADataElement::PathElement::IndexData::~IndexData()
91 const HLADataElement::PathElement::IndexData*
92 HLADataElement::PathElement::IndexData::toIndexData() const
98 HLADataElement::PathElement::IndexData::less(const Data* data) const
100 const IndexData* indexData = data->toIndexData();
101 // IndexData is allways smaller than FieldData
104 return _index < indexData->_index;
108 HLADataElement::PathElement::IndexData::equal(const Data* data) const
110 const IndexData* indexData = data->toIndexData();
111 // IndexData is allways smaller than FieldData
114 return _index == indexData->_index;
118 HLADataElement::PathElement::IndexData::append(std::string& s) const
120 s.append(1, std::string::value_type('['));
121 unsigned value = _index;
123 s.append(1, std::string::value_type('0' + value % 10));
124 } while (value /= 10);
125 s.append(1, std::string::value_type(']'));
128 HLADataElement::~HLADataElement()
133 HLADataElement::toString(const Path& path)
136 for (Path::const_iterator i = path.begin(); i != path.end(); ++i)
141 HLADataElement::AttributePathPair
142 HLADataElement::toAttributePathPair(const std::string& s)
145 // Skip the initial attribute name if given
146 std::string::size_type i = s.find_first_of("[.");
147 std::string attribute = s.substr(0, i);
148 while (i < s.size()) {
152 while (i < s.size()) {
155 unsigned v = s[i] - '0';
158 SG_LOG(SG_NETWORK, SG_WARN, "HLADataElement: invalid character in array subscript for \""
159 << s << "\" at \"" << attribute << toString(path) << "\"!");
160 return AttributePathPair();
166 path.push_back(index);
171 // Error, . cannot be last
172 if (s.size() <= ++i) {
173 SG_LOG(SG_NETWORK, SG_WARN, "HLADataElement: invalid terminating '.' for \""
175 return AttributePathPair();
177 std::string::size_type e = s.find_first_of("[.", i);
178 path.push_back(s.substr(i, e - i));
184 return AttributePathPair(attribute, path);