Menu

[r733]: / framework / branches / refactoring / encoding.cpp  Maximize  Restore  History

Download this file

254 lines (227 with data), 6.4 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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "encoding_validators.h"
#include "encoding.h"
#include <iconv.h>
#include <errno.h>
#include <string>
#include <stdexcept>
namespace cppcms { namespace encoding {
namespace {
char const *native_unicode_encoding()
{
char const *le="UTF-32LE";
char const *be="UTF-32BE";
uint16_t v=0x0a0b;
char const *utf=*(char*)&v== 0x0a ? be : le;
return utf;
}
char const *native_wchar_encoding()
{
if(sizeof(wchar_t)==4)
return native_unicode_encoding();
if(sizeof(wchar_t)!=2)
throw std::runtime_error("wchar_t does not support unicode!");
char const *le="UTF-16LE";
char const *be="UTF-16BE";
uint16_t v=0x0a0b;
char const *utf=*(char*)&v== 0x0a ? be : le;
return utf;
}
}
class iconv_validator {
private:
iconv_t descriptor_;
iconv_validator(iconv_validator const &other);
iconv_validator const &operator=(iconv_validator const &);
public:
iconv_validator(std::string const &charset) :
descriptor_((iconv_t)(-1))
{
descriptor_=iconv_open(native_unicode_encoding(),charset.c_str());
if(descriptor_==(iconv_t)(-1)) {
throw std::runtime_error("Failed to load iconv tables for:" + charset);
}
}
~iconv_validator()
{
if(descriptor_!=(iconv_t)(-1))
iconv_close(descriptor_);
}
bool check_symbols(uint32_t const *begin,uint32_t const *end)
{
while(begin!=end) {
uint32_t c=*begin++;
if(c==0x09 || c==0xA || c==0xD)
continue;
if(c<0x20 || (0x7F<=c && c<=0x9F))
return false;
}
return true;
}
bool valid(char const *begin,char const *end)
{
iconv(descriptor_,0,0,0,0); // reset
uint32_t buffer[64];
size_t input=end-begin;
while(begin!=end) {
uint32_t *output=buffer;
size_t outsize=sizeof(buffer);
size_t res=iconv(descriptor_,const_cast<char**>(&begin),&input,(char**)&output,&outsize);
if(res==(size_t)(-1) && errno==E2BIG) {
if(!check_symbols(buffer,output))
return false;
continue;
}
if(res!=(size_t)(-1) && input==0)
return check_symbols(buffer,output);
return false;
}
return true;
}
};
validator::validator(encoding_tester_type enc) :
tester_(enc),
iconv_(0)
{
}
validator::validator(std::string s):
charset_(s),
tester_(0),
iconv_(new iconv_validator(s))
{
}
validator::validator(validator const &other) :
charset_(other.charset_),
tester_(other.tester_),
iconv_( other.iconv_ ? (new iconv_validator(charset_)) : 0)
{
}
validator const &validator::operator=(validator const &other)
{
if(&other!=this) {
if(iconv_) delete iconv_;
charset_=other.charset_;
tester_=other.tester_;
if(other.iconv_)
iconv_=new iconv_validator(charset_);
}
return *this;
}
validator::~validator()
{
if(iconv_)
delete iconv_;
}
bool validator::valid(std::string const &s)
{
return valid(s.data(),s.data()+s.size());
}
bool validator::valid(char const *begin,char const *end)
{
if(tester_)
return tester_(begin,end);
return iconv_->valid(begin,end);
}
validators_set::validators_set()
{
predefined_["utf-8"]=&utf8_valid<char const *>;
encoding_tester_type iso_tester=&iso_8859_1_2_4_5_9_10_13_14_15_16_valid<char const *>;
predefined_["iso-8859-1"]=iso_tester;
predefined_["iso-8859-2"]=iso_tester;
predefined_["iso-8859-4"]=iso_tester;
predefined_["iso-8859-5"]=iso_tester;
predefined_["iso-8859-9"]=iso_tester;
predefined_["iso-8859-10"]=iso_tester;
predefined_["iso-8859-13"]=iso_tester;
predefined_["iso-8859-14"]=iso_tester;
predefined_["iso-8859-15"]=iso_tester;
predefined_["iso-8859-16"]=iso_tester;
predefined_["iso-8859-3"]=&iso_8859_3_valid<char const *>;
predefined_["iso-8859-6"]=&iso_8859_6_valid<char const *>;
predefined_["iso-8859-7"]=&iso_8859_7_valid<char const *>;
predefined_["iso-8859-8"]=&iso_8859_8_valid<char const *>;
predefined_["iso-8859-11"]=&iso_8859_11_valid<char const *>;
predefined_["windows-1250"]=&windows_1250_valid<char const *>;
predefined_["windows-1251"]=&windows_1251_valid<char const *>;
predefined_["windows-1252"]=&windows_1252_valid<char const *>;
predefined_["windows-1253"]=&windows_1253_valid<char const *>;
predefined_["windows-1255"]=&windows_1255_valid<char const *>;
predefined_["windows-1256"]=&windows_1256_valid<char const *>;
predefined_["windows-1257"]=&windows_1257_valid<char const *>;
predefined_["windows-1258"]=&windows_1258_valid<char const *>;
predefined_["koi8-r"]=&koi8_valid<char const *>;
predefined_["koi8-u"]=&koi8_valid<char const *>;
}
validator validators_set::operator[](std::string s) const
{
for(unsigned i=0;i<s.size();i++) {
if('A'<=s[i] && s[i]<='Z')
s[i]=s[i]-'A'+'a';
}
std::map<std::string,encoding_tester_type>::const_iterator p;
if((p=predefined_.find(s))!=predefined_.end()) {
return validator(p->second);
}
return validator(s);
}
std::string to_string(std::wstring const &s)
{
std::string result;
result.reserve(s.size());
if(sizeof(wchar_t)==2) {
std::wstring::const_iterator p=s.begin(),e=s.end();
while(p!=e) {
uint32_t point;
if(sizeof(wchar_t)==2) {
point=utf16::next(p,e);
if(point==utf::illegal)
break;
}
else {
point=*p++;
if(!utf::valid(point))
break;
}
utf8::seq s=utf8::encode(point);
result+=s.c[0];
result.append(s.c+1);
}
}
return result;
}
std::string to_string(std::wstring const &s,std::locale const &locale)
{
throw std::runtime_error("Unsuppoteed");
}
std::string to_string(std::wstring const &s,std::string const &encoding)
{
throw std::runtime_error("Unsuppoteed");
}
std::wstring to_wstring(std::string const &s)
{
std::wstring result;
result.reserve(s.size());
std::string::const_iterator p=s.begin(),e=s.end();
while(p!=e) {
uint32_t point=utf8::next(p,e);
if(point==utf::illegal)
break;
if(sizeof(wchar_t)==2) {
utf16::seq s=utf16::encode(point);
result+=wchar_t(s.c[0]);
if(s.c[1]) result+=wchar_t(s.c[1]);
}
else {
result+=wchar_t(point);
}
}
return result;
}
std::wstring to_wstring(std::string const &s,std::locale const &locale)
{
throw std::runtime_error("Unsuppoteed");
}
std::wstring to_wstring(std::string const &s,std::string const &encoding)
{
throw std::runtime_error("Unsuppoteed");
}
} } // cppcms::encoding
MongoDB Logo MongoDB