Tidying.
[libdcp.git] / src / locale_convert.cc
1 /*
2     Copyright (C) 2016-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/locale_convert.cc
36  *  @brief Methods to convert to/from string using the current locale
37  */
38
39
40 #include "locale_convert.h"
41 #include <string>
42 #include <inttypes.h>
43
44
45 using std::string;
46 using std::wstring;
47
48
49 template<>
50 string
51 dcp::locale_convert (unsigned char x, int, bool)
52 {
53         char buffer[64];
54         snprintf (buffer, sizeof(buffer), "%hhd", x);
55         return buffer;
56 }
57
58
59 template<>
60 string
61 dcp::locale_convert (unsigned short int x, int, bool)
62 {
63         char buffer[64];
64         snprintf (buffer, sizeof(buffer), "%hd", x);
65         return buffer;
66 }
67
68
69 template<>
70 string
71 dcp::locale_convert (int x, int, bool)
72 {
73         char buffer[64];
74         snprintf (buffer, sizeof(buffer), "%d", x);
75         return buffer;
76 }
77
78
79 template<>
80 string
81 dcp::locale_convert (unsigned int x, int, bool)
82 {
83         char buffer[64];
84         snprintf (buffer, sizeof(buffer), "%u", x);
85         return buffer;
86 }
87
88
89 template<>
90 string
91 dcp::locale_convert (long int x, int, bool)
92 {
93         char buffer[64];
94 #ifdef LIBDCP_WINDOWS
95         __mingw_snprintf (buffer, sizeof(buffer), "%ld", x);
96 #else
97         snprintf (buffer, sizeof(buffer), "%ld", x);
98 #endif
99         return buffer;
100 }
101
102
103 template<>
104 string
105 dcp::locale_convert (unsigned long int x, int, bool)
106 {
107         char buffer[64];
108         snprintf (buffer, sizeof(buffer), "%lu", x);
109         return buffer;
110 }
111
112
113 template<>
114 string
115 dcp::locale_convert (long long int x, int, bool)
116 {
117         char buffer[64];
118 #ifdef LIBDCP_WINDOWS
119         __mingw_snprintf (buffer, sizeof(buffer), "%lld", x);
120 #else
121         snprintf (buffer, sizeof(buffer), "%lld", x);
122 #endif
123         return buffer;
124 }
125
126
127 template<>
128 string
129 dcp::locale_convert (unsigned long long int x, int, bool)
130 {
131         char buffer[64];
132 #ifdef LIBDCP_WINDOWS
133         __mingw_snprintf (buffer, sizeof(buffer), "%llu", x);
134 #else
135         snprintf (buffer, sizeof(buffer), "%llu", x);
136 #endif
137         return buffer;
138 }
139
140
141 template<>
142 string
143 dcp::locale_convert (float x, int precision, bool fixed)
144 {
145         char format[64];
146         if (fixed) {
147                 snprintf (format, sizeof(format), "%%.%df", precision);
148         } else {
149                 snprintf (format, sizeof(format), "%%.%dg", precision);
150         }
151         char buffer[64];
152         snprintf (buffer, sizeof(buffer), format, x);
153         return buffer;
154 }
155
156
157 template<>
158 string
159 dcp::locale_convert (double x, int precision, bool fixed)
160 {
161         char format[64];
162         if (fixed) {
163                 snprintf (format, sizeof(format), "%%.%df", precision);
164         } else {
165                 snprintf (format, sizeof(format), "%%.%dg", precision);
166         }
167         char buffer[64];
168         snprintf (buffer, sizeof(buffer), format, x);
169         return buffer;
170 }
171
172
173 template<>
174 string
175 dcp::locale_convert (string x, int, bool)
176 {
177         return x;
178 }
179
180
181 template<>
182 string
183 dcp::locale_convert (char* x, int, bool)
184 {
185         return x;
186 }
187
188
189 template<>
190 string
191 dcp::locale_convert (char const * x, int, bool)
192 {
193         return x;
194 }
195
196
197 template<>
198 string
199 dcp::locale_convert (wchar_t const * x, int, bool)
200 {
201         wstring s (x);
202         return string (s.begin(), s.end());
203 }
204
205
206 template<>
207 string
208 dcp::locale_convert (char x, int, bool)
209 {
210         string s;
211         s += x;
212         return s;
213 }
214
215
216 template<>
217 string
218 dcp::locale_convert (boost::filesystem::path x, int, bool)
219 {
220         return x.string();
221 }
222
223
224 template<>
225 unsigned char
226 dcp::locale_convert (string x, int, bool)
227 {
228         unsigned char y = 0;
229         sscanf (x.c_str(), "%hhu", &y);
230         return y;
231 }
232
233
234 template<>
235 unsigned short int
236 dcp::locale_convert (string x, int, bool)
237 {
238         unsigned short int y = 0;
239         sscanf (x.c_str(), "%hu", &y);
240         return y;
241 }
242
243
244 template<>
245 unsigned int
246 dcp::locale_convert (string x, int, bool)
247 {
248         unsigned int y = 0;
249         sscanf (x.c_str(), "%u", &y);
250         return y;
251 }
252
253
254 template<>
255 int
256 dcp::locale_convert (string x, int, bool)
257 {
258         int y = 0;
259         sscanf (x.c_str(), "%d", &y);
260         return y;
261 }
262
263
264 template<>
265 long
266 dcp::locale_convert (string x, int, bool)
267 {
268         long int y = 0;
269         sscanf (x.c_str(), "%ld", &y);
270         return y;
271 }
272
273
274 template<>
275 unsigned long
276 dcp::locale_convert (string x, int, bool)
277 {
278         unsigned long y = 0;
279 #ifdef LIBDCP_WINDOWS
280         __mingw_sscanf (x.c_str(), "%lud", &y);
281 #else
282         sscanf (x.c_str(), "%lud", &y);
283 #endif
284         return y;
285 }
286
287
288 template<>
289 long long
290 dcp::locale_convert (string x, int, bool)
291 {
292         long long y = 0;
293 #ifdef LIBDCP_WINDOWS
294         __mingw_sscanf (x.c_str(), "%lld", &y);
295 #else
296         sscanf (x.c_str(), "%lld", &y);
297 #endif
298         return y;
299 }
300
301
302 template<>
303 unsigned long long
304 dcp::locale_convert (string x, int, bool)
305 {
306         unsigned long long y = 0;
307 #ifdef LIBDCP_WINDOWS
308         __mingw_sscanf (x.c_str(), "%llud", &y);
309 #else
310         sscanf (x.c_str(), "%llud", &y);
311 #endif
312         return y;
313 }
314
315
316 template<>
317 float
318 dcp::locale_convert (string x, int, bool)
319 {
320         float y = 0;
321         sscanf (x.c_str(), "%f", &y);
322         return y;
323 }
324
325
326 template<>
327 double
328 dcp::locale_convert (string x, int, bool)
329 {
330         double y = 0;
331         sscanf (x.c_str(), "%lf", &y);
332         return y;
333 }