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