-
Notifications
You must be signed in to change notification settings - Fork 76
/
NameGeneration.cpp
168 lines (142 loc) · 4.2 KB
/
NameGeneration.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "stdafx.h"
#include "MUSHclient.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
static CStringArray FirstNames;
static CStringArray MiddleNames;
static CStringArray LastNames;
bool bNamesRead = false;
// asterisk as the filename means forced reload
void ReadNames (const LPCTSTR sName, const bool bNoDialog)
{
bNamesRead = false;
CString strFileName = sName;
CString sBuf;
BOOL bOK;
FirstNames.RemoveAll ();
MiddleNames.RemoveAll ();
LastNames.RemoveAll ();
// if file name not specified, take one from last time
if (strFileName.IsEmpty () || strFileName == "*")
strFileName = App.m_strDefaultNameGenerationFile;
bool bFoundFile = false;
if (strcmp (sName, "*")) // don't check if forced reload wanted
{
try
{
CStdioFile f (strFileName, CFile::modeRead|CFile::shareDenyNone|CFile::typeText);
bFoundFile = true;
}
catch (CException* e)
{
e->Delete();
}
} // end of not wanting another file anyway
// if the file isn't there, and they don't want us to ignore that,
// ask the user where it is
if (!bFoundFile && !bNoDialog)
{
CFileDialog dlg(TRUE, // TRUE for FileOpen
"txt", // default extension
strFileName, // default file name
OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,
"Name files (*.txt;*.nam)|*.txt;*.nam|All files (*.*)|*.*||");
dlg.m_ofn.lpstrTitle = "Select names file";
ChangeToFileBrowsingDirectory ();
int nResult = dlg.DoModal();
ChangeToStartupDirectory ();
if (nResult != IDOK)
return;
strFileName = dlg.GetPathName();
// remember its location to stop annoying me every time
App.db_write_string ("prefs", "DefaultNameGenerationFile",
strFileName);
App.m_strDefaultNameGenerationFile = strFileName; // save in app memory
} // end of no file name supplied
CStdioFile f (strFileName, CFile::modeRead|CFile::shareDenyNone|CFile::typeText);
// look for [start]
while (bOK = f.ReadString (sBuf))
{
sBuf.TrimRight ();
sBuf.TrimLeft ();
if (sBuf.Left (2) == "/*")
continue; // ignore comments
if (sBuf.CompareNoCase ("[start]") == 0)
break;
if (sBuf.CompareNoCase ("[startstav]") == 0)
break;
}
if (!bOK)
ThrowErrorException ("No [start] in names file");
// look for [middle]
while (bOK = f.ReadString (sBuf))
{
sBuf.TrimRight ();
sBuf.TrimLeft ();
if (sBuf.Left (2) == "/*")
continue; // ignore comments
if (sBuf.CompareNoCase ("[middle]") == 0)
break;
else if (sBuf.CompareNoCase ("[mittstav]") == 0)
break;
else
FirstNames.Add (sBuf);
}
if (!bOK)
ThrowErrorException ("No [middle] in names file");
// look for [end]
while (bOK = f.ReadString (sBuf))
{
sBuf.TrimRight ();
sBuf.TrimLeft ();
if (sBuf.Left (2) == "/*")
continue; // ignore comments
if (sBuf.CompareNoCase ("[end]") == 0)
break;
else if (sBuf.CompareNoCase ("[slutstav]") == 0)
break;
else
MiddleNames.Add (sBuf);
}
if (!bOK)
ThrowErrorException ("No [end] in names file");
// look for [stop]
while (bOK = f.ReadString (sBuf))
{
sBuf.TrimRight ();
sBuf.TrimLeft ();
if (sBuf.Left (2) == "/*")
continue; // ignore comments
if (sBuf.CompareNoCase ("[stop]") == 0)
break;
else
LastNames.Add (sBuf);
}
if (!bOK)
ThrowErrorException ("No [stop] in names file");
bNamesRead = true;
} // end of ReadNames
CString GenerateName (void)
{
CString strName;
try
{
if (!bNamesRead)
ReadNames ("");
}
catch (CException* e)
{
e->ReportError();
e->Delete();
}
if (bNamesRead)
{
strName = FirstNames [rand () % FirstNames.GetSize ()];
strName += MiddleNames [rand () % MiddleNames.GetSize ()];
strName += LastNames [rand () % LastNames.GetSize ()];
}
return strName;
} // end of GenerateName