aboutsummaryrefslogtreecommitdiffstats
path: root/makenimitoki.c
blob: 52c09888fbb34e2eaf28734a2651cc3fb9773833 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Copyright (c) 2021, la-ninpre
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * *************************************************************************
 * 
 * this file creates necessary data files for nimisewi.c.
 * 
 * i thought that it would be stupid to hardcode all the tokipona words into
 * actual code, so this little program is created. it reads nimitoki.txt file
 * which is in such format that one word is on separate line. i hope it's
 * easy now to add and remove desired words.
 * 
 * it's use is very narrow and tied to nimisewi.c, but it could be adapted.
 * 
 * TODO: make use of templates, maybe?
 */

#include <config.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "makenimitoki.h"

void
make_header(FILE *nimitoki_h)
{
    fprintf(nimitoki_h,
            _MAKENIMITOKI_COMMENT_
            "#ifndef NIMI_TOKI_H\n"
            "#define NIMI_TOKI_H\n\n"

            "#include <stdlib.h>\n\n"

            "extern const char *nimi_toki[];    /* list of words */\n"
            "extern const unsigned long suli_pi_nimi_toki   /* length of word list */;\n\n"

            "#endif /* NIMI_TOKI_H */\n");
}

void
make_nimi_toki(FILE *nimitoki_txt, FILE *nimitoki_c)
{
    char buf[NIMI_TOKI_MAX_LENGTH];

    fprintf(nimitoki_c,
            _MAKENIMITOKI_COMMENT_
            "#include \"nimitoki.h\"\n\n");

    fprintf(nimitoki_c, "const char *nimi_toki[] = {\n");
    
    while (fgets(buf, NIMI_TOKI_MAX_LENGTH, nimitoki_txt) != NULL) {
        buf[strlen(buf)-1] = '\0';  /* i know it's weird, but :) */
        fprintf(nimitoki_c, "    \"%s\",\n", buf);
    }
    
    fprintf(nimitoki_c, "};\n\n");

    fprintf(nimitoki_c, "const unsigned long suli_pi_nimi_toki = "
                        "sizeof(nimi_toki)/sizeof(const char *);\n");
}

int
main(int argc, char *argv[])
{
    char *nimitoki_txt_path; /* file path for wordlist file */
    FILE *nimitoki_txt;      /* list of words */
    FILE *nimitoki_h;        /* output header file */
    FILE *nimitoki_c;        /* output code file */

#ifdef HAVE_PLEDGE
    if (pledge("stdio rpath wpath cpath", NULL) == -1) {
        err(EXIT_FAILURE, "pledge");
    }
#endif

    if (argc < 1 || argc > 2) {
        err(EXIT_FAILURE, "usage: makenimitoki <nimitoki.txt>");
    }

    nimitoki_txt_path = argv[1];

    nimitoki_txt = fopen(nimitoki_txt_path, "r");
    if (nimitoki_txt == NULL) {
        err(EXIT_FAILURE, "file not found");
    }

    nimitoki_h = fopen("nimitoki.h", "w");
    make_header(nimitoki_h);
    fclose(nimitoki_h);

    nimitoki_c = fopen("nimitoki.c", "w");
    make_nimi_toki(nimitoki_txt, nimitoki_c);
    fclose(nimitoki_txt);
    fclose(nimitoki_c);

    return 0;
}