forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
names.c
85 lines (73 loc) · 1.4 KB
/
names.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "arraysize.h"
#include "names.h"
#include "mtwist.h"
static char *vowel[] = {
"a",
"e",
"i",
"o",
"u",
};
static char *consonant[] = {
"b", "ch", "d", "f", "g", "j", "k", "l", "m", "n", "p", "r", "s",
"t", "v", "x", "z", "ph", "sh", "th", "sp", "st",
};
static char *pattern[] = {
"vcvc",
"cvcvc",
"cvcv",
"vcv",
"cvc",
"cvv",
"cvcvv",
};
static void append_stuff(struct mtwist_state *mt, char *s, char *a[], int asize)
{
int e;
e = mtwist_next(mt) % asize;
strcat(s, a[e]);
}
char *random_name(struct mtwist_state *mt)
{
char *i;
int p;
char *result;
result = malloc(100);
p = mtwist_next(mt) % ARRAYSIZE(pattern);
memset(result, 0, 100);
for (i = pattern[p]; *i; i++) {
if (*i == 'v')
append_stuff(mt, result, vowel, ARRAYSIZE(vowel));
else
append_stuff(mt, result, consonant, ARRAYSIZE(consonant));
/* printf("zzz result = %s, pattern = %s, *i = %c\n", result, pattern[p], *i); */
}
for (i = result; *i; i++)
*i = toupper(*i);
return result;
}
#ifdef TEST_NAMES
#include <stdio.h>
int main(int argc, char *argv[])
{
int i, seed;
char *n;
struct mtwist_state *mt;
if (argc > 1) {
sscanf(argv[1], "%d", &seed);
mt = mtwist_init(seed);
} else {
mt = mtwist_init(12345);
}
for (i = 0; i < 1000; i++) {
n = random_name(mt);
printf("%s\n", n);
free(n);
}
return 0;
}
#endif