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