libdcp
raw_convert.cc
1 /*
2  Copyright (C) 2014-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 #include "raw_convert.h"
36 #include "locale_convert.h"
37 #include <boost/algorithm/string.hpp>
38 
39 
40 using std::string;
41 using std::wstring;
42 
43 
45 static
46 string
47 make_raw (string v)
48 {
49  struct lconv* lc = localeconv ();
50  /* thousands_sep may be . so remove them before changing decimal points */
51  boost::algorithm::replace_all (v, lc->thousands_sep, "");
52  boost::algorithm::replace_all (v, lc->decimal_point, ".");
53  return v;
54 }
55 
56 
57 static
58 string
59 make_local (string v)
60 {
61  struct lconv* lc = localeconv ();
62  boost::algorithm::replace_all (v, ".", lc->decimal_point);
63  /* We hope it's ok not to add in thousands separators here */
64  return v;
65 }
66 
67 
68 template <>
69 string
70 dcp::raw_convert (unsigned char v, int precision, bool fixed)
71 {
72  return make_raw (locale_convert<string> (v, precision, fixed));
73 }
74 
75 
76 template <>
77 string
78 dcp::raw_convert (unsigned short int v, int precision, bool fixed)
79 {
80  return make_raw (locale_convert<string> (v, precision, fixed));
81 }
82 
83 
84 template <>
85 string
86 dcp::raw_convert (int v, int precision, bool fixed)
87 {
88  return make_raw (locale_convert<string> (v, precision, fixed));
89 }
90 
91 
92 template <>
93 string
94 dcp::raw_convert (unsigned int v, int precision, bool fixed)
95 {
96  return make_raw (locale_convert<string> (v, precision, fixed));
97 }
98 
99 
100 template <>
101 string
102 dcp::raw_convert (long v, int precision, bool fixed)
103 {
104  return make_raw (locale_convert<string> (v, precision, fixed));
105 }
106 
107 
108 template <>
109 string
110 dcp::raw_convert (unsigned long v, int precision, bool fixed)
111 {
112  return make_raw (locale_convert<string> (v, precision, fixed));
113 }
114 
115 
116 template <>
117 string
118 dcp::raw_convert (long long v, int precision, bool fixed)
119 {
120  return make_raw (locale_convert<string> (v, precision, fixed));
121 }
122 
123 
124 template <>
125 string
126 dcp::raw_convert (unsigned long long v, int precision, bool fixed)
127 {
128  return make_raw (locale_convert<string> (v, precision, fixed));
129 }
130 
131 
132 template <>
133 string
134 dcp::raw_convert (float v, int precision, bool fixed)
135 {
136  return make_raw (locale_convert<string> (v, precision, fixed));
137 }
138 
139 
140 template <>
141 string
142 dcp::raw_convert (double v, int precision, bool fixed)
143 {
144  return make_raw (locale_convert<string> (v, precision, fixed));
145 }
146 
147 
148 template <>
149 string
150 dcp::raw_convert (char const * v, int, bool)
151 {
152  return v;
153 }
154 
155 
156 template <>
157 string
158 dcp::raw_convert (char* v, int, bool)
159 {
160  return v;
161 }
162 
163 
164 template <>
165 string
166 dcp::raw_convert (string v, int, bool)
167 {
168  return v;
169 }
170 
171 
172 template <>
173 string
174 dcp::raw_convert (char v, int, bool)
175 {
176  string s;
177  s += v;
178  return s;
179 }
180 
181 
182 template <>
183 string
184 dcp::raw_convert (wchar_t const * v, int, bool)
185 {
186  wstring w (v);
187  return string (w.begin(), w.end());
188 }
189 
190 
191 template <>
192 unsigned char
193 dcp::raw_convert (std::string v, int precision, bool fixed)
194 {
195  return locale_convert<unsigned char> (make_local (v), precision, fixed);
196 }
197 
198 
199 template <>
200 unsigned short int
201 dcp::raw_convert (std::string v, int precision, bool fixed)
202 {
203  return locale_convert<unsigned short int> (make_local (v), precision, fixed);
204 }
205 
206 
207 template <>
208 int
209 dcp::raw_convert (string v, int precision, bool fixed)
210 {
211  return locale_convert<int> (make_local (v), precision, fixed);
212 }
213 
214 
215 template <>
216 long
217 dcp::raw_convert (string v, int precision, bool fixed)
218 {
219  return locale_convert<long> (make_local (v), precision, fixed);
220 }
221 
222 
223 template <>
224 unsigned long
225 dcp::raw_convert (string v, int precision, bool fixed)
226 {
227  return locale_convert<unsigned long> (make_local (v), precision, fixed);
228 }
229 
230 
231 template <>
232 long long
233 dcp::raw_convert (string v, int precision, bool fixed)
234 {
235  return locale_convert<long long> (make_local (v), precision, fixed);
236 }
237 
238 
239 template <>
240 unsigned long long
241 dcp::raw_convert (string v, int precision, bool fixed)
242 {
243  return locale_convert<unsigned long long> (make_local (v), precision, fixed);
244 }
245 
246 
247 template <>
248 int
249 dcp::raw_convert (char const * v, int precision, bool fixed)
250 {
251  return locale_convert<int> (make_local (v), precision, fixed);
252 }
253 
254 
255 template <>
256 float
257 dcp::raw_convert (string v, int precision, bool fixed)
258 {
259  return locale_convert<float> (make_local (v), precision, fixed);
260 }
261 
262 
263 template <>
264 float
265 dcp::raw_convert (char const * v, int precision, bool fixed)
266 {
267  return locale_convert<float> (make_local (v), precision, fixed);
268 }
269 
270 
271 template <>
272 double
273 dcp::raw_convert (string v, int precision, bool fixed)
274 {
275  return locale_convert<double> (make_local (v), precision, fixed);
276 }
277 
278 
279 template <>
280 double
281 dcp::raw_convert (char const * v, int precision, bool fixed)
282 {
283  return locale_convert<double> (make_local (v), precision, fixed);
284 }
P raw_convert(Q, int precision=16, bool fixed=false)
Definition: raw_convert.h:57
Methods for conversion to/from string.