Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/GenericToolbox.h @ 1080:287ec78f63b4
GenericToolbox (fast c-string --> double or integer) + refactoring to be able
to set structures lut into configurator + fixed missing revision bump in
configurator + UT
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 21 Oct 2019 16:01:29 +0200 |
parents | |
children | 6cf58788f37f 141593f1aa88 |
comparison
equal
deleted
inserted
replaced
1072:391fb6d6905d | 1080:287ec78f63b4 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program 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. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 #pragma once | |
22 | |
23 #include <string> | |
24 | |
25 namespace OrthancStone | |
26 { | |
27 namespace GenericToolbox | |
28 { | |
29 inline bool LegitDoubleString(const char* text) | |
30 { | |
31 const char* p = text; | |
32 if(*p == '-') | |
33 p++; | |
34 size_t period = 0; | |
35 while(*p != 0) | |
36 { | |
37 if (*p >= '0' && *p <= '9') | |
38 ++p; | |
39 else if(*p == '.') | |
40 { | |
41 if(period > 0) | |
42 return false; | |
43 else | |
44 period++; | |
45 ++p; | |
46 } | |
47 else | |
48 return false; | |
49 } | |
50 return true; | |
51 } | |
52 | |
53 inline bool LegitIntegerString(const char* text) | |
54 { | |
55 const char* p = text; | |
56 if (*p == '-') | |
57 p++; | |
58 while (*p != 0) | |
59 { | |
60 if (*p >= '0' && *p <= '9') | |
61 ++p; | |
62 else | |
63 return false; | |
64 } | |
65 return true; | |
66 } | |
67 | |
68 /* | |
69 String to doubles with at most 18 digits | |
70 */ | |
71 inline bool StringToDouble(double& r, const char* text) | |
72 { | |
73 if(!LegitDoubleString(text)) | |
74 return false; | |
75 | |
76 static const double FRAC_FACTORS[] = | |
77 { | |
78 1.0, | |
79 0.1, | |
80 0.01, | |
81 0.001, | |
82 0.0001, | |
83 0.00001, | |
84 0.000001, | |
85 0.0000001, | |
86 0.00000001, | |
87 0.000000001, | |
88 0.0000000001, | |
89 0.00000000001, | |
90 0.000000000001, | |
91 0.0000000000001, | |
92 0.00000000000001, | |
93 0.000000000000001, | |
94 0.0000000000000001, | |
95 0.00000000000000001, | |
96 0.000000000000000001, | |
97 0.0000000000000000001 | |
98 }; | |
99 const size_t FRAC_FACTORS_LEN = sizeof(FRAC_FACTORS)/sizeof(double); | |
100 | |
101 r = 0.0; | |
102 double neg = 1.0; | |
103 const char* p = text; | |
104 | |
105 if (*p == '-') | |
106 { | |
107 neg = -1.0; | |
108 ++p; | |
109 } | |
110 // 12345.67890 | |
111 while (*p >= '0' && *p <= '9') | |
112 { | |
113 r = (r*10.0) + (*p - '0'); // 1 12 123 123 12345 | |
114 ++p; | |
115 } | |
116 if (*p == '.') | |
117 { | |
118 double f = 0.0; | |
119 int n = 1; | |
120 ++p; | |
121 while (*p >= '0' && *p <= '9' && n < FRAC_FACTORS_LEN) | |
122 { | |
123 f += (*p - '0') * FRAC_FACTORS[n]; | |
124 ++p; | |
125 ++n; | |
126 } | |
127 r += f; | |
128 } | |
129 r *= neg; | |
130 if (*p == 0 || (*p >= '0' && *p <= '9') ) | |
131 return true; | |
132 else | |
133 return false; | |
134 } | |
135 | |
136 inline bool StringToDouble(double& r, const std::string& text) | |
137 { | |
138 return StringToDouble(r, text.c_str()); | |
139 } | |
140 | |
141 template<typename T> | |
142 inline bool StringToInteger(T& r, const char* text) | |
143 { | |
144 if (!LegitDoubleString(text)) | |
145 return false; | |
146 | |
147 r = 0; | |
148 T neg = 1; | |
149 const char* p = text; | |
150 | |
151 if (*p == '-') | |
152 { | |
153 neg = -1; | |
154 ++p; | |
155 } | |
156 while (*p >= '0' && *p <= '9') | |
157 { | |
158 r = (r * 10) + (*p - '0'); // 1 12 123 123 12345 | |
159 ++p; | |
160 } | |
161 r *= neg; | |
162 if (*p == 0) | |
163 return true; | |
164 else | |
165 return false; | |
166 } | |
167 | |
168 template<typename T> | |
169 inline bool StringToInteger(T& r, const std::string& text) | |
170 { | |
171 return StringToInteger<T>(r, text.c_str()); | |
172 } | |
173 | |
174 /** | |
175 "rgb(12,23,255)" --> red, green, blue and returns true | |
176 "everything else" --> returns false (other values left untouched) | |
177 */ | |
178 bool GetRgbValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, const char* text); | |
179 | |
180 /** | |
181 See other overload | |
182 */ | |
183 inline bool GetRgbValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, const std::string& text) | |
184 { | |
185 return GetRgbValuesFromString(red, green, blue, text.c_str()); | |
186 } | |
187 | |
188 bool GetRgbaValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha, const char* text); | |
189 | |
190 inline bool GetRgbaValuesFromString(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha, const std::string& text) | |
191 { | |
192 return GetRgbaValuesFromString(red, green, blue, alpha, text.c_str()); | |
193 } | |
194 } | |
195 } |