#include "TextEncoder.h"
#include "CharacterSet.h"
#include "ECI.h"
#include "Utf.h"
#include "ZXAlgorithms.h"
#ifdef ZXING_USE_ZINT
#include <zint.h>
#else
#include "libzueci/zueci.h"
#endif
#include <stdexcept>
namespace ZXing {
void TextEncoder::GetBytes(const std::string& str, CharacterSet charset, std::string& bytes)
{
int eci = ToInt(ToECI(charset));
const int str_len = narrow_cast<int>(str.length());
int eci_len;
if (eci == -1)
eci = 899;
bytes.clear();
#ifdef ZXING_USE_ZINT
int error_number = ZBarcode_Dest_Len_ECI(eci, reinterpret_cast<const unsigned char *>(str.data()), str_len, &eci_len);
if (error_number >= ZINT_ERROR) throw std::logic_error("Internal error `ZBarcode_Dest_Len_ECI()`");
bytes.resize(eci_len);
error_number = ZBarcode_UTF8_To_ECI(eci,reinterpret_cast<const unsigned char *>(str.data()), str_len,
reinterpret_cast<unsigned char *>(bytes.data()), &eci_len);
if (error_number >= ZINT_ERROR) {
bytes.clear();
throw std::invalid_argument("Unexpected charcode");
}
#else
int error_number = zueci_dest_len_eci(eci, reinterpret_cast<const unsigned char *>(str.data()), str_len, &eci_len);
if (error_number >= ZUECI_ERROR) throw std::logic_error("Internal error `zueci_dest_len_eci()`");
bytes.resize(eci_len);
error_number = zueci_utf8_to_eci(eci, reinterpret_cast<const unsigned char *>(str.data()), str_len,
reinterpret_cast<unsigned char *>(bytes.data()), &eci_len);
if (error_number >= ZUECI_ERROR) {
bytes.clear();
throw std::invalid_argument("Unexpected charcode");
}
#endif
bytes.resize(eci_len); }
void TextEncoder::GetBytes(const std::wstring& str, CharacterSet charset, std::string& bytes)
{
GetBytes(ToUtf8(str), charset, bytes);
}
}