aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/jacl/encapsulate.cpp
blob: 8daba293d2883025da8d947bf00c022024334563 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/jacl/jacl.h"
#include "glk/jacl/types.h"
#include "glk/jacl/prototypes.h"

namespace Glk {
namespace JACL {

extern struct synonym_type *synonym_table;
extern struct filter_type *filter_table;

char            text_buffer[1024];

/* THIS IS A STRING CONSTANT TO POINT TO WHENEVER A COMMA IS
 * USED IN THE PLAYER'S INPUT */
const char     *comma = "comma\0";
const char     *then = "then\0";
const char     *word[MAX_WORDS];
int             quoted[MAX_WORDS];
int             percented[MAX_WORDS];
int             wp;

void
encapsulate() {
	int             index,
	                length;
	int             position = 0;
	int             new_word = TRUE;

	length = strlen(text_buffer);

	/* QUOTED IS USED TO STORE WHETHER EACH WORD WAS ENCLOSED IN QUOTES
	 * IN THE PLAYERS COMMAND - RESET EACH WORD TO NO */
	for (index = 0; index < MAX_WORDS; index++) {
		quoted[index] = 0;
		percented[index] = 0;
	}

	for (index = 0; index < length; index++) {

		switch (text_buffer[index]) {
		case ':':
		case '\t':
		case ' ':
		case ',':
			text_buffer[index] = 0;
			new_word = TRUE;
			break;
		case ';':
		case '#':
		case '\r':
		case '\n':
			/* TERMINATE THE WHOLE COMMAND ON HITTING A NEWLINE CHARACTER, A
			 * SEMICOLON OR A HASH */
			text_buffer[index] = 0;
			index = length;
			break;
		case '"':
			index++;
			/* NEED TO REMEMBER THAT THIS WORD WAS ENCLOSED IN QUOTES FOR
			 * THE COMMAND 'write'*/
			quoted[position] = 1;

			word[position] = &text_buffer[index];

			if (position < MAX_WORDS)
				position++;

			/* IF A WORD IS ENCLOSED IN QUOTES, KEEP GOING UNTIL THE END
			 * OF THE LINE OR A CLOSING QUOTE IS FOUND, NOT BREAKING AT
			 * WHITESPACE AS USUAL */
			for (; index < length; index++) {
				if (text_buffer[index] == '"') {
					text_buffer[index] = 0;
					new_word = TRUE;
					break;
				} else if (text_buffer[index] == '\r' || text_buffer[index] == '\n') {
					text_buffer[index] = 0;
					index = length;
					break;
				}
			}
			break;
		default:
			if (new_word) {
				if (text_buffer[index] == '%' && text_buffer[index + 1] != ' ' && text_buffer[index + 1] != '\t') {
					percented[position]++;
					break;
				}
				word[position] = &text_buffer[index];
				new_word = FALSE;
				if (position < MAX_WORDS)
					position++;
			}
			break;
		}

	}

	/* NULL OUT ALL THE WORD POINTERS BEYOND THE LAST WORD */
	for (index = position; index < MAX_WORDS; index++)
		word[index] = NULL;

	wp = 0;
}

// THIS VERSION OF ENCAPSULATE DOESN'T LOOK FOR CERTAIN SPECIAL CHARACTERS
void
command_encapsulate() {
	int             index,
	                length;
	int             position = 0;
	int             new_word = TRUE;

	length = strlen(text_buffer);

	// QUOTED IS USED TO STORE WHETHER EACH WORD WAS ENCLOSED IN QUOTES
	// IN THE PLAYERS COMMAND - RESET EACH WORD TO NO
	for (index = 0; index < MAX_WORDS; index++) {
		quoted[index] = 0;
	}

	for (index = 0; index < length; index++) {

		// REDUSE EVERYTHING TO LOWER CASE EXCEPT TEXT ENCLOSED IN QUOTES
		text_buffer[index] = tolower((int) text_buffer[index]);

		switch (text_buffer[index]) {
		case ':':
		case '\t':
		case ' ':
			text_buffer[index] = 0;
			new_word = TRUE;
			break;
		case ',':
			text_buffer[index] = 0;
			// SET THIS WORD TO POINT TO A STRING CONSTANT OF 'comma' AS THE
			// COMMA ITSELF WILL BE NULLED OUT TO TERMINATE THE PRECEEDING
			// WORD IN THE COMMAND.
			word[position] = comma;
			if (position < MAX_WORDS)
				position++;
			new_word = TRUE;
			break;
		case '.':
			text_buffer[index] = 0;
			// SET THIS WORD TO POINT TO A STRING CONSTANT OF 'comma' AS THE
			// COMMA ITSELF WILL BE NULLED OUT TO TERMINATE THE PRECEEDING
			// WORD IN THE COMMAND
			word[position] = then;
			if (position < MAX_WORDS)
				position++;
			new_word = TRUE;
			break;
		case ';':
		case '\r':
		case '\n':
			// TERMINATE THE WHOLE COMMAND ON HITTING A NEWLINE CHARACTER, A
			// SEMICOLON OR A HASH
			text_buffer[index] = 0;
			index = length;
			break;
		case '"':
			index++;
			// NEED TO REMEMBER THAT THIS WORD WAS ENCLOSED IN QUOTES FOR
			// THE COMMAND 'write'
			quoted[position] = 1;

			word[position] = &text_buffer[index];

			if (position < MAX_WORDS)
				position++;

			// IF A WORD IS ENCLOSED IN QUOTES, KEEP GOING UNTIL THE END
			// OF THE LINE OR A CLOSING QUOTE IS FOUND, NOT BREAKING AT
			// WHITESPACE AS USUAL
			for (; index < length; index++) {
				if (text_buffer[index] == '"') {
					text_buffer[index] = 0;
					new_word = TRUE;
					break;
				} else if (text_buffer[index] == '\r' || text_buffer[index] == '\n') {
					text_buffer[index] = 0;
					index = length;
					break;
				}
			}
			break;
		default:
			if (new_word) {
				word[position] = &text_buffer[index];
				new_word = FALSE;
				if (position < MAX_WORDS)
					position++;
			}
			break;
		}

	}

	// NULL OUT ALL THE WORD POINTERS BEYOND THE LAST WORD
	for (index = position; index < MAX_WORDS; index++) {
		word[index] = NULL;
	}

	wp = 0;
}

void
jacl_truncate() {
	int             index,
	                counter,
	                match;
	int             position = 0;

	struct synonym_type *synonym;
	struct filter_type *filter = filter_table;

	// REMOVE ALL THE DEFINED 'filter's FROM THE PLAYER'S COMMAND
	if (filter != NULL) {
		while (word[position] != NULL) {
			match = FALSE;
			do {
				if (!strcmp(word[position], filter->word)) {
					for (index = position; word[index + 1] != NULL;
					        index++)
						word[index] = word[index + 1];
					word[index] = NULL;
					match = TRUE;
				}
				filter = filter->next_filter;
			} while (filter != NULL && !match);
			filter = filter_table;
			if (!match)
				position++;
		};
	}

	// SUBTITUTE ALL THE DEFINED 'synonym's IN THE PLAYER'S COMMAND
	if (synonym_table != NULL) {
		for (counter = 0; word[counter] != NULL; counter++) {
			synonym = synonym_table;
			do {
				if (!strcmp(word[counter], synonym->original)) {
					word[counter] = synonym->standard;
					break;
				}
				if (synonym->next_synonym != NULL)
					synonym = synonym->next_synonym;
				else
					break;
			} while (TRUE);
		}
	}
}

} // End of namespace JACL
} // End of namespace Glk