/* 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 #include #include #include #include #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_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; }