More raw/locale_convert.
[libdcp.git] / src / locale_convert.cc
1 /*
2     Copyright (C) 2016 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 #include "locale_convert.h"
35 #include <string>
36 #include <inttypes.h>
37
38 using std::string;
39 using std::wstring;
40
41 template<>
42 string
43 dcp::locale_convert (unsigned char x, int, bool)
44 {
45         char buffer[64];
46         snprintf (buffer, sizeof(buffer), "%hhd", x);
47         return buffer;
48 }
49
50 template<>
51 string
52 dcp::locale_convert (unsigned short int x, int, bool)
53 {
54         char buffer[64];
55         snprintf (buffer, sizeof(buffer), "%hd", x);
56         return buffer;
57 }
58
59 template<>
60 string
61 dcp::locale_convert (int x, int, bool)
62 {
63         char buffer[64];
64         snprintf (buffer, sizeof(buffer), "%d", x);
65         return buffer;
66 }
67
68 template<>
69 string
70 dcp::locale_convert (unsigned int x, int, bool)
71 {
72         char buffer[64];
73         snprintf (buffer, sizeof(buffer), "%u", x);
74         return buffer;
75 }
76
77 template<>
78 string
79 dcp::locale_convert (long int x, int, bool)
80 {
81         char buffer[64];
82 #ifdef LIBDCP_WINDOWS
83         __mingw_snprintf (buffer, sizeof(buffer), "%ld", x);
84 #else
85         snprintf (buffer, sizeof(buffer), "%ld", x);
86 #endif
87         return buffer;
88 }
89
90 template<>
91 string
92 dcp::locale_convert (unsigned long int x, int, bool)
93 {
94         char buffer[64];
95         snprintf (buffer, sizeof(buffer), "%lu", x);
96         return buffer;
97 }
98
99 template<>
100 string
101 dcp::locale_convert (long long int x, int, bool)
102 {
103         char buffer[64];
104 #ifdef LIBDCP_WINDOWS
105         __mingw_snprintf (buffer, sizeof(buffer), "%lld", x);
106 #else
107         snprintf (buffer, sizeof(buffer), "%lld", x);
108 #endif
109         return buffer;
110 }
111
112 template<>
113 string
114 dcp::locale_convert (unsigned long long int x, int, bool)
115 {
116         char buffer[64];
117 #ifdef LIBDCP_WINDOWS
118         __mingw_snprintf (buffer, sizeof(buffer), "%llu", x);
119 #else
120         snprintf (buffer, sizeof(buffer), "%llu", x);
121 #endif
122         return buffer;
123 }
124
125 template<>
126 string
127 dcp::locale_convert (float x, int precision, bool fixed)
128 {
129         char format[64];
130         if (fixed) {
131                 snprintf (format, sizeof(format), "%%.%df", precision);
132         } else {
133                 snprintf (format, sizeof(format), "%%.%dg", precision);
134         }
135         char buffer[64];
136         snprintf (buffer, sizeof(buffer), format, x);
137         return buffer;
138 }
139
140 template<>
141 string
142 dcp::locale_convert (double x, int precision, bool fixed)
143 {
144         char format[64];
145         if (fixed) {
146                 snprintf (format, sizeof(format), "%%.%df", precision);
147         } else {
148                 snprintf (format, sizeof(format), "%%.%dg", precision);
149         }
150         char buffer[64];
151         snprintf (buffer, sizeof(buffer), format, x);
152         return buffer;
153 }
154
155 template<>
156 string
157 dcp::locale_convert (string x, int, bool)
158 {
159         return x;
160 }
161
162 template<>
163 string
164 dcp::locale_convert (char* x, int, bool)
165 {
166         return x;
167 }
168
169 template<>
170 string
171 dcp::locale_convert (char const * x, int, bool)
172 {
173         return x;
174 }
175
176 template<>
177 string
178 dcp::locale_convert (wchar_t const * x, int, bool)
179 {
180         wstring s (x);
181         return string (s.begin(), s.end());
182 }
183
184 template<>
185 string
186 dcp::locale_convert (char x, int, bool)
187 {
188         string s;
189         s += x;
190         return s;
191 }
192
193 template<>
194 string
195 dcp::locale_convert (boost::filesystem::path x, int, bool)
196 {
197         return x.string();
198 }
199
200 template<>
201 unsigned char
202 dcp::locale_convert (string x, int, bool)
203 {
204         unsigned char y = 0;
205         sscanf (x.c_str(), "%hhu", &y);
206         return y;
207 }
208
209 template<>
210 unsigned short int
211 dcp::locale_convert (string x, int, bool)
212 {
213         unsigned short int y = 0;
214         sscanf (x.c_str(), "%hu", &y);
215         return y;
216 }
217
218 template<>
219 unsigned int
220 dcp::locale_convert (string x, int, bool)
221 {
222         int y = 0;
223         sscanf (x.c_str(), "%u", &y);
224         return y;
225 }
226
227 template<>
228 int
229 dcp::locale_convert (string x, int, bool)
230 {
231         int y = 0;
232         sscanf (x.c_str(), "%d", &y);
233         return y;
234 }
235
236 template<>
237 long
238 dcp::locale_convert (string x, int, bool)
239 {
240         long int y = 0;
241         sscanf (x.c_str(), "%ld", &y);
242         return y;
243 }
244
245 template<>
246 long long
247 dcp::locale_convert (string x, int, bool)
248 {
249         long long y = 0;
250 #ifdef LIBDCP_WINDOWS
251         __mingw_sscanf (x.c_str(), "%lld", &y);
252 #else
253         sscanf (x.c_str(), "%lld", &y);
254 #endif
255         return y;
256 }
257
258 template<>
259 float
260 dcp::locale_convert (string x, int, bool)
261 {
262         float y = 0;
263         sscanf (x.c_str(), "%f", &y);
264         return y;
265 }
266
267 template<>
268 double
269 dcp::locale_convert (string x, int, bool)
270 {
271         double y = 0;
272         sscanf (x.c_str(), "%lf", &y);
273         return y;
274 }