1
// Generated by gtkmmproc -- DO NOT MODIFY!
3
#include <glibmm/convert.h>
4
#include <glibmm/private/convert_p.h>
9
/* Copyright (C) 2002 The gtkmm Development Team
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.
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.
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.
26
#include <glib/gconvert.h>
27
#include <glib/gmessages.h>
28
#include <glib/gunicode.h>
30
#include <glib/gtestutils.h>
32
#include <glibmm/utility.h>
38
/**** Glib::IConv **********************************************************/
40
IConv::IConv(const std::string& to_codeset, const std::string& from_codeset)
42
gobject_ (g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
44
if(gobject_ == reinterpret_cast<GIConv>(-1))
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);
53
// If this should ever fail we're fucked.
56
Error::throw_exception(error);
60
IConv::IConv(GIConv gobject)
67
g_iconv_close(gobject_);
70
size_t IConv::iconv(char** inbuf, gsize* inbytes_left, char** outbuf, gsize* outbytes_left)
72
return g_iconv(gobject_, inbuf, inbytes_left, outbuf, outbytes_left);
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.)
82
gsize inbytes_left = 0;
83
gsize outbytes_left = 0;
85
g_iconv(gobject_, 0, &inbytes_left, &outbuf, &outbytes_left);
88
std::string IConv::convert(const std::string& str)
90
gsize bytes_written = 0;
93
char *const buf = g_convert_with_iconv(
94
str.data(), str.size(), gobject_, 0, &bytes_written, &error);
97
Error::throw_exception(error);
99
return std::string(ScopedPtr<char>(buf).get(), bytes_written);
103
/**** charset conversion functions *****************************************/
107
return g_get_charset(0);
110
bool get_charset(std::string& charset)
112
const char* charset_cstr = 0;
113
const bool is_utf8 = g_get_charset(&charset_cstr);
115
charset = charset_cstr;
119
std::string convert(const std::string& str,
120
const std::string& to_codeset,
121
const std::string& from_codeset)
123
gsize bytes_written = 0;
126
char *const buf = g_convert(
127
str.data(), str.size(), to_codeset.c_str(), from_codeset.c_str(),
128
0, &bytes_written, &error);
131
Error::throw_exception(error);
133
return std::string(ScopedPtr<char>(buf).get(), bytes_written);
136
std::string convert_with_fallback(const std::string& str,
137
const std::string& to_codeset,
138
const std::string& from_codeset)
140
gsize bytes_written = 0;
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);
148
Error::throw_exception(error);
150
return std::string(ScopedPtr<char>(buf).get(), bytes_written);
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)
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(),
163
const_cast<char*>(fallback.c_str()), 0, &bytes_written, &error);
166
Error::throw_exception(error);
168
return std::string(ScopedPtr<char>(buf).get(), bytes_written);
171
Glib::ustring locale_to_utf8(const std::string& opsys_string)
173
gsize bytes_written = 0;
176
char *const buf = g_locale_to_utf8(
177
opsys_string.data(), opsys_string.size(), 0, &bytes_written, &error);
180
Error::throw_exception(error);
182
const ScopedPtr<char> scoped_buf (buf);
183
return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
186
std::string locale_from_utf8(const Glib::ustring& utf8_string)
188
gsize bytes_written = 0;
191
char *const buf = g_locale_from_utf8(
192
utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &error);
195
Error::throw_exception(error);
197
return std::string(ScopedPtr<char>(buf).get(), bytes_written);
200
Glib::ustring filename_to_utf8(const std::string& opsys_string)
202
gsize bytes_written = 0;
205
char *const buf = g_filename_to_utf8(
206
opsys_string.data(), opsys_string.size(), 0, &bytes_written, &error);
209
Error::throw_exception(error);
211
const ScopedPtr<char> scoped_buf (buf);
212
return Glib::ustring(scoped_buf.get(), scoped_buf.get() + bytes_written);
215
std::string filename_from_utf8(const Glib::ustring& utf8_string)
217
gsize bytes_written = 0;
220
char *const buf = g_filename_from_utf8(
221
utf8_string.data(), utf8_string.bytes(), 0, &bytes_written, &error);
224
Error::throw_exception(error);
226
return std::string(ScopedPtr<char>(buf).get(), bytes_written);
229
std::string filename_from_uri(const Glib::ustring& uri, Glib::ustring& hostname)
231
char* hostname_buf = 0;
234
char *const buf = g_filename_from_uri(uri.c_str(), &hostname_buf, &error);
237
Error::throw_exception(error);
239
// Let's take ownership at this point.
240
const ScopedPtr<char> scoped_buf (buf);
243
hostname = ScopedPtr<char>(hostname_buf).get();
247
return std::string(scoped_buf.get());
250
std::string filename_from_uri(const Glib::ustring& uri)
253
char *const buf = g_filename_from_uri(uri.c_str(), 0, &error);
256
Error::throw_exception(error);
258
return std::string(ScopedPtr<char>(buf).get());
261
Glib::ustring filename_to_uri(const std::string& filename, const Glib::ustring& hostname)
264
char *const buf = g_filename_to_uri(filename.c_str(), hostname.c_str(), &error);
267
Error::throw_exception(error);
269
return Glib::ustring(ScopedPtr<char>(buf).get());
272
Glib::ustring filename_to_uri(const std::string& filename)
275
char *const buf = g_filename_to_uri(filename.c_str(), 0, &error);
278
Error::throw_exception(error);
280
return Glib::ustring(ScopedPtr<char>(buf).get());
283
Glib::ustring filename_display_basename(const std::string& filename)
285
char *const buf = g_filename_display_basename(filename.c_str());
287
return Glib::ustring(ScopedPtr<char>(buf).get());
291
Glib::ustring filename_display_name(const std::string& filename)
293
char *const buf = g_filename_display_name(filename.c_str());
295
return Glib::ustring(ScopedPtr<char>(buf).get());
303
} // anonymous namespace
306
Glib::ConvertError::ConvertError(Glib::ConvertError::Code error_code, const Glib::ustring& error_message)
308
Glib::Error (G_CONVERT_ERROR, error_code, error_message)
311
Glib::ConvertError::ConvertError(GError* gobject)
313
Glib::Error (gobject)
316
Glib::ConvertError::Code Glib::ConvertError::code() const
318
return static_cast<Code>(Glib::Error::code());
321
void Glib::ConvertError::throw_func(GError* gobject)
323
throw Glib::ConvertError(gobject);