5 * Copyright (C) Bob 2009
7 * boblight is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * boblight is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
32 void PrintError(const std::string
& error
);
33 bool GetWord(std::string
& data
, std::string
& word
);
34 void ConvertFloatLocale(std::string
& strfloat
);
36 template <class Value
>
37 inline std::string
ToString(Value value
)
40 std::stringstream valuestream
;
46 inline std::string
GetErrno()
48 return strerror(errno
);
51 inline std::string
GetErrno(int err
)
56 template <class A
, class B
, class C
>
57 inline A
Clamp(A value
, B min
, C max
)
59 return value
< max
? (value
> min
? value
: min
) : max
;
62 template <class A
, class B
>
63 inline A
Max(A value1
, B value2
)
65 return value1
> value2
? value1
: value2
;
68 template <class A
, class B
, class C
>
69 inline A
Max(A value1
, B value2
, C value3
)
71 return (value1
> value2
) ? (value1
> value3
? value1
: value3
) : (value2
> value3
? value2
: value3
);
74 template <class A
, class B
>
75 inline A
Min(A value1
, B value2
)
77 return value1
< value2
? value1
: value2
;
80 template <class A
, class B
, class C
>
81 inline A
Min(A value1
, B value2
, C value3
)
83 return (value1
< value2
) ? (value1
< value3
? value1
: value3
) : (value2
< value3
? value2
: value3
);
89 return value
> 0 ? value
: -value
;
92 template <class A
, class B
>
93 inline A
Round(B value
)
101 return (A
)(value
+ 0.5);
105 return (A
)(value
- 0.5);
109 //inline int32_t Round32(float value)
111 // return lroundf(value);
114 //inline int32_t Round32(double value)
116 // return lround(value);
119 //inline int64_t Round64(float value)
121 // return llroundf(value);
124 //inline int64_t Round64(double value)
126 // return llround(value);
129 inline bool StrToInt(const std::string
& data
, int& value
)
131 return sscanf(data
.c_str(), "%i", &value
) == 1;
134 inline bool HexStrToInt(const std::string
& data
, int& value
)
136 return sscanf(data
.c_str(), "%x", &value
) == 1;
139 inline bool StrToFloat(const std::string
& data
, float& value
)
141 return sscanf(data
.c_str(), "%f", &value
) == 1;
144 inline bool StrToFloat(const std::string
& data
, double& value
)
146 return sscanf(data
.c_str(), "%lf", &value
) == 1;
149 inline bool StrToBool(const std::string
& data
, bool& value
)
151 std::string data2
= data
;
153 if (!GetWord(data2
, word
))
156 if (word
== "1" || word
== "true" || word
== "on" || word
== "yes")
161 else if (word
== "0" || word
== "false" || word
== "off" || word
== "no")
169 if (StrToInt(word
, ivalue
))