~ubuntu-branches/ubuntu/vivid/ardour/vivid-proposed

« back to all changes in this revision

Viewing changes to libs/glibmm2/glibmm/convert.cc

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler, Jaromír Mikeš, Felipe Sateler
  • Date: 2014-05-22 14:39:25 UTC
  • mfrom: (29 sid)
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: package-import@ubuntu.com-20140522143925-vwqfo9287pmkrroe
Tags: 1:2.8.16+git20131003-3
* Team upload

[ Jaromír Mikeš ]
* Add -dbg package

[ Felipe Sateler ]
* Upload to experimental

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Generated by gtkmmproc -- DO NOT MODIFY!
 
2
 
 
3
#include <glibmm/convert.h>
 
4
#include <glibmm/private/convert_p.h>
 
5
 
 
6
// -*- c++ -*-
 
7
/* $Id$ */
 
8
 
 
9
/* Copyright (C) 2002 The gtkmm Development Team
 
10
 *
 
11
 * This library is free software; you can redistribute it and/or
 
12
 * modify it under the terms of the GNU Library General Public
 
13
 * License as published by the Free Software Foundation; either
 
14
 * version 2 of the License, or (at your option) any later version.
 
15
 *
 
16
 * This library is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
19
 * Library General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU Library General Public
 
22
 * License along with this library; if not, write to the Free
 
23
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
24
 */
 
25
 
 
26
#include <glib/gconvert.h>
 
27
#include <glib/gmessages.h>
 
28
#include <glib/gunicode.h>
 
29
#ifndef g_assert
 
30
#include <glib/gtestutils.h>
 
31
#endif
 
32
#include <glibmm/utility.h>
 
33
 
 
34
 
 
35
namespace Glib
 
36
{
 
37
 
 
38
/**** Glib::IConv **********************************************************/
 
39
 
 
40
IConv::IConv(const std::string& to_codeset, const std::string& from_codeset)
 
41
:
 
42
  gobject_ (g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
 
43
{
 
44
  if(gobject_ == reinterpret_cast<GIConv>(-1))
 
45
  {
 
46
    GError* error = 0;
 
47
 
 
48
    // Abuse g_convert() to create a GError object.  This may seem a weird
 
49
    // thing to do, but it gives us consistently translated error messages
 
50
    // at no further cost.
 
51
    g_convert("", 0, to_codeset.c_str(), from_codeset.c_str(), 0, 0, &error);
 
52
 
 
53
    // If this should ever fail we're fucked.
 
54
    g_assert(error != 0);
 
55
 
 
56
    Error::throw_exception(error);
 
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
std::string IConv::convert(const std::string& str)
 
89
{
 
90
  gsize bytes_written = 0;
 
91
  GError* error = 0;
 
92
 
 
93
  char *const buf = g_convert_with_iconv(
 
94
      str.data(), str.size(), gobject_, 0, &bytes_written, &error);
 
95
 
 
96
  if(error)
 
97
    Error::throw_exception(error);
 
98
 
 
99
  return std::string(ScopedPtr<char>(buf).get(), bytes_written);
 
100
}
 
101
 
 
102
 
 
103
/**** charset conversion functions *****************************************/
 
104
 
 
105
bool get_charset()
 
106
{
 
107
  return g_get_charset(0);
 
108
}
 
109
 
 
110
bool get_charset(std::string& charset)
 
111
{
 
112
  const char* charset_cstr = 0;
 
113
  const bool is_utf8 = g_get_charset(&charset_cstr);
 
114
 
 
115
  charset = charset_cstr;
 
116
  return is_utf8;
 
117
}
 
118
 
 
119
std::string convert(const std::string& str,
 
120
                    const std::string& to_codeset,
 
121
                    const std::string& from_codeset)
 
122
{
 
123
  gsize bytes_written = 0;
 
124
  GError* error = 0;
 
125
 
 
126
  char *const buf = g_convert(
 
127
      str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
 
128
      0, &bytes_written, &error);
 
129
 
 
130
  if(error)
 
131
    Error::throw_exception(error);
 
132
 
 
133
  return std::string(ScopedPtr<char>(buf).get(), bytes_written);
 
134
}
 
135
 
 
136
std::string convert_with_fallback(const std::string& str,
 
137
                                  const std::string& to_codeset,
 
138
                                  const std::string& from_codeset)
 
139
{
 
140
  gsize bytes_written = 0;
 
141
  GError* error = 0;
 
142
 
 
143
  char *const buf = g_convert_with_fallback(
 
144
      str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(), 0,
 
145
      0, &bytes_written, &error);
 
146
 
 
147
  if(error)
 
148
    Error::throw_exception(error);
 
149
 
 
150
  return std::string(ScopedPtr<char>(buf).get(), bytes_written);
 
151
}
 
152
 
 
153
std::string convert_with_fallback(const std::string& str,
 
154
                                  const std::string& to_codeset,
 
155
                                  const std::string& from_codeset,
 
156
                                  const Glib::ustring& fallback)
 
157
{
 
158
  gsize bytes_written = 0;
 
159
  GError* error = 0;
 
160
 
 
161
  char *const buf = g_convert_with_fallback(
 
162
      str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
 
163
      const_cast<char*>(fallback.c_str()), 0, &bytes_written, &error);
 
164
 
 
165
  if(error)
 
166
    Error::throw_exception(error);
 
167
 
 
168
  return std::string(ScopedPtr<char>(buf).get(), bytes_written);
 
169
}
 
170
 
 
171
Glib::ustring locale_to_utf8(const std::string& opsys_string)
 
172
{
 
173
  gsize bytes_written = 0;
 
174
  GError* error = 0;
 
175
 
 
176
  char *const buf = g_locale_to_utf8(
 
177
      opsys_string.data(), opsys_string.size(), 0, &bytes_written, &error);
 
178
 
 
179
  if(error)
 
180
    Error::throw_exception(error);
 
181
 
 
182
  const ScopedPtr<char> scoped_buf (buf);
 
183
  return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
 
184
}
 
185
 
 
186
std::string locale_from_utf8(const Glib::ustring& utf8_string)
 
187
{
 
188
  gsize bytes_written = 0;
 
189
  GError* error = 0;
 
190
 
 
191
  char *const buf = g_locale_from_utf8(
 
192
      utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &error);
 
193
 
 
194
  if(error)
 
195
    Error::throw_exception(error);
 
196
 
 
197
  return std::string(ScopedPtr<char>(buf).get(), bytes_written);
 
198
}
 
199
 
 
200
Glib::ustring filename_to_utf8(const std::string& opsys_string)
 
201
{
 
202
  gsize bytes_written = 0;
 
203
  GError* error = 0;
 
204
 
 
205
  char *const buf = g_filename_to_utf8(
 
206
      opsys_string.data(), opsys_string.size(), 0, &bytes_written, &error);
 
207
 
 
208
  if(error)
 
209
    Error::throw_exception(error);
 
210
 
 
211
  const ScopedPtr<char> scoped_buf (buf);
 
212
  return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
 
213
}
 
214
 
 
215
std::string filename_from_utf8(const Glib::ustring& utf8_string)
 
216
{
 
217
  gsize bytes_written = 0;
 
218
  GError* error = 0;
 
219
 
 
220
  char *const buf = g_filename_from_utf8(
 
221
      utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &error);
 
222
 
 
223
  if(error)
 
224
    Error::throw_exception(error);
 
225
 
 
226
  return std::string(ScopedPtr<char>(buf).get(), bytes_written);
 
227
}
 
228
 
 
229
std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname)
 
230
{
 
231
  char* hostname_buf = 0;
 
232
  GError* error = 0;
 
233
 
 
234
  char *const buf = g_filename_from_uri(uri.c_str(), &hostname_buf, &error);
 
235
 
 
236
  if(error)
 
237
    Error::throw_exception(error);
 
238
 
 
239
  // Let's take ownership at this point.
 
240
  const ScopedPtr<char> scoped_buf (buf);
 
241
 
 
242
  if(hostname_buf)
 
243
    hostname = ScopedPtr<char>(hostname_buf).get();
 
244
  else
 
245
    hostname.erase();
 
246
 
 
247
  return std::string(scoped_buf.get());
 
248
}
 
249
 
 
250
std::string filename_from_uri(const Glib::ustring& uri)
 
251
{
 
252
  GError* error = 0;
 
253
  char *const buf = g_filename_from_uri(uri.c_str(), 0, &error);
 
254
 
 
255
  if(error)
 
256
    Error::throw_exception(error);
 
257
 
 
258
  return std::string(ScopedPtr<char>(buf).get());
 
259
}
 
260
 
 
261
Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname)
 
262
{
 
263
  GError* error = 0;
 
264
  char *const buf = g_filename_to_uri(filename.c_str(), hostname.c_str(), &error);
 
265
 
 
266
  if(error)
 
267
    Error::throw_exception(error);
 
268
 
 
269
  return Glib::ustring(ScopedPtr<char>(buf).get());
 
270
}
 
271
 
 
272
Glib::ustring filename_to_uri(const std::string& filename)
 
273
{
 
274
  GError* error = 0;
 
275
  char *const buf = g_filename_to_uri(filename.c_str(), 0, &error);
 
276
 
 
277
  if(error)
 
278
    Error::throw_exception(error);
 
279
 
 
280
  return Glib::ustring(ScopedPtr<char>(buf).get());
 
281
}
 
282
 
 
283
Glib::ustring filename_display_basename(const std::string& filename)
 
284
{
 
285
  char *const buf = g_filename_display_basename(filename.c_str());
 
286
  
 
287
  return Glib::ustring(ScopedPtr<char>(buf).get());
 
288
}
 
289
 
 
290
 
 
291
Glib::ustring filename_display_name(const std::string& filename)
 
292
{
 
293
  char *const buf = g_filename_display_name(filename.c_str());
 
294
  
 
295
  return Glib::ustring(ScopedPtr<char>(buf).get());
 
296
}
 
297
 
 
298
} // namespace Glib
 
299
 
 
300
 
 
301
namespace
 
302
{
 
303
} // anonymous namespace
 
304
 
 
305
 
 
306
Glib::ConvertError::ConvertError(Glib::ConvertError::Code error_code, const Glib::ustring& error_message)
 
307
:
 
308
  Glib::Error (G_CONVERT_ERROR, error_code, error_message)
 
309
{}
 
310
 
 
311
Glib::ConvertError::ConvertError(GError* gobject)
 
312
:
 
313
  Glib::Error (gobject)
 
314
{}
 
315
 
 
316
Glib::ConvertError::Code Glib::ConvertError::code() const
 
317
{
 
318
  return static_cast<Code>(Glib::Error::code());
 
319
}
 
320
 
 
321
void Glib::ConvertError::throw_func(GError* gobject)
 
322
{
 
323
  throw Glib::ConvertError(gobject);
 
324
}
 
325
 
 
326