Line |
Branch |
Exec |
Source |
1 |
|
|
// Copyright (c) 2014, LAAS-CNRS |
2 |
|
|
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr) |
3 |
|
|
// |
4 |
|
|
|
5 |
|
|
// Redistribution and use in source and binary forms, with or without |
6 |
|
|
// modification, are permitted provided that the following conditions are |
7 |
|
|
// met: |
8 |
|
|
// |
9 |
|
|
// 1. Redistributions of source code must retain the above copyright |
10 |
|
|
// notice, this list of conditions and the following disclaimer. |
11 |
|
|
// |
12 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
|
// notice, this list of conditions and the following disclaimer in the |
14 |
|
|
// documentation and/or other materials provided with the distribution. |
15 |
|
|
// |
16 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 |
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 |
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 |
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 |
|
|
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 |
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 |
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 |
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 |
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 |
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
27 |
|
|
// DAMAGE. |
28 |
|
|
|
29 |
|
|
#include "hpp/util/factories/sequence.hh" |
30 |
|
|
|
31 |
|
|
#include <hpp/util/string.hh> |
32 |
|
|
#include <iostream> |
33 |
|
|
|
34 |
|
|
#include "hpp/util/debug.hh" |
35 |
|
|
|
36 |
|
|
namespace hpp { |
37 |
|
|
namespace util { |
38 |
|
|
namespace parser { |
39 |
|
|
namespace { |
40 |
|
|
|
41 |
|
|
// copy/paste from tinyxml2.cpp |
42 |
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && (!defined WINCE) |
43 |
|
|
#define TIXML_SSCANF sscanf_s |
44 |
|
|
#else |
45 |
|
|
#define TIXML_SSCANF sscanf |
46 |
|
|
#endif |
47 |
|
|
|
48 |
|
|
struct StringIsEmpty { |
49 |
|
|
bool operator()(std::string s) const { return s.empty(); } |
50 |
|
|
}; |
51 |
|
|
|
52 |
|
|
template <typename ValueType> |
53 |
|
|
bool cast(const std::string& str, ValueType* val) { |
54 |
|
|
hppDout(error, "Unkown type."); |
55 |
|
|
return false; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
template <> |
59 |
|
✗ |
bool cast<int>(const std::string& str, int* val) { |
60 |
|
✗ |
if (TIXML_SSCANF(str.c_str(), "%d", val) == 1) return true; |
61 |
|
✗ |
*val = 0; |
62 |
|
✗ |
return false; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
template <> |
66 |
|
✗ |
bool cast<unsigned int>(const std::string& str, unsigned int* val) { |
67 |
|
✗ |
if (TIXML_SSCANF(str.c_str(), "%u", val) == 1) return true; |
68 |
|
✗ |
*val = 0; |
69 |
|
✗ |
return false; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
template <> |
73 |
|
✗ |
bool cast<double>(const std::string& str, double* val) { |
74 |
|
✗ |
if (TIXML_SSCANF(str.c_str(), "%lf", val) == 1) return true; |
75 |
|
✗ |
*val = 0; |
76 |
|
✗ |
return false; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
template <> |
80 |
|
✗ |
bool cast<float>(const std::string& str, float* val) { |
81 |
|
✗ |
if (TIXML_SSCANF(str.c_str(), "%f", val) == 1) return true; |
82 |
|
✗ |
*val = 0; |
83 |
|
✗ |
return false; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
template <> |
87 |
|
✗ |
bool cast<bool>(const std::string& str, bool* val) { |
88 |
|
|
int iVal; |
89 |
|
✗ |
if (cast<int>(str, &iVal)) { |
90 |
|
✗ |
*val = (iVal == 0) ? false : true; |
91 |
|
✗ |
return true; |
92 |
|
|
} |
93 |
|
✗ |
if (str.compare("true") == 0) { |
94 |
|
✗ |
*val = true; |
95 |
|
✗ |
return true; |
96 |
|
|
} |
97 |
|
✗ |
if (str.compare("false") == 0) { |
98 |
|
✗ |
*val = false; |
99 |
|
✗ |
return true; |
100 |
|
|
} |
101 |
|
✗ |
*val = 0; |
102 |
|
✗ |
return false; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
template <> |
106 |
|
✗ |
bool cast<std::string>(const std::string& str, std::string* val) { |
107 |
|
✗ |
*val = str; |
108 |
|
✗ |
return true; |
109 |
|
|
} |
110 |
|
|
} // namespace |
111 |
|
|
|
112 |
|
|
template <typename ValueType> |
113 |
|
✗ |
void SequenceFactory<ValueType>::addTextChild(const XMLText* text) { |
114 |
|
✗ |
values_.clear(); |
115 |
|
✗ |
std::string t(text->Value()); |
116 |
|
|
typedef std::vector<std::string> strings_t; |
117 |
|
✗ |
strings_t values = string_split(t.begin(), t.end(), " \n\t\r"); |
118 |
|
✗ |
auto end = std::remove(values.begin(), values.end(), std::string()); |
119 |
|
✗ |
values.erase(end, values.end()); |
120 |
|
✗ |
if (size_ > 0 && values.size() != size_) |
121 |
|
✗ |
throw std::invalid_argument("Wrong sequence size"); |
122 |
|
|
|
123 |
|
✗ |
ValueType v; |
124 |
|
✗ |
for (const std::string& s : values) { |
125 |
|
✗ |
if (!cast<ValueType>(s, &v)) { |
126 |
|
|
hppDout(error, "could not parse value " << s); |
127 |
|
|
} |
128 |
|
✗ |
values_.push_back(v); |
129 |
|
|
} |
130 |
|
✗ |
} |
131 |
|
|
|
132 |
|
|
template <typename ValueType> |
133 |
|
✗ |
void SequenceFactory<ValueType>::impl_write(XMLElement* element) const { |
134 |
|
✗ |
std::stringstream ss; |
135 |
|
✗ |
for (typename OutType::const_iterator it = values_.begin(); |
136 |
|
✗ |
it != values_.end(); ++it) { |
137 |
|
✗ |
ss << *it << " "; |
138 |
|
|
} |
139 |
|
✗ |
XMLElement* el = element->GetDocument()->NewElement(ss.str().c_str()); |
140 |
|
✗ |
element->InsertEndChild(el); |
141 |
|
✗ |
} |
142 |
|
|
|
143 |
|
|
template class SequenceFactory<bool>; |
144 |
|
|
template class SequenceFactory<int>; |
145 |
|
|
template class SequenceFactory<unsigned int>; |
146 |
|
|
template class SequenceFactory<double>; |
147 |
|
|
template class SequenceFactory<float>; |
148 |
|
|
template class SequenceFactory<std::string>; |
149 |
|
|
} // namespace parser |
150 |
|
|
} // namespace util |
151 |
|
|
} // namespace hpp |
152 |
|
|
|