-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodepages.cpp
More file actions
168 lines (157 loc) · 5.55 KB
/
codepages.cpp
File metadata and controls
168 lines (157 loc) · 5.55 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
#include "codepages.hpp"
static long int the_index= 0;
static long int the_length= 0;
static long int the_char= 0;
static long int the_byte= 0;
static char *the_input;
// Neutral defaults (7-bit characters only), but changeable
static std::string encodeError= ".";
static std::string decodeError= "?";
static std::string utfError= "~";
static bool encodeWarning= false;
static bool decodeWarning= false;
std::string fromCodePage(RAWCODEPAGE nmCodePage, char* input) {
std::string temp="";
encodeWarning=false;
for(unsigned long int x=0; x<strlen(input); ++x) {
if(input[x]<0x80) {
temp+=input[x];
}
else {
char xtest[8];
int xout=utf8_encode(xtest,nmCodePage[input[x]-0x80]);
if(xout==0) {
temp+=encodeError;
encodeWarning=true;
}
else {
for(unsigned int y=0; y<xout; ++y) {
temp+=xtest[y];
}
}
}
}
return temp;
}
std::string toCodePage(CODEPAGE &codepage, char* input) {
std::string temp="";
utf8_decode_init(input,strlen(input));
decodeWarning=false;
while(true) {
long int one=utf8_decode_next();
if(one<0) {
if(one==UTF8_ERROR) {
temp+=utfError;
}
break;
}
else {
if(one<0x80) {
temp+=(char)one;
}
else {
char test=(char)codepage[(unsigned int)one];
if(test<0x20) {
if(!((test==0x0D) || (test==0x0A))) {
temp+=decodeError;
decodeWarning=true;
continue;
}
}
temp+=test;
}
}
}
return temp;
}
void prepareCodePage(CODEPAGE &codepage, RAWCODEPAGE cpdef) {
codepage.clear();
for(unsigned int x=0; x<128; ++x) {
codepage[cpdef[x]]=0x80+x;
}
return;
}
bool encodeWarningState(void) {
return encodeWarning;
}
bool decodeWarningState(void) {
return decodeWarning;
}
bool loadCodePage(char *libName, HINSTANCE &hCodePageLib, HGLOBAL &hCodePageDefinition, RAWCODEPAGE &rawCodePage) {
char testString[9];
char testString2[17];
hCodePageLib=LoadLibrary(libName);
if(hCodePageLib < 32) {
return false;
}
else {
if(LoadString(hCodePageLib,IDS_LIBTYPE,testString,9)) {
if(((std::string)testString)=="CODEPAGE") {
if(LoadString(hCodePageLib,IDS_USEDWORD,testString,9)) {
// non-DWORD code page definition is the only allowed in this version (might be changed in the future)
if(((std::string)testString)=="0") {
hCodePageDefinition=LoadResource(hCodePageLib,FindResource(hCodePageLib,MAKEINTRESOURCE(IDR_CODEPAGE),RT_RCDATA));
if(hCodePageDefinition==NULL) {
return false;
}
else {
// Load code page definition
rawCodePage=(int*)LockResource(hCodePageDefinition);
// And now load optional error strings (change neutral defaults to something another)
if(LoadString(hCodePageLib,IDS_ENCODEERROR,testString2,17)) {
// Make it UTF-8 string immediately (to avoid any further errors)
encodeError=fromCodePage(rawCodePage,testString2);
}
if(LoadString(hCodePageLib,IDS_DECODEERROR,testString2,17)) {
decodeError=testString2;
}
if(LoadString(hCodePageLib,IDS_UTFERROR,testString2,17)) {
utfError=testString2;
}
// Finish successfully
return true;
}
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}
}
void unloadCodePage(HINSTANCE &hCodePageLib, HGLOBAL &hCodePageDefinition) {
if(hCodePageDefinition!=NULL) {
UnlockResource(hCodePageDefinition);
}
if(hCodePageDefinition!=NULL) {
FreeResource(hCodePageDefinition);
}
if(hCodePageLib!=NULL) {
FreeLibrary(hCodePageLib);
}
return;
}
bool loadAndPrepareCodePage(MAINSETTINGS &mainSettings, LIBRARIES &libraries, HINSTANCE &hCodePageLib, HGLOBAL &hCodePageDefinition, RAWCODEPAGE &rawCodePage, CODEPAGE &mappedCodePage) {
std::string foundCodePage="";
if(!loadCodePage((char*)mainSettings.selectedCodePage.c_str(),hCodePageLib,hCodePageDefinition,rawCodePage)) {
foundCodePage=findAnyCodePage(libraries);
if(loadCodePage((char*)foundCodePage.c_str(),hCodePageLib,hCodePageDefinition,rawCodePage)) {
mainSettings.selectedCodePage=foundCodePage;
}
else {
return false;
}
}
prepareCodePage(mappedCodePage,rawCodePage);
return true;
}