Re-add glibmm2 properly.
[ardour.git] / libs / glibmm2 / glib / src / convert.ccg
1 // -*- c++ -*-
2 /* $Id: convert.ccg,v 1.4 2006/06/05 17:32:14 murrayc Exp $ */
3
4 /* Copyright (C) 2002 The gtkmm Development Team
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <glib/gconvert.h>
22 #include <glib/gmessages.h>
23 #include <glib/gunicode.h>
24 #include <glibmm/utility.h>
25
26
27 namespace Glib
28 {
29
30 /**** Glib::IConv **********************************************************/
31
32 IConv::IConv(const std::string& to_codeset, const std::string& from_codeset)
33 :
34   gobject_ (g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
35 {
36   if(gobject_ == reinterpret_cast<GIConv>(-1))
37   {
38     GError* gerror = 0;
39
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);
44
45     // If this should ever fail we're fucked.
46     g_assert(gerror != 0);
47
48     #ifdef GLIBMM_EXCEPTIONS_ENABLED
49     if(gerror) ::Glib::Error::throw_exception(gerror);
50     #endif //GLIBMM_EXCEPTIONS_ENABLED
51   }
52 }
53
54 IConv::IConv(GIConv gobject)
55 :
56   gobject_ (gobject)
57 {}
58
59 IConv::~IConv()
60 {
61   g_iconv_close(gobject_);
62 }
63
64 size_t IConv::iconv(char** inbuf, gsize* inbytes_left, char** outbuf, gsize* outbytes_left)
65 {
66   return g_iconv(gobject_, inbuf, inbytes_left, outbuf, outbytes_left);
67 }
68
69 void IConv::reset()
70 {
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.)
74
75   char* outbuf        = 0;
76   gsize inbytes_left  = 0;
77   gsize outbytes_left = 0;
78
79   g_iconv(gobject_, 0, &inbytes_left, &outbuf, &outbytes_left);
80 }
81
82 #ifdef GLIBMM_EXCEPTIONS_ENABLED
83 std::string IConv::convert(const std::string& str)
84 #else
85 std::string IConv::convert(const std::string& str, std::auto_ptr<Glib::Error>& error)
86 #endif //GLIBMM_EXCEPTIONS_ENABLED
87 {
88   gsize bytes_written = 0;
89   GError* gerror = 0;
90
91   char *const buf = g_convert_with_iconv(
92       str.data(), str.size(), gobject_, 0, &bytes_written, &gerror);
93
94   #ifdef GLIBMM_EXCEPTIONS_ENABLED
95   if(gerror) ::Glib::Error::throw_exception(gerror);
96   #else
97   if(gerror) error = ::Glib::Error::throw_exception(gerror);
98   #endif //GLIBMM_EXCEPTIONS_ENABLED
99
100   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
101 }
102
103
104 /**** charset conversion functions *****************************************/
105
106 bool get_charset()
107 {
108   return g_get_charset(0);
109 }
110
111 bool get_charset(std::string& charset)
112 {
113   const char* charset_cstr = 0;
114   const bool is_utf8 = g_get_charset(&charset_cstr);
115
116   charset = charset_cstr;
117   return is_utf8;
118 }
119
120
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)
125 #else
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
130 {
131   gsize bytes_written = 0;
132   GError* gerror = 0;
133
134   char *const buf = g_convert(
135       str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
136       0, &bytes_written, &gerror);
137
138   #ifdef GLIBMM_EXCEPTIONS_ENABLED
139   if(gerror) ::Glib::Error::throw_exception(gerror);
140   #else
141   if(gerror) error = ::Glib::Error::throw_exception(gerror);
142   #endif //GLIBMM_EXCEPTIONS_ENABLED
143
144   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
145 }
146
147
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)
152 #else
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
157 {
158   gsize bytes_written = 0;
159   GError* gerror = 0;
160
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);
164
165   #ifdef GLIBMM_EXCEPTIONS_ENABLED
166   if(gerror) ::Glib::Error::throw_exception(gerror);
167   #else
168   if(gerror) error = ::Glib::Error::throw_exception(gerror);
169   #endif //GLIBMM_EXCEPTIONS_ENABLED
170
171   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
172 }
173
174
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)
180 #else
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
186 {
187   gsize bytes_written = 0;
188   GError* gerror = 0;
189
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);
193
194   #ifdef GLIBMM_EXCEPTIONS_ENABLED
195   if(gerror) ::Glib::Error::throw_exception(gerror);
196   #else
197   if(gerror) error = ::Glib::Error::throw_exception(gerror);
198   #endif //GLIBMM_EXCEPTIONS_ENABLED
199
200   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
201 }
202
203
204 #ifdef GLIBMM_EXCEPTIONS_ENABLED
205 Glib::ustring locale_to_utf8(const std::string& opsys_string)
206 #else
207 Glib::ustring locale_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
208 #endif //GLIBMM_EXCEPTIONS_ENABLED
209 {
210   gsize bytes_written = 0;
211   GError* gerror = 0;
212
213   char *const buf = g_locale_to_utf8(
214       opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
215
216   #ifdef GLIBMM_EXCEPTIONS_ENABLED
217   if(gerror) ::Glib::Error::throw_exception(gerror);
218   #else
219   if(gerror) error = ::Glib::Error::throw_exception(gerror);
220   #endif //GLIBMM_EXCEPTIONS_ENABLED
221
222   const ScopedPtr<char> scoped_buf (buf);
223   return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
224 }
225
226
227 #ifdef GLIBMM_EXCEPTIONS_ENABLED
228 std::string locale_from_utf8(const Glib::ustring& utf8_string)
229 #else
230 std::string locale_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
231 #endif //GLIBMM_EXCEPTIONS_ENABLED
232 {
233   gsize bytes_written = 0;
234   GError* gerror = 0;
235
236   char *const buf = g_locale_from_utf8(
237       utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
238
239   #ifdef GLIBMM_EXCEPTIONS_ENABLED
240   if(gerror) ::Glib::Error::throw_exception(gerror);
241   #else
242   if(gerror) error = ::Glib::Error::throw_exception(gerror);
243   #endif //GLIBMM_EXCEPTIONS_ENABLED
244
245   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
246 }
247
248
249 #ifdef GLIBMM_EXCEPTIONS_ENABLED
250 Glib::ustring filename_to_utf8(const std::string& opsys_string)
251 #else
252 Glib::ustring filename_to_utf8(const std::string& opsys_string, std::auto_ptr<Glib::Error>& error)
253 #endif //GLIBMM_EXCEPTIONS_ENABLED
254 {
255   gsize bytes_written = 0;
256   GError* gerror = 0;
257
258   char *const buf = g_filename_to_utf8(
259       opsys_string.data(), opsys_string.size(), 0, &bytes_written, &gerror);
260
261   #ifdef GLIBMM_EXCEPTIONS_ENABLED
262   if(gerror) ::Glib::Error::throw_exception(gerror);
263   #else
264   if(gerror) error = ::Glib::Error::throw_exception(gerror);
265   #endif //GLIBMM_EXCEPTIONS_ENABLED
266
267   const ScopedPtr<char> scoped_buf (buf);
268   return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
269 }
270
271
272 #ifdef GLIBMM_EXCEPTIONS_ENABLED
273 std::string filename_from_utf8(const Glib::ustring& utf8_string)
274 #else
275 std::string filename_from_utf8(const Glib::ustring& utf8_string, std::auto_ptr<Glib::Error>& error)
276 #endif //GLIBMM_EXCEPTIONS_ENABLED
277 {
278   gsize bytes_written = 0;
279   GError* gerror = 0;
280
281   char *const buf = g_filename_from_utf8(
282       utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &gerror);
283
284   #ifdef GLIBMM_EXCEPTIONS_ENABLED
285   if(gerror) ::Glib::Error::throw_exception(gerror);
286   #else
287   if(gerror) error = ::Glib::Error::throw_exception(gerror);
288   #endif //GLIBMM_EXCEPTIONS_ENABLED
289
290   return std::string(ScopedPtr<char>(buf).get(), bytes_written);
291 }
292
293
294 #ifdef GLIBMM_EXCEPTIONS_ENABLED
295 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname)
296 #else
297 std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname, std::auto_ptr<Glib::Error>& error)
298 #endif //GLIBMM_EXCEPTIONS_ENABLED
299 {
300   char* hostname_buf = 0;
301   GError* gerror = 0;
302
303   char *const buf = g_filename_from_uri(uri.c_str(), &hostname_buf, &gerror);
304
305   #ifdef GLIBMM_EXCEPTIONS_ENABLED
306   if(gerror) ::Glib::Error::throw_exception(gerror);
307   #else
308   if(gerror) error = ::Glib::Error::throw_exception(gerror);
309   #endif //GLIBMM_EXCEPTIONS_ENABLED
310
311   // Let's take ownership at this point.
312   const ScopedPtr<char> scoped_buf (buf);
313
314   if(hostname_buf)
315     hostname = ScopedPtr<char>(hostname_buf).get();
316   else
317     hostname.erase();
318
319   return std::string(scoped_buf.get());
320 }
321
322
323 #ifdef GLIBMM_EXCEPTIONS_ENABLED
324 std::string filename_from_uri(const Glib::ustring& uri)
325 #else
326 std::string filename_from_uri(const Glib::ustring& uri, std::auto_ptr<Glib::Error>& error)
327 #endif //GLIBMM_EXCEPTIONS_ENABLED
328 {
329   GError* gerror = 0;
330   char *const buf = g_filename_from_uri(uri.c_str(), 0, &gerror);
331
332   #ifdef GLIBMM_EXCEPTIONS_ENABLED
333   if(gerror) ::Glib::Error::throw_exception(gerror);
334   #else
335   if(gerror) error = ::Glib::Error::throw_exception(gerror);
336   #endif //GLIBMM_EXCEPTIONS_ENABLED
337
338   return std::string(ScopedPtr<char>(buf).get());
339 }
340
341
342 #ifdef GLIBMM_EXCEPTIONS_ENABLED
343 Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname)
344 #else
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
347 {
348   GError* gerror = 0;
349   char *const buf = g_filename_to_uri(filename.c_str(), hostname.c_str(), &gerror);
350
351   #ifdef GLIBMM_EXCEPTIONS_ENABLED
352   if(gerror) ::Glib::Error::throw_exception(gerror);
353   #else
354   if(gerror) error = ::Glib::Error::throw_exception(gerror);
355   #endif //GLIBMM_EXCEPTIONS_ENABLED
356
357   return Glib::ustring(ScopedPtr<char>(buf).get());
358 }
359
360
361 #ifdef GLIBMM_EXCEPTIONS_ENABLED
362 Glib::ustring filename_to_uri(const std::string& filename)
363 #else
364 Glib::ustring filename_to_uri(const std::string& filename, std::auto_ptr<Glib::Error>& error)
365 #endif //GLIBMM_EXCEPTIONS_ENABLED
366 {
367   GError* gerror = 0;
368   char *const buf = g_filename_to_uri(filename.c_str(), 0, &gerror);
369
370   #ifdef GLIBMM_EXCEPTIONS_ENABLED
371   if(gerror) ::Glib::Error::throw_exception(gerror);
372   #else
373   if(gerror) error = ::Glib::Error::throw_exception(gerror);
374   #endif //GLIBMM_EXCEPTIONS_ENABLED
375
376   return Glib::ustring(ScopedPtr<char>(buf).get());
377 }
378
379 Glib::ustring filename_display_basename(const std::string& filename)
380 {
381   char *const buf = g_filename_display_basename(filename.c_str());
382   
383   return Glib::ustring(ScopedPtr<char>(buf).get());
384 }
385
386
387 Glib::ustring filename_display_name(const std::string& filename)
388 {
389   char *const buf = g_filename_display_name(filename.c_str());
390   
391   return Glib::ustring(ScopedPtr<char>(buf).get());
392 }
393
394 } // namespace Glib
395