]> git.lyx.org Git - features.git/blob - src/support/convert.cpp
Revert "Amend 3093789e for cmake build"
[features.git] / src / support / convert.cpp
1 /**
2  * \file convert.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "support/convert.h"
15 #include "support/docstring.h"
16
17 #include <boost/lexical_cast.hpp>
18
19 #include <string>
20 #include <sstream>
21 //needed for Mac OSX 10.5.2 Leopard
22 #include <cstdlib>
23
24 using namespace std;
25
26 namespace {
27
28 // A version of lexical cast that does not throw. Useful for when we convert to string
29 template<typename To, typename From>
30 To lexical_cast(From const & value, To const & defaultResult = To())
31 {
32         try {
33                 return boost::lexical_cast<To>(value);
34         } catch(...) {
35                 // Ignore all exceptions and use default.
36                 return defaultResult;
37         }
38 }
39
40 } // namespace
41
42
43 namespace lyx {
44
45 template<>
46 string convert<string>(bool b)
47 {
48         return (b ? "true" : "false");
49 }
50
51
52 template<>
53 string convert<string>(char c)
54 {
55         return string(1, c);
56 }
57
58
59 template<>
60 string convert<string>(short unsigned int sui)
61 {
62         return lexical_cast<string>(sui);
63 }
64
65
66 template<>
67 string convert<string>(int i)
68 {
69         return lexical_cast<string>(i);
70 }
71
72
73 template<>
74 docstring convert<docstring>(int i)
75 {
76         return from_ascii(lexical_cast<string>(i));
77 }
78
79
80 template<>
81 string convert<string>(unsigned int ui)
82 {
83         return lexical_cast<string>(ui);
84 }
85
86
87 template<>
88 docstring convert<docstring>(unsigned int ui)
89 {
90         return from_ascii(lexical_cast<string>(ui));
91 }
92
93
94 template<>
95 string convert<string>(unsigned long ul)
96 {
97         return lexical_cast<string>(ul);
98 }
99
100
101 template<>
102 docstring convert<docstring>(unsigned long ul)
103 {
104         return from_ascii(lexical_cast<string>(ul));
105 }
106
107
108 #ifdef HAVE_LONG_LONG_INT
109 template<>
110 string convert<string>(unsigned long long ull)
111 {
112         return lexical_cast<string>(ull);
113 }
114
115
116 template<>
117 docstring convert<docstring>(unsigned long long ull)
118 {
119         return from_ascii(lexical_cast<string>(ull));
120 }
121
122
123 template<>
124 string convert<string>(long long ll)
125 {
126         return lexical_cast<string>(ll);
127 }
128
129
130 template<>
131 docstring convert<docstring>(long long ll)
132 {
133         return from_ascii(lexical_cast<string>(ll));
134 }
135
136
137 template<>
138 unsigned long long convert<unsigned long long>(string const s)
139 {
140         return strtoull(s.c_str(), nullptr, 10);
141 }
142
143
144 /* not presently needed
145 template<>
146 long long convert<long long>(string const s)
147 {
148         return strtoll(s.c_str(), nullptr, 10);
149 }
150 */
151 #endif
152
153
154 template<>
155 string convert<string>(long l)
156 {
157         return lexical_cast<string>(l);
158 }
159
160
161 template<>
162 docstring convert<docstring>(long l)
163 {
164         return from_ascii(lexical_cast<string>(l));
165 }
166
167
168 template<>
169 string convert<string>(float f)
170 {
171         std::ostringstream val;
172         val << f;
173         return val.str();
174 }
175
176
177 template<>
178 string convert<string>(double d)
179 {
180         std::ostringstream val;
181         val << d;
182         return val.str();
183 }
184
185
186 template<>
187 docstring convert<docstring>(double d)
188 {
189         return from_ascii(convert<string>(d));
190 }
191
192
193 template<>
194 int convert<int>(string const s)
195 {
196         return int(strtol(s.c_str(), nullptr, 10));
197 }
198
199
200 int convert(std::string const & s, int base)
201 {
202         return int(strtol(s.c_str(), nullptr, base));
203 }
204
205
206 template<>
207 int convert<int>(docstring const s)
208 {
209         return int(strtol(to_ascii(s).c_str(), nullptr, 10));
210 }
211
212
213 template<>
214 unsigned int convert<unsigned int>(string const s)
215 {
216         return static_cast<unsigned int>(strtoul(s.c_str(), nullptr, 10));
217 }
218
219
220 template<>
221 unsigned long convert<unsigned long>(string const s)
222 {
223         return strtoul(s.c_str(), nullptr, 10);
224 }
225
226
227 template<>
228 double convert<double>(string const s)
229 {
230         return strtod(s.c_str(), nullptr);
231 }
232
233
234 template<>
235 int convert<int>(char const * cptr)
236 {
237         return int(strtol(cptr, nullptr, 10));
238 }
239
240
241 template<>
242 double convert<double>(char const * cptr)
243 {
244         return strtod(cptr, nullptr);
245 }
246
247
248 } // namespace lyx