Replace incorrect uses of raw_convert with a new locale_convert.
[dcpomatic.git] / src / lib / locale_convert.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "locale_convert.h"
22 #include <string>
23 #include <inttypes.h>
24
25 using std::string;
26
27 template<>
28 string
29 locale_convert (int x, int)
30 {
31         char buffer[64];
32         snprintf (buffer, sizeof(buffer), "%d", x);
33         return buffer;
34 }
35
36 template<>
37 string
38 locale_convert (int64_t x, int)
39 {
40         char buffer[64];
41         snprintf (buffer, sizeof(buffer), "%" PRId64, x);
42         return buffer;
43 }
44
45 template<>
46 string
47 locale_convert (float x, int precision)
48 {
49         char format[64];
50         snprintf (format, sizeof(format), "%%.%df", precision);
51         char buffer[64];
52         snprintf (buffer, sizeof(buffer), format, x);
53         return buffer;
54 }
55
56 template<>
57 string
58 locale_convert (double x, int precision)
59 {
60         char format[64];
61         snprintf (format, sizeof(format), "%%.%df", precision);
62         char buffer[64];
63         snprintf (buffer, sizeof(buffer), format, x);
64         return buffer;
65 }
66
67 template<>
68 string
69 locale_convert (string x, int)
70 {
71         return x;
72 }
73
74 template<>
75 string
76 locale_convert (char* x, int)
77 {
78         return x;
79 }
80
81 template<>
82 string
83 locale_convert (char const * x, int)
84 {
85         return x;
86 }
87
88 template<>
89 int
90 locale_convert (string x, int)
91 {
92         int y = 0;
93         sscanf (x.c_str(), "%d", &y);
94         return y;
95 }
96
97 template<>
98 int64_t
99 locale_convert (string x, int)
100 {
101         int64_t y = 0;
102         sscanf (x.c_str(), "%" PRId64, &y);
103         return y;
104 }
105
106 template<>
107 float
108 locale_convert (string x, int)
109 {
110         float y = 0;
111         sscanf (x.c_str(), "%f", &y);
112         return y;
113 }
114
115 template<>
116 double
117 locale_convert (string x, int)
118 {
119         double y = 0;
120         sscanf (x.c_str(), "%lf", &y);
121         return y;
122 }