HuntnGather – Diff between revs 41 and 43

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