makenimitoki.c (view raw)
1/* Copyright (c) 2021, la-ninpre
2 *
3 * this file creates necessary data files for nimisewi.c.
4 *
5 * i thought that it would be stupid to hardcode all the tokipona words into
6 * actual code, so this little program is created. it reads nimitoki.txt file
7 * which is in such format that one word is on separate line. i hope it's
8 * easy now to add and remove desired words.
9 *
10 * it's use is very narrow and tied to nimisewi.c, but it could be adapted.
11 *
12 * TODO: make use of templates, maybe?
13 */
14#include <err.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include "makenimitoki.h"
21
22void
23make_header(FILE *nimitoki_h)
24{
25 fprintf(nimitoki_h,
26 _MAKENIMITOKI_COMMENT_
27 "#ifndef NIMI_TOKI_H\n"
28 "#define NIMI_TOKI_H\n\n"
29
30 "extern const char *nimi_toki[]; /* list of words */\n"
31 "extern const unsigned long suli_pi_nimi_toki /* length of word list */;\n\n"
32
33 "#endif /* NIMI_TOKI_H */\n");
34}
35
36void
37make_nimi_toki(FILE *nimitoki_txt, FILE *nimitoki_c)
38{
39 char buf[NIMI_TOKI_MAX_LENGTH];
40
41 fprintf(nimitoki_c,
42 _MAKENIMITOKI_COMMENT_
43 "#include \"nimitoki.h\"\n\n");
44
45 fprintf(nimitoki_c, "const char *nimi_toki[] = {\n");
46
47 while (fgets(buf, NIMI_TOKI_MAX_LENGTH, nimitoki_txt) != NULL) {
48 buf[strlen(buf)-1] = '\0'; /* i know it's weird, but :) */
49 fprintf(nimitoki_c, " \"%s\",\n", buf);
50 }
51
52 fprintf(nimitoki_c, "};\n\n");
53
54 fprintf(nimitoki_c, "const unsigned long suli_pi_nimi_toki = "
55 "sizeof(nimi_toki)/sizeof(const char *);\n");
56}
57
58int
59main(int argc, char *argv[])
60{
61 FILE *nimitoki_txt; /* list of words */
62 FILE *nimitoki_h; /* output header file */
63 FILE *nimitoki_c; /* output code file */
64 char *nimitoki_txt_path;
65
66#ifdef __OpenBSD__
67 if (pledge("stdio rpath wpath cpath", NULL) == -1) {
68 err(EXIT_FAILURE, "pledge");
69 }
70#endif
71
72 if (argc < 1 || argc > 2) {
73 err(EXIT_FAILURE, "usage: makenimitoki <nimitoki.txt>");
74 }
75
76 nimitoki_txt_path = argv[1];
77
78 nimitoki_txt = fopen(nimitoki_txt_path, "r");
79 if (nimitoki_txt == NULL) {
80 err(EXIT_FAILURE, "file not found");
81 }
82
83 nimitoki_h = fopen("nimitoki.h", "w");
84 make_header(nimitoki_h);
85 fclose(nimitoki_h);
86
87 nimitoki_c = fopen("nimitoki.c", "w");
88 make_nimi_toki(nimitoki_txt, nimitoki_c);
89 fclose(nimitoki_txt);
90 fclose(nimitoki_c);
91
92 return 0;
93}