cec: fix compilation on windows after interface changes
[deb_libcec.git] / src / lib / util / misc.h
... / ...
CommitLineData
1#pragma once
2
3/*
4 * boblight
5 * Copyright (C) Bob 2009
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <string>
22#include <sstream>
23#include <exception>
24#include <stdexcept>
25
26#include <stdint.h>
27#include <errno.h>
28#include <string.h>
29#include <stdio.h>
30#include <math.h>
31
32void PrintError(const std::string& error);
33bool GetWord(std::string& data, std::string& word);
34void ConvertFloatLocale(std::string& strfloat);
35
36template <class Value>
37inline std::string ToString(Value value)
38{
39 std::string data;
40 std::stringstream valuestream;
41 valuestream << value;
42 valuestream >> data;
43 return data;
44}
45
46inline std::string GetErrno()
47{
48 return strerror(errno);
49}
50
51inline std::string GetErrno(int err)
52{
53 return strerror(err);
54}
55
56template <class A, class B, class C>
57inline A Clamp(A value, B min, C max)
58{
59 return value < max ? (value > min ? value : min) : max;
60}
61
62template <class A, class B>
63inline A Max(A value1, B value2)
64{
65 return value1 > value2 ? value1 : value2;
66}
67
68template <class A, class B, class C>
69inline A Max(A value1, B value2, C value3)
70{
71 return (value1 > value2) ? (value1 > value3 ? value1 : value3) : (value2 > value3 ? value2 : value3);
72}
73
74template <class A, class B>
75inline A Min(A value1, B value2)
76{
77 return value1 < value2 ? value1 : value2;
78}
79
80template <class A, class B, class C>
81inline A Min(A value1, B value2, C value3)
82{
83 return (value1 < value2) ? (value1 < value3 ? value1 : value3) : (value2 < value3 ? value2 : value3);
84}
85
86template <class T>
87inline T Abs(T value)
88{
89 return value > 0 ? value : -value;
90}
91
92template <class A, class B>
93inline A Round(B value)
94{
95 if (value == 0.0)
96 {
97 return 0;
98 }
99 else if (value > 0.0)
100 {
101 return (A)(value + 0.5);
102 }
103 else
104 {
105 return (A)(value - 0.5);
106 }
107}
108
109//inline int32_t Round32(float value)
110//{
111// return lroundf(value);
112//}
113//
114//inline int32_t Round32(double value)
115//{
116// return lround(value);
117//}
118//
119//inline int64_t Round64(float value)
120//{
121// return llroundf(value);
122//}
123//
124//inline int64_t Round64(double value)
125//{
126// return llround(value);
127//}
128
129inline bool StrToInt(const std::string& data, int& value)
130{
131 return sscanf(data.c_str(), "%i", &value) == 1;
132}
133
134inline bool HexStrToInt(const std::string& data, int& value)
135{
136 return sscanf(data.c_str(), "%x", &value) == 1;
137}
138
139inline bool StrToFloat(const std::string& data, float& value)
140{
141 return sscanf(data.c_str(), "%f", &value) == 1;
142}
143
144inline bool StrToFloat(const std::string& data, double& value)
145{
146 return sscanf(data.c_str(), "%lf", &value) == 1;
147}
148
149inline bool StrToBool(const std::string& data, bool& value)
150{
151 std::string data2 = data;
152 std::string word;
153 if (!GetWord(data2, word))
154 return false;
155
156 if (word == "1" || word == "true" || word == "on" || word == "yes")
157 {
158 value = true;
159 return true;
160 }
161 else if (word == "0" || word == "false" || word == "off" || word == "no")
162 {
163 value = false;
164 return true;
165 }
166 else
167 {
168 int ivalue;
169 if (StrToInt(word, ivalue))
170 {
171 value = ivalue != 0;
172 return true;
173 }
174 }
175
176 return false;
177}