1 // Generated by gmmproc 2.64.2 -- DO NOT MODIFY!
2
3
4 #include <glibmm.h>
5
6 #include <glibmm/convert.h>
7 #include <glibmm/private/convert_p.h>
8
9
10 /* Copyright (C) 2002 The gtkmm Development Team
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 //#include <glib/gtestutils.h> //For g_assert() in glib >= 2.15.0
27 //#include <glib/gmessages.h> //For g_assert() in glib < 2.15.0
28 #include <glib.h> //For g_assert() in all versions of glib.
29
30 #include <glibmm/utility.h>
31
32 namespace Glib
33 {
34
35 /**** Glib::IConv **********************************************************/
36
IConv(const std::string & to_codeset,const std::string & from_codeset)37 IConv::IConv(const std::string& to_codeset, const std::string& from_codeset)
38 : gobject_(g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
39 {
40 if (gobject_ == reinterpret_cast<GIConv>(-1))
41 {
42 GError* gerror = nullptr;
43
44 // Abuse g_convert() to create a GError object. This may seem a weird
45 // thing to do, but it gives us consistently translated error messages
46 // at no further cost.
47 g_convert("", 0, to_codeset.c_str(), from_codeset.c_str(), nullptr, nullptr, &gerror);
48
49 // If this should ever fail we're fucked.
50 g_assert(gerror != nullptr);
51
52 if (gerror)
53 ::Glib::Error::throw_exception(gerror);
54 }
55 }
56
IConv(GIConv gobject)57 IConv::IConv(GIConv gobject) : gobject_(gobject)
58 {
59 }
60
~IConv()61 IConv::~IConv()
62 {
63 g_iconv_close(gobject_);
64 }
65
66 std::size_t
iconv(char ** inbuf,gsize * inbytes_left,char ** outbuf,gsize * outbytes_left)67 IConv::iconv(char** inbuf, gsize* inbytes_left, char** outbuf, gsize* outbytes_left)
68 {
69 return g_iconv(gobject_, inbuf, inbytes_left, outbuf, outbytes_left);
70 }
71
72 void
reset()73 IConv::reset()
74 {
75 // Apparently iconv() on Solaris <= 7 segfaults if you pass in
76 // NULL for anything but inbuf; work around that. (NULL outbuf
77 // or NULL *outbuf is allowed by Unix98.)
78
79 char* outbuf = nullptr;
80 gsize inbytes_left = 0;
81 gsize outbytes_left = 0;
82
83 g_iconv(gobject_, nullptr, &inbytes_left, &outbuf, &outbytes_left);
84 }
85
86 std::string
convert(const std::string & str)87 IConv::convert(const std::string& str)
88 {
89 gsize bytes_written = 0;
90 GError* gerror = nullptr;
91
92 char* const buf =
93 g_convert_with_iconv(str.data(), str.size(), gobject_, nullptr, &bytes_written, &gerror);
94
95 if (gerror)
96 ::Glib::Error::throw_exception(gerror);
97
98 // TODO: Avoid the copy by using a perfect-forwarding std::string constructor?
99 return std::string(make_unique_ptr_gfree(buf).get(), bytes_written);
100 }
101
102 /**** charset conversion functions *****************************************/
103
104 bool
get_charset()105 get_charset()
106 {
107 return g_get_charset(nullptr);
108 }
109
110 bool
get_charset(std::string & charset)111 get_charset(std::string& charset)
112 {
113 const char* charset_cstr = nullptr;
114 const bool is_utf8 = g_get_charset(&charset_cstr);
115
116 charset = charset_cstr;
117 return is_utf8;
118 }
119
120 std::string
convert(const std::string & str,const std::string & to_codeset,const std::string & from_codeset)121 convert(const std::string& str, const std::string& to_codeset, const std::string& from_codeset)
122 {
123 gsize bytes_written = 0;
124 GError* gerror = nullptr;
125
126 char* const buf = g_convert(str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
127 nullptr, &bytes_written, &gerror);
128
129 if (gerror)
130 ::Glib::Error::throw_exception(gerror);
131
132 // TODO: Avoid the copy by using a perfect-forwarding std::string constructor?
133 return std::string(make_unique_ptr_gfree(buf).get(), bytes_written);
134 }
135
136 std::string
convert_with_fallback(const std::string & str,const std::string & to_codeset,const std::string & from_codeset)137 convert_with_fallback(
138 const std::string& str, const std::string& to_codeset, const std::string& from_codeset)
139 {
140 gsize bytes_written = 0;
141 GError* gerror = nullptr;
142
143 char* const buf = g_convert_with_fallback(str.data(), str.size(), to_codeset.c_str(),
144 from_codeset.c_str(), nullptr, nullptr, &bytes_written, &gerror);
145
146 if (gerror)
147 ::Glib::Error::throw_exception(gerror);
148
149 // TODO: Avoid the copy by using a perfect-forwarding std::string constructor?
150 return std::string(make_unique_ptr_gfree(buf).get(), bytes_written);
151 }
152
153 std::string
convert_with_fallback(const std::string & str,const std::string & to_codeset,const std::string & from_codeset,const Glib::ustring & fallback)154 convert_with_fallback(const std::string& str, const std::string& to_codeset,
155 const std::string& from_codeset, const Glib::ustring& fallback)
156 {
157 gsize bytes_written = 0;
158 GError* gerror = nullptr;
159
160 char* const buf = g_convert_with_fallback(str.data(), str.size(), to_codeset.c_str(),
161 from_codeset.c_str(), const_cast<char*>(fallback.c_str()), nullptr, &bytes_written, &gerror);
162
163 if (gerror)
164 ::Glib::Error::throw_exception(gerror);
165
166 return std::string(make_unique_ptr_gfree(buf).get(), bytes_written);
167 }
168
169 Glib::ustring
locale_to_utf8(const std::string & opsys_string)170 locale_to_utf8(const std::string& opsys_string)
171 {
172 gsize bytes_written = 0;
173 GError* gerror = nullptr;
174
175 char* const buf =
176 g_locale_to_utf8(opsys_string.data(), opsys_string.size(), nullptr, &bytes_written, &gerror);
177
178 if (gerror)
179 ::Glib::Error::throw_exception(gerror);
180
181 const auto scoped_buf = make_unique_ptr_gfree(buf);
182 return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
183 }
184
185 std::string
locale_from_utf8(const Glib::ustring & utf8_string)186 locale_from_utf8(const Glib::ustring& utf8_string)
187 {
188 gsize bytes_written = 0;
189 GError* gerror = nullptr;
190
191 char* const buf =
192 g_locale_from_utf8(utf8_string.data(), utf8_string.bytes(), nullptr, &bytes_written, &gerror);
193
194 if (gerror)
195 ::Glib::Error::throw_exception(gerror);
196
197 return std::string(make_unique_ptr_gfree(buf).get(), bytes_written);
198 }
199
200 Glib::ustring
filename_to_utf8(const std::string & opsys_string)201 filename_to_utf8(const std::string& opsys_string)
202 {
203 gsize bytes_written = 0;
204 GError* gerror = nullptr;
205
206 char* const buf =
207 g_filename_to_utf8(opsys_string.data(), opsys_string.size(), nullptr, &bytes_written, &gerror);
208
209 if (gerror)
210 ::Glib::Error::throw_exception(gerror);
211
212 const auto scoped_buf = make_unique_ptr_gfree(buf);
213 return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
214 }
215
216 std::string
filename_from_utf8(const Glib::ustring & utf8_string)217 filename_from_utf8(const Glib::ustring& utf8_string)
218 {
219 gsize bytes_written = 0;
220 GError* gerror = nullptr;
221
222 char* const buf =
223 g_filename_from_utf8(utf8_string.data(), utf8_string.bytes(), nullptr, &bytes_written, &gerror);
224
225 if (gerror)
226 ::Glib::Error::throw_exception(gerror);
227
228 return std::string(make_unique_ptr_gfree(buf).get(), bytes_written);
229 }
230
231 std::string
filename_from_uri(const Glib::ustring & uri,Glib::ustring & hostname)232 filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname)
233 {
234 char* hostname_buf = nullptr;
235 GError* gerror = nullptr;
236
237 char* const buf = g_filename_from_uri(uri.c_str(), &hostname_buf, &gerror);
238
239 if (gerror)
240 ::Glib::Error::throw_exception(gerror);
241
242 // Let's take ownership at this point.
243 const auto scoped_buf = make_unique_ptr_gfree(buf);
244
245 if (hostname_buf)
246 hostname = make_unique_ptr_gfree(buf).get();
247 else
248 hostname.erase();
249
250 return std::string(scoped_buf.get());
251 }
252
253 std::string
filename_from_uri(const Glib::ustring & uri)254 filename_from_uri(const Glib::ustring& uri)
255 {
256 GError* gerror = nullptr;
257 char* const buf = g_filename_from_uri(uri.c_str(), nullptr, &gerror);
258
259 if (gerror)
260 ::Glib::Error::throw_exception(gerror);
261
262 return std::string(make_unique_ptr_gfree(buf).get());
263 }
264
265 Glib::ustring
filename_to_uri(const std::string & filename,const Glib::ustring & hostname)266 filename_to_uri(const std::string& filename, const Glib::ustring& hostname)
267 {
268 GError* gerror = nullptr;
269 char* const buf = g_filename_to_uri(filename.c_str(), hostname.c_str(), &gerror);
270
271 if (gerror)
272 ::Glib::Error::throw_exception(gerror);
273
274 return Glib::ustring(make_unique_ptr_gfree(buf).get());
275 }
276
277 Glib::ustring
filename_to_uri(const std::string & filename)278 filename_to_uri(const std::string& filename)
279 {
280 GError* gerror = nullptr;
281 char* const buf = g_filename_to_uri(filename.c_str(), nullptr, &gerror);
282
283 if (gerror)
284 ::Glib::Error::throw_exception(gerror);
285
286 return Glib::ustring(make_unique_ptr_gfree(buf).get());
287 }
288
289 Glib::ustring
filename_display_basename(const std::string & filename)290 filename_display_basename(const std::string& filename)
291 {
292 char* const buf = g_filename_display_basename(filename.c_str());
293
294 return Glib::ustring(make_unique_ptr_gfree(buf).get());
295 }
296
297 Glib::ustring
filename_display_name(const std::string & filename)298 filename_display_name(const std::string& filename)
299 {
300 char* const buf = g_filename_display_name(filename.c_str());
301
302 return Glib::ustring(make_unique_ptr_gfree(buf).get());
303 }
304
305 } // namespace Glib
306
307 namespace
308 {
309 } // anonymous namespace
310
311
ConvertError(Glib::ConvertError::Code error_code,const Glib::ustring & error_message)312 Glib::ConvertError::ConvertError(Glib::ConvertError::Code error_code, const Glib::ustring& error_message)
313 :
314 Glib::Error (G_CONVERT_ERROR, error_code, error_message)
315 {}
316
ConvertError(GError * gobject)317 Glib::ConvertError::ConvertError(GError* gobject)
318 :
319 Glib::Error (gobject)
320 {}
321
code() const322 Glib::ConvertError::Code Glib::ConvertError::code() const
323 {
324 return static_cast<Code>(Glib::Error::code());
325 }
326
throw_func(GError * gobject)327 void Glib::ConvertError::throw_func(GError* gobject)
328 {
329 throw Glib::ConvertError(gobject);
330 }
331
332
333