-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
73 lines (66 loc) · 1.96 KB
/
main.c
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
/* skandinav
* Copyright (C) 2024 Anosh D. Ullenius
*
* GPL-3.0-only
*
* This file is part of skandinav
*
* skandinav is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3.0.
*
* skandinav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#define HI_BYTE 0xC3
int main(int argc, char **argv) {
unsigned char ch = 0;
FILE *in = NULL;
if (argc != 2) {
fprintf(stderr, "Usage: skandinav filename\n");
return -1;
}
in = fopen(argv[1], "rb");
if (in == NULL) {
fprintf(stderr, "Failed to open file\n");
return -1;
}
fseek(in, 0, SEEK_SET);
while (fread(&ch, 1, 1, in) == 1) {
const unsigned int val = ch & 0x80;
if (val == 0) {
putchar(ch);
}
else if (ch == HI_BYTE) {
const unsigned int bytesRead = fread(&ch, 1, 1, in);
if (bytesRead == 0) {
fprintf(stderr, "Broken UTF-8 file\n");
fclose(in);
return -1;
}
switch (ch) {
case 0xA5: /* å */
case 0xA4: /* ä */
case 0xB6: /* ö */
case 0x85: /* Å */
case 0x84: /* Ä */
case 0x96: /* Ö */
case 0x86: /* Æ */
case 0x98: /* Ø */
case 0xA6: /* æ */
case 0xB8: /* ø */
putchar(HI_BYTE);
putchar(ch);
break;
}
}
}
fclose(in);
return 0;
}