2 /* $Id: convert.ccg,v 1.4 2006/06/05 17:32:14 murrayc Exp $ */
4 /* Copyright (C) 2002 The gtkmm Development Team
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <glib/gconvert.h>
22 #include <glib/gmessages.h>
23 #include <glib/gunicode.h>
24 #include <glibmm/utility.h>
30 /**** Glib::IConv **********************************************************/
32 IConv::IConv(const std::string& to_codeset, const std::string& from_codeset)
34 gobject_ (g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
36 if(gobject_ == reinterpret_cast<GIConv>(-1))
40 // Abuse g_convert() to create a GError object. This may seem a weird
41 // thing to do, but it gives us consistently translated error messages
42 // at no further cost.
43 g_convert("", 0, to_codeset.c_str(), from_codeset.c_str(), 0, 0, &gerror);
45 // If this should ever fail we're fucked.
46 g_assert(gerror != 0);
48 #ifdef GLIBMM_EXCEPTIONS_ENABLED
49 if(gerror) ::Glib::Error::throw_exception(gerror);
50 #endif //GLIBMM_EXCEPTIONS_ENABLED
54 IConv::IConv(GIConv gobject)
61 g_iconv_close(gobject_);
64 size_t IConv::iconv(char** inbuf, gsize* inbytes_left, char** outbuf, gsize* outbytes_left)
66 return g_iconv(gobject_, inbuf, inbytes_left, outbuf, outbytes_left);
71 // Apparently iconv() on Solaris <= 7 segfaults if you pass in
72 // NULL for anything but inbuf; work around that. (NULL outbuf
73 // or NULL *outbuf is allowed by Unix98.)
76 gsize inbytes_left = 0;
77 gsize outbytes_left = 0;
79 g_iconv(gobject_, 0, &inbytes_left, &outbuf, &outbytes_left);
82 #ifdef GLIBMM_EXCEPTIONS_ENABLED
83 std::string IConv::convert(const std::string& str)
85 std::string IConv::convert(const std::string& str, std::auto_ptr<Glib::Error>& error)
86 #endif //GLIBMM_EXCEPTIONS_ENABLED
88 gsize bytes_written = 0;
91 char *const buf = g_convert_with_iconv(
92 str.data(), str.size(), gobject_, 0, &bytes_written, &gerror);
94 #ifdef GLIBMM_EXCEPTIONS_ENABLED
95 if(gerror) ::Glib::Error::throw_exception(gerror);
97 if(gerror) error = ::Glib::Error::throw_exception(gerror);
98 #endif //GLIBMM_EXCEPTIONS_ENABLED
100 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
104 /**** charset conversion functions *****************************************/
108 return g_get_charset(0);
111 bool get_charset(std::string& charset)
113 const char* charset_cstr = 0;
114 const bool is_utf8 = g_get_charset(&charset_cstr);
116 charset = charset_cstr;
121 #ifdef GLIBMM_EXCEPTIONS_ENABLED
122 std::string convert(const std::string& str,
123 const std::string& to_codeset,
124 const std::string& from_codeset)
126 std::string convert(const std::string& str,
127 const std::string& to_codeset,
128 const std::string& from_codeset, std::auto_ptr<Glib::Error>& error)
129 #endif //GLIBMM_EXCEPTIONS_ENABLED
131 gsize bytes_written = 0;
134 char *const buf = g_convert(
135 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
136 0, &bytes_written, &gerror);
138 #ifdef GLIBMM_EXCEPTIONS_ENABLED
139 if(gerror) ::Glib::Error::throw_exception(gerror);
141 if(gerror) error = ::Glib::Error::throw_exception(gerror);
142 #endif //GLIBMM_EXCEPTIONS_ENABLED
144 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
148 #ifdef GLIBMM_EXCEPTIONS_ENABLED
149 std::string convert_with_fallback(const std::string& str,
150 const std::string& to_codeset,
151 const std::string& from_codeset)
153 std::string convert_with_fallback(const std::string& str,
154 const std::string& to_codeset,
155 const std::string& from_codeset, std::auto_ptr<Glib::Error>& error)
156 #endif //GLIBMM_EXCEPTIONS_ENABLED
158 gsize bytes_written = 0;
161 char *const buf = g_convert_with_fallback(
162 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(), 0,
163 0, &bytes_written, &gerror);
165 #ifdef GLIBMM_EXCEPTIONS_ENABLED
166 if(gerror) ::Glib::Error::throw_exception(gerror);
168 if(gerror) error = ::Glib::Error::throw_exception(gerror);
169 #endif //GLIBMM_EXCEPTIONS_ENABLED
171 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
175 #ifdef GLIBMM_EXCEPTIONS_ENABLED
176 std::string convert_with_fallback(const std::string& str,
177 const std::string& to_codeset,
178 const std::string& from_codeset,
179 const Glib::ustring& fallback)
181 std::string convert_with_fallback(const std::string& str,
182 const std::string& to_codeset,
183 const std::string& from_codeset,
184 const Glib::ustring& fallback, std::auto_ptr<Glib::Error>& error)
185 #endif //GLIBMM_EXCEPTIONS_ENABLED
187 gsize bytes_written = 0;
190 char *const buf = g_convert_with_fallback(
191 str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
192 const_cast<char*>(fallback.c_str()), 0, &bytes_written, &gerror);
194 #ifdef GLIBMM_EXCEPTIONS_ENABLED
195 if(gerror) ::Glib::Error::throw_exception(gerror);
197 if(gerror) error = ::Glib::Error::throw_exception(gerror);
198 #endif //GLIBMM_EXCEPTIONS_ENABLED
200 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
204 #ifdef GLIBMM_EXCEPTIONS_ENABLED
205 Glib::ustring locale_to_utf8(const std::string& opsys_string)
207 Glib::ustring locale_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
208 #endif //GLIBMM_EXCEPTIONS_ENABLED
210 gsize bytes_written = 0;
213 char *const buf = g_locale_to_utf8(
214 opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
216 #ifdef GLIBMM_EXCEPTIONS_ENABLED
217 if(gerror) ::Glib::Error::throw_exception(gerror);
219 if(gerror) error = ::Glib::Error::throw_exception(gerror);
220 #endif //GLIBMM_EXCEPTIONS_ENABLED
222 const ScopedPtr<char> scoped_buf (buf);
223 return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
227 #ifdef GLIBMM_EXCEPTIONS_ENABLED
228 std::string locale_from_utf8(const Glib::ustring& utf8_string)
230 std::string locale_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
231 #endif //GLIBMM_EXCEPTIONS_ENABLED
233 gsize bytes_written = 0;
236 char *const buf = g_locale_from_utf8(
237 utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
239 #ifdef GLIBMM_EXCEPTIONS_ENABLED
240 if(gerror) ::Glib::Error::throw_exception(gerror);
242 if(gerror) error = ::Glib::Error::throw_exception(gerror);
243 #endif //GLIBMM_EXCEPTIONS_ENABLED
245 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
249 #ifdef GLIBMM_EXCEPTIONS_ENABLED
250 Glib::ustring filename_to_utf8(const std::string& opsys_string)
252 Glib::ustring filename_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
253 #endif //GLIBMM_EXCEPTIONS_ENABLED
255 gsize bytes_written = 0;
258 char *const buf = g_filename_to_utf8(
259 opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
261 #ifdef GLIBMM_EXCEPTIONS_ENABLED
262 if(gerror) ::Glib::Error::throw_exception(gerror);
264 if(gerror) error = ::Glib::Error::throw_exception(gerror);
265 #endif //GLIBMM_EXCEPTIONS_ENABLED
267 const ScopedPtr<char> scoped_buf (buf);
268 return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
272 #ifdef GLIBMM_EXCEPTIONS_ENABLED
273 std::string filename_from_utf8(const Glib::ustring& utf8_string)
275 std::string filename_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
276 #endif //GLIBMM_EXCEPTIONS_ENABLED
278 gsize bytes_written = 0;
281 char *const buf = g_filename_from_utf8(
282 utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
284 #ifdef GLIBMM_EXCEPTIONS_ENABLED
285 if(gerror) ::Glib::Error::throw_exception(gerror);
287 if(gerror) error = ::Glib::Error::throw_exception(gerror);
288 #endif //GLIBMM_EXCEPTIONS_ENABLED
290 return std::string(ScopedPtr<char>(buf).get(), bytes_written);
294 #ifdef GLIBMM_EXCEPTIONS_ENABLED
295 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname)
297 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname, std::auto_ptr<Glib::Error>& error)
298 #endif //GLIBMM_EXCEPTIONS_ENABLED
300 char* hostname_buf = 0;
303 char *const buf = g_filename_from_uri(uri.c_str(), &hostname_buf, &gerror);
305 #ifdef GLIBMM_EXCEPTIONS_ENABLED
306 if(gerror) ::Glib::Error::throw_exception(gerror);
308 if(gerror) error = ::Glib::Error::throw_exception(gerror);
309 #endif //GLIBMM_EXCEPTIONS_ENABLED
311 // Let's take ownership at this point.
312 const ScopedPtr<char> scoped_buf (buf);
315 hostname = ScopedPtr<char>(hostname_buf).get();
319 return std::string(scoped_buf.get());
323 #ifdef GLIBMM_EXCEPTIONS_ENABLED
324 std::string filename_from_uri(const Glib::ustring& uri)
326 std::string filename_from_uri(const Glib::ustring& uri, std::auto_ptr<Glib::Error>& error)
327 #endif //GLIBMM_EXCEPTIONS_ENABLED
330 char *const buf = g_filename_from_uri(uri.c_str(), 0, &gerror);
332 #ifdef GLIBMM_EXCEPTIONS_ENABLED
333 if(gerror) ::Glib::Error::throw_exception(gerror);
335 if(gerror) error = ::Glib::Error::throw_exception(gerror);
336 #endif //GLIBMM_EXCEPTIONS_ENABLED
338 return std::string(ScopedPtr<char>(buf).get());
342 #ifdef GLIBMM_EXCEPTIONS_ENABLED
343 Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname)
345 Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname, std::auto_ptr<Glib::Error>& error)
346 #endif //GLIBMM_EXCEPTIONS_ENABLED
349 char *const buf = g_filename_to_uri(filename.c_str(), hostname.c_str(), &gerror);
351 #ifdef GLIBMM_EXCEPTIONS_ENABLED
352 if(gerror) ::Glib::Error::throw_exception(gerror);
354 if(gerror) error = ::Glib::Error::throw_exception(gerror);
355 #endif //GLIBMM_EXCEPTIONS_ENABLED
357 return Glib::ustring(ScopedPtr<char>(buf).get());
361 #ifdef GLIBMM_EXCEPTIONS_ENABLED
362 Glib::ustring filename_to_uri(const std::string& filename)
364 Glib::ustring filename_to_uri(const std::string& filename, std::auto_ptr<Glib::Error>& error)
365 #endif //GLIBMM_EXCEPTIONS_ENABLED
368 char *const buf = g_filename_to_uri(filename.c_str(), 0, &gerror);
370 #ifdef GLIBMM_EXCEPTIONS_ENABLED
371 if(gerror) ::Glib::Error::throw_exception(gerror);
373 if(gerror) error = ::Glib::Error::throw_exception(gerror);
374 #endif //GLIBMM_EXCEPTIONS_ENABLED
376 return Glib::ustring(ScopedPtr<char>(buf).get());
379 Glib::ustring filename_display_basename(const std::string& filename)
381 char *const buf = g_filename_display_basename(filename.c_str());
383 return Glib::ustring(ScopedPtr<char>(buf).get());
387 Glib::ustring filename_display_name(const std::string& filename)
389 char *const buf = g_filename_display_name(filename.c_str());
391 return Glib::ustring(ScopedPtr<char>(buf).get());