Skip to content

Commit 93d7729

Browse files
committed
Convert SewerRat.cpp to new Message system
1 parent ac6176f commit 93d7729

File tree

1 file changed

+8
-185
lines changed

1 file changed

+8
-185
lines changed

scripts/SewerRat.cpp

Lines changed: 8 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
// Filename: SewerRat.cpp
2323
// Version: 3
2424
/////////////////////////////////////////////////////////////////////
25-
#include <stdio.h>
26-
#include <stdlib.h>
27-
#include <string.h>
28-
#include <ctype.h>
25+
#include <cstring>
26+
#include <map>
27+
#include <string>
28+
2929
#include "osiris_import.h"
3030
#include "osiris_common.h"
3131
#include "DallasFuncs.cpp"
@@ -118,180 +118,12 @@ void RestoreGlobalActionCtrs(void *file_ptr) {
118118
// Message File Data
119119
// =================
120120

121-
#define MAX_SCRIPT_MESSAGES 256
122-
#define MAX_MSG_FILEBUF_LEN 1024
123-
#define NO_MESSAGE_STRING "*Message Not Found*"
124-
#define INV_MSGNAME_STRING "*Message Name Invalid*"
125-
#define WHITESPACE_CHARS " \t\r\n"
126-
127-
// Structure for storing a script message
128-
struct tScriptMessage {
129-
char *name; // the name of the message
130-
char *message; // the actual message text
131-
};
132-
133121
// Global storage for level script messages
134-
tScriptMessage *message_list[MAX_SCRIPT_MESSAGES];
135-
int num_messages;
136-
137-
// ======================
138-
// Message File Functions
139-
// ======================
140-
141-
// Initializes the Message List
142-
void InitMessageList(void) {
143-
for (int j = 0; j < MAX_SCRIPT_MESSAGES; j++)
144-
message_list[j] = NULL;
145-
num_messages = 0;
146-
}
147-
148-
// Clear the Message List
149-
void ClearMessageList(void) {
150-
for (int j = 0; j < num_messages; j++) {
151-
free(message_list[j]->name);
152-
free(message_list[j]->message);
153-
free(message_list[j]);
154-
message_list[j] = NULL;
155-
}
156-
num_messages = 0;
157-
}
158-
159-
// Adds a message to the list
160-
int AddMessageToList(char *name, char *msg) {
161-
int pos;
162-
163-
// Make sure there is room in the list
164-
if (num_messages >= MAX_SCRIPT_MESSAGES)
165-
return false;
166-
167-
// Allocate memory for this message entry
168-
pos = num_messages;
169-
message_list[pos] = (tScriptMessage *)malloc(sizeof(tScriptMessage));
170-
if (message_list[pos] == NULL)
171-
return false;
172-
173-
// Allocate memory for the message name
174-
message_list[pos]->name = (char *)malloc(strlen(name) + 1);
175-
if (message_list[pos]->name == NULL) {
176-
free(message_list[pos]);
177-
return false;
178-
}
179-
strcpy(message_list[pos]->name, name);
180-
181-
// Allocate memory for the message name
182-
message_list[pos]->message = (char *)malloc(strlen(msg) + 1);
183-
if (message_list[pos]->message == NULL) {
184-
free(message_list[pos]->name);
185-
free(message_list[pos]);
186-
return false;
187-
}
188-
strcpy(message_list[pos]->message, msg);
189-
num_messages++;
190-
191-
return true;
192-
}
193-
194-
// Removes any whitespace padding from the end of a string
195-
void RemoveTrailingWhitespace(char *s) {
196-
int last_char_pos;
197-
198-
last_char_pos = strlen(s) - 1;
199-
while (last_char_pos >= 0 && isspace(s[last_char_pos])) {
200-
s[last_char_pos] = '\0';
201-
last_char_pos--;
202-
}
203-
}
204-
205-
// Returns a pointer to the first non-whitespace char in given string
206-
char *SkipInitialWhitespace(char *s) {
207-
while ((*s) != '\0' && isspace(*s))
208-
s++;
209-
210-
return (s);
211-
}
122+
std::map<std::string, std::string> Messages;
212123

213-
// Read in the Messages
214-
int ReadMessageFile(const char *filename) {
215-
void *infile;
216-
char filebuffer[MAX_MSG_FILEBUF_LEN + 1];
217-
char *line, *msg_start;
218-
int line_num;
219-
bool next_msgid_found;
220-
221-
// Try to open the file for loading
222-
infile = File_Open(filename, "rt");
223-
if (!infile)
224-
return false;
225-
226-
line_num = 0;
227-
next_msgid_found = true;
228-
229-
// Clear the message list
230-
ClearMessageList();
231-
232-
// Read in and parse each line of the file
233-
while (!File_eof(infile)) {
234-
235-
// Clear the buffer
236-
strcpy(filebuffer, "");
237-
238-
// Read in a line from the file
239-
File_ReadString(filebuffer, MAX_MSG_FILEBUF_LEN, infile);
240-
line_num++;
241-
242-
// Remove whitespace padding at start and end of line
243-
RemoveTrailingWhitespace(filebuffer);
244-
line = SkipInitialWhitespace(filebuffer);
245-
246-
// If line is a comment, or empty, discard it
247-
if (strlen(line) == 0 || strncmp(line, "//", 2) == 0)
248-
continue;
249-
250-
if (!next_msgid_found) { // Parse out the last message ID number
251-
252-
// Grab the first keyword, make sure it's valid
253-
line = strtok(line, WHITESPACE_CHARS);
254-
if (line == NULL)
255-
continue;
256-
257-
// Grab the second keyword, and assign it as the next message ID
258-
line = strtok(NULL, WHITESPACE_CHARS);
259-
if (line == NULL)
260-
continue;
261-
262-
next_msgid_found = true;
263-
} else { // Parse line as a message line
264-
265-
// Find the start of message, and mark it
266-
msg_start = strchr(line, '=');
267-
if (msg_start == NULL)
268-
continue;
269-
msg_start[0] = '\0';
270-
msg_start++;
271-
272-
// Add the message to the list
273-
AddMessageToList(line, msg_start);
274-
}
275-
}
276-
File_Close(infile);
277-
278-
return true;
279-
}
280-
281-
// Find a message
282-
const char *GetMessage(const char *name) {
283-
// Make sure given name is valid
284-
if (name == NULL)
285-
return INV_MSGNAME_STRING;
286-
287-
// Search message list for name
288-
for (int j = 0; j < num_messages; j++)
289-
if (strcmp(message_list[j]->name, name) == 0)
290-
return (message_list[j]->message);
291-
292-
// Couldn't find it
293-
return NO_MESSAGE_STRING;
294-
}
124+
#define TXT(MSG) GetMessageNew(MSG, Messages)
125+
#define ReadMessageFile(filename) CreateMessageMap(filename, Messages)
126+
#define ClearMessageList() DestroyMessageMap(Messages)
295127

296128
//======================
297129
// Name List Arrays
@@ -338,10 +170,6 @@ int Matcen_indexes[NUM_MATCEN_NAMES];
338170
const char **Goal_names = NULL;
339171
int *Goal_indexes = NULL;
340172

341-
#define NUM_MESSAGE_NAMES 0
342-
const char **Message_names = NULL;
343-
const char **Message_strings = NULL;
344-
345173
// ===============
346174
// InitializeDLL()
347175
// ===============
@@ -355,7 +183,6 @@ char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) {
355183

356184
ClearGlobalActionCtrs();
357185
dfInit();
358-
InitMessageList();
359186

360187
// Build the filename of the message file
361188
char filename[_MAX_PATH + 32];
@@ -412,10 +239,6 @@ char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) {
412239
for (j = 0; j < NUM_GOAL_NAMES; j++)
413240
Goal_indexes[j] = Scrpt_FindLevelGoalName(Goal_names[j]);
414241

415-
// Do Message Name lookups
416-
for (j = 0; j < NUM_MESSAGE_NAMES; j++)
417-
Message_strings[j] = GetMessage(Message_names[j]);
418-
419242
return 1;
420243
}
421244

0 commit comments

Comments
 (0)