Debian Codegraph @

common/keymap2cc.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #include <errno.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #ifdef WINDOWS
23 #define KEYMAP_CC "keymap.cpp"
24 #define KEYMAP2CC "keymap2cpp"
25 #else
26 #define KEYMAP_CC "keymap.cc"
27 #define KEYMAP2CC "keymap2cc"
28 #endif
29
30 FILE *out;
31
32 void write_bytes(FILE *file) {
33 int pos;
34 int first = 1;
35 int c;
36 while ((c = fgetc(file)) != EOF) {
37 int width = c < 10 ? 1 : c < 100 ? 2 : 3;
38 if (first) {
39 first = 0;
40 fprintf(out, " ");
41 pos = 4;
42 } else if (pos + width > 74) {
43 fprintf(out, ",\n ");
44 pos = 4;
45 } else {
46 fprintf(out, ", ");
47 pos += 2;
48 }
49 fprintf(out, "%d", c);
50 pos += width;
51 }
52 }
53
54 int main(int argc, char *argv[]) {
55 FILE *inp;
56
57 out = fopen(KEYMAP_CC, "w");
58 if (out == NULL) {
59 fprintf(stderr, "%s: can't open output file \"%s\"\n", KEYMAP2CC, KEYMAP_CC);
60 return 1;
61 }
62
63 fprintf(out,
64 "/* %s\n"
65 " * Contains the default key map for Free42.\n"
66 " * This file is generated by the %s program from the keymap.txt file.\n"
67 " * NOTE: this is a generated file; do not edit!\n"
68 " */\n\n", KEYMAP_CC, KEYMAP2CC);
69
70
71 fprintf(out, "/***********************/\n");
72 fprintf(out, "/* Size of keymap file */\n");
73 fprintf(out, "/***********************/\n\n");
74
75 inp = fopen("keymap.txt", "rb");
76 if (inp == NULL) {
77 int err = errno;
78 fprintf(stderr, "Can't open \"keymap.txt\": %s (%d)\n",
79 strerror(err), err);
80 fclose(out);
81 remove(KEYMAP_CC);
82 return 1;
83 }
84 fseek(inp, 0, SEEK_END);
85 fprintf(out, "extern const long keymap_filesize = %ld;\n\n\n", ftell(inp));
86 fclose(inp);
87
88
89 fprintf(out, "/********************/\n");
90 fprintf(out, "/* Keymap file data */\n");
91 fprintf(out, "/********************/\n\n");
92
93 inp = fopen("keymap.txt", "rb");
94 if (inp == NULL) {
95 int err = errno;
96 fprintf(stderr, "Can't open \"keymap.txt\": %s (%d)\n",
97 strerror(err), err);
98 fclose(out);
99 remove(KEYMAP_CC);
100 return 1;
101 }
102 fprintf(out, "extern const char keymap_filedata[] = {\n");
103 write_bytes(inp);
104 fprintf(out, "\n};\n\n\n");
105 fclose(inp);
106
107
108 fprintf(out, "/***********/\n");
109 fprintf(out, "/* The End */\n");
110 fprintf(out, "/***********/\n");
111 fclose(out);
112 return 0;
113 }