| 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 |
#include "namegen.h" |
| 27 |
|
| 28 |
#include <stdbool.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include <cdogs/utils.h> |
| 34 |
|
| 35 |
|
| 36 |
static void LoadFile(CArray *strings, const char *filename); |
| 37 |
void NameGenInit( |
| 38 |
NameGen *g, |
| 39 |
const char *prefixFile, const char *suffixFile, const char *suffixNameFile) |
| 40 |
{ |
| 41 |
CArrayInit(&g->prefixes, sizeof(char *)); |
| 42 |
CArrayInit(&g->suffixes, sizeof(char *)); |
| 43 |
CArrayInit(&g->suffixNames, sizeof(char *)); |
| 44 |
LoadFile(&g->prefixes, prefixFile); |
| 45 |
LoadFile(&g->suffixes, suffixFile); |
| 46 |
LoadFile(&g->suffixNames, suffixNameFile); |
| 47 |
} |
| 48 |
static void LoadFile(CArray *strings, const char *filename) |
| 49 |
{ |
| 50 |
FILE *file = fopen(filename, "r"); |
| 51 |
char buf[256]; |
| 52 |
while (fgets(buf, 256, file) != NULL) |
| 53 |
{ |
| 54 |
char *str; |
| 55 |
CSTRDUP(str, buf); |
| 56 |
str[strlen(buf) - 1] = '\0'; |
| 57 |
CArrayPushBack(strings, &str); |
| 58 |
} |
| 59 |
fclose(file); |
| 60 |
} |
| 61 |
static void UnloadStrings(CArray *strings); |
| 62 |
void NameGenTerminate(NameGen *g) |
| 63 |
{ |
| 64 |
UnloadStrings(&g->prefixes); |
| 65 |
UnloadStrings(&g->suffixes); |
| 66 |
UnloadStrings(&g->suffixNames); |
| 67 |
} |
| 68 |
static void UnloadStrings(CArray *strings) |
| 69 |
{ |
| 70 |
for (int i = 0; i < (int)strings->size; i++) |
| 71 |
{ |
| 72 |
char **str = CArrayGet(strings, i); |
| 73 |
free(*str); |
| 74 |
} |
| 75 |
CArrayTerminate(strings); |
| 76 |
} |
| 77 |
|
| 78 |
static bool IsSameWord(const char *l, const char *r); |
| 79 |
void NameGenMake(const NameGen *g, char *buf) |
| 80 |
{ |
| 81 |
for (;;) |
| 82 |
{ |
| 83 |
char **prefix = CArrayGet(&g->prefixes, rand() % g->prefixes.size); |
| 84 |
int suffixIndex = rand() % (g->suffixes.size + g->suffixNames.size); |
| 85 |
char **suffix; |
| 86 |
if (suffixIndex < (int)g->suffixes.size) |
| 87 |
{ |
| 88 |
suffix = CArrayGet(&g->suffixes, suffixIndex); |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
suffix = CArrayGet( |
| 93 |
&g->suffixNames, suffixIndex - (int)g->suffixes.size); |
| 94 |
} |
| 95 |
|
| 96 |
if (IsSameWord(*prefix, *suffix)) |
| 97 |
{ |
| 98 |
continue; |
| 99 |
} |
| 100 |
strcpy(buf, *prefix); |
| 101 |
|
| 102 |
if (suffixIndex >= (int)g->suffixes.size) |
| 103 |
{ |
| 104 |
strcat(buf, " "); |
| 105 |
} |
| 106 |
strcat(buf, *suffix); |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
static char LowerCase(const char c); |
| 111 |
static bool IsSameWord(const char *l, const char *r) |
| 112 |
{ |
| 113 |
while (*l && *r) |
| 114 |
{ |
| 115 |
if (LowerCase(*l) != LowerCase(*r)) |
| 116 |
{ |
| 117 |
return false; |
| 118 |
} |
| 119 |
l++; |
| 120 |
r++; |
| 121 |
} |
| 122 |
if (*l || *r) |
| 123 |
{ |
| 124 |
return false; |
| 125 |
} |
| 126 |
return true; |
| 127 |
} |
| 128 |
static char LowerCase(const char c) |
| 129 |
{ |
| 130 |
if (c >= 'A' && c <= 'Z') |
| 131 |
{ |
| 132 |
return c - 32; |
| 133 |
} |
| 134 |
return c; |
| 135 |
} |