1 // Generated by gtkmmproc -- DO NOT MODIFY!
4 #include <glibmm/convert.h>
5 #include <glibmm/private/convert_p.h>
8 /* $Id: convert.ccg,v 1.4 2006/06/05 17:32:14 murrayc Exp $ */
10 /* Copyright (C) 2002 The gtkmm Development Team
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
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 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the Free
24 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 //#include <glib/gtestutils.h> //For g_assert() in glib >= 2.15.0
28 //#include <glib/gmessages.h> //For g_assert() in glib < 2.15.0
29 #include <glib.h> //For g_assert() in all versions of glib.
31 #include <glibmm/utility.h>
37 /**** Glib::IConv **********************************************************/
39 IConv::IConv(const std::string& to_codeset, const std::string& from_codeset)
41 gobject_ (g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
43 if(gobject_ == reinterpret_cast<GIConv>(-1))
47 // Abuse g_convert() to create a GError object. This may seem a weird
48 // thing to do, but it gives us consistently translated error messages
49 // at no further cost.
50 g_convert("", 0, to_codeset.c_str(), from_codeset.c_str(), 0, 0, &gerror);
52 // If this should ever fail we're fucked.
53 g_assert(gerror != 0);
55 #ifdef GLIBMM_EXCEPTIONS_ENABLED
56 if(gerror) ::Glib::Error::throw_exception(gerror);
57 #endif //GLIBMM_EXCEPTIONS_ENABLED
61 IConv::IConv(GIConv gobject)
68 g_iconv_close(gobject_);
71 size_t IConv::iconv(char** inbuf, gsize* inbytes_left, char** outbuf, gsize* outbytes_left)
73 return g_iconv(gobject_, inbuf, inbytes_left, outbuf, outbytes_left);
78 // Apparently iconv() on Solaris <= 7 segfaults if you pass in
79 // NULL for anything but inbuf; work around that. (NULL outbuf
80 // or NULL *outbuf is allowed by Unix98.)
83 gsize inbytes_left = 0;
84 gsize outbytes_left = 0;
86 g_iconv(gobject_, 0, &inbytes_left, &outbuf, &outbytes_left);
89 #ifdef GLIBMM_EXCEPTIONS_ENABLED
90 std::string IConv::convert(const std::string& str)
92 std::string IConv::convert(const std::string& str, std::auto_ptr<Glib::Error>& error)
93 #endif //GLIBMM_EXCEPTIONS_ENABLED
95 gsize bytes_written = 0;
98 char *const buf = g_convert_with_iconv(
99 str.data(), str.size(), gobject_, 0, &bytes_written, &gerror);
101 #ifdef GLIBMM_EXCEPTIONS_ENABLED
102 if(gerror) ::Glib::Error::throw_exception(gerror);
104 if(gerror) error = ::Glib::Error::throw_exception(gerror);
105 #endif //GLIBMM_EXCEPTIONS_ENABLED
107 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
111 /**** charset conversion functions *****************************************/
115 return g_get_charset(0);
118 bool get_charset(std::string& charset)
120 const char* charset_cstr = 0;
121 const bool is_utf8 = g_get_charset(&charset_cstr);
123 charset = charset_cstr;
128 #ifdef GLIBMM_EXCEPTIONS_ENABLED
129 std::string convert(const std::string& str,
130 const std::string& to_codeset,
131 const std::string& from_codeset)
133 std::string convert(const std::string& str,
134 const std::string& to_codeset,
135 const std::string& from_codeset, std::auto_ptr<Glib::Error>& error)
136 #endif //GLIBMM_EXCEPTIONS_ENABLED
138 gsize bytes_written = 0;
141 char *const buf = g_convert(
142 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
143 0, &bytes_written, &gerror);
145 #ifdef GLIBMM_EXCEPTIONS_ENABLED
146 if(gerror) ::Glib::Error::throw_exception(gerror);
148 if(gerror) error = ::Glib::Error::throw_exception(gerror);
149 #endif //GLIBMM_EXCEPTIONS_ENABLED
151 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
155 #ifdef GLIBMM_EXCEPTIONS_ENABLED
156 std::string convert_with_fallback(const std::string& str,
157 const std::string& to_codeset,
158 const std::string& from_codeset)
160 std::string convert_with_fallback(const std::string& str,
161 const std::string& to_codeset,
162 const std::string& from_codeset, std::auto_ptr<Glib::Error>& error)
163 #endif //GLIBMM_EXCEPTIONS_ENABLED
165 gsize bytes_written = 0;
168 char *const buf = g_convert_with_fallback(
169 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(), 0,
170 0, &bytes_written, &gerror);
172 #ifdef GLIBMM_EXCEPTIONS_ENABLED
173 if(gerror) ::Glib::Error::throw_exception(gerror);
175 if(gerror) error = ::Glib::Error::throw_exception(gerror);
176 #endif //GLIBMM_EXCEPTIONS_ENABLED
178 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
182 #ifdef GLIBMM_EXCEPTIONS_ENABLED
183 std::string convert_with_fallback(const std::string& str,
184 const std::string& to_codeset,
185 const std::string& from_codeset,
186 const Glib::ustring& fallback)
188 std::string convert_with_fallback(const std::string& str,
189 const std::string& to_codeset,
190 const std::string& from_codeset,
191 const Glib::ustring& fallback, std::auto_ptr<Glib::Error>& error)
192 #endif //GLIBMM_EXCEPTIONS_ENABLED
194 gsize bytes_written = 0;
197 char *const buf = g_convert_with_fallback(
198 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
199 const_cast<char*>(fallback.c_str()), 0, &bytes_written, &gerror);
201 #ifdef GLIBMM_EXCEPTIONS_ENABLED
202 if(gerror) ::Glib::Error::throw_exception(gerror);
204 if(gerror) error = ::Glib::Error::throw_exception(gerror);
205 #endif //GLIBMM_EXCEPTIONS_ENABLED
207 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
211 #ifdef GLIBMM_EXCEPTIONS_ENABLED
212 Glib::ustring locale_to_utf8(const std::string& opsys_string)
214 Glib::ustring locale_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
215 #endif //GLIBMM_EXCEPTIONS_ENABLED
217 gsize bytes_written = 0;
220 char *const buf = g_locale_to_utf8(
221 opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
223 #ifdef GLIBMM_EXCEPTIONS_ENABLED
224 if(gerror) ::Glib::Error::throw_exception(gerror);
226 if(gerror) error = ::Glib::Error::throw_exception(gerror);
227 #endif //GLIBMM_EXCEPTIONS_ENABLED
229 const ScopedPtr<char> scoped_buf (buf);
230 return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
234 #ifdef GLIBMM_EXCEPTIONS_ENABLED
235 std::string locale_from_utf8(const Glib::ustring& utf8_string)
237 std::string locale_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
238 #endif //GLIBMM_EXCEPTIONS_ENABLED
240 gsize bytes_written = 0;
243 char *const buf = g_locale_from_utf8(
244 utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
246 #ifdef GLIBMM_EXCEPTIONS_ENABLED
247 if(gerror) ::Glib::Error::throw_exception(gerror);
249 if(gerror) error = ::Glib::Error::throw_exception(gerror);
250 #endif //GLIBMM_EXCEPTIONS_ENABLED
252 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
256 #ifdef GLIBMM_EXCEPTIONS_ENABLED
257 Glib::ustring filename_to_utf8(const std::string& opsys_string)
259 Glib::ustring filename_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
260 #endif //GLIBMM_EXCEPTIONS_ENABLED
262 gsize bytes_written = 0;
265 char *const buf = g_filename_to_utf8(
266 opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
268 #ifdef GLIBMM_EXCEPTIONS_ENABLED
269 if(gerror) ::Glib::Error::throw_exception(gerror);
271 if(gerror) error = ::Glib::Error::throw_exception(gerror);
272 #endif //GLIBMM_EXCEPTIONS_ENABLED
274 const ScopedPtr<char> scoped_buf (buf);
275 return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
279 #ifdef GLIBMM_EXCEPTIONS_ENABLED
280 std::string filename_from_utf8(const Glib::ustring& utf8_string)
282 std::string filename_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
283 #endif //GLIBMM_EXCEPTIONS_ENABLED
285 gsize bytes_written = 0;
288 char *const buf = g_filename_from_utf8(
289 utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
291 #ifdef GLIBMM_EXCEPTIONS_ENABLED
292 if(gerror) ::Glib::Error::throw_exception(gerror);
294 if(gerror) error = ::Glib::Error::throw_exception(gerror);
295 #endif //GLIBMM_EXCEPTIONS_ENABLED
297 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
301 #ifdef GLIBMM_EXCEPTIONS_ENABLED
302 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname)
304 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname, std::auto_ptr<Glib::Error>& error)
305 #endif //GLIBMM_EXCEPTIONS_ENABLED
307 char* hostname_buf = 0;
310 char *const buf = g_filename_from_uri(uri.c_str(), &hostname_buf, &gerror);
312 #ifdef GLIBMM_EXCEPTIONS_ENABLED
313 if(gerror) ::Glib::Error::throw_exception(gerror);
315 if(gerror) error = ::Glib::Error::throw_exception(gerror);
316 #endif //GLIBMM_EXCEPTIONS_ENABLED
318 // Let's take ownership at this point.
319 const ScopedPtr<char> scoped_buf (buf);
322 hostname = ScopedPtr<char>(hostname_buf).get();
326 return std::string(scoped_buf.get());
330 #ifdef GLIBMM_EXCEPTIONS_ENABLED
331 std::string filename_from_uri(const Glib::ustring& uri)
333 std::string filename_from_uri(const Glib::ustring& uri, std::auto_ptr<Glib::Error>& error)
334 #endif //GLIBMM_EXCEPTIONS_ENABLED
337 char *const buf = g_filename_from_uri(uri.c_str(), 0, &gerror);
339 #ifdef GLIBMM_EXCEPTIONS_ENABLED
340 if(gerror) ::Glib::Error::throw_exception(gerror);
342 if(gerror) error = ::Glib::Error::throw_exception(gerror);
343 #endif //GLIBMM_EXCEPTIONS_ENABLED
345 return std::string(ScopedPtr<char>(buf).get());
349 #ifdef GLIBMM_EXCEPTIONS_ENABLED
350 Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname)
352 Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname, std::auto_ptr<Glib::Error>& error)
353 #endif //GLIBMM_EXCEPTIONS_ENABLED
356 char *const buf = g_filename_to_uri(filename.c_str(), hostname.c_str(), &gerror);
358 #ifdef GLIBMM_EXCEPTIONS_ENABLED
359 if(gerror) ::Glib::Error::throw_exception(gerror);
361 if(gerror) error = ::Glib::Error::throw_exception(gerror);
362 #endif //GLIBMM_EXCEPTIONS_ENABLED
364 return Glib::ustring(ScopedPtr<char>(buf).get());
368 #ifdef GLIBMM_EXCEPTIONS_ENABLED
369 Glib::ustring filename_to_uri(const std::string& filename)
371 Glib::ustring filename_to_uri(const std::string& filename, std::auto_ptr<Glib::Error>& error)
372 #endif //GLIBMM_EXCEPTIONS_ENABLED
375 char *const buf = g_filename_to_uri(filename.c_str(), 0, &gerror);
377 #ifdef GLIBMM_EXCEPTIONS_ENABLED
378 if(gerror) ::Glib::Error::throw_exception(gerror);
380 if(gerror) error = ::Glib::Error::throw_exception(gerror);
381 #endif //GLIBMM_EXCEPTIONS_ENABLED
383 return Glib::ustring(ScopedPtr<char>(buf).get());
386 Glib::ustring filename_display_basename(const std::string& filename)
388 char *const buf = g_filename_display_basename(filename.c_str());
390 return Glib::ustring(ScopedPtr<char>(buf).get());
394 Glib::ustring filename_display_name(const std::string& filename)
396 char *const buf = g_filename_display_name(filename.c_str());
398 return Glib::ustring(ScopedPtr<char>(buf).get());
406 } // anonymous namespace
409 Glib::ConvertError::ConvertError(Glib::ConvertError::Code error_code, const Glib::ustring& error_message)
411 Glib::Error (G_CONVERT_ERROR, error_code, error_message)
414 Glib::ConvertError::ConvertError(GError* gobject)
416 Glib::Error (gobject)
419 Glib::ConvertError::Code Glib::ConvertError::code() const
421 return static_cast<Code>(Glib::Error::code());
424 #ifdef GLIBMM_EXCEPTIONS_ENABLED
425 void Glib::ConvertError::throw_func(GError* gobject)
427 throw Glib::ConvertError(gobject);
430 //When not using exceptions, we just pass the Exception object around without throwing it:
431 std::auto_ptr<Glib::Error> Glib::ConvertError::throw_func(GError* gobject)
433 return std::auto_ptr<Glib::Error>(new Glib::ConvertError(gobject));
435 #endif //GLIBMM_EXCEPTIONS_ENABLED