HuntnGather – Diff between revs 38 and 39

Subversion Repositories:
Rev:
Only display areas with differencesRegard whitespace
Rev 38 Rev 39
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // 2 // Copyright (C) 2021 Wizardry and Steamworks - License: MIT //
3 /////////////////////////////////////////////////////////////////////////// 3 ///////////////////////////////////////////////////////////////////////////
4   4  
5 #include <stdio.h> 5 #include <stdio.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <string.h> 7 #include <string.h>
8 #include <ctype.h> 8 #include <ctype.h>
9 #if !defined ___AmigaOS___ 9 #if !defined ___AmigaOS___
10 #include <dirent.h> 10 #include <dirent.h>
11 #include <sys/stat.h> 11 #include <sys/stat.h>
12 #include <signal.h> 12 #include <signal.h>
13 #endif 13 #endif
14   14  
15 #include <sys/types.h> 15 #include <sys/types.h>
16 #include <sys/syslimits.h> 16 #include <sys/syslimits.h>
17   17  
18 #if defined ___AmigaOS___ 18 #if defined ___AmigaOS___
19 #include <proto/dos.h> 19 #include <proto/dos.h>
20 #include <proto/exec.h> 20 #include <proto/exec.h>
21 #include <proto/locale.h> 21 #include <proto/locale.h>
22 #include <clib/utility_protos.h> 22 #include <clib/utility_protos.h>
23 #endif 23 #endif
24   24  
25 #if defined ___AsyncIO___ 25 #if defined ___AsyncIO___
26 #include <asyncio.h> 26 #include <asyncio.h>
27 #endif 27 #endif
28   28  
29 #include "/shared/utilities.h" 29 #include "/shared/utilities.h"
30   30  
31 /* 31 /*
32 * 32 *
33 * Returns largest of strings. 33 * Returns largest of strings.
34 */ 34 */
35 #if defined ___AmigaOS___ 35 #if defined ___AmigaOS___
36 LONG StringLenMax(char *a, char *b) { 36 LONG StringLenMax(char *a, char *b) {
37 LONG p = strlen(a); 37 LONG p = strlen(a);
38 LONG q = strlen(b); 38 LONG q = strlen(b);
39 #else 39 #else
40 int StrlenMax(char *a, char *b) { 40 int StrlenMax(char *a, char *b) {
41 int q = strlen(a); 41 int q = strlen(a);
42 int p = strlen(b); 42 int p = strlen(b);
43 #endif 43 #endif
44 return p > q ? p : q; 44 return p > q ? p : q;
45 } 45 }
46   46  
47 /* 47 /*
48 * 48 *
49 * Returns largest of strings. 49 * Returns largest of strings.
50 */ 50 */
51 #if defined ___AmigaOS___ 51 #if defined ___AmigaOS___
52 LONG StringLenMin(char *a, char *b) { 52 LONG StringLenMin(char *a, char *b) {
53 LONG p = strlen(a); 53 LONG p = strlen(a);
54 LONG q = strlen(b); 54 LONG q = strlen(b);
55 #else 55 #else
56 int StrlenMin(char *a, char *b) { 56 int StrlenMin(char *a, char *b) {
57 int q = strlen(a); 57 int q = strlen(a);
58 int p = strlen(b); 58 int p = strlen(b);
59 #endif 59 #endif
60 return p > q ? q : p; 60 return p > q ? q : p;
61 } 61 }
62   62  
63 /* 63 /*
64 * 64 *
65 * Converts a string to case. 65 * Converts a string to case.
66 */ 66 */
67 void StrUpr(char *s) { 67 void StrUpr(char *s) {
68 while(*s != '\0') { 68 while(*s != '\0') {
69 #if defined ___AmigaOS___ 69 #if defined ___AmigaOS___
70 *s = ToUpper(*s); 70 *s = ToUpper(*s);
71 #else 71 #else
72 *s = toupper((unsigned char)*s); 72 *s = toupper((unsigned char)*s);
73 #endif 73 #endif
74 ++s; 74 ++s;
75 } 75 }
76 } 76 }
77   77  
78 /* 78 /*
79 * 79 *
80 * Gets the filesystem type of a file or directory. 80 * Gets the filesystem type of a file or directory.
81 */ 81 */
82 FS_TYPE GetFsType(char *path) { 82 FS_TYPE GetFsType(char *path) {
83 #if defined ___AmigaOS___ 83 #if defined ___AmigaOS___
84 struct FileInfoBlock *FIB; 84 struct FileInfoBlock *FIB;
85 BPTR lock; 85 BPTR lock;
86 #else 86 #else
87 struct stat dirStat; 87 struct stat dirStat;
88 #endif 88 #endif
89   89  
90 #if defined ___AmigaOS___ 90 #if defined ___AmigaOS___
91 if((lock = Lock(path, ACCESS_READ)) == NULL) { 91 if((lock = Lock(path, ACCESS_READ)) == NULL) {
92 fprintf(stderr, "Lock failed for path '%s'.\n", path); 92 fprintf(stderr, "Lock failed for path '%s'.\n", path);
93 return UNKNOWN; 93 return UNKNOWN;
94 } 94 }
95   95  
96 if((FIB = AllocDosObject(DOS_FIB, NULL)) == NULL) { 96 if((FIB = AllocDosObject(DOS_FIB, NULL)) == NULL) {
97 fprintf(stderr, "Could not allocated file information block for path '%s'.\n", path); 97 fprintf(stderr, "Could not allocated file information block for path '%s'.\n", path);
98 UnLock(lock); 98 UnLock(lock);
99 return UNKNOWN; 99 return UNKNOWN;
100 } 100 }
101   101  
102 if(Examine(lock, FIB) == FALSE) { 102 if(Examine(lock, FIB) == FALSE) {
103 fprintf(stderr, "Path '%s' could not be examined.\n", path); 103 fprintf(stderr, "Path '%s' could not be examined.\n", path);
104 UnLock(lock); 104 UnLock(lock);
105 FreeDosObject(DOS_FIB, FIB); 105 FreeDosObject(DOS_FIB, FIB);
106 FIB = NULL; 106 FIB = NULL;
107 return UNKNOWN; 107 return UNKNOWN;
108 } 108 }
109   109  
110 if(FIB->fib_DirEntryType < 0) { 110 if(FIB->fib_DirEntryType < 0) {
111 UnLock(lock); 111 UnLock(lock);
112 FreeDosObject(DOS_FIB, FIB); 112 FreeDosObject(DOS_FIB, FIB);
113 FIB = NULL; 113 FIB = NULL;
114 return REGULAR; 114 return REGULAR;
115 #else 115 #else
116 stat(path, &dirStat); 116 stat(path, &dirStat);
117 if(!S_ISDIR(dirStat.st_mode)) { 117 if(!S_ISDIR(dirStat.st_mode)) {
118 return REGULAR; 118 return REGULAR;
119 #endif 119 #endif
120 } 120 }
121   121  
122 #if defined ___AmigaOS___ 122 #if defined ___AmigaOS___
123 UnLock(lock); 123 UnLock(lock);
124 FreeDosObject(DOS_FIB, FIB); 124 FreeDosObject(DOS_FIB, FIB);
125 FIB = NULL; 125 FIB = NULL;
126 #endif 126 #endif
127   127  
128 return DIRECTORY; 128 return DIRECTORY;
129 } 129 }
130   130  
131 /* 131 /*
132 * 132 *
133 * Counts the lines of a file. 133 * Counts the lines of a file.
134 */ 134 */
135 int CountFileLines(char *dbFile) { 135 int CountFileLines(char *dbFile) {
136 #if defined ___AsyncIO___ 136 #if defined ___AsyncIO___
137 struct AsyncFile *fp; 137 struct AsyncFile *fp;
138 LONG c; 138 LONG c;
139 #else 139 #else
140 FILE *fp; 140 FILE *fp;
141 char c; 141 char c;
142 #endif 142 #endif
143 int lines; 143 int lines;
144   144  
145 #if defined ___AsyncIO___ 145 #if defined ___AsyncIO___
146 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { 146 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
147 #else 147 #else
148 if((fp = fopen(dbFile, "r")) == NULL) { 148 if((fp = fopen(dbFile, "r")) == NULL) {
149 #endif 149 #endif
150 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); 150 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
151 return -1; 151 return -1;
152 } 152 }
153   153  
154 lines = 0; 154 lines = 0;
155 if(PROGRAM_VERBOSE) { 155 if(PROGRAM_VERBOSE) {
156 fprintf(stdout, "Counting lines in '%s'...\r", dbFile); 156 fprintf(stdout, "Counting lines in '%s'...\r", dbFile);
157 } 157 }
158   158  
159 #if defined ___AsyncIO___ 159 #if defined ___AsyncIO___
160 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) { 160 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
161 #else 161 #else
162 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) { 162 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) {
163 #endif 163 #endif
164 #if defined ___AmigaOS___ 164 #if defined ___AmigaOS___
165 // Check if CTRL+C was pressed and abort the program. 165 // Check if CTRL+C was pressed and abort the program.
166 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 166 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
167 PROGRAM_RUN = FALSE; 167 PROGRAM_RUN = FALSE;
168 continue; 168 continue;
169 } 169 }
170 #endif 170 #endif
171 switch(c) { 171 switch(c) {
172 case '\n': 172 case '\n':
173 ++lines; 173 ++lines;
174   174  
175 if(PROGRAM_VERBOSE) { 175 if(PROGRAM_VERBOSE) {
176 fprintf(stdout, "Counting lines in '%s': %d.\r", dbFile, lines); 176 fprintf(stdout, "Counting lines in '%s': %d.\r", dbFile, lines);
177 } 177 }
178 break; 178 break;
179 } 179 }
180 } 180 }
181   181  
182 #if defined ___AsyncIO___ 182 #if defined ___AsyncIO___
183 CloseAsync(fp); 183 CloseAsync(fp);
184 #else 184 #else
185 fclose(fp); 185 fclose(fp);
186 #endif 186 #endif
187   187  
188 if(PROGRAM_VERBOSE) { 188 if(PROGRAM_VERBOSE) {
189 fprintf(stdout, "\n"); 189 fprintf(stdout, "\n");
190 } 190 }
191   191  
192 return lines; 192 return lines;
193 } 193 }
194   194  
195 /* 195 /*
196 * 196 *
197 * Gets the absolute path to file by name. 197 * Gets the absolute path to file by name.
198 */ 198 */
199 char *PathToAbsolute(char *path) { 199 char *PathToAbsolute(char *path) {
200 char *abs; 200 char *abs;
201 #if defined ___AmigaOS___ 201 #if defined ___AmigaOS___
202 BPTR lock; 202 BPTR lock;
203 #endif 203 #endif
204   204  
205 #if defined ___AmigaOS___ 205 #if defined ___AmigaOS___
206 if((abs = malloc(PATH_MAX * sizeof(*abs))) == NULL) { 206 if((abs = malloc(PATH_MAX * sizeof(*abs))) == NULL) {
207 fprintf(stderr, "Memory allocation failure.\n"); 207 fprintf(stderr, "Memory allocation failure.\n");
208 return NULL; 208 return NULL;
209 } 209 }
210 if((lock = Lock(path, SHARED_LOCK)) == 0) { 210 if((lock = Lock(path, SHARED_LOCK)) == 0) {
211 fprintf(stderr, "Lock failed for path '%s'.\n", path); 211 fprintf(stderr, "Lock failed for path '%s'.\n", path);
212 return NULL; 212 return NULL;
213 } 213 }
214 if(NameFromLock(lock, abs, PATH_MAX) == FALSE) { 214 if(NameFromLock(lock, abs, PATH_MAX) == FALSE) {
215 fprintf(stderr, "Full path for '%s' could not be retrieved.\n", path); 215 fprintf(stderr, "Full path for '%s' could not be retrieved.\n", path);
216 UnLock(lock); 216 UnLock(lock);
217 return NULL; 217 return NULL;
218 } 218 }
219 UnLock(lock); 219 UnLock(lock);
220 #else 220 #else
221 /* 221 /*
222 * On POSIX this should be: 222 * On POSIX this should be:
223 * abs = realpath(path, NULL); 223 * abs = realpath(path, NULL);
224 * 224 *
225 */ 225 */
226 if((abs = malloc((strlen(path) + 1) * sizeof(*abs))) == NULL) { 226 if((abs = malloc((strlen(path) + 1) * sizeof(*abs))) == NULL) {
227 fprintf(stderr, "Memory allocation failure.\n"); 227 fprintf(stderr, "Memory allocation failure.\n");
228 return NULL; 228 return NULL;
229 } 229 }
230 sprintf(abs, "%s", path); 230 sprintf(abs, "%s", path);
231 #endif 231 #endif
232   232  
233 return abs; 233 return abs;
234 } 234 }
235   235  
236 /* 236 /*
237 * 237 *
238 * Compares path parts for equality. 238 * Compares path parts for equality.
239 */ 239 */
240 #if defined ___AmigaOS___ 240 #if defined ___AmigaOS___
241 BOOL PathCompare(char *path, char *look) { 241 BOOL PathCompare(char *path, char *look) {
242 #else 242 #else
243 int PathCompare(char *path, char *look) { 243 int PathCompare(char *path, char *look) {
244 #endif 244 #endif
245 char *a; 245 char *a;
246 char *b; 246 char *b;
247   247  
248 for(a = path, b = look; *a != '\0' && *b != '\0'; ++a, ++b) { 248 for(a = path, b = look; *a != '\0' && *b != '\0'; ++a, ++b) {
249 if(*b != '\0' && *a != *b) { 249 if(*b != '\0' && *a != *b) {
250 return FALSE; 250 return FALSE;
251 } 251 }
252 } 252 }
253   253  
254 return *b == '\0'; 254 return *b == '\0';
255 } 255 }
256   256  
257 /* 257 /*
258 * 258 *
259 * Creates a temporary file and returns its name. 259 * Creates a temporary file and returns its name.
260 */ 260 */
261 char *CreateTemporaryFile(void) { 261 char *CreateTemporaryFile(void) {
262 char *name; 262 char *name;
263   263  
264 name = tmpnam(NULL); 264 name = tmpnam(NULL);
265   265  
266 return name; 266 return name;
267 } 267 }
268   268  
269 /* 269 /*
270 * 270 *
271 * Create multiple temporary files and return their names. 271 * Create multiple temporary files and return their names.
272 */ 272 */
273 VECTOR *CreateTemporaryFiles(int files) { 273 VECTOR *CreateTemporaryFiles(int files) {
274 VECTOR *tmpNames; 274 VECTOR *tmpNames;
-   275 int total;
275   276  
276 if((tmpNames = malloc(1 * sizeof(*tmpNames))) == NULL) { 277 if((tmpNames = malloc(1 * sizeof(*tmpNames))) == NULL) {
277 fprintf(stderr, "Memory allocation failure.\n"); 278 fprintf(stderr, "Memory allocation failure.\n");
278 return NULL; 279 return NULL;
279 } 280 }
280   281  
281 if((tmpNames->array = malloc(files * sizeof(*tmpNames->array))) == NULL) { 282 if((tmpNames->array = malloc(files * sizeof(*tmpNames->array))) == NULL) {
282 fprintf(stderr, "Memory allocation failure.\n"); 283 fprintf(stderr, "Memory allocation failure.\n");
283 return NULL; 284 return NULL;
284 } 285 }
285   286  
286 if(PROGRAM_VERBOSE) { 287 if(PROGRAM_VERBOSE) {
287 fprintf(stdout, "Creating temporary files...\r"); 288 fprintf(stdout, "Creating temporary files...\r");
288 } 289 }
289   290  
290 tmpNames->length = 0; 291 tmpNames->length = 0;
-   292 total = files;
291 while(PROGRAM_RUN && --files > -1) { 293 while(PROGRAM_RUN && --files > -1) {
292 #if defined ___AmigaOS___ 294 #if defined ___AmigaOS___
293 // Check if CTRL+C was pressed and abort the program. 295 // Check if CTRL+C was pressed and abort the program.
294 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 296 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
295 PROGRAM_RUN = FALSE; 297 PROGRAM_RUN = FALSE;
296 continue; 298 continue;
297 } 299 }
298 #endif 300 #endif
299 tmpNames[files] = CreateTemporaryFile(); 301 tmpNames->array[files] = CreateTemporaryFile();
300 ++tmpNames->length; 302 ++tmpNames->length;
301   303  
302 if(PROGRAM_VERBOSE) { 304 if(PROGRAM_VERBOSE) {
303 fprintf(stdout, "Creating temporary files: %d%%.\r", 100 - (int)(((float)count / files) * 100.0)); 305 fprintf(stdout, "Creating temporary files: %d%%.\r", (int)(((float)tmpNames->length / total) * 100.0));
304 } 306 }
305 } 307 }
306   308  
307 if(PROGRAM_VERBOSE) { 309 if(PROGRAM_VERBOSE) {
308 fprintf(stdout, "\n"); 310 fprintf(stdout, "\n");
309 } 311 }
310   312  
311 return tmpNames; 313 return tmpNames;
312 } 314 }
313   315  
314 /* 316 /*
315 * 317 *
316 * Skips a line in a file. 318 * Skips a line in a file.
317 */ 319 */
318 #if defined ___AsyncIO___ 320 #if defined ___AsyncIO___
319 void SkipLine(struct AsyncFile *fp) { 321 void SkipLine(struct AsyncFile *fp) {
320 LONG c; 322 LONG c;
321 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) { 323 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
322 #else 324 #else
323 void SkipLine(FILE *fp) { 325 void SkipLine(FILE *fp) {
324 char c; 326 char c;
325 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) { 327 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) {
326 #endif 328 #endif
327 #if defined ___AmigaOS___ 329 #if defined ___AmigaOS___
328 // Check if CTRL+C was pressed and abort the program. 330 // Check if CTRL+C was pressed and abort the program.
329 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 331 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
330 PROGRAM_RUN = FALSE; 332 PROGRAM_RUN = FALSE;
331 continue; 333 continue;
332 } 334 }
333 #endif 335 #endif
334 switch(c) { 336 switch(c) {
335 case '\n': 337 case '\n':
336 return; 338 return;
337 } 339 }
338 } 340 }
339 } 341 }
340   342  
341 /* 343 /*
342 * 344 *
343 * Peeks for the next line in a file. 345 * Peeks for the next line in a file.
344 */ 346 */
345 #if defined ___AsyncIO___ 347 #if defined ___AsyncIO___
346 dbLine *PeekLine(struct AsyncFile *fp) { 348 dbLine *PeekLine(struct AsyncFile *fp) {
347 #else 349 #else
348 dbLine *PeekLine(FILE *fp) { 350 dbLine *PeekLine(FILE *fp) {
349 #endif 351 #endif
350 dbLine *line; 352 dbLine *line;
351   353  
352 // Read the next line. 354 // Read the next line.
353 if((line = ReadLine(fp)) == NULL) { 355 if((line = ReadLine(fp)) == NULL) {
354 return NULL; 356 return NULL;
355 } 357 }
356   358  
357 // Rewind the file by the number of read characters. 359 // Rewind the file by the number of read characters.
358 #if defined ___AsyncIO___ 360 #if defined ___AsyncIO___
359 if(SeekAsync(fp, -(line->length + 1), MODE_CURRENT) == -1) { 361 if(SeekAsync(fp, -(line->length + 1), MODE_CURRENT) == -1) {
360 fprintf(stderr, "Could not seek in file.\n"); 362 fprintf(stderr, "Could not seek in file.\n");
361 free(line->string); 363 free(line->string);
362 free(line); 364 free(line);
363 line = NULL; 365 line = NULL;
364 return NULL; 366 return NULL;
365 } 367 }
366 #else 368 #else
367 if(fseek(fp, -(line->length + 1), SEEK_CUR) != 0) { 369 if(fseek(fp, -(line->length + 1), SEEK_CUR) != 0) {
368 fprintf(stderr, "Could not seek in file.\n"); 370 fprintf(stderr, "Could not seek in file.\n");
369 free(line->string); 371 free(line->string);
370 free(line); 372 free(line);
371 line = NULL; 373 line = NULL;
372 return NULL; 374 return NULL;
373 } 375 }
374 #endif 376 #endif
375   377  
376 return line; 378 return line;
377 } 379 }
378   380  
379 /* 381 /*
380 * 382 *
381 * Reads the next line in a file. 383 * Reads the next line in a file.
382 */ 384 */
383 #if defined ___AsyncIO___ 385 #if defined ___AsyncIO___
384 dbLine *ReadLine(struct AsyncFile *fp) { 386 dbLine *ReadLine(struct AsyncFile *fp) {
385 LONG c; 387 LONG c;
386 #else 388 #else
387 dbLine *ReadLine(FILE *fp) { 389 dbLine *ReadLine(FILE *fp) {
388 char c; 390 char c;
389 #endif 391 #endif
390 int i; 392 int i;
391 dbLine *line; 393 dbLine *line;
392   394  
393 i = 0; 395 i = 0;
394 #if defined ___AsyncIO___ 396 #if defined ___AsyncIO___
395 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) { 397 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
396 #else 398 #else
397 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) { 399 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) {
398 #endif 400 #endif
399 #if defined ___AmigaOS___ 401 #if defined ___AmigaOS___
400 // Check if CTRL+C was pressed and abort the program. 402 // Check if CTRL+C was pressed and abort the program.
401 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 403 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
402 PROGRAM_RUN = FALSE; 404 PROGRAM_RUN = FALSE;
403 continue; 405 continue;
404 } 406 }
405 #endif 407 #endif
406 switch(c) { 408 switch(c) {
407 case '\n': 409 case '\n':
408 // Rewind the file by the number of read characters. 410 // Rewind the file by the number of read characters.
409 #if defined ___AsyncIO___ 411 #if defined ___AsyncIO___
410 if(SeekAsync(fp, -(i + 1), MODE_CURRENT) == -1) { 412 if(SeekAsync(fp, -(i + 1), MODE_CURRENT) == -1) {
411 fprintf(stderr, "Could not seek in file.\n"); 413 fprintf(stderr, "Could not seek in file.\n");
412 fprintf(stderr, "index at: %d\n", i); 414 fprintf(stderr, "index at: %d\n", i);
413 return NULL; 415 return NULL;
414 } 416 }
415 #else 417 #else
416 if(fseek(fp, -(i + 1), SEEK_CUR) != 0) { 418 if(fseek(fp, -(i + 1), SEEK_CUR) != 0) {
417 fprintf(stderr, "Could not seek in file.\n"); 419 fprintf(stderr, "Could not seek in file.\n");
418 return NULL; 420 return NULL;
419 } 421 }
420 #endif 422 #endif
421 goto LINE; 423 goto LINE;
422 } 424 }
423   425  
424 ++i; 426 ++i;
425 } 427 }
426   428  
427 LINE: 429 LINE:
428   430  
429 if(i == 0) { 431 if(i == 0) {
430 return NULL; 432 return NULL;
431 } 433 }
432   434  
433 if((line = malloc(1 * sizeof(*line))) == NULL) { 435 if((line = malloc(1 * sizeof(*line))) == NULL) {
434 fprintf(stderr, "Memory allocation error.\n"); 436 fprintf(stderr, "Memory allocation error.\n");
435 return NULL; 437 return NULL;
436 } 438 }
437   439  
438 if((line->string = malloc((i + 1) * sizeof(*line->string))) == NULL) { 440 if((line->string = malloc((i + 1) * sizeof(*line->string))) == NULL) {
439 fprintf(stderr, "Memory allocation error.\n"); 441 fprintf(stderr, "Memory allocation error.\n");
440 free(line); 442 free(line);
441 line = NULL; 443 line = NULL;
442 return NULL; 444 return NULL;
443 } 445 }
444   446  
445 #if defined ___AsyncIO___ 447 #if defined ___AsyncIO___
446 if(ReadAsync(fp, line->string, i) != i) { 448 if(ReadAsync(fp, line->string, i) != i) {
447 #else 449 #else
448 if(fread(line->string, sizeof(char), i, fp) != i) { 450 if(fread(line->string, sizeof(char), i, fp) != i) {
449 #endif 451 #endif
450 fprintf(stderr, "Unable to read line.\n"); 452 fprintf(stderr, "Unable to read line.\n");
451 free(line->string); 453 free(line->string);
452 free(line); 454 free(line);
453 line = NULL; 455 line = NULL;
454 return NULL; 456 return NULL;
455 } 457 }
456   458  
457 // Clear newline. 459 // Clear newline.
458 #if defined ___AsyncIO___ 460 #if defined ___AsyncIO___
459 ReadCharAsync(fp); 461 ReadCharAsync(fp);
460 #else 462 #else
461 fgetc(fp); 463 fgetc(fp);
462 #endif 464 #endif
463   465  
464 line->length = i; 466 line->length = i;
465   467  
466 return line; 468 return line;
467 } 469 }
468   470  
469 /* 471 /*
470 * 472 *
471 * Delete a file. 473 * Delete a file.
472 */ 474 */
473 #if defined ___AmigaOS___ 475 #if defined ___AmigaOS___
474 BOOL RemoveFile(char *name) { 476 BOOL RemoveFile(char *name) {
475 return DeleteFile(name); 477 return DeleteFile(name);
476 #else 478 #else
477 int RemoveFile(char *name) { 479 int RemoveFile(char *name) {
478 return remove(name) == 0; 480 return remove(name) == 0;
479 #endif 481 #endif
480 } 482 }
481   -  
482   483  
483 /* 484 /*
484 * 485 *
485 * Deletes files. 486 * Deletes files.
486 */ 487 */
487 void RemoveFiles(char **names, int count) { 488 void RemoveFiles(VECTOR *names) {
488 int i; 489 int i;
489 for(i = 0; i < count; ++i) { 490 for(i = 0; i < names->length; ++i) {
490 if(RemoveFile(names[i]) == FALSE) { 491 if(RemoveFile(names->array[i]) == FALSE) {
491 fprintf(stderr, "Could not remove file '%s'.\n", names[i]); 492 fprintf(stderr, "Could not remove file '%s'.\n", (char *)names->array[i]);
492 continue; 493 continue;
493 } 494 }
494 fprintf(stderr, "Removing file '%s'\n", names[i]); 495 fprintf(stderr, "Removing file '%s'\n", (char *)names->array[i]);
495 } 496 }
496 } 497 }
497   498  
498 /* 499 /*
499 * 500 *
500 * Copies a file to another file by name. 501 * Copies a file to another file by name.
501 */ 502 */
502 void CopyFile(char *a, char *b) { 503 void CopyFile(char *a, char *b) {
503 #if defined ___AsyncIO___ 504 #if defined ___AsyncIO___
504 struct AsyncFile *ap; 505 struct AsyncFile *ap;
505 struct AsyncFile *bp; 506 struct AsyncFile *bp;
506 LONG c; 507 LONG c;
507 #else 508 #else
508 FILE *ap; 509 FILE *ap;
509 FILE *bp; 510 FILE *bp;
510 char c; 511 char c;
511 #endif 512 #endif
512   513  
513 // Open database file for writing. 514 // Open database file for writing.
514 #if defined ___AsyncIO___ 515 #if defined ___AsyncIO___
515 if((ap = OpenAsync(a, MODE_READ, ASYNC_BUF)) == NULL) { 516 if((ap = OpenAsync(a, MODE_READ, ASYNC_BUF)) == NULL) {
516 #else 517 #else
517 if((ap = fopen(a, "r")) == NULL) { 518 if((ap = fopen(a, "r")) == NULL) {
518 #endif 519 #endif
519 fprintf(stderr, "Could not open file '%s' for reading.\n", a); 520 fprintf(stderr, "Could not open file '%s' for reading.\n", a);
520 return; 521 return;
521 } 522 }
522   523  
523 // Open temporary file for reading. 524 // Open temporary file for reading.
524 #if defined ___AsyncIO___ 525 #if defined ___AsyncIO___
525 if((bp = OpenAsync(b, MODE_WRITE, ASYNC_BUF)) == NULL) { 526 if((bp = OpenAsync(b, MODE_WRITE, ASYNC_BUF)) == NULL) {
526 #else 527 #else
527 if((bp = fopen(b, "w")) == NULL) { 528 if((bp = fopen(b, "w")) == NULL) {
528 #endif 529 #endif
529 fprintf(stderr, "Could not open file '%s' for writing.\n", b); 530 fprintf(stderr, "Could not open file '%s' for writing.\n", b);
530   531  
531 // Close database file. 532 // Close database file.
532 #if defined ___AsyncIO___ 533 #if defined ___AsyncIO___
533 CloseAsync(ap); 534 CloseAsync(ap);
534 #else 535 #else
535 fclose(ap); 536 fclose(ap);
536 #endif 537 #endif
537   538  
538 return; 539 return;
539 } 540 }
540   541  
541 #if defined ___AsyncIO___ 542 #if defined ___AsyncIO___
542 while(PROGRAM_RUN && (c = ReadCharAsync(ap)) != -1) { 543 while(PROGRAM_RUN && (c = ReadCharAsync(ap)) != -1) {
543 #else 544 #else
544 while(PROGRAM_RUN && fscanf(ap, "%c", &c) == 1) { 545 while(PROGRAM_RUN && fscanf(ap, "%c", &c) == 1) {
545 #endif 546 #endif
546 #if defined ___AmigaOS___ 547 #if defined ___AmigaOS___
547 // Check if CTRL+C was pressed and abort the program. 548 // Check if CTRL+C was pressed and abort the program.
548 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 549 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
549 PROGRAM_RUN = FALSE; 550 PROGRAM_RUN = FALSE;
550 continue; 551 continue;
551 } 552 }
552 #endif 553 #endif
553 #if defined ___AsyncIO___ 554 #if defined ___AsyncIO___
554 if(WriteCharAsync(bp, (UBYTE)c) != 1) { 555 if(WriteCharAsync(bp, (UBYTE)c) != 1) {
555 #else 556 #else
556 if(fprintf(bp, "%c", c) != 1) { 557 if(fprintf(bp, "%c", c) != 1) {
557 #endif 558 #endif
558 fprintf(stderr, "Could not write to file '%s'.\n", b); 559 fprintf(stderr, "Could not write to file '%s'.\n", b);
559 break; 560 break;
560 } 561 }
561 } 562 }
562   563  
563 #if defined ___AsyncIO___ 564 #if defined ___AsyncIO___
564 CloseAsync(ap); 565 CloseAsync(ap);
565 CloseAsync(bp); 566 CloseAsync(bp);
566 #else 567 #else
567 fclose(ap); 568 fclose(ap);
568 fclose(bp); 569 fclose(bp);
569 #endif 570 #endif
570 } 571 }
571   572  
572 /* 573 /*
573 * 574 *
574 * Create a database entry from a line of text. 575 * Create a database entry from a line of text.
575 */ 576 */
576 dbEntry* CreateDatabaseEntry(dbLine *line) { 577 dbEntry* CreateDatabaseEntry(dbLine *line) {
577 dbEntry *entry; 578 dbEntry *entry;
578 char *ptr; 579 char *ptr;
579 int side; 580 int side;
580 int i; 581 int i;
581 int j; 582 int j;
582   583  
583 if((entry = malloc(1 * sizeof(*entry))) == NULL) { 584 if((entry = malloc(1 * sizeof(*entry))) == NULL) {
584 fprintf(stderr, "Memory allocation failure.\n"); 585 fprintf(stderr, "Memory allocation failure.\n");
585 return NULL; 586 return NULL;
586 } 587 }
587   588  
588 if((entry->name = malloc((line->length + 1) * sizeof(*entry->name))) == NULL) { 589 if((entry->name = malloc((line->length + 1) * sizeof(*entry->name))) == NULL) {
589 fprintf(stderr, "Memory allocation failure.\n"); 590 fprintf(stderr, "Memory allocation failure.\n");
590 return NULL; 591 return NULL;
591 } 592 }
592   593  
593 if((entry->path = malloc((line->length + 1) * sizeof(*entry->path))) == NULL) { 594 if((entry->path = malloc((line->length + 1) * sizeof(*entry->path))) == NULL) {
594 fprintf(stderr, "Memory allocation failure.\n"); 595 fprintf(stderr, "Memory allocation failure.\n");
595 return NULL; 596 return NULL;
596 } 597 }
597   598  
598 for(ptr = line->string, side = 0, i = 0, j = 0; PROGRAM_RUN && *ptr != '\0'; ++ptr) { 599 for(ptr = line->string, side = 0, i = 0, j = 0; PROGRAM_RUN && *ptr != '\0'; ++ptr) {
599 #if defined ___AmigaOS___ 600 #if defined ___AmigaOS___
600 // Check if CTRL+C was pressed and abort the program. 601 // Check if CTRL+C was pressed and abort the program.
601 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 602 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
602 PROGRAM_RUN = FALSE; 603 PROGRAM_RUN = FALSE;
603 continue; 604 continue;
604 } 605 }
605 #endif 606 #endif
606 switch(*ptr) { 607 switch(*ptr) {
607 case '\t': 608 case '\t':
608 entry->name[i] = '\0'; 609 entry->name[i] = '\0';
609 ++side; 610 ++side;
610 break; 611 break;
611 case '\n': 612 case '\n':
612 entry->path[j] = '\0'; 613 entry->path[j] = '\0';
613 return entry; 614 return entry;
614 default: 615 default:
615 switch(side) { 616 switch(side) {
616 case 0: 617 case 0:
617 entry->name[i++] = *ptr; 618 entry->name[i++] = *ptr;
618 break; 619 break;
619 case 1: 620 case 1:
620 entry->path[j++] = *ptr; 621 entry->path[j++] = *ptr;
621 break; 622 break;
622 } 623 }
623 break; 624 break;
624 } 625 }
625 } 626 }
626   627  
627 return entry; 628 return entry;
628 } 629 }
629   630  
630 /* 631 /*
631 * Compare two strings. 632 * Compare two strings.
632 */ 633 */
633 #if defined ___AmigaOS___ 634 #if defined ___AmigaOS___
634 BOOL StringMatch(char *a, char *b) { 635 BOOL StringMatch(char *a, char *b) {
635 #else 636 #else
636 int StringMatch(char *a, char *b) { 637 int StringMatch(char *a, char *b) {
637 #endif 638 #endif
638 #if defined ___AmigaOS___ 639 #if defined ___AmigaOS___
639 ULONG size; 640 ULONG size;
640 BOOL success; 641 BOOL success;
641 UBYTE *pattern; 642 UBYTE *pattern;
642   643  
643 // "must be at least 2 times as large plus 2 bytes" 644 // "must be at least 2 times as large plus 2 bytes"
644 size = strlen(b) * 2 + 2; 645 size = strlen(b) * 2 + 2;
645   646  
646 success = FALSE; 647 success = FALSE;
647   648  
648 if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { 649 if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) {
649 switch(ParsePatternNoCase(b, pattern, (LONG)size)) { 650 switch(ParsePatternNoCase(b, pattern, (LONG)size)) {
650 case 1: // the pattern contains wildcards 651 case 1: // the pattern contains wildcards
651 success = MatchPatternNoCase(pattern, a); 652 success = MatchPatternNoCase(pattern, a);
652   653  
653 break; 654 break;
654 case 0: // no wildcards so fall back to exact name match 655 case 0: // no wildcards so fall back to exact name match
655 #if defined ___NOCASE_FS___ 656 #if defined ___NOCASE_FS___
656 success = (Strnicmp(a, b, StringLenMax(a, b)) == 0); 657 success = (Strnicmp(a, b, StringLenMax(a, b)) == 0);
657 #else 658 #else
658 success = (StrnCmp(a, b, StringLenMax(a, b)) == 0); 659 success = (StrnCmp(a, b, StringLenMax(a, b)) == 0);
659 #endif 660 #endif
660   661  
661 break; 662 break;
662 } 663 }
663   664  
664 FreeVec(pattern); 665 FreeVec(pattern);
665 pattern = NULL; 666 pattern = NULL;
666 } 667 }
667   668  
668 return success; 669 return success;
669 #else 670 #else
670 int success; 671 int success;
671 char *e = a; 672 char *e = a;
672 char *n = b; 673 char *n = b;
673   674  
674 success = FALSE; 675 success = FALSE;
675   676  
676 #if defined ___NOCASE_FS___ 677 #if defined ___NOCASE_FS___
677 StrUpr(e); 678 StrUpr(e);
678 StrUpr(n); 679 StrUpr(n);
679 #endif 680 #endif
680   681  
681 // search for substring 682 // search for substring
682 success = strstr(e, n) != NULL; 683 success = strstr(e, n) != NULL;
683   684  
684 return success; 685 return success;
685 #endif 686 #endif
686 } 687 }
687   688  
688   689  
689   690  
690   691  
691   692  
692   693