1 /***************************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011-2013 PariahSoft LLC **
5 ***************************************/
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <boost/algorithm/string.hpp> // for iequals
32 #include <Gosu/Math.hpp>
38 * Matches regex /\s*-?\d+/
40 bool isInteger(const std::string
& s
)
48 for (size_t i
= 0; i
< s
.size(); i
++) {
51 if (isspace(c
)) continue;
56 if (c
== '-') continue;
59 if (isdigit(c
)) continue;
67 * Matches regex /\s*-?\d+\.?\d* / [sic: star-slash ends comment]
69 bool isDecimal(const std::string
& s
)
79 for (size_t i
= 0; i
< s
.size(); i
++) {
83 if (isspace(c
)) continue;
87 if (c
== '-') continue;
89 if (isdigit(c
)) continue;
93 if (c
== '.') continue;
96 if (isdigit(c
)) continue;
104 * Matches "5-7,2,12-14" no whitespace.
106 bool isRanges(const std::string
& s
)
117 for (size_t i
= 0; i
< s
.size(); i
++) {
122 if (c
== '-' || c
== '+') break;
124 if (isdigit(c
)) break;
129 if (dashed
) return false;
147 bool parseBool(const std::string
& s
)
149 // boost::equals is case-insensative
150 return boost::iequals(s
, "true") ||
151 boost::iequals(s
, "yes") ||
152 boost::iequals(s
, "on") ||
156 int parseUInt(const std::string
& s
)
158 int i
= atoi(s
.c_str());
159 return Gosu::clamp(i
, 0, INT_MAX
);
162 int parseInt100(const std::string
& s
)
164 int i
= atoi(s
.c_str());
165 return Gosu::clamp(i
, 0, 100);
168 std::vector
<std::string
> splitStr(const std::string
& input
,
169 const std::string
& delimiter
)
171 std::vector
<std::string
> strlist
;
174 for (size_t pos
= input
.find(delimiter
); pos
!= std::string::npos
; pos
= input
.find(delimiter
, i
)) {
175 if (input
.size() != i
) // Don't save empty strings
176 strlist
.push_back(input
.substr(i
, pos
- i
)); // Save
177 i
= pos
+ delimiter
.size();
180 if (input
.size() != i
)
181 strlist
.push_back(input
.substr(i
));
185 std::vector
<int> parseRanges(const std::string
& format
)
187 std::vector
<int> ints
;
188 typedef std::vector
<std::string
> StringVector
;
189 StringVector ranges
= splitStr(format
, ",");
190 for (StringVector::const_iterator it
= ranges
.begin(); it
!= ranges
.end(); it
++) {
191 const std::string
& range
= *it
;
192 size_t dash
= range
.find("-");
193 if (dash
== std::string::npos
) {
194 if (!isInteger(range
)) {
195 Log::err("parseRanges", "not an integer");
198 int i
= atoi(range
.c_str());
202 std::string rngbeg
= range
.substr(0, dash
);
203 std::string rngend
= range
.substr(dash
+ 1);
204 if (!isInteger(rngbeg
) || !isInteger(rngend
)) {
205 Log::err("parseRanges", "not an integer");
208 int beg
= atoi(rngbeg
.c_str());
209 int end
= atoi(rngend
.c_str());
211 Log::err("parseRanges", "beg > end");
214 for (int i
= beg
; i
<= end
; i
++)
221 std::string
itostr(int in
)
223 std::stringstream out
;