all repos — nimisewi_c @ main

simple random toki pona phrase generator

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 <config.h>
 30
 31#include <err.h>
 32#include <stdio.h>
 33#include <stdlib.h>
 34#include <string.h>
 35#include <unistd.h>
 36
 37#include "makenimitoki.h"
 38
 39void
 40make_header(FILE *nimitoki_h)
 41{
 42    fprintf(nimitoki_h,
 43            _MAKENIMITOKI_COMMENT_
 44            "#ifndef NIMI_TOKI_H\n"
 45            "#define NIMI_TOKI_H\n\n"
 46
 47            "#include <stdlib.h>\n\n"
 48
 49            "extern const char *nimi_toki[];    /* list of words */\n"
 50            "extern const unsigned long suli_pi_nimi_toki   /* length of word list */;\n\n"
 51
 52            "#endif /* NIMI_TOKI_H */\n");
 53}
 54
 55void
 56make_nimi_toki(FILE *nimitoki_txt, FILE *nimitoki_c)
 57{
 58    char buf[NIMI_TOKI_MAX_LENGTH];
 59
 60    fprintf(nimitoki_c,
 61            _MAKENIMITOKI_COMMENT_
 62            "#include \"nimitoki.h\"\n\n");
 63
 64    fprintf(nimitoki_c, "const char *nimi_toki[] = {\n");
 65    
 66    while (fgets(buf, NIMI_TOKI_MAX_LENGTH, nimitoki_txt) != NULL) {
 67        buf[strlen(buf)-1] = '\0';  /* i know it's weird, but :) */
 68        fprintf(nimitoki_c, "    \"%s\",\n", buf);
 69    }
 70    
 71    fprintf(nimitoki_c, "};\n\n");
 72
 73    fprintf(nimitoki_c, "const unsigned long suli_pi_nimi_toki = "
 74                        "sizeof(nimi_toki)/sizeof(const char *);\n");
 75}
 76
 77int
 78main(int argc, char *argv[])
 79{
 80    char *nimitoki_txt_path; /* file path for wordlist file */
 81    FILE *nimitoki_txt;      /* list of words */
 82    FILE *nimitoki_h;        /* output header file */
 83    FILE *nimitoki_c;        /* output code file */
 84
 85#ifdef HAVE_PLEDGE
 86    if (pledge("stdio rpath wpath cpath", NULL) == -1) {
 87        err(EXIT_FAILURE, "pledge");
 88    }
 89#endif
 90
 91    if (argc < 1 || argc > 2) {
 92        err(EXIT_FAILURE, "usage: makenimitoki <nimitoki.txt>");
 93    }
 94
 95    nimitoki_txt_path = argv[1];
 96
 97    nimitoki_txt = fopen(nimitoki_txt_path, "r");
 98    if (nimitoki_txt == NULL) {
 99        err(EXIT_FAILURE, "file not found");
100    }
101
102    nimitoki_h = fopen("nimitoki.h", "w");
103    make_header(nimitoki_h);
104    fclose(nimitoki_h);
105
106    nimitoki_c = fopen("nimitoki.c", "w");
107    make_nimi_toki(nimitoki_txt, nimitoki_c);
108    fclose(nimitoki_txt);
109    fclose(nimitoki_c);
110
111    return 0;
112}