diff options
| author | la-ninpre <leobrekalini@gmail.com> | 2021-05-31 23:22:36 +0000 |
|---|---|---|
| committer | la-ninpre <leobrekalini@gmail.com> | 2021-05-31 23:22:36 +0000 |
| commit | 57f683f181495b802761d23e36b6d7ff75863613 (patch) | |
| tree | a0a8777e21cdce16a6e10d49a8e6440bd38f7518 /makenimitoki.c | |
| parent | cc2975038177e2738ea9d75100d534a0296fa260 (diff) | |
| download | nimisewi_c-57f683f181495b802761d23e36b6d7ff75863613.tar.gz nimisewi_c-57f683f181495b802761d23e36b6d7ff75863613.zip | |
added initial filesv0.0.1
FossilOrigin-Name: ec9a2c52b6452b8e2ad9d1e6e692c3288c28c0dde71fb72371885b7750ce08c8
Diffstat (limited to 'makenimitoki.c')
| -rw-r--r-- | makenimitoki.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/makenimitoki.c b/makenimitoki.c new file mode 100644 index 0000000..e2b5ea7 --- /dev/null +++ b/makenimitoki.c @@ -0,0 +1,93 @@ +/* Copyright (c) 2021, la-ninpre + * + * 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 <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" + + "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[]) +{ + FILE *nimitoki_txt; /* list of words */ + FILE *nimitoki_h; /* output header file */ + FILE *nimitoki_c; /* output code file */ + char *nimitoki_txt_path; + +#ifdef __OpenBSD__ + 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; +} |
