allow the user to override the rootCommand from their ~/.blackboxrc
[blackbox.git] / lib / Unicode.hh
blobc5b7fcef67c9d1dc6323455ae1dcf9c0d5a001c8
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Unicode.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 // Bradley T Hughes <bhughes at trolltech.com>
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
25 #ifndef __Unicode_hh
26 #define __Unicode_hh
28 #include <string>
30 namespace bt
33 * Returns true if the system supports Unicode conversions.
35 bool hasUnicode();
38 * Unicode character type.
40 typedef unsigned int Uchar;
43 * Unicode string type.
45 typedef std::basic_string<Uchar> ustring;
48 * Converts multibyte locale-encoded string to wide-char Unicode
49 * string (aka UTF-32).
51 ustring toUnicode(const std::string &string);
54 * Converts wide-char Unicode string (aka UTF-32) to multibyte
55 * locale-encoded string.
57 std::string toLocale(const ustring &string);
60 * Converts UTF-32 to UTF-8.
62 std::string toUtf8(const ustring &utf32);
65 * Convert UTF-8 to UTF-32.
67 ustring toUtf32(const std::string &utf8);
69 } // namespace bt
72 * Workaround incomplete std::char_traits<> implementation in come
73 * versions of the GNU C++ compiler.
75 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
76 # if __GNUC__ == 3
77 namespace std {
79 template<>
80 struct char_traits<bt::Uchar>
82 typedef bt::Uchar char_type;
83 typedef unsigned int int_type;
84 typedef streampos pos_type;
85 typedef streamoff off_type;
86 typedef mbstate_t state_type;
88 static void
89 assign(bt::Uchar &c1, const bt::Uchar &c2)
90 { c1 = c2; }
92 static bool
93 eq(const bt::Uchar &c1, const bt::Uchar &c2)
94 { return c1 == c2; }
96 static bool
97 lt(const bt::Uchar &c1, const bt::Uchar &c2)
98 { return c1 < c2; }
100 static int
101 compare(const bt::Uchar *s1, const bt::Uchar *s2, size_t n) {
102 for (size_t x = 0; x < n; ++x) {
103 if (lt(s1[x], s2[x]))
104 return -1;
105 if (lt(s2[x], s1[x]))
106 return 1;
108 return 0;
111 static size_t
112 length(const bt::Uchar *s)
114 size_t x = 0;
115 while (!eq(s[x], bt::Uchar()))
116 ++x;
117 return x;
120 static const bt::Uchar *
121 find(const bt::Uchar *s, std::size_t n, const bt::Uchar &c)
123 for (std::size_t x = 0; x < n; ++x)
124 if (eq(s[x], c))
125 return s + x;
126 return 0;
129 static bt::Uchar *
130 move(bt::Uchar *d, const bt::Uchar *s, std::size_t n) {
131 return (static_cast<bt::Uchar *>
132 (std::memmove(d, s, n *sizeof(bt::Uchar ))));
135 static bt::Uchar *
136 copy(bt::Uchar *d, const bt::Uchar *s, std::size_t n) {
137 std::copy(s, s + n, d);
138 return d;
141 static bt::Uchar *
142 assign(bt::Uchar *s, std::size_t n, bt::Uchar c) {
143 std::fill_n(s, n, c);
144 return s;
147 static bt::Uchar
148 to_char_type(const unsigned int &c)
149 { return c; }
151 static unsigned int
152 to_int_type(const bt::Uchar &c)
153 { return c; }
155 static bool
156 eq_int_type(const unsigned int &c1, const unsigned int &c2)
157 { return c1 == c2; }
159 static int_type
160 eof()
161 { return static_cast<unsigned int>(EOF); }
163 static unsigned int
164 not_eof(const unsigned int &c)
165 { return (c == eof()) ? 0 : c; }
169 } // namespace std
170 # endif // __GNUC__ == 3
171 #endif // __GNUC__ && !__INTEL_COMPILER
173 #endif // __Unicode_hh