Debian Codegraph @

src/namegen.c

raw symbols SCIP
1 /*
2 Copyright (c) 2014, 2019 Cong Xu
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.
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 // Don't generate same prefix/suffix
96 if (IsSameWord(*prefix, *suffix))
97 {
98 continue;
99 }
100 strcpy(buf, *prefix);
101 // Add a space if we're using a suffix name
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 }