]> git.lyx.org Git - lyx.git/blob - src/support/convert.cpp
Fix bug #9193: Spacing modification not exact
[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 template<>
94 string convert<string>(long l)
95 {
96         return lexical_cast<string>(l);
97 }
98
99
100 template<>
101 docstring convert<docstring>(long l)
102 {
103         return from_ascii(lexical_cast<string>(l));
104 }
105
106
107 template<>
108 string convert<string>(float f)
109 {
110         std::ostringstream val;
111         val << f;
112         return val.str();
113 }
114
115
116 template<>
117 string convert<string>(double d)
118 {
119         std::ostringstream val;
120         val << d;
121         return val.str();
122 }
123
124
125 template<>
126 int convert<int>(string const s)
127 {
128         return strtol(s.c_str(), 0, 10);
129 }
130
131
132 template<>
133 int convert<int>(docstring const s)
134 {
135         return strtol(to_ascii(s).c_str(), 0, 10);
136 }
137
138
139 template<>
140 unsigned int convert<unsigned int>(string const s)
141 {
142         return strtoul(s.c_str(), 0, 10);
143 }
144
145
146 template<>
147 unsigned long convert<unsigned long>(string const s)
148 {
149         return strtoul(s.c_str(), 0, 10);
150 }
151
152
153 template<>
154 double convert<double>(string const s)
155 {
156         return strtod(s.c_str(), 0);
157 }
158
159
160 template<>
161 int convert<int>(char const * cptr)
162 {
163         return strtol(cptr, 0, 10);
164 }
165
166
167 template<>
168 double convert<double>(char const * cptr)
169 {
170         return strtod(cptr, 0);
171 }
172
173
174 } // namespace lyx