]> git.lyx.org Git - lyx.git/blob - src/support/docstring.h
remove reference to LaTeXStream
[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 /// convert \p s from ucs4 to the encoding of the file system.
63 std::string const to_filesystem8bit(docstring const & s);
64
65 /// normalize \p s to precomposed form c
66 docstring const normalize_c(docstring const & s);
67
68 /// Compare a docstring with a C string of ASCII characters
69 bool operator==(lyx::docstring const &, char const *);
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 /// Compare a docstring with a C string of ASCII characters
75 inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); }
76
77 /// Compare a C string of ASCII characters with a docstring
78 inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); }
79
80 /// Concatenate a docstring and a C string of ASCII characters
81 lyx::docstring operator+(lyx::docstring const &, char const *);
82
83 /// Concatenate a C string of ASCII characters and a docstring
84 lyx::docstring operator+(char const *, lyx::docstring const &);
85
86 /// Concatenate a docstring and a single ASCII character
87 lyx::docstring operator+(lyx::docstring const & l, char r);
88
89 /// Concatenate a single ASCII character and a docstring
90 lyx::docstring operator+(char l, lyx::docstring const & r);
91
92 /// Append a C string of ASCII characters to a docstring
93 lyx::docstring & operator+=(lyx::docstring &, char const *);
94
95 /// Append a single ASCII character to a docstring
96 lyx::docstring & operator+=(lyx::docstring & l, char r);
97
98 } // namespace lyx
99
100
101 #if ! defined(USE_WCHAR_T) && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
102 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
103 namespace std {
104
105 template<typename T> void
106 char_traits<T>::assign(char_type & c1, char_type const & c2)
107 {
108         c1 = c2;
109 }
110
111
112 template<typename T> bool
113 char_traits<T>::eq(char_type const & c1, char_type const & c2)
114 {
115         return c1 == c2;
116 }
117
118
119 template<typename T> bool
120 char_traits<T>::lt(char_type const & c1, char_type const & c2)
121 {
122         return c1 < c2;
123 }
124
125
126 template<typename T> int
127 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
128 {
129         for (std::size_t i = 0; i < n; ++i)
130                 if (lt(s1[i], s2[i]))
131                         return -1;
132                 else if (lt(s2[i], s1[i]))
133                         return 1;
134         return 0;
135 }
136
137
138 template<typename T> std::size_t
139 char_traits<T>::length(char_type const * p)
140 {
141         std::size_t i = 0;
142         while (!eq(p[i], char_type()))
143                 ++i;
144         return i;
145 }
146
147
148 template<typename T> typename char_traits<T>::char_type const *
149 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
150 {
151         for (std::size_t i = 0; i < n; ++i)
152                 if (eq(s[i], a))
153                         return s + i;
154         return 0;
155 }
156
157
158 template<typename T> typename char_traits<T>::char_type *
159 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
160 {
161         return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
162 }
163
164
165 template<typename T> typename char_traits<T>::char_type *
166 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
167 {
168         std::copy(s2, s2 + n, s1);
169         return s1;
170 }
171
172
173 template<typename T> typename char_traits<T>::char_type *
174 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
175 {
176         std::fill_n(s, n, a);
177         return s;
178 }
179
180
181 template<typename T> typename char_traits<T>::char_type
182 char_traits<T>::to_char_type(int_type const & c)
183 {
184         return static_cast<char_type>(c);
185 }
186
187
188 template<typename T> typename char_traits<T>::int_type
189 char_traits<T>::to_int_type(char_type const & c)
190 {
191         return static_cast<int_type>(c);
192 }
193
194
195 template<typename T> bool
196 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
197 {
198         return c1 == c2;
199 }
200
201
202 template<typename T> typename char_traits<T>::int_type
203 char_traits<T>::eof()
204 {
205         return static_cast<int_type>(EOF);
206 }
207
208
209 template<typename T> typename char_traits<T>::int_type
210 char_traits<T>::not_eof(int_type const & c)
211 {
212         return !eq_int_type(c, eof()) ? c : to_int_type(char_type());
213 }
214
215 }
216 #endif
217 #endif