DEADSOFTWARE

portability: avoid errors on some compilers
[flatwaifu.git] / src / common / cp866.c
1 /* Copyright (C) 2020 SovietPony
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
16 #include "common/cp866.h"
18 int cp866_isalpha (int ch) {
19 return (ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A) || (ch >= 0x80 && ch <= 0xAF) || (ch >= 0xE0 && ch <= 0xF7);
20 }
22 int cp866_isupper (int ch) {
23 return (ch >= 0x41 && ch <= 0x5A) || (ch >= 0x80 && ch <= 0x9F) || ch == 0xF0 || ch == 0xF2 || ch == 0xF4 || ch == 0xF6;
24 }
26 int cp866_islower (int ch) {
27 return (ch >= 0x61 && ch <= 0x7A) || (ch >= 0xA0 && ch <= 0xAF) || (ch >= 0xE0 && ch <= 0xEF) || ch == 0xF1 || ch == 0xF3 || ch == 0xF5 || ch == 0xF7;
28 }
30 int cp866_toupper (int ch) {
31 if (ch >= 0x61 && ch <= 0x7A) {
32 return ch - 0x61 + 0x41;
33 } else if (ch >= 0xA0 && ch <= 0xAF) {
34 return ch - 0xA0 + 0x80;
35 } else if (ch >= 0xE0 && ch <= 0xEF) {
36 return ch - 0xE0 + 0x90;
37 } else {
38 switch (ch) {
39 case 0xF1:
40 case 0xF3:
41 case 0xF5:
42 case 0xF7:
43 return ch - 1;
44 default:
45 return ch;
46 }
47 }
48 }
50 int cp866_tolower (int ch) {
51 if (ch >= 0x41 && ch <= 0x5A) {
52 return ch - 0x41 + 0x61;
53 } else if (ch >= 0x80 && ch <= 0x8F) {
54 return ch - 0x80 + 0xA0;
55 } else if (ch >= 0x90 && ch <= 0x9F) {
56 return ch - 0x90 + 0xE0;
57 } else {
58 switch (ch) {
59 case 0xF0:
60 case 0xF2:
61 case 0xF4:
62 case 0xF6:
63 return ch + 1;
64 default:
65 return ch;
66 }
67 }
68 }
70 int cp866_strcasecmp (const char *a, const char *b) {
71 const char *aa = a;
72 const char *bb = b;
73 while (*aa != 0 && cp866_tolower(*aa) == cp866_tolower(*bb)) {
74 aa++;
75 bb++;
76 }
77 return *(const unsigned char *)aa - *(const unsigned char *)bb;
78 }
80 int cp866_strncasecmp (const char *a, const char *b, unsigned int n) {
81 const char *aa = a;
82 const char *bb = b;
83 unsigned int i = n;
84 while (i > 0 && *aa != 0 && cp866_tolower(*aa) == cp866_tolower(*bb)) {
85 i--;
86 aa++;
87 bb++;
88 }
89 return i == 0 ? 0 : *(const unsigned char *)aa - *(const unsigned char *)bb;
90 }
92 int cp866_ctou (int ch) {
93 switch (ch) {
94 case 0xB0: return 0x2591;
95 case 0xB1: return 0x2592;
96 case 0xB2: return 0x2593;
97 case 0xB3: return 0x2502;
98 case 0xB4: return 0x2524;
99 case 0xB5: return 0x2561;
100 case 0xB6: return 0x2562;
101 case 0xB7: return 0x2556;
102 case 0xB8: return 0x2555;
103 case 0xB9: return 0x2563;
104 case 0xBA: return 0x2551;
105 case 0xBB: return 0x2557;
106 case 0xBC: return 0x255D;
107 case 0xBD: return 0x255C;
108 case 0xBE: return 0x255B;
109 case 0xBF: return 0x2510;
110 case 0xC0: return 0x2514;
111 case 0xC1: return 0x2534;
112 case 0xC2: return 0x252C;
113 case 0xC3: return 0x251C;
114 case 0xC4: return 0x2500;
115 case 0xC5: return 0x253C;
116 case 0xC6: return 0x255E;
117 case 0xC7: return 0x255F;
118 case 0xC8: return 0x255A;
119 case 0xC9: return 0x2554;
120 case 0xCA: return 0x2569;
121 case 0xCB: return 0x2566;
122 case 0xCC: return 0x2560;
123 case 0xCD: return 0x2550;
124 case 0xCE: return 0x256C;
125 case 0xCF: return 0x2567;
126 case 0xD0: return 0x2568;
127 case 0xD1: return 0x2564;
128 case 0xD2: return 0x2565;
129 case 0xD3: return 0x2559;
130 case 0xD4: return 0x2558;
131 case 0xD5: return 0x2552;
132 case 0xD6: return 0x2553;
133 case 0xD7: return 0x256B;
134 case 0xD8: return 0x256A;
135 case 0xD9: return 0x2518;
136 case 0xDA: return 0x250C;
137 case 0xDB: return 0x2588;
138 case 0xDC: return 0x2584;
139 case 0xDD: return 0x258C;
140 case 0xDE: return 0x2590;
141 case 0xDF: return 0x2580;
142 case 0xF0: return 0x0401;
143 case 0xF1: return 0x0451;
144 case 0xF2: return 0x0404;
145 case 0xF3: return 0x0454;
146 case 0xF4: return 0x0407;
147 case 0xF5: return 0x0457;
148 case 0xF6: return 0x040E;
149 case 0xF7: return 0x045E;
150 case 0xF8: return 0x00B0;
151 case 0xF9: return 0x2219;
152 case 0xFA: return 0x00B7;
153 case 0xFB: return 0x221A;
154 case 0xFC: return 0x2116;
155 case 0xFD: return 0x00A4;
156 case 0xFE: return 0x25A0;
157 case 0xFF: return 0x00A0;
158 default:
159 if (ch >= 0x20 && ch <= 0x7E) {
160 return ch;
161 } else if (ch >= 0x80 && ch <= 0xAF) {
162 return ch - 0x80 + 0x410;
163 } else if (ch >= 0xE0 && ch <= 0xEF) {
164 return ch - 0xE0 + 0x440;
165 } else {
166 return ch;
171 int cp866_ctoug (int ch) {
172 switch (ch) {
173 case 0x00: return 0x0000;
174 case 0x01: return 0x263A;
175 case 0x02: return 0x263B;
176 case 0x03: return 0x2665;
177 case 0x04: return 0x2666;
178 case 0x05: return 0x2663;
179 case 0x06: return 0x2660;
180 case 0x07: return 0x2022;
181 case 0x08: return 0x25D8;
182 case 0x09: return 0x25CB;
183 case 0x0A: return 0x25D9;
184 case 0x0B: return 0x2642;
185 case 0x0C: return 0x2640;
186 case 0x0D: return 0x266A;
187 case 0x0E: return 0x266B;
188 case 0x0F: return 0x263C;
189 case 0x10: return 0x25BA;
190 case 0x11: return 0x25C4;
191 case 0x12: return 0x2195;
192 case 0x13: return 0x203C;
193 case 0x14: return 0x00B6;
194 case 0x15: return 0x00A7;
195 case 0x16: return 0x25AC;
196 case 0x17: return 0x21A8;
197 case 0x18: return 0x2191;
198 case 0x19: return 0x2193;
199 case 0x1A: return 0x2192;
200 case 0x1B: return 0x2190;
201 case 0x1C: return 0x221F;
202 case 0x1D: return 0x2194;
203 case 0x1E: return 0x25B2;
204 case 0x1F: return 0x25BC;
205 case 0x7F: return 0x2302;
206 default: return cp866_ctou(ch);
210 int cp866_utoc (int ch) {
211 if (ch >= 0x00 && ch <= 0x7F) {
212 return ch;
213 } else {
214 switch (ch) {
215 case 0x263A: return 0x01;
216 case 0x263B: return 0x02;
217 case 0x2665: return 0x03;
218 case 0x2666: return 0x04;
219 case 0x2663: return 0x05;
220 case 0x2660: return 0x06;
221 case 0x2022: return 0x07;
222 case 0x25D8: return 0x08;
223 case 0x25CB: return 0x09;
224 case 0x25D9: return 0x0A;
225 case 0x2642: return 0x0B;
226 case 0x2640: return 0x0C;
227 case 0x266A: return 0x0D;
228 case 0x266B: return 0x0E;
229 case 0x263C: return 0x0F;
230 case 0x25BA: return 0x10;
231 case 0x25C4: return 0x11;
232 case 0x2195: return 0x12;
233 case 0x203C: return 0x13;
234 case 0x00B6: return 0x14;
235 case 0x00A7: return 0x15;
236 case 0x25AC: return 0x16;
237 case 0x21A8: return 0x17;
238 case 0x2191: return 0x18;
239 case 0x2193: return 0x19;
240 case 0x2192: return 0x1A;
241 case 0x2190: return 0x1B;
242 case 0x221F: return 0x1C;
243 case 0x2194: return 0x1D;
244 case 0x25B2: return 0x1E;
245 case 0x25BC: return 0x1F;
246 case 0x2302: return 0x7F;
247 case 0x2591: return 0xB0;
248 case 0x2592: return 0xB1;
249 case 0x2593: return 0xB2;
250 case 0x2502: return 0xB3;
251 case 0x2524: return 0xB4;
252 case 0x2561: return 0xB5;
253 case 0x2562: return 0xB6;
254 case 0x2556: return 0xB7;
255 case 0x2555: return 0xB8;
256 case 0x2563: return 0xB9;
257 case 0x2551: return 0xBA;
258 case 0x2557: return 0xBB;
259 case 0x255D: return 0xBC;
260 case 0x255C: return 0xBD;
261 case 0x255B: return 0xBE;
262 case 0x2510: return 0xBF;
263 case 0x2514: return 0xC0;
264 case 0x2534: return 0xC1;
265 case 0x252C: return 0xC2;
266 case 0x251C: return 0xC3;
267 case 0x2500: return 0xC4;
268 case 0x253C: return 0xC5;
269 case 0x255E: return 0xC6;
270 case 0x255F: return 0xC7;
271 case 0x255A: return 0xC8;
272 case 0x2554: return 0xC9;
273 case 0x2569: return 0xCA;
274 case 0x2566: return 0xCB;
275 case 0x2560: return 0xCC;
276 case 0x2550: return 0xCD;
277 case 0x256C: return 0xCE;
278 case 0x2567: return 0xCF;
279 case 0x2568: return 0xD0;
280 case 0x2564: return 0xD1;
281 case 0x2565: return 0xD2;
282 case 0x2559: return 0xD3;
283 case 0x2558: return 0xD4;
284 case 0x2552: return 0xD5;
285 case 0x2553: return 0xD6;
286 case 0x256B: return 0xD7;
287 case 0x256A: return 0xD8;
288 case 0x2518: return 0xD9;
289 case 0x250C: return 0xDA;
290 case 0x2588: return 0xDB;
291 case 0x2584: return 0xDC;
292 case 0x258C: return 0xDD;
293 case 0x2590: return 0xDE;
294 case 0x2580: return 0xDF;
295 case 0x0401: return 0xF0;
296 case 0x0451: return 0xF1;
297 case 0x0404: return 0xF2;
298 case 0x0454: return 0xF3;
299 case 0x0407: return 0xF4;
300 case 0x0457: return 0xF5;
301 case 0x040E: return 0xF6;
302 case 0x045E: return 0xF7;
303 case 0x00B0: return 0xF8;
304 case 0x2219: return 0xF9;
305 case 0x00B7: return 0xFA;
306 case 0x221A: return 0xFB;
307 case 0x2116: return 0xFC;
308 case 0x00A4: return 0xFD;
309 case 0x25A0: return 0xFE;
310 case 0x00A0: return 0xFF;
311 default:
312 if (ch >= 0x410 && ch <= 0x43F) {
313 return ch - 0x410 + 0x80;
314 } else if (ch >= 0x440 && ch <= 0x44F) {
315 return ch - 0x440 + 0xE0;
316 } else {
317 return -1;