1 // Generated by gtkmmproc -- DO NOT MODIFY!
4 #include <glibmm/convert.h>
5 #include <glibmm/private/convert_p.h>
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/gconvert.h>
28 #include <glib/gmessages.h>
29 #include <glib/gunicode.h>
30 #include <glibmm/utility.h>
36 /**** Glib::IConv **********************************************************/
38 IConv::IConv(const std::string& to_codeset, const std::string& from_codeset)
40 gobject_ (g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
42 if(gobject_ == reinterpret_cast<GIConv>(-1))
46 // Abuse g_convert() to create a GError object. This may seem a weird
47 // thing to do, but it gives us consistently translated error messages
48 // at no further cost.
49 g_convert("", 0, to_codeset.c_str(), from_codeset.c_str(), 0, 0, &gerror);
51 // If this should ever fail we're fucked.
52 g_assert(gerror != 0);
54 #ifdef GLIBMM_EXCEPTIONS_ENABLED
55 if(gerror) ::Glib::Error::throw_exception(gerror);
56 #endif //GLIBMM_EXCEPTIONS_ENABLED
60 IConv::IConv(GIConv gobject)
67 g_iconv_close(gobject_);
70 size_t IConv::iconv(char** inbuf, gsize* inbytes_left, char** outbuf, gsize* outbytes_left)
72 return g_iconv(gobject_, inbuf, inbytes_left, outbuf, outbytes_left);
77 // Apparently iconv() on Solaris <= 7 segfaults if you pass in
78 // NULL for anything but inbuf; work around that. (NULL outbuf
79 // or NULL *outbuf is allowed by Unix98.)
82 gsize inbytes_left = 0;
83 gsize outbytes_left = 0;
85 g_iconv(gobject_, 0, &inbytes_left, &outbuf, &outbytes_left);
88 #ifdef GLIBMM_EXCEPTIONS_ENABLED
89 std::string IConv::convert(const std::string& str)
91 std::string IConv::convert(const std::string& str, std::auto_ptr<Glib::Error>& error)
92 #endif //GLIBMM_EXCEPTIONS_ENABLED
94 gsize bytes_written = 0;
97 char *const buf = g_convert_with_iconv(
98 str.data(), str.size(), gobject_, 0, &bytes_written, &gerror);
100 #ifdef GLIBMM_EXCEPTIONS_ENABLED
101 if(gerror) ::Glib::Error::throw_exception(gerror);
103 if(gerror) error = ::Glib::Error::throw_exception(gerror);
104 #endif //GLIBMM_EXCEPTIONS_ENABLED
106 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
110 /**** charset conversion functions *****************************************/
114 return g_get_charset(0);
117 bool get_charset(std::string& charset)
119 const char* charset_cstr = 0;
120 const bool is_utf8 = g_get_charset(&charset_cstr);
122 charset = charset_cstr;
127 #ifdef GLIBMM_EXCEPTIONS_ENABLED
128 std::string convert(const std::string& str,
129 const std::string& to_codeset,
130 const std::string& from_codeset)
132 std::string convert(const std::string& str,
133 const std::string& to_codeset,
134 const std::string& from_codeset, std::auto_ptr<Glib::Error>& error)
135 #endif //GLIBMM_EXCEPTIONS_ENABLED
137 gsize bytes_written = 0;
140 char *const buf = g_convert(
141 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
142 0, &bytes_written, &gerror);
144 #ifdef GLIBMM_EXCEPTIONS_ENABLED
145 if(gerror) ::Glib::Error::throw_exception(gerror);
147 if(gerror) error = ::Glib::Error::throw_exception(gerror);
148 #endif //GLIBMM_EXCEPTIONS_ENABLED
150 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
154 #ifdef GLIBMM_EXCEPTIONS_ENABLED
155 std::string convert_with_fallback(const std::string& str,
156 const std::string& to_codeset,
157 const std::string& from_codeset)
159 std::string convert_with_fallback(const std::string& str,
160 const std::string& to_codeset,
161 const std::string& from_codeset, std::auto_ptr<Glib::Error>& error)
162 #endif //GLIBMM_EXCEPTIONS_ENABLED
164 gsize bytes_written = 0;
167 char *const buf = g_convert_with_fallback(
168 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(), 0,
169 0, &bytes_written, &gerror);
171 #ifdef GLIBMM_EXCEPTIONS_ENABLED
172 if(gerror) ::Glib::Error::throw_exception(gerror);
174 if(gerror) error = ::Glib::Error::throw_exception(gerror);
175 #endif //GLIBMM_EXCEPTIONS_ENABLED
177 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
181 #ifdef GLIBMM_EXCEPTIONS_ENABLED
182 std::string convert_with_fallback(const std::string& str,
183 const std::string& to_codeset,
184 const std::string& from_codeset,
185 const Glib::ustring& fallback)
187 std::string convert_with_fallback(const std::string& str,
188 const std::string& to_codeset,
189 const std::string& from_codeset,
190 const Glib::ustring& fallback, std::auto_ptr<Glib::Error>& error)
191 #endif //GLIBMM_EXCEPTIONS_ENABLED
193 gsize bytes_written = 0;
196 char *const buf = g_convert_with_fallback(
197 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
198 const_cast<char*>(fallback.c_str()), 0, &bytes_written, &gerror);
200 #ifdef GLIBMM_EXCEPTIONS_ENABLED
201 if(gerror) ::Glib::Error::throw_exception(gerror);
203 if(gerror) error = ::Glib::Error::throw_exception(gerror);
204 #endif //GLIBMM_EXCEPTIONS_ENABLED
206 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
210 #ifdef GLIBMM_EXCEPTIONS_ENABLED
211 Glib::ustring locale_to_utf8(const std::string& opsys_string)
213 Glib::ustring locale_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
214 #endif //GLIBMM_EXCEPTIONS_ENABLED
216 gsize bytes_written = 0;
219 char *const buf = g_locale_to_utf8(
220 opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
222 #ifdef GLIBMM_EXCEPTIONS_ENABLED
223 if(gerror) ::Glib::Error::throw_exception(gerror);
225 if(gerror) error = ::Glib::Error::throw_exception(gerror);
226 #endif //GLIBMM_EXCEPTIONS_ENABLED
228 const ScopedPtr<char> scoped_buf (buf);
229 return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
233 #ifdef GLIBMM_EXCEPTIONS_ENABLED
234 std::string locale_from_utf8(const Glib::ustring& utf8_string)
236 std::string locale_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
237 #endif //GLIBMM_EXCEPTIONS_ENABLED
239 gsize bytes_written = 0;
242 char *const buf = g_locale_from_utf8(
243 utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
245 #ifdef GLIBMM_EXCEPTIONS_ENABLED
246 if(gerror) ::Glib::Error::throw_exception(gerror);
248 if(gerror) error = ::Glib::Error::throw_exception(gerror);
249 #endif //GLIBMM_EXCEPTIONS_ENABLED
251 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
255 #ifdef GLIBMM_EXCEPTIONS_ENABLED
256 Glib::ustring filename_to_utf8(const std::string& opsys_string)
258 Glib::ustring filename_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
259 #endif //GLIBMM_EXCEPTIONS_ENABLED
261 gsize bytes_written = 0;
264 char *const buf = g_filename_to_utf8(
265 opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
267 #ifdef GLIBMM_EXCEPTIONS_ENABLED
268 if(gerror) ::Glib::Error::throw_exception(gerror);
270 if(gerror) error = ::Glib::Error::throw_exception(gerror);
271 #endif //GLIBMM_EXCEPTIONS_ENABLED
273 const ScopedPtr<char> scoped_buf (buf);
274 return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
278 #ifdef GLIBMM_EXCEPTIONS_ENABLED
279 std::string filename_from_utf8(const Glib::ustring& utf8_string)
281 std::string filename_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
282 #endif //GLIBMM_EXCEPTIONS_ENABLED
284 gsize bytes_written = 0;
287 char *const buf = g_filename_from_utf8(
288 utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
290 #ifdef GLIBMM_EXCEPTIONS_ENABLED
291 if(gerror) ::Glib::Error::throw_exception(gerror);
293 if(gerror) error = ::Glib::Error::throw_exception(gerror);
294 #endif //GLIBMM_EXCEPTIONS_ENABLED
296 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
300 #ifdef GLIBMM_EXCEPTIONS_ENABLED
301 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname)
303 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname, std::auto_ptr<Glib::Error>& error)
304 #endif //GLIBMM_EXCEPTIONS_ENABLED
306 char* hostname_buf = 0;
309 char *const buf = g_filename_from_uri(uri.c_str(), &hostname_buf, &gerror);
311 #ifdef GLIBMM_EXCEPTIONS_ENABLED
312 if(gerror) ::Glib::Error::throw_exception(gerror);
314 if(gerror) error = ::Glib::Error::throw_exception(gerror);
315 #endif //GLIBMM_EXCEPTIONS_ENABLED
317 // Let's take ownership at this point.
318 const ScopedPtr<char> scoped_buf (buf);
321 hostname = ScopedPtr<char>(hostname_buf).get();
325 return std::string(scoped_buf.get());
329 #ifdef GLIBMM_EXCEPTIONS_ENABLED
330 std::string filename_from_uri(const Glib::ustring& uri)
332 std::string filename_from_uri(const Glib::ustring& uri, std::auto_ptr<Glib::Error>& error)
333 #endif //GLIBMM_EXCEPTIONS_ENABLED
336 char *const buf = g_filename_from_uri(uri.c_str(), 0, &gerror);
338 #ifdef GLIBMM_EXCEPTIONS_ENABLED
339 if(gerror) ::Glib::Error::throw_exception(gerror);
341 if(gerror) error = ::Glib::Error::throw_exception(gerror);
342 #endif //GLIBMM_EXCEPTIONS_ENABLED
344 return std::string(ScopedPtr<char>(buf).get());
348 #ifdef GLIBMM_EXCEPTIONS_ENABLED
349 Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname)
351 Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname, std::auto_ptr<Glib::Error>& error)
352 #endif //GLIBMM_EXCEPTIONS_ENABLED
355 char *const buf = g_filename_to_uri(filename.c_str(), hostname.c_str(), &gerror);
357 #ifdef GLIBMM_EXCEPTIONS_ENABLED
358 if(gerror) ::Glib::Error::throw_exception(gerror);
360 if(gerror) error = ::Glib::Error::throw_exception(gerror);
361 #endif //GLIBMM_EXCEPTIONS_ENABLED
363 return Glib::ustring(ScopedPtr<char>(buf).get());
367 #ifdef GLIBMM_EXCEPTIONS_ENABLED
368 Glib::ustring filename_to_uri(const std::string& filename)
370 Glib::ustring filename_to_uri(const std::string& filename, std::auto_ptr<Glib::Error>& error)
371 #endif //GLIBMM_EXCEPTIONS_ENABLED
374 char *const buf = g_filename_to_uri(filename.c_str(), 0, &gerror);
376 #ifdef GLIBMM_EXCEPTIONS_ENABLED
377 if(gerror) ::Glib::Error::throw_exception(gerror);
379 if(gerror) error = ::Glib::Error::throw_exception(gerror);
380 #endif //GLIBMM_EXCEPTIONS_ENABLED
382 return Glib::ustring(ScopedPtr<char>(buf).get());
385 Glib::ustring filename_display_basename(const std::string& filename)
387 char *const buf = g_filename_display_basename(filename.c_str());
389 return Glib::ustring(ScopedPtr<char>(buf).get());
393 Glib::ustring filename_display_name(const std::string& filename)
395 char *const buf = g_filename_display_name(filename.c_str());
397 return Glib::ustring(ScopedPtr<char>(buf).get());
405 } // anonymous namespace
408 Glib::ConvertError::ConvertError(Glib::ConvertError::Code error_code, const Glib::ustring& error_message)
410 Glib::Error (G_CONVERT_ERROR, error_code, error_message)
413 Glib::ConvertError::ConvertError(GError* gobject)
415 Glib::Error (gobject)
418 Glib::ConvertError::Code Glib::ConvertError::code() const
420 return static_cast<Code>(Glib::Error::code());
423 #ifdef GLIBMM_EXCEPTIONS_ENABLED
424 void Glib::ConvertError::throw_func(GError* gobject)
426 throw Glib::ConvertError(gobject);
429 //When not using exceptions, we just pass the Exception object around without throwing it:
430 std::auto_ptr<Glib::Error> Glib::ConvertError::throw_func(GError* gobject)
432 return std::auto_ptr<Glib::Error>(new Glib::ConvertError(gobject));
434 #endif //GLIBMM_EXCEPTIONS_ENABLED