Updated libglibmm2 to glibmm-2.12.5
[ardour.git] / libs / glibmm2 / glibmm / convert.cc
1 // Generated by gtkmmproc -- DO NOT MODIFY!
2
3
4 #include <glibmm/convert.h>
5 #include <glibmm/private/convert_p.h>
6
7 // -*- c++ -*-
8 /* $Id$ */
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 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.
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  * Library General Public License for more details.
21  *
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.
25  */
26
27 #include <glib/gconvert.h>
28 #include <glib/gmessages.h>
29 #include <glib/gunicode.h>
30 #include <glibmm/utility.h>
31
32
33 namespace Glib
34 {
35
36 /**** Glib::IConv **********************************************************/
37
38 IConv::IConv(const std::string& to_codeset, const std::string& from_codeset)
39 :
40   gobject_ (g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
41 {
42   if(gobject_ == reinterpret_cast<GIConv>(-1))
43   {
44     GError* gerror = 0;
45
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);
50
51     // If this should ever fail we're fucked.
52     g_assert(gerror != 0);
53
54     #ifdef GLIBMM_EXCEPTIONS_ENABLED
55     if(gerror) ::Glib::Error::throw_exception(gerror);
56     #endif //GLIBMM_EXCEPTIONS_ENABLED
57   }
58 }
59
60 IConv::IConv(GIConv gobject)
61 :
62   gobject_ (gobject)
63 {}
64
65 IConv::~IConv()
66 {
67   g_iconv_close(gobject_);
68 }
69
70 size_t IConv::iconv(char** inbuf, gsize* inbytes_left, char** outbuf, gsize* outbytes_left)
71 {
72   return g_iconv(gobject_, inbuf, inbytes_left, outbuf, outbytes_left);
73 }
74
75 void IConv::reset()
76 {
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.)
80
81   char* outbuf        = 0;
82   gsize inbytes_left  = 0;
83   gsize outbytes_left = 0;
84
85   g_iconv(gobject_, 0, &inbytes_left, &outbuf, &outbytes_left);
86 }
87
88 #ifdef GLIBMM_EXCEPTIONS_ENABLED
89 std::string IConv::convert(const std::string& str)
90 #else
91 std::string IConv::convert(const std::string& str, std::auto_ptr<Glib::Error>& error)
92 #endif //GLIBMM_EXCEPTIONS_ENABLED
93 {
94   gsize bytes_written = 0;
95   GError* gerror = 0;
96
97   char *const buf = g_convert_with_iconv(
98       str.data(), str.size(), gobject_, 0, &bytes_written, &gerror);
99
100   #ifdef GLIBMM_EXCEPTIONS_ENABLED
101   if(gerror) ::Glib::Error::throw_exception(gerror);
102   #else
103   if(gerror) error = ::Glib::Error::throw_exception(gerror);
104   #endif //GLIBMM_EXCEPTIONS_ENABLED
105
106   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
107 }
108
109
110 /**** charset conversion functions *****************************************/
111
112 bool get_charset()
113 {
114   return g_get_charset(0);
115 }
116
117 bool get_charset(std::string& charset)
118 {
119   const char* charset_cstr = 0;
120   const bool is_utf8 = g_get_charset(&charset_cstr);
121
122   charset = charset_cstr;
123   return is_utf8;
124 }
125
126
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)
131 #else
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
136 {
137   gsize bytes_written = 0;
138   GError* gerror = 0;
139
140   char *const buf = g_convert(
141       str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
142       0, &bytes_written, &gerror);
143
144   #ifdef GLIBMM_EXCEPTIONS_ENABLED
145   if(gerror) ::Glib::Error::throw_exception(gerror);
146   #else
147   if(gerror) error = ::Glib::Error::throw_exception(gerror);
148   #endif //GLIBMM_EXCEPTIONS_ENABLED
149
150   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
151 }
152
153
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)
158 #else
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
163 {
164   gsize bytes_written = 0;
165   GError* gerror = 0;
166
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);
170
171   #ifdef GLIBMM_EXCEPTIONS_ENABLED
172   if(gerror) ::Glib::Error::throw_exception(gerror);
173   #else
174   if(gerror) error = ::Glib::Error::throw_exception(gerror);
175   #endif //GLIBMM_EXCEPTIONS_ENABLED
176
177   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
178 }
179
180
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)
186 #else
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
192 {
193   gsize bytes_written = 0;
194   GError* gerror = 0;
195
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);
199
200   #ifdef GLIBMM_EXCEPTIONS_ENABLED
201   if(gerror) ::Glib::Error::throw_exception(gerror);
202   #else
203   if(gerror) error = ::Glib::Error::throw_exception(gerror);
204   #endif //GLIBMM_EXCEPTIONS_ENABLED
205
206   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
207 }
208
209
210 #ifdef GLIBMM_EXCEPTIONS_ENABLED
211 Glib::ustring locale_to_utf8(const std::string& opsys_string)
212 #else
213 Glib::ustring locale_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
214 #endif //GLIBMM_EXCEPTIONS_ENABLED
215 {
216   gsize bytes_written = 0;
217   GError* gerror = 0;
218
219   char *const buf = g_locale_to_utf8(
220       opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
221
222   #ifdef GLIBMM_EXCEPTIONS_ENABLED
223   if(gerror) ::Glib::Error::throw_exception(gerror);
224   #else
225   if(gerror) error = ::Glib::Error::throw_exception(gerror);
226   #endif //GLIBMM_EXCEPTIONS_ENABLED
227
228   const ScopedPtr<char> scoped_buf (buf);
229   return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
230 }
231
232
233 #ifdef GLIBMM_EXCEPTIONS_ENABLED
234 std::string locale_from_utf8(const Glib::ustring& utf8_string)
235 #else
236 std::string locale_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
237 #endif //GLIBMM_EXCEPTIONS_ENABLED
238 {
239   gsize bytes_written = 0;
240   GError* gerror = 0;
241
242   char *const buf = g_locale_from_utf8(
243       utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
244
245   #ifdef GLIBMM_EXCEPTIONS_ENABLED
246   if(gerror) ::Glib::Error::throw_exception(gerror);
247   #else
248   if(gerror) error = ::Glib::Error::throw_exception(gerror);
249   #endif //GLIBMM_EXCEPTIONS_ENABLED
250
251   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
252 }
253
254
255 #ifdef GLIBMM_EXCEPTIONS_ENABLED
256 Glib::ustring filename_to_utf8(const std::string& opsys_string)
257 #else
258 Glib::ustring filename_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
259 #endif //GLIBMM_EXCEPTIONS_ENABLED
260 {
261   gsize bytes_written = 0;
262   GError* gerror = 0;
263
264   char *const buf = g_filename_to_utf8(
265       opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
266
267   #ifdef GLIBMM_EXCEPTIONS_ENABLED
268   if(gerror) ::Glib::Error::throw_exception(gerror);
269   #else
270   if(gerror) error = ::Glib::Error::throw_exception(gerror);
271   #endif //GLIBMM_EXCEPTIONS_ENABLED
272
273   const ScopedPtr<char> scoped_buf (buf);
274   return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
275 }
276
277
278 #ifdef GLIBMM_EXCEPTIONS_ENABLED
279 std::string filename_from_utf8(const Glib::ustring& utf8_string)
280 #else
281 std::string filename_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
282 #endif //GLIBMM_EXCEPTIONS_ENABLED
283 {
284   gsize bytes_written = 0;
285   GError* gerror = 0;
286
287   char *const buf = g_filename_from_utf8(
288       utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
289
290   #ifdef GLIBMM_EXCEPTIONS_ENABLED
291   if(gerror) ::Glib::Error::throw_exception(gerror);
292   #else
293   if(gerror) error = ::Glib::Error::throw_exception(gerror);
294   #endif //GLIBMM_EXCEPTIONS_ENABLED
295
296   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
297 }
298
299
300 #ifdef GLIBMM_EXCEPTIONS_ENABLED
301 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname)
302 #else
303 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname, std::auto_ptr<Glib::Error>& error)
304 #endif //GLIBMM_EXCEPTIONS_ENABLED
305 {
306   char* hostname_buf = 0;
307   GError* gerror = 0;
308
309   char *const buf = g_filename_from_uri(uri.c_str(), &hostname_buf, &gerror);
310
311   #ifdef GLIBMM_EXCEPTIONS_ENABLED
312   if(gerror) ::Glib::Error::throw_exception(gerror);
313   #else
314   if(gerror) error = ::Glib::Error::throw_exception(gerror);
315   #endif //GLIBMM_EXCEPTIONS_ENABLED
316
317   // Let's take ownership at this point.
318   const ScopedPtr<char> scoped_buf (buf);
319
320   if(hostname_buf)
321     hostname = ScopedPtr<char>(hostname_buf).get();
322   else
323     hostname.erase();
324
325   return std::string(scoped_buf.get());
326 }
327
328
329 #ifdef GLIBMM_EXCEPTIONS_ENABLED
330 std::string filename_from_uri(const Glib::ustring& uri)
331 #else
332 std::string filename_from_uri(const Glib::ustring& uri, std::auto_ptr<Glib::Error>& error)
333 #endif //GLIBMM_EXCEPTIONS_ENABLED
334 {
335   GError* gerror = 0;
336   char *const buf = g_filename_from_uri(uri.c_str(), 0, &gerror);
337
338   #ifdef GLIBMM_EXCEPTIONS_ENABLED
339   if(gerror) ::Glib::Error::throw_exception(gerror);
340   #else
341   if(gerror) error = ::Glib::Error::throw_exception(gerror);
342   #endif //GLIBMM_EXCEPTIONS_ENABLED
343
344   return std::string(ScopedPtr<char>(buf).get());
345 }
346
347
348 #ifdef GLIBMM_EXCEPTIONS_ENABLED
349 Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname)
350 #else
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
353 {
354   GError* gerror = 0;
355   char *const buf = g_filename_to_uri(filename.c_str(), hostname.c_str(), &gerror);
356
357   #ifdef GLIBMM_EXCEPTIONS_ENABLED
358   if(gerror) ::Glib::Error::throw_exception(gerror);
359   #else
360   if(gerror) error = ::Glib::Error::throw_exception(gerror);
361   #endif //GLIBMM_EXCEPTIONS_ENABLED
362
363   return Glib::ustring(ScopedPtr<char>(buf).get());
364 }
365
366
367 #ifdef GLIBMM_EXCEPTIONS_ENABLED
368 Glib::ustring filename_to_uri(const std::string& filename)
369 #else
370 Glib::ustring filename_to_uri(const std::string& filename, std::auto_ptr<Glib::Error>& error)
371 #endif //GLIBMM_EXCEPTIONS_ENABLED
372 {
373   GError* gerror = 0;
374   char *const buf = g_filename_to_uri(filename.c_str(), 0, &gerror);
375
376   #ifdef GLIBMM_EXCEPTIONS_ENABLED
377   if(gerror) ::Glib::Error::throw_exception(gerror);
378   #else
379   if(gerror) error = ::Glib::Error::throw_exception(gerror);
380   #endif //GLIBMM_EXCEPTIONS_ENABLED
381
382   return Glib::ustring(ScopedPtr<char>(buf).get());
383 }
384
385 Glib::ustring filename_display_basename(const std::string& filename)
386 {
387   char *const buf = g_filename_display_basename(filename.c_str());
388   
389   return Glib::ustring(ScopedPtr<char>(buf).get());
390 }
391
392
393 Glib::ustring filename_display_name(const std::string& filename)
394 {
395   char *const buf = g_filename_display_name(filename.c_str());
396   
397   return Glib::ustring(ScopedPtr<char>(buf).get());
398 }
399
400 } // namespace Glib
401
402
403 namespace
404 {
405 } // anonymous namespace
406
407
408 Glib::ConvertError::ConvertError(Glib::ConvertError::Code error_code, const Glib::ustring& error_message)
409 :
410   Glib::Error (G_CONVERT_ERROR, error_code, error_message)
411 {}
412
413 Glib::ConvertError::ConvertError(GError* gobject)
414 :
415   Glib::Error (gobject)
416 {}
417
418 Glib::ConvertError::Code Glib::ConvertError::code() const
419 {
420   return static_cast<Code>(Glib::Error::code());
421 }
422
423 #ifdef GLIBMM_EXCEPTIONS_ENABLED
424 void Glib::ConvertError::throw_func(GError* gobject)
425 {
426   throw Glib::ConvertError(gobject);
427 }
428 #else
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)
431 {
432   return std::auto_ptr<Glib::Error>(new Glib::ConvertError(gobject));
433 }
434 #endif //GLIBMM_EXCEPTIONS_ENABLED
435
436