Debian Codegraph @

texk/gregorio/gregorio-src/src/encode_utf8strings.c

raw
1 /*
2 * Utility program to convert utf8strings.h.in into utf8strings.h
3 *
4 * Copyright (C) 2015-2025 The Gregorio Project (see CONTRIBUTORS.md)
5 *
6 * This file is part of Gregorio.
7 *
8 * Gregorio is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Gregorio is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Gregorio. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 int main(int argc, char **argv)
27 {
28 char buf[5];
29 FILE *input, *output;
30 int c;
31
32 if (argc != 3) {
33 fprintf(stderr, "Incorrect arguments\n");
34 return -1;
35 }
36
37 input = fopen(argv[1], "rb");
38 if (input == NULL) {
39 fprintf(stderr, "Error opening %s: %s\n", argv[1], strerror(errno));
40 return -1;
41 }
42
43 output = fopen(argv[2], "wb");
44 if (output == NULL) {
45 fprintf(stderr, "Error creating %s: %s\n", argv[2], strerror(errno));
46 return -1;
47 }
48
49 while ((c = fgetc(input)) != EOF) {
50 if (c & 0x80) {
51 snprintf(buf, sizeof(buf), "\\%03o", c);
52 if (fwrite(buf, sizeof(buf) - sizeof(char), 1, output) != 1) {
53 fprintf(stderr, "Error writing %s: %s\n", argv[2],
54 strerror(errno));
55 return -1;
56 }
57 } else {
58 if (fputc(c, output) != c) {
59 fprintf(stderr, "Error writing %s: %s\n", argv[2],
60 strerror(errno));
61 return -1;
62 }
63 }
64 }
65
66 if (fclose(output) != 0) {
67 fprintf(stderr, "Error closing %s: %s\n", argv[2], strerror(errno));
68 return -1;
69 }
70 if (fclose(input) != 0) {
71 fprintf(stderr, "Error closing %s: %s\n", argv[1], strerror(errno));
72 return -1;
73 }
74 return 0;
75 }