Fix missing version string when Popen communicate returns byte strings.
[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 #ifdef LIBDCP_WINDOWS
65         __mingw_snprintf (buffer, sizeof(buffer), "%ld", x);
66 #else
67         snprintf (buffer, sizeof(buffer), "%ld", x);
68 #endif
69         return buffer;
70 }
71
72 template<>
73 string
74 dcp::locale_convert (unsigned long int x, int, bool)
75 {
76         char buffer[64];
77         snprintf (buffer, sizeof(buffer), "%lu", x);
78         return buffer;
79 }
80
81 template<>
82 string
83 dcp::locale_convert (long long int x, int, bool)
84 {
85         char buffer[64];
86 #ifdef LIBDCP_WINDOWS
87         __mingw_snprintf (buffer, sizeof(buffer), "%lld", x);
88 #else
89         snprintf (buffer, sizeof(buffer), "%lld", x);
90 #endif
91         return buffer;
92 }
93
94 template<>
95 string
96 dcp::locale_convert (unsigned long long int x, int, bool)
97 {
98         char buffer[64];
99 #ifdef LIBDCP_WINDOWS
100         __mingw_snprintf (buffer, sizeof(buffer), "%llu", x);
101 #else
102         snprintf (buffer, sizeof(buffer), "%llu", x);
103 #endif
104         return buffer;
105 }
106
107 template<>
108 string
109 dcp::locale_convert (float x, int precision, bool fixed)
110 {
111         char format[64];
112         if (fixed) {
113                 snprintf (format, sizeof(format), "%%.%df", precision);
114         } else {
115                 snprintf (format, sizeof(format), "%%.%dg", precision);
116         }
117         char buffer[64];
118         snprintf (buffer, sizeof(buffer), format, x);
119         return buffer;
120 }
121
122 template<>
123 string
124 dcp::locale_convert (double x, int precision, bool fixed)
125 {
126         char format[64];
127         if (fixed) {
128                 snprintf (format, sizeof(format), "%%.%df", precision);
129         } else {
130                 snprintf (format, sizeof(format), "%%.%dg", precision);
131         }
132         char buffer[64];
133         snprintf (buffer, sizeof(buffer), format, x);
134         return buffer;
135 }
136
137 template<>
138 string
139 dcp::locale_convert (string x, int, bool)
140 {
141         return x;
142 }
143
144 template<>
145 string
146 dcp::locale_convert (char* x, int, bool)
147 {
148         return x;
149 }
150
151 template<>
152 string
153 dcp::locale_convert (char const * x, int, bool)
154 {
155         return x;
156 }
157
158 template<>
159 string
160 dcp::locale_convert (wchar_t const * x, int, bool)
161 {
162         wstring s (x);
163         return string (s.begin(), s.end());
164 }
165
166 template<>
167 string
168 dcp::locale_convert (char x, int, bool)
169 {
170         string s;
171         s += x;
172         return s;
173 }
174
175 template<>
176 string
177 dcp::locale_convert (boost::filesystem::path x, int, bool)
178 {
179         return x.string();
180 }
181
182 template<>
183 int
184 dcp::locale_convert (string x, int, bool)
185 {
186         int y = 0;
187         sscanf (x.c_str(), "%d", &y);
188         return y;
189 }
190
191 template<>
192 long
193 dcp::locale_convert (string x, int, bool)
194 {
195         long int y = 0;
196         sscanf (x.c_str(), "%ld", &y);
197         return y;
198 }
199
200 template<>
201 long long
202 dcp::locale_convert (string x, int, bool)
203 {
204         long long y = 0;
205 #ifdef LIBDCP_WINDOWS
206         __mingw_sscanf (x.c_str(), "%lld", &y);
207 #else
208         sscanf (x.c_str(), "%lld", &y);
209 #endif
210         return y;
211 }
212
213 template<>
214 float
215 dcp::locale_convert (string x, int, bool)
216 {
217         float y = 0;
218         sscanf (x.c_str(), "%f", &y);
219         return y;
220 }
221
222 template<>
223 double
224 dcp::locale_convert (string x, int, bool)
225 {
226         double y = 0;
227         sscanf (x.c_str(), "%lf", &y);
228         return y;
229 }