]> git.lyx.org Git - lyx.git/blob - src/support/convert.cpp
Disable CheckTeX while buffer is processed
[lyx.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 LYX_USE_LONG_LONG
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 #endif
122
123
124 template<>
125 string convert<string>(long l)
126 {
127         return lexical_cast<string>(l);
128 }
129
130
131 template<>
132 docstring convert<docstring>(long l)
133 {
134         return from_ascii(lexical_cast<string>(l));
135 }
136
137
138 #ifdef LYX_USE_LONG_LONG
139 template<>
140 string convert<string>(long long ll)
141 {
142         return lexical_cast<string>(ll);
143 }
144
145
146 template<>
147 docstring convert<docstring>(long long ll)
148 {
149         return from_ascii(lexical_cast<string>(ll));
150 }
151 #endif
152
153
154 template<>
155 string convert<string>(float f)
156 {
157         std::ostringstream val;
158         val << f;
159         return val.str();
160 }
161
162
163 template<>
164 string convert<string>(double d)
165 {
166         std::ostringstream val;
167         val << d;
168         return val.str();
169 }
170
171
172 template<>
173 docstring convert<docstring>(double d)
174 {
175         return from_ascii(convert<string>(d));
176 }
177
178
179 template<>
180 int convert<int>(string const s)
181 {
182         return strtol(s.c_str(), 0, 10);
183 }
184
185
186 template<>
187 int convert<int>(docstring const s)
188 {
189         return strtol(to_ascii(s).c_str(), 0, 10);
190 }
191
192
193 template<>
194 unsigned int convert<unsigned int>(string const s)
195 {
196         return strtoul(s.c_str(), 0, 10);
197 }
198
199
200 template<>
201 unsigned long convert<unsigned long>(string const s)
202 {
203         return strtoul(s.c_str(), 0, 10);
204 }
205
206
207 template<>
208 double convert<double>(string const s)
209 {
210         return strtod(s.c_str(), 0);
211 }
212
213
214 template<>
215 int convert<int>(char const * cptr)
216 {
217         return strtol(cptr, 0, 10);
218 }
219
220
221 template<>
222 double convert<double>(char const * cptr)
223 {
224         return strtod(cptr, 0);
225 }
226
227
228 } // namespace lyx