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