]> git.lyx.org Git - lyx.git/blob - src/support/convert.cpp
Hack to display section symbol
[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 <string>
18 #include <sstream>
19 //needed for Mac OSX 10.5.2 Leopard
20 #include <cstdlib>
21
22 using namespace std;
23
24 namespace lyx {
25
26 template<>
27 string convert<string>(bool b)
28 {
29         return (b ? "true" : "false");
30 }
31
32
33 template<>
34 string convert<string>(char c)
35 {
36         return string(1, c);
37 }
38
39
40 template<>
41 string convert<string>(short unsigned int sui)
42 {
43         return to_string(sui);
44 }
45
46
47 template<>
48 string convert<string>(int i)
49 {
50         return to_string(i);
51 }
52
53
54 template<>
55 docstring convert<docstring>(int i)
56 {
57         return from_ascii(to_string(i));
58 }
59
60
61 template<>
62 string convert<string>(unsigned int ui)
63 {
64         return to_string(ui);
65 }
66
67
68 template<>
69 docstring convert<docstring>(unsigned int ui)
70 {
71         return from_ascii(to_string(ui));
72 }
73
74
75 template<>
76 string convert<string>(unsigned long ul)
77 {
78         return to_string(ul);
79 }
80
81
82 template<>
83 docstring convert<docstring>(unsigned long ul)
84 {
85         return from_ascii(to_string(ul));
86 }
87
88
89 #ifdef HAVE_LONG_LONG_INT
90 template<>
91 string convert<string>(unsigned long long ull)
92 {
93         return to_string(ull);
94 }
95
96
97 template<>
98 docstring convert<docstring>(unsigned long long ull)
99 {
100         return from_ascii(to_string(ull));
101 }
102
103
104 template<>
105 string convert<string>(long long ll)
106 {
107         return to_string(ll);
108 }
109
110
111 template<>
112 docstring convert<docstring>(long long ll)
113 {
114         return from_ascii(to_string(ll));
115 }
116
117
118 template<>
119 unsigned long long convert<unsigned long long>(string const s)
120 {
121         return strtoull(s.c_str(), nullptr, 10);
122 }
123
124
125 /* not presently needed
126 template<>
127 long long convert<long long>(string const s)
128 {
129         return strtoll(s.c_str(), nullptr, 10);
130 }
131 */
132 #endif
133
134
135 template<>
136 string convert<string>(long l)
137 {
138         return to_string(l);
139 }
140
141
142 template<>
143 docstring convert<docstring>(long l)
144 {
145         return from_ascii(to_string(l));
146 }
147
148
149 template<>
150 string convert<string>(float f)
151 {
152         ostringstream val;
153         val << f;
154         return val.str();
155 }
156
157
158 template<>
159 string convert<string>(double d)
160 {
161         ostringstream val;
162         val << d;
163         return val.str();
164 }
165
166
167 template<>
168 docstring convert<docstring>(double d)
169 {
170         return from_ascii(convert<string>(d));
171 }
172
173
174 template<>
175 int convert<int>(string const s)
176 {
177         return int(strtol(s.c_str(), nullptr, 10));
178 }
179
180
181 int convert(string const & s, int base)
182 {
183         return int(strtol(s.c_str(), nullptr, base));
184 }
185
186
187 template<>
188 int convert<int>(docstring const s)
189 {
190         return int(strtol(to_ascii(s).c_str(), nullptr, 10));
191 }
192
193
194 template<>
195 unsigned int convert<unsigned int>(string const s)
196 {
197         return static_cast<unsigned int>(strtoul(s.c_str(), nullptr, 10));
198 }
199
200
201 template<>
202 unsigned long convert<unsigned long>(string const s)
203 {
204         return strtoul(s.c_str(), nullptr, 10);
205 }
206
207
208 template<>
209 double convert<double>(string const s)
210 {
211         return strtod(s.c_str(), nullptr);
212 }
213
214
215 template<>
216 int convert<int>(char const * cptr)
217 {
218         return int(strtol(cptr, nullptr, 10));
219 }
220
221
222 template<>
223 double convert<double>(char const * cptr)
224 {
225         return strtod(cptr, nullptr);
226 }
227
228
229 } // namespace lyx