Debian Codegraph @

terps/jacl/utils.c

raw symbols SCIP
1 /* utils.c --- General utility functions
2 * (C) 2008 Stuart Allen, distribute and use
3 * according to GNU GPL, see file COPYING for details.
4 */
5
6
7 #include "jacl.h"
8 #include "language.h"
9 #include "types.h"
10 #include "prototypes.h"
11 #include <string.h>
12
13
14 static int jacl_whitespace(int character);
15
16 void
17 eachturn()
18 {
19 /* INCREMENT THE TOTAL NUMBER OF MOVES MADE AND CALL THE 'EACHTURN'
20 * FUNCTION FOR THE CURRENT LOCATION AND THE GLOBAL 'EACHTURN'
21 * FUNCTION. THESE FUNCTIONS CONTAIN ANY CODE THAT SIMULATED EVENTS
22 * OCCURING DUE TO THE PASSING OF TIME */
23 TOTAL_MOVES->value++;
24 execute("+eachturn");
25 strcpy(function_name, "eachturn_");
26 strcat(function_name, object[HERE]->label);
27 execute(function_name);
28 execute("+system_eachturn");
29
30 /* SET TIME TO FALSE SO THAT NO MORE eachturn FUNCTIONS ARE EXECUTED
31 * UNTIL EITHER THE COMMAND PROMPT IS RETURNED TO (AND TIME IS SET
32 * TO TRUE AGAIN, OR IT IS MANUALLY SET TO TRUE BY A VERB THAT CALLS
33 * MORE THAN ONE proxy COMMAND. THIS IS BECAUSE OTHERWISE A VERB THAT
34 * USES A proxy COMMAND TO TRANSLATE THE VERB IS RESULT IN TWO MOVES
35 * (OR MORE) HAVING PASSED FOR THE ONE ACTION. */
36 TIME->value = FALSE;
37 }
38
39 int
40 get_here()
41 {
42 /* THIS FUNCTION RETURNS THE VALUE OF 'here' IN A SAFE, ERROR CHECKED
43 * WAY */
44 if (player < 1 || player > objects) {
45 badplrrun(player);
46 terminate(44);
47 } else if (object[player]->PARENT < 1 || object[player]->PARENT> objects || object[player]->PARENT== player) {
48 badparrun();
49 terminate(44);
50 } else {
51 return (object[player]->PARENT);
52 }
53
54 /* SHOULDN'T GET HERE, JUST TRYING TO KEEP VisualC++ HAPPY */
55 return 1;
56 }
57
58 char *
59 strip_return (char *string)
60 {
61 /* STRIP ANY TRAILING RETURN OR NEWLINE OFF THE END OF A STRING */
62 int index;
63 int length = strlen(string);
64
65 for (index = 0; index < length; index++) {
66 switch (string[index]) {
67 case '\r':
68 case '\n':
69 string[index] = 0;
70 break;
71 }
72 }
73
74 return string;
75 }
76
77 int
78 random_number()
79 {
80 /* GENERATE A RANDOM NUMBER BETWEEN 0 AND THE CURRENT VALUE OF
81 * THE JACL VARIABLE MAX_RAND */
82
83 rand();
84 return (1 + (int) ((float) MAX_RAND->value * rand() / (RAND_MAX + 1.0)));
85 }
86
87 void
88 create_paths(char *full_path)
89 {
90 int index;
91 char *last_slash;
92
93 /* SAVE A COPY OF THE SUPPLIED GAMEFILE NAME */
94 strcpy(game_file, full_path);
95
96 /* FIND THE LAST SLASH IN THE SPECIFIED GAME PATH AND REMOVE THE GAME
97 * FILE SUFFIX IF ANY EXISTS */
98 last_slash = (char *) NULL;
99
100 /* GET A POINTER TO THE LAST SLASH IN THE FULL PATH */
101 last_slash = strrchr(full_path, DIR_SEPARATOR);
102
103 for (index = strlen(full_path); index >= 0; index--) {
104 if (full_path[index] == DIR_SEPARATOR){
105 /* NO '.' WAS FOUND BEFORE THE LAST SLASH WAS REACHED,
106 * THERE IS NO FILE EXTENSION */
107 break;
108 } else if (full_path[index] == '.') {
109 full_path[index] = 0;
110 break;
111 }
112 }
113
114 /* STORE THE GAME PATH AND THE GAME FILENAME PARTS SEPARATELY */
115 if (last_slash == (char *) NULL) {
116 /* GAME MUST BE IN CURRENT DIRECTORY SO THERE WILL BE NO GAME PATH */
117 strcpy(prefix, full_path);
118 game_path[0] = 0;
119
120 /* THIS ADDITION OF ./ TO THE FRONT OF THE GAMEFILE IF IT IS IN THE
121 * CURRENT DIRECTORY IS REQUIRED TO KEEP Gargoyle HAPPY. */
122 #ifdef __NDS__
123 sprintf (temp_buffer, "%c%s", DIR_SEPARATOR, game_file);
124 #else
125 sprintf (temp_buffer, ".%c%s", DIR_SEPARATOR, game_file);
126 #endif
127 strcpy (game_file, temp_buffer);
128 } else {
129 /* STORE THE DIRECTORY THE GAME FILE IS IN WITH THE TRAILING
130 * SLASH IF THERE IS ONE */
131 last_slash++;
132 strcpy(prefix, last_slash);
133 *last_slash = '\0';
134 strcpy(game_path, full_path);
135 }
136
137 #ifdef GLK
138 /* SET DEFAULT WALKTHRU FILE NAME */
139 sprintf(walkthru, "%s.walkthru", prefix);
140
141 /* SET DEFAULT SAVED GAME FILE NAME */
142 sprintf(bookmark, "%s.bookmark", prefix);
143
144 /* SET DEFAULT BLORB FILE NAME */
145 #ifdef GARGLK
146 // Gargoyle uses glkunix_stream_open_pathname to open this file, but
147 // it's not (necessarily) going to be in the current working directory,
148 // so provide the full path.
149 sprintf(blorb, "%s/%s.blorb", game_path, prefix);
150 #else
151 sprintf(blorb, "%s.blorb", prefix);
152 #endif
153 #endif
154
155 /* SET DEFAULT FILE LOCATIONS IF NOT SET BY THE USER IN CONFIG */
156 if (include_directory[0] == 0) {
157 strcpy(include_directory, game_path);
158 strcat(include_directory, INCLUDE_DIR);
159 }
160
161 if (temp_directory[0] == 0) {
162 strcpy(temp_directory, game_path);
163 strcat(temp_directory, TEMP_DIR);
164 }
165
166 if (data_directory[0] == 0) {
167 strcpy(data_directory, game_path);
168 strcat(data_directory, DATA_DIR);
169 }
170 }
171
172 int
173 jacl_whitespace(int character)
174 {
175 /* CHECK IF A CHARACTER IS CONSIDERED WHITE SPACE IN THE JACL LANGUAGE */
176 switch (character) {
177 case ':':
178 case '\t':
179 case ' ':
180 return(TRUE);
181 default:
182 return(FALSE);
183 }
184 }
185
186 char *
187 stripwhite (char *string)
188 {
189 int i;
190
191 /* STRIP WHITESPACE FROM THE START AND END OF STRING. */
192 while (jacl_whitespace (*string)) string++;
193
194 i = strlen (string) - 1;
195
196 while (i >= 0 && ((jacl_whitespace (*(string+ i))) || *(string + i) == '\n' || *(string + i) == '\r')) i--;
197
198 #ifdef _WIN32
199 i++;
200 *(string + i) = '\r';
201 #endif
202 i++;
203 *(string + i) = '\n';
204 i++;
205 *(string + i) = '\0';
206
207 return string;
208 }
209
210 void
211 jacl_encrypt(char *string)
212 {
213 int index, length;
214
215 length = strlen(string);
216
217 for (index = 0; index < length; index++) {
218 if (string[index] == '\n' || string[index] == '\r') {
219 return;
220 }
221 string[index] = string[index] ^ 255;
222 }
223 }
224
225 void
226 jacl_decrypt(char *string)
227 {
228 int index, length;
229
230 length = strlen(string);
231
232 for (index = 0; index < length; index++) {
233 if (string[index] == '\n' || string[index] == '\r') {
234 return;
235 }
236 string[index] = string[index] ^ 255;
237 }
238 }
239