Debian Codegraph @

translations/generate_translations_header.c

raw symbols SCIP
1 // Copyright (c) 2013, Sergey Lyubka
2 // Copyright (c) 2017-2022, The Monero Project
3 // All rights reserved.
4 // Released under the MIT license.
5
6 // This program takes a list of files as an input, and produces C++ code that
7 // contains the contents of all these files as a collection of strings.
8 //
9 // Usage:
10 // 1. Compile this file:
11 // cc -o generate-translations-header generate-translations-header.c
12 //
13 // 2. Convert list of files into single header:
14 // ./generate-translations-header monero_fr.qm monero_it.qm > translations_files.h
15 //
16 // 3. In your application code, include translations_files.h, then you can
17 // access the files using this function:
18 // static bool find_embedded_file(const std::string &file_name, std::string &data);
19 // std::string data;
20 // find_embedded_file("monero_fr.qm", data);
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 static const char *code =
26 "static bool find_embedded_file(const std::string &name, std::string &data) {\n"
27 " const struct embedded_file *p;\n"
28 " for (p = embedded_files; p->name != NULL; p++) {\n"
29 " if (*p->name == name) {\n"
30 " data = *p->data;\n"
31 " return true;\n"
32 " }\n"
33 " }\n"
34 " return false;\n"
35 "}\n";
36
37 int main(int argc, char *argv[]) {
38 FILE *fp, *foutput;
39 int i, j, ch;
40
41 if((foutput = fopen("translation_files.h", "w")) == NULL) {
42 exit(EXIT_FAILURE);
43 }
44
45 fprintf(foutput, "#ifndef TRANSLATION_FILES_H\n");
46 fprintf(foutput, "#define TRANSLATION_FILES_H\n\n");
47 fprintf(foutput, "#include <string>\n\n");
48
49 for (i = 1; i < argc; i++) {
50 if ((fp = fopen(argv[i], "rb")) == NULL) {
51 fclose(foutput);
52 exit(EXIT_FAILURE);
53 } else {
54 fprintf(foutput, "static const std::string translation_file_name_%d = \"%s\";\n", i, argv[i]);
55 fprintf(foutput, "static const std::string translation_file_data_%d = std::string(", i);
56 for (j = 0; (ch = fgetc(fp)) != EOF; j++) {
57 if ((j % 16) == 0) {
58 if (j > 0) {
59 fprintf(foutput, "%s", "\"");
60 }
61 fprintf(foutput, "%s", "\n \"");
62 }
63 fprintf(foutput, "\\x%02x", ch);
64 }
65 fprintf(foutput, "\",\n %d);\n\n", j);
66 fclose(fp);
67 }
68 }
69
70 fprintf(foutput, "%s", "static const struct embedded_file {\n");
71 fprintf(foutput, "%s", " const std::string *name;\n");
72 fprintf(foutput, "%s", " const std::string *data;\n");
73 fprintf(foutput, "%s", "} embedded_files[] = {\n");
74
75 for (i = 1; i < argc; i++) {
76 fprintf(foutput, " {&translation_file_name_%d, &translation_file_data_%d},\n", i, i);
77 }
78 fprintf(foutput, "%s", " {NULL, NULL}\n");
79 fprintf(foutput, "%s", "};\n\n");
80 fprintf(foutput, "%s\n", code);
81
82 fprintf(foutput, "#endif /* TRANSLATION_FILES_H */\n");
83
84 fclose(foutput);
85
86 return EXIT_SUCCESS;
87 }