makenimitoki.c (view raw)
1/* Copyright (c) 2021, la-ninpre
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 *
15 * *************************************************************************
16 *
17 * this file creates necessary data files for nimisewi.c.
18 *
19 * i thought that it would be stupid to hardcode all the tokipona words into
20 * actual code, so this little program is created. it reads nimitoki.txt file
21 * which is in such format that one word is on separate line. i hope it's
22 * easy now to add and remove desired words.
23 *
24 * it's use is very narrow and tied to nimisewi.c, but it could be adapted.
25 *
26 * TODO: make use of templates, maybe?
27 */
28
29#include <err.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include "makenimitoki.h"
36
37void
38make_header(FILE *nimitoki_h)
39{
40 fprintf(nimitoki_h,
41 _MAKENIMITOKI_COMMENT_
42 "#ifndef NIMI_TOKI_H\n"
43 "#define NIMI_TOKI_H\n\n"
44
45 "extern const char *nimi_toki[]; /* list of words */\n"
46 "extern const unsigned long suli_pi_nimi_toki /* length of word list */;\n\n"
47
48 "#endif /* NIMI_TOKI_H */\n");
49}
50
51void
52make_nimi_toki(FILE *nimitoki_txt, FILE *nimitoki_c)
53{
54 char buf[NIMI_TOKI_MAX_LENGTH];
55
56 fprintf(nimitoki_c,
57 _MAKENIMITOKI_COMMENT_
58 "#include \"nimitoki.h\"\n\n");
59
60 fprintf(nimitoki_c, "const char *nimi_toki[] = {\n");
61
62 while (fgets(buf, NIMI_TOKI_MAX_LENGTH, nimitoki_txt) != NULL) {
63 buf[strlen(buf)-1] = '\0'; /* i know it's weird, but :) */
64 fprintf(nimitoki_c, " \"%s\",\n", buf);
65 }
66
67 fprintf(nimitoki_c, "};\n\n");
68
69 fprintf(nimitoki_c, "const unsigned long suli_pi_nimi_toki = "
70 "sizeof(nimi_toki)/sizeof(const char *);\n");
71}
72
73int
74main(int argc, char *argv[])
75{
76 char *nimitoki_txt_path; /* file path for wordlist file */
77 FILE *nimitoki_txt; /* list of words */
78 FILE *nimitoki_h; /* output header file */
79 FILE *nimitoki_c; /* output code file */
80
81#ifdef __OpenBSD__
82 if (pledge("stdio rpath wpath cpath", NULL) == -1) {
83 err(EXIT_FAILURE, "pledge");
84 }
85#endif
86
87 if (argc < 1 || argc > 2) {
88 err(EXIT_FAILURE, "usage: makenimitoki <nimitoki.txt>");
89 }
90
91 nimitoki_txt_path = argv[1];
92
93 nimitoki_txt = fopen(nimitoki_txt_path, "r");
94 if (nimitoki_txt == NULL) {
95 err(EXIT_FAILURE, "file not found");
96 }
97
98 nimitoki_h = fopen("nimitoki.h", "w");
99 make_header(nimitoki_h);
100 fclose(nimitoki_h);
101
102 nimitoki_c = fopen("nimitoki.c", "w");
103 make_nimi_toki(nimitoki_txt, nimitoki_c);
104 fclose(nimitoki_txt);
105 fclose(nimitoki_c);
106
107 return 0;
108}