aboutsummaryrefslogtreecommitdiffstats
path: root/nimisewi.c
diff options
context:
space:
mode:
Diffstat (limited to 'nimisewi.c')
-rw-r--r--nimisewi.c163
1 files changed, 104 insertions, 59 deletions
diff --git a/nimisewi.c b/nimisewi.c
index de2fed9..0e9b82e 100644
--- a/nimisewi.c
+++ b/nimisewi.c
@@ -20,10 +20,10 @@
* toki pona is a small constructed language created by sonja lang
* (see https://tokipona.org).
*
- * functions and variables here a primarily named in tokipona, just because i
- * can. but just in case there are annotations in english.
+ * functions and variables here a primarily named in tokipona, just because
+ * i can. but just in case there are annotations in english.
*
- * sona nanpa li ike. taso ona li pali e nimi pona.
+ * sona nanpa li ike. taso ona li pali e ilo pona.
*/
#include <err.h>
@@ -31,14 +31,19 @@
#include <stdlib.h>
#include <unistd.h>
+#include <string.h>
+#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined( _NetBSD__) || defined(__DragonFly__)
+#define HAS_STRLCAT
+#endif
+
#include <sys/types.h>
#include <sys/time.h>
#include "nimisewi.h"
#include "nimitoki.h" /* nimi_toki, suli_pi_nimi_toki */
-void
-open_e_nanpa_sewi()
+static void
+open_e_nanpa_sewi(void)
{
/* initialize pseudorandom generator using pid, microseconds and seconds
*
@@ -54,10 +59,8 @@ open_e_nanpa_sewi()
srand(pid * time.tv_usec * time.tv_sec);
}
-const char
-*pana_e_nimi(const char *nimi_mute[],
- const int suli_pi_nimi_mute,
- const int nanpa_nimi)
+static const char
+*pana_e_nimi(const int nanpa_nimi)
{
/* wrapper function to get word from array
*
@@ -76,95 +79,137 @@ const char
* very heavy phrases.
*/
- if (nanpa_nimi > suli_pi_nimi_mute) {
- err(EXIT_FAILURE, "index out of bounds");
- } else if (nanpa_nimi == -1) {
+ if (nanpa_nimi == -1) {
return "pi";
+ } else if ((unsigned long) nanpa_nimi > suli_pi_nimi_toki) {
+ err(EXIT_FAILURE, "index out of bounds");
} else {
- return nimi_mute[nanpa_nimi];
+ return nimi_toki[nanpa_nimi];
}
}
-void
-pana_e_nanpa_nimi(struct nanpa_nimi *nn)
+static void
+nanpa_nimi_pana_e_pi(struct nanpa_nimi *nn)
{
- /* generate random number, which is later used as number of words
- * in the phrase
- *
- * also here is made decision, whether to insert 'pi'
- */
- nn->pi_li_lon = 0;
- nn->nanpa_pi = -1;
- nn->nanpa_nimi = (rand() % 6);
-
+ /* handle a 'pi' instertion */
if (nn->nanpa_nimi == 2) {
+ /* this is made to allow for phrases with following structures:
+ *
+ * - word1 word2 word3
+ * - word1 pi word2 word3
+ */
nn->pi_li_lon = rand() % 2;
} else if (nn->nanpa_nimi > 2) {
nn->pi_li_lon = 1;
}
if (nn->pi_li_lon) {
+ /* since we insert whole word, number of words must be increased too */
nn->nanpa_nimi++;
nn->nanpa_pi = (nn->nanpa_nimi / 2);
}
}
-void
-pana_e_nanpa_pi_nimi_sewi(int *nanpa_sewi_mute,
- const int suli_nimi,
- struct nanpa_nimi *nn)
+static struct nanpa_nimi
+*pana_e_nanpa_nimi(void)
{
- /* create specified amount of random numbers
+ /* generate nanpa_nimi with all the values useful for phrase generation
*
- * they are used later as indices to get words from wordlist
+ * when used, one should call weka_e_nanpa_nimi();
*/
int i;
+ struct nanpa_nimi *nn;
- for (i = 0; i <= nn->nanpa_nimi; i++) {
- nanpa_sewi_mute[i] = rand() % suli_nimi;
+ nn = malloc(sizeof(struct nanpa_nimi));
+ if (nn == NULL) {
+ return NULL;
}
+ /* initialize nanpa_nimi with default values */
+ nn->pi_li_lon = 0;
+ nn->nanpa_pi = -1;
+ nn->nanpa_sewi_nimi = NULL;
+ nn->suli_pi_nimi_sewi = 0;
+
+ nn->nanpa_nimi = (rand() % 6);
+
+ /* to use with arbitrary wordlist, remove following function call */
+ nanpa_nimi_pana_e_pi(nn);
+
+ nn->nanpa_sewi_nimi = calloc(nn->nanpa_nimi + 1, sizeof(int));
+ if (nn->nanpa_sewi_nimi == NULL) {
+ free(nn);
+ return NULL;
+ }
+ for (i = 0; i <= nn->nanpa_nimi; i++) {
+ nn->nanpa_sewi_nimi[i] = rand() % suli_pi_nimi_toki;
+ }
if (nn->pi_li_lon) {
- nanpa_sewi_mute[nn->nanpa_pi] = -1;
+ nn->nanpa_sewi_nimi[nn->nanpa_pi] = -1;
+ }
+
+ for (i = 0; i <= nn->nanpa_nimi; i++) {
+ nn->suli_pi_nimi_sewi += sizeof(char);
+ nn->suli_pi_nimi_sewi += strlen(pana_e_nimi(nn->nanpa_sewi_nimi[i]));
}
+
+ return nn;
}
-int
-main(void)
+static void
+weka_e_nanpa_nimi(struct nanpa_nimi *nn)
{
- int *nanpa_sewi_nimi; /* container for random indices */
- int i;
- struct nanpa_nimi nn; /* see nimisewi.h */
-
-#ifdef __OpenBSD__
- if (pledge("stdio", NULL) == -1) {
- err(EXIT_FAILURE, "pledge");
+ if (nn != NULL) {
+ free(nn->nanpa_sewi_nimi);
+ free(nn);
}
+}
+
+static void
+string_cat(char *dst, const char *src, size_t size) {
+#ifdef HAS_STRLCAT
+ strlcat(dst, src, size);
+#else
+ strcat(dst, src);
#endif
+}
+
+char
+*nimi_sewi()
+{
+ int i;
+ char *nimi_pana;
+ struct nanpa_nimi *nn;
+
open_e_nanpa_sewi();
- pana_e_nanpa_nimi(&nn);
+ nn = pana_e_nanpa_nimi();
- nanpa_sewi_nimi = (int *) calloc(nn.nanpa_nimi + 1, sizeof(int));
- if (nanpa_sewi_nimi == NULL) {
- err(EXIT_FAILURE, "calloc");
+ nimi_pana = calloc(nn->suli_pi_nimi_sewi + 1, sizeof(char));
+ if (nimi_pana == NULL) {
+ weka_e_nanpa_nimi(nn);
+ return NULL;
}
- pana_e_nanpa_pi_nimi_sewi(nanpa_sewi_nimi, suli_pi_nimi_toki, &nn);
-
- /* i think that this section could be improved. i had an idea that
- * it would be cool to include options to output in cgi format instead
- * of just text
- */
- for (i = 0; i < nn.nanpa_nimi; i++) {
- printf("%s ",
- pana_e_nimi(nimi_toki, suli_pi_nimi_toki, nanpa_sewi_nimi[i]));
+ for (i = 0; i < nn->nanpa_nimi; i++) {
+ string_cat(nimi_pana,
+ pana_e_nimi(nn->nanpa_sewi_nimi[i]),
+ nn->suli_pi_nimi_sewi);
+ string_cat(nimi_pana, " ", nn->suli_pi_nimi_sewi);
}
- printf("%s", pana_e_nimi(nimi_toki, suli_pi_nimi_toki, nanpa_sewi_nimi[i]));
- printf("\n");
+ string_cat(nimi_pana,
+ pana_e_nimi(nn->nanpa_sewi_nimi[i]),
+ nn->suli_pi_nimi_sewi);
- free(nanpa_sewi_nimi);
+ weka_e_nanpa_nimi(nn);
- return 0;
+ return nimi_pana;
}
+void
+weka_e_nimi_sewi(char *nimi_sewi)
+{
+ if (nimi_sewi != NULL) {
+ free(nimi_sewi);
+ }
+}