forked from zxing-cpp/zxing-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEncoder.cpp
More file actions
50 lines (37 loc) · 1.26 KB
/
Copy pathTextEncoder.cpp
File metadata and controls
50 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Copyright 2016 Nu-book Inc.
*/
// SPDX-License-Identifier: Apache-2.0
#include "TextEncoder.h"
#include "CharacterSet.h"
#include "ECI.h"
#include "Utf.h"
#include "ZXAlgorithms.h"
#include "libzueci/zueci.h"
#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; // Binary
bytes.clear();
int error_number = zueci_dest_len_eci(eci, reinterpret_cast<const unsigned char *>(str.data()), str_len, &eci_len);
if (error_number >= ZUECI_ERROR) // Shouldn't happen
throw std::logic_error("Internal error `zueci_dest_len_eci()`");
bytes.resize(eci_len); // Sufficient but approximate length
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");
}
bytes.resize(eci_len); // Actual length
}
void TextEncoder::GetBytes(const std::wstring& str, CharacterSet charset, std::string& bytes)
{
GetBytes(ToUtf8(str), charset, bytes);
}
} // ZXing