fixes compile
[platform/upstream/ibus-libpinyin.git] / src / PYString.h
1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
4  *
5  * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21 #ifndef __PY_STRING_H_
22 #define __PY_STRING_H_
23
24 #include <glib.h>
25 #include <stdarg.h>
26 #include <string>
27
28 namespace PY {
29
30 class String : public std::string {
31 public:
32     String () : std::string () { }
33     String (const gchar *str) : std::string (str) { }
34     String (const std::string &str) : std::string (str) { }
35     String (gint len) : std::string () { reserve (len); }
36
37     String & printf (const gchar *fmt, ...)
38     {
39         gchar *str;
40         va_list args;
41
42         va_start (args, fmt);
43         str = g_strdup_vprintf (fmt, args);
44         va_end (args);
45
46         assign (str);
47         g_free (str);
48         return *this;
49     }
50
51     String & appendPrintf (const gchar *fmt, ...)
52     {
53         gchar *str;
54         va_list args;
55
56         va_start (args, fmt);
57         str = g_strdup_vprintf (fmt, args);
58         va_end (args);
59
60         append (str);
61         g_free (str);
62
63         return *this;
64     }
65
66     String & appendUnichar (gunichar ch)
67     {
68         gchar str[12];
69         gint len;
70         len = g_unichar_to_utf8 (ch, str);
71         str[len] = 0;
72         append (str);
73         return *this;
74     }
75
76     String & insert (gint i, gchar ch)
77     {
78         std::string::insert (i, 1, ch);
79         return *this;
80     }
81
82     String & truncate (guint len)
83     {
84         erase(len);
85         return *this;
86     }
87
88     gsize utf8Length (void) const
89     {
90         return g_utf8_strlen (c_str(), -1);
91     }
92
93     String & operator<< (gint i)
94     {
95         return appendPrintf ("%d", i);
96     }
97
98     String & operator<< (guint i)
99     {
100         return appendPrintf ("%u", i);
101     }
102
103     String & operator<< (const gchar ch)
104     {
105         append (1, ch);
106         return *this;
107     }
108
109     String & operator<< (const gchar *str)
110     {
111         append (str);
112         return *this;
113     }
114
115     String & operator<< (const gunichar *wstr)
116     {
117         gchar *str;
118         GError *error;
119         str = g_ucs4_to_utf8 (wstr, -1, NULL, NULL, &error);
120         if (str == NULL) {
121             g_warning ("convert ucs4 to utf8 failed: %s", error->message);
122             g_error_free (error);
123         }
124         else {
125             append (str);
126             g_free (str);
127         }
128         return *this;
129     }
130
131     gchar operator[] (gint i)
132     {
133         return std::string::operator[] (i);
134     }
135
136     String & operator<< (const std::string &str)
137     {
138         return operator<< (str.c_str ());
139     }
140
141     String & operator<< (const String &str)
142     {
143         return operator<< ((const gchar *)str);
144     }
145
146     String & operator= (const gchar * str)
147     {
148         assign (str);
149         return *this;
150     }
151
152     operator const gchar *(void) const
153     {
154         return this->c_str ();
155     }
156
157     operator gboolean (void) const
158     {
159         return ! empty ();
160     }
161 };
162
163 };
164 #endif