Remove another stringstream from DecryptedKDM.
[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 (int64_t x, int, bool)
52 {
53         char buffer[64];
54         snprintf (buffer, sizeof(buffer), "%" PRId64, x);
55         return buffer;
56 }
57
58 template<>
59 string
60 dcp::locale_convert (uint64_t x, int, bool)
61 {
62         char buffer[64];
63         snprintf (buffer, sizeof(buffer), "%" PRIu64, x);
64         return buffer;
65 }
66
67 template<>
68 string
69 dcp::locale_convert (float x, int precision, bool fixed)
70 {
71         char format[64];
72         if (fixed) {
73                 snprintf (format, sizeof(format), "%%.%df", precision);
74         } else {
75                 snprintf (format, sizeof(format), "%%.%dg", precision);
76         }
77         char buffer[64];
78         snprintf (buffer, sizeof(buffer), format, x);
79         return buffer;
80 }
81
82 template<>
83 string
84 dcp::locale_convert (double x, int precision, bool fixed)
85 {
86         char format[64];
87         if (fixed) {
88                 snprintf (format, sizeof(format), "%%.%df", precision);
89         } else {
90                 snprintf (format, sizeof(format), "%%.%dg", precision);
91         }
92         char buffer[64];
93         snprintf (buffer, sizeof(buffer), format, x);
94         return buffer;
95 }
96
97 template<>
98 string
99 dcp::locale_convert (string x, int, bool)
100 {
101         return x;
102 }
103
104 template<>
105 string
106 dcp::locale_convert (char* x, int, bool)
107 {
108         return x;
109 }
110
111 template<>
112 string
113 dcp::locale_convert (char const * x, int, bool)
114 {
115         return x;
116 }
117
118 template<>
119 int
120 dcp::locale_convert (string x, int, bool)
121 {
122         int y = 0;
123         sscanf (x.c_str(), "%d", &y);
124         return y;
125 }
126
127 template<>
128 int64_t
129 dcp::locale_convert (string x, int, bool)
130 {
131         int64_t y = 0;
132         sscanf (x.c_str(), "%" PRId64, &y);
133         return y;
134 }
135
136 template<>
137 float
138 dcp::locale_convert (string x, int, bool)
139 {
140         float y = 0;
141         sscanf (x.c_str(), "%f", &y);
142         return y;
143 }
144
145 template<>
146 double
147 dcp::locale_convert (string x, int, bool)
148 {
149         double y = 0;
150         sscanf (x.c_str(), "%lf", &y);
151         return y;
152 }