HuntnGather – Diff between revs 43 and 45

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