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