all repos — nimisewi_c @ 0846a40229ad8753419a0ab8a303d3a293df305e

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