]> git.lyx.org Git - lyx.git/blob - src/support/docstring.h
Fix several filename and environment variable encoding problems
[lyx.git] / src / support / docstring.h
1 // -*- C++ -*-
2 /**
3  * \file docstring.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Georg Baum
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef LYX_DOCSTRING_H
14 #define LYX_DOCSTRING_H
15
16 #include "support/types.h"
17
18 #include <string>
19 #include <typeinfo>
20
21 namespace lyx {
22
23 /// String type for storing the main text in UCS4 encoding
24 typedef std::basic_string<char_type> docstring;
25
26 /// Creates a docstring from a C string of ASCII characters
27 docstring const from_ascii(char const *);
28
29 /// Creates a docstring from a std::string of ASCII characters
30 docstring const from_ascii(std::string const &);
31
32 /// Creates a std::string of ASCII characters from a docstring
33 std::string const to_ascii(docstring const &);
34
35 /// Creates a docstring from a UTF8 string. This should go eventually.
36 docstring const from_utf8(std::string const &);
37
38 /// Creates a UTF8 string from a docstring. This should go eventually.
39 std::string const to_utf8(docstring const &);
40
41 /// convert \p s from the encoding of the locale to ucs4.
42 docstring const from_local8bit(std::string const & s);
43
44 /// Exception thrown by to_local8bit if the string could not be converted
45 class to_local8bit_failure : public std::bad_cast {
46 public:
47         to_local8bit_failure() throw() : std::bad_cast() {}
48         virtual ~to_local8bit_failure() throw() {}
49         virtual const char* what() const throw();
50 };
51
52 /**
53  * Convert \p s from ucs4 to the encoding of the locale.
54  * This may fail and throw an exception, the caller is expected to act
55  * appropriately.
56  */
57 std::string const to_local8bit(docstring const & s);
58
59 /// convert \p s from the encoding of the file system to ucs4.
60 docstring const from_filesystem8bit(std::string const & s);
61
62 /// Compare a docstring with a C string of ASCII characters
63 bool operator==(lyx::docstring const &, char const *);
64
65 /// Compare a C string of ASCII characters with a docstring
66 inline bool operator==(char const * l, lyx::docstring const & r) { return r == l; }
67
68 /// Compare a docstring with a C string of ASCII characters
69 inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); }
70
71 /// Compare a C string of ASCII characters with a docstring
72 inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); }
73
74 /// Concatenate a docstring and a C string of ASCII characters
75 lyx::docstring operator+(lyx::docstring const &, char const *);
76
77 /// Concatenate a C string of ASCII characters and a docstring
78 lyx::docstring operator+(char const *, lyx::docstring const &);
79
80 /// Concatenate a docstring and a single ASCII character
81 lyx::docstring operator+(lyx::docstring const & l, char r);
82
83 /// Concatenate a single ASCII character and a docstring
84 lyx::docstring operator+(char l, lyx::docstring const & r);
85
86 /// Append a C string of ASCII characters to a docstring
87 lyx::docstring & operator+=(lyx::docstring &, char const *);
88
89 /// Append a single ASCII character to a docstring
90 lyx::docstring & operator+=(lyx::docstring & l, char r);
91
92 } // namespace lyx
93
94
95 #if SIZEOF_WCHAR_T != 4 && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
96 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
97 namespace std {$
98
99 template<typename T> void
100 char_traits<T>::assign(char_type & c1, char_type const & c2)
101 {
102         c1 = c2;
103 }
104
105
106 template<typename T> bool
107 char_traits<T>::eq(char_type const & c1, char_type const & c2)
108 {
109         return c1 == c2;
110 }
111
112
113 template<typename T> bool
114 char_traits<T>::lt(char_type const & c1, char_type const & c2)
115 {
116         return c1 < c2;
117 }
118
119
120 template<typename T> int
121 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
122 {
123         for (std::size_t i = 0; i < n; ++i)
124                 if (lt(s1[i], s2[i]))
125                         return -1;
126                 else if (lt(s2[i], s1[i]))
127                         return 1;
128         return 0;
129 }
130
131
132 template<typename T> std::size_t
133 char_traits<T>::length(char_type const * p)
134 {
135         std::size_t i = 0;
136         while (!eq(p[i], char_type()))
137                 ++i;
138         return i;
139 }
140
141
142 template<typename T> typename char_traits<T>::char_type const *
143 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
144 {
145         for (std::size_t i = 0; i < n; ++i)
146                 if (eq(s[i], a))
147                         return s + i;
148         return 0;
149 }
150
151
152 template<typename T> typename char_traits<T>::char_type *
153 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
154 {
155         return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
156 }
157
158
159 template<typename T> typename char_traits<T>::char_type *
160 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
161 {
162         std::copy(s2, s2 + n, s1);
163         return s1;
164 }
165
166
167 template<typename T> typename char_traits<T>::char_type *
168 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
169 {
170         std::fill_n(s, n, a);
171         return s;
172 }
173
174
175 template<typename T> typename char_traits<T>::char_type
176 char_traits<T>::to_char_type(int_type const & c)
177 {
178         return static_cast<char_type>(c);
179 }
180
181
182 template<typename T> typename char_traits<T>::int_type
183 char_traits<T>::to_int_type(char_type const & c)
184 {
185         return static_cast<int_type>(c);
186 }
187
188
189 template<typename T> bool
190 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
191 {
192         return c1 == c2;
193 }
194
195
196 template<typename T> typename char_traits<T>::int_type
197 char_traits<T>::eof()
198 {
199         return static_cast<int_type>(EOF);
200 }
201
202
203 template<typename T> typename char_traits<T>::int_type
204 char_traits<T>::not_eof(int_type const & c)
205 {
206         return !eq_int_type(c, eof()) ? c : to_int_type(char_type());
207 }
208
209 }
210 #endif
211 #endif