37 throws(throw_exceptions)
39 Reset(to_code, translit);
66 if (to_code ==
"utf-8")
69 current_code = to_code;
76 std::string to(to_code), from(
"utf-8");
79 fromutf = iconv_open(to.c_str(), from.c_str());
80 if (fromutf == (iconv_t)(-1))
86 toutf = iconv_open(to.c_str(), from.c_str());
87 if (toutf == (iconv_t)(-1))
92 current_code = to_code;
107 return std::string();
108 return fromUTF8(str, str.
Size() * 2, stat);
119 std::string CharsetConverter::fromUTF8(
const StringUTF8 &str,
size_t buff, Status *stat)
const
122 return std::string();
123 const char *in(&str[0]);
124 size_t ins = str.
Size();
126 return std::string();
128 std::vector<char> ret(outs,
'\0');
129 char *out = &ret.front();
131 size_t ans = iconv(fromutf, (
const char**)&in, &ins, &out, &outs);
133 size_t ans = iconv(fromutf, (
char**)&in, &ins, &out, &outs);
136 return fromUTF8(str, buff * 2, stat);
137 if (ans == (
size_t)(-1))
142 return fromUTF8(str, buff * 2, stat);
145 throw ExceptionInvalidCharacter(
_(
"Invalid character."));
151 throw ExceptionIncompleteCode(
_(
"Incomplete multibyte character."));
157 return std::string(&ret.front());
173 return toUTF8(str, str.size() * 2, stat);
184 StringUTF8 CharsetConverter::toUTF8(
const std::string &str,
size_t buff, Status *stat)
const
188 const char *in(str.c_str());
189 size_t ins = str.size();
193 std::vector<char> ret(outs,
'\0');
194 char *out = &ret.front();
196 size_t ans = iconv(toutf, (
const char**)&in, &ins, &out, &outs);
198 size_t ans = iconv(toutf, (
char**)&in, &ins, &out, &outs);
201 return toUTF8(str, buff * 2, stat);
202 if (ans == (
size_t)(-1))
207 return toUTF8(str, buff * 2, stat);
210 throw ExceptionInvalidCharacter(
_(
"Invalid character in: ") + str);
216 throw ExceptionIncompleteCode(
_(
"Incomplete multibyte character in: ") + str);
~CharsetConverter()
Destructor.
ExceptionIncompleteCode()
Default constructor.
std::string FromUTF8(const crn::StringUTF8 &str, Status *stat=nullptr) const
Converts from unicode to the selected charset.
void Reset(const std::string &to_code, bool translit=true)
Changes the charset to convert.
bool IsEmpty() const noexcept
Checks if the string is empty.
CharsetConverter(const std::string &to_code, bool translit=true, bool throw_exceptions=true)
Constructor.
ExceptionInvalidCharacter() noexcept
Default constructor.
crn::StringUTF8 ToUTF8(const std::string &str, Status *stat=nullptr) const
Converts to unicode.
std::string & Std()&noexcept
Conversion to std string.
size_t Size() const noexcept
Returns the number of bytes in the string.
A character string class.
Base class for exceptions.
Exception() noexcept
Default constructor.
Invalid argument error (e.g.: nullptr pointer)