Try to normalise use of int64/long int across 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 (int x, int, bool)
44 {
45         char buffer[64];
46         snprintf (buffer, sizeof(buffer), "%d", x);
47         return buffer;
48 }
49
50 template<>
51 string
52 dcp::locale_convert (unsigned int x, int, bool)
53 {
54         char buffer[64];
55         snprintf (buffer, sizeof(buffer), "%u", x);
56         return buffer;
57 }
58
59 template<>
60 string
61 dcp::locale_convert (long int x, int, bool)
62 {
63         char buffer[64];
64         snprintf (buffer, sizeof(buffer), "%ld", x);
65         return buffer;
66 }
67
68 template<>
69 string
70 dcp::locale_convert (unsigned long int x, int, bool)
71 {
72         char buffer[64];
73         snprintf (buffer, sizeof(buffer), "%lu", x);
74         return buffer;
75 }
76
77 template<>
78 string
79 dcp::locale_convert (long long int x, int, bool)
80 {
81         char buffer[64];
82 #ifdef LIBDCP_WINDOWS
83         __mingw_snprintf (buffer, sizeof(buffer), "%lld", x);
84 #else
85         snprintf (buffer, sizeof(buffer), "%lld", x);
86 #endif
87         return buffer;
88 }
89
90 template<>
91 string
92 dcp::locale_convert (unsigned long long int x, int, bool)
93 {
94         char buffer[64];
95 #ifdef LIBDCP_WINDOWS
96         __mingw_snprintf (buffer, sizeof(buffer), "%llu", x);
97 #else
98         snprintf (buffer, sizeof(buffer), "%llu", x);
99 #endif
100         return buffer;
101 }
102
103 template<>
104 string
105 dcp::locale_convert (float x, int precision, bool fixed)
106 {
107         char format[64];
108         if (fixed) {
109                 snprintf (format, sizeof(format), "%%.%df", precision);
110         } else {
111                 snprintf (format, sizeof(format), "%%.%dg", precision);
112         }
113         char buffer[64];
114         snprintf (buffer, sizeof(buffer), format, x);
115         return buffer;
116 }
117
118 template<>
119 string
120 dcp::locale_convert (double x, int precision, bool fixed)
121 {
122         char format[64];
123         if (fixed) {
124                 snprintf (format, sizeof(format), "%%.%df", precision);
125         } else {
126                 snprintf (format, sizeof(format), "%%.%dg", precision);
127         }
128         char buffer[64];
129         snprintf (buffer, sizeof(buffer), format, x);
130         return buffer;
131 }
132
133 template<>
134 string
135 dcp::locale_convert (string x, int, bool)
136 {
137         return x;
138 }
139
140 template<>
141 string
142 dcp::locale_convert (char* x, int, bool)
143 {
144         return x;
145 }
146
147 template<>
148 string
149 dcp::locale_convert (char const * x, int, bool)
150 {
151         return x;
152 }
153
154 template<>
155 string
156 dcp::locale_convert (wchar_t const * x, int, bool)
157 {
158         wstring s (x);
159         return string (s.begin(), s.end());
160 }
161
162 template<>
163 string
164 dcp::locale_convert (char x, int, bool)
165 {
166         string s;
167         s += x;
168         return s;
169 }
170
171 template<>
172 string
173 dcp::locale_convert (boost::filesystem::path x, int, bool)
174 {
175         return x.string();
176 }
177
178 template<>
179 int
180 dcp::locale_convert (string x, int, bool)
181 {
182         int y = 0;
183         sscanf (x.c_str(), "%d", &y);
184         return y;
185 }
186
187 template<>
188 long int
189 dcp::locale_convert (string x, int, bool)
190 {
191         long int y = 0;
192         sscanf (x.c_str(), "%ld", &y);
193         return y;
194 }
195
196 template<>
197 float
198 dcp::locale_convert (string x, int, bool)
199 {
200         float y = 0;
201         sscanf (x.c_str(), "%f", &y);
202         return y;
203 }
204
205 template<>
206 double
207 dcp::locale_convert (string x, int, bool)
208 {
209         double y = 0;
210         sscanf (x.c_str(), "%lf", &y);
211         return y;
212 }