HuntnGather – Diff between revs 33 and 37

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 33 Rev 37
Line 48... Line 48...
48 // Toggle the run flag to stop execution. 48 // Toggle the run flag to stop execution.
49 PROGRAM_RUN = FALSE; 49 PROGRAM_RUN = FALSE;
50 } 50 }
Line 51... Line 51...
51   51  
52 /* -  
53 * Compare two strings. -  
54 */ -  
55 #if defined ___AmigaOS___ -  
56 BOOL compare(char *a, char *b) { -  
57 #else -  
58 int compare(char *a, char *b) { -  
59 #endif -  
60 #if defined ___AmigaOS___ -  
61 ULONG size; -  
62 BOOL success; -  
63 UBYTE *pattern; -  
64   -  
65 // "must be at least 2 times as large plus 2 bytes" -  
66 size = strlen(b) * 2 + 2; -  
67   -  
68 success = FALSE; -  
69   -  
70 if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { -  
71 switch(ParsePatternNoCase(b, pattern, (LONG)size)) { -  
72 case 1: // the pattern contains wildcards -  
73 success = MatchPatternNoCase(pattern, a); -  
74   -  
75 break; -  
76 case 0: // no wildcards so fall back to exact name match -  
77 #if defined ___NOCASE_FS___ -  
78 success = (Strnicmp(a, b, StringLenMax(a, b)) == 0); -  
79 #else -  
80 success = (StrnCmp(a, b, StringLenMax(a, b)) == 0); -  
81 #endif -  
82   -  
83 break; -  
84 } -  
85   -  
86 FreeVec(pattern); -  
87 } -  
88   -  
89 return success; -  
90 #else -  
91 int success; -  
92 char *e = a; -  
93 char *n = b; -  
94   -  
95 success = FALSE; -  
96   -  
97 #if defined ___NOCASE_FS___ -  
98 e = StrUpr(e); -  
99 n = StrUpr(n); -  
100 #endif -  
101   -  
102 // search for substring -  
103 success = strstr(e, n) != NULL; -  
104   -  
105 return success; -  
106 #endif -  
107 } -  
108   -  
109   -  
110 /* 52 /*
111 * 53 *
112 * Search the database for a matching string. 54 * Search the database for a matching string.
113 */ 55 */
114 void SearchDatabase(char *dbFile, char* needle) { 56 void SearchDatabase(char *dbFile, char* needle) {
115 #if defined ___AsyncIO___ 57 #if defined ___AsyncIO___
116 struct AsyncFile *fp; -  
117 LONG c; 58 struct AsyncFile *fp;
118 #else 59 #else
119 FILE *fp; -  
120 char c; 60 FILE *fp;
121 #endif 61 #endif
122 char *name; -  
123 int name_size; 62 dbEntry *entry;
124 char *path; -  
125 int path_size; -  
126 int i; -  
127 int side; -  
128 int match; -  
Line -... Line 63...
-   63 char *line = NULL;
129 int total; 64  
130   65 // Open database file for reading.
131 #if defined ___AsyncIO___ 66 #if defined ___AsyncIO___
132 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { 67 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
133 #else 68 #else
134 if((fp = fopen(dbFile, "r")) == NULL) { 69 if((fp = fopen(dbFile, "r")) == NULL) {
135 #endif 70 #endif
136 fprintf(stderr, "Could not open '%s' for reading.\n", dbFile); 71 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
Line 137... Line -...
137 return; -  
138 } -  
139   -  
140 name_size = NAME_BUF; -  
141 name = malloc(name_size * sizeof(*name)); -  
142 path_size = PATH_BUF; -  
143 path = malloc(path_size * sizeof(*path)); -  
144   -  
145 i = 0; -  
146 side = 0; -  
147 match = FALSE; -  
148 total = 0; 72 return;
149   -  
150 #if defined ___AsyncIO___ -  
151 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) { -  
152 #else 73 }
153 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) { 74  
154 #endif 75 while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) {
155 #if defined ___AmigaOS___ 76 #if defined ___AmigaOS___
156 // Check if CTRL+C was pressed and abort the program. 77 // Check if CTRL+C was pressed and abort the program.
157 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 78 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
158 PROGRAM_RUN = FALSE; 79 PROGRAM_RUN = FALSE;
Line 159... Line -...
159 continue; -  
160 } -  
161 #endif -  
162   80 continue;
163 switch(c) { 81 }
164 case '\n': -  
165 ++total; -  
166 if(match) { -  
167 fprintf(stdout, "%s\n", path); 82 #endif
168 match = FALSE; 83  
169 } 84 if((entry = CreateDatabaseEntry(line)) == NULL) {
170 if(name != NULL) { 85 fprintf(stderr, "Unable to create database entry.\n");
171 free(name); 86 free(line);
172 name_size = NAME_BUF; 87 #if defined ___AsyncIO___
173 name = malloc(name_size * sizeof(*name)); 88 CloseAsync(fp);
174 } -  
175 --side; -  
176 i = 0; -  
177 break; -  
178 case '\t': 89 #else
179 // Case insensitive match. -  
180 if(compare(name, needle)) { -  
181 match = TRUE; -  
182 } -  
183 if(path != NULL) { 90 fclose(fp);
184 free(path); -  
185 path_size = PATH_BUF; -  
186 path = malloc(path_size * sizeof(*name)); -  
187 } -  
188 ++side; -  
189 i = 0; -  
190 break; 91 #endif
191 default: -  
192 switch(side) { -  
193 case 0: -  
194 if(strlen(name) == name_size) { -  
195 name_size = 1.5 * name_size; -  
196 name = realloc(name, name_size * sizeof(*name)); -  
197 } -  
198 name[i] = c; -  
199 name[i + 1] = '\0'; -  
200 break; -  
201 case 1: -  
202 if(strlen(path) == path_size) { -  
203 path_size = 1.5 * path_size; -  
204 path = realloc(path, path_size * sizeof(*path)); -  
205 } -  
206 path[i] = c; 92 return;
207 path[i + 1] = '\0'; -  
208 break; -  
209 default: -  
210 fprintf(stderr, "Database corrupted.\n"); -  
211 break; 93 }
-   94  
-   95 if(StringMatch(entry->name, needle)) {
-   96 fprintf(stdout, "%s\n", entry->path);
-   97 }
212 } 98  
Line 213... Line 99...
213 ++i; 99 free(entry->name);
214 break; 100 free(entry->path);
-   101 free(line);
Line 215... Line 102...
215 } 102 }
216 } 103  
217   104 if(line != NULL) {
218 free(name); 105 free(line);
Line 266... Line 153...
266 case 'h': 153 case 'h':
267 usage(argv[0]); 154 usage(argv[0]);
268 return 0; 155 return 0;
269 case '?': 156 case '?':
270 fprintf(stderr, "Invalid option %c.\n", optopt);; 157 fprintf(stderr, "Invalid option %c.\n", optopt);;
271 return 1; 158 return 5;
272 } 159 }
273 } 160 }
Line 274... Line 161...
274   161  
275 if(optind >= argc) { 162 if(optind >= argc) {
276 usage(argv[0]); 163 usage(argv[0]);
277 return 1; 164 return 5;
Line 278... Line 165...
278 } 165 }
279   166  
280 switch(GetFsType(dbFile)) { 167 switch(GetFsType(dbFile)) {
281 case UNKNOWN: 168 case UNKNOWN:
282 case DIRECTORY: 169 case DIRECTORY:
283 fprintf(stderr, "'%s' is not a file.\n", dbFile); 170 fprintf(stderr, "'%s' is not a file.\n", dbFile);
284 return 1; 171 return 10;
285 case REGULAR: 172 case REGULAR:
286 Hunt(dbFile, argv[optind]); 173 Hunt(dbFile, argv[optind]);