Add another conversion.
[libsub.git] / src / locale_convert.cc
1 /*
2     Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "locale_convert.h"
21
22 using std::string;
23
24 template<>
25 string
26 sub::locale_convert (int x, int, bool)
27 {
28         char buffer[64];
29         snprintf (buffer, sizeof(buffer), "%d", x);
30         return buffer;
31 }
32
33 template<>
34 string
35 sub::locale_convert (long int x, int, bool)
36 {
37         char buffer[64];
38 #ifdef LIBSUB_WINDOWS
39         __mingw_snprintf (buffer, sizeof(buffer), "%ld", x);
40 #else
41         snprintf (buffer, sizeof(buffer), "%ld", x);
42 #endif
43         return buffer;
44 }
45
46 template<>
47 string
48 sub::locale_convert (unsigned long int x, int, bool)
49 {
50         char buffer[64];
51         snprintf (buffer, sizeof(buffer), "%lu", x);
52         return buffer;
53 }
54
55 template<>
56 string
57 sub::locale_convert (unsigned long long x, int, bool)
58 {
59         char buffer[64];
60 #ifdef LIBSUB_WINDOWS
61         __mingw_snprintf (buffer, sizeof(buffer), "%lld", x);
62 #else
63         snprintf (buffer, sizeof(buffer), "%lld", x);
64 #endif
65         return buffer;
66 }
67
68 template<>
69 string
70 sub::locale_convert (string x, int, bool)
71 {
72         return x;
73 }
74
75 template<>
76 int
77 sub::locale_convert (string x, int, bool)
78 {
79         int y = 0;
80         sscanf (x.c_str(), "%d", &y);
81         return y;
82 }
83
84 template<>
85 float
86 sub::locale_convert (string x, int, bool)
87 {
88         float y = 0;
89         sscanf (x.c_str(), "%f", &y);
90         return y;
91 }