HuntnGather

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 36  →  ?path2? @ 37
/trunk/HuntnGather/Hunt/Hunt.c
@@ -50,64 +50,6 @@
}
 
/*
* Compare two strings.
*/
#if defined ___AmigaOS___
BOOL compare(char *a, char *b) {
#else
int compare(char *a, char *b) {
#endif
#if defined ___AmigaOS___
ULONG size;
BOOL success;
UBYTE *pattern;
 
// "must be at least 2 times as large plus 2 bytes"
size = strlen(b) * 2 + 2;
 
success = FALSE;
 
if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) {
switch(ParsePatternNoCase(b, pattern, (LONG)size)) {
case 1: // the pattern contains wildcards
success = MatchPatternNoCase(pattern, a);
 
break;
case 0: // no wildcards so fall back to exact name match
#if defined ___NOCASE_FS___
success = (Strnicmp(a, b, StringLenMax(a, b)) == 0);
#else
success = (StrnCmp(a, b, StringLenMax(a, b)) == 0);
#endif
 
break;
}
 
FreeVec(pattern);
}
 
return success;
#else
int success;
char *e = a;
char *n = b;
 
success = FALSE;
 
#if defined ___NOCASE_FS___
e = StrUpr(e);
n = StrUpr(n);
#endif
 
// search for substring
success = strstr(e, n) != NULL;
 
return success;
#endif
}
 
 
/*
*
* Search the database for a matching string.
*/
@@ -114,109 +56,54 @@
void SearchDatabase(char *dbFile, char* needle) {
#if defined ___AsyncIO___
struct AsyncFile *fp;
LONG c;
#else
FILE *fp;
char c;
#endif
char *name;
int name_size;
char *path;
int path_size;
int i;
int side;
int match;
int total;
dbEntry *entry;
char *line = NULL;
 
// Open database file for reading.
#if defined ___AsyncIO___
if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
#else
if((fp = fopen(dbFile, "r")) == NULL) {
#endif
fprintf(stderr, "Could not open '%s' for reading.\n", dbFile);
fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
return;
}
 
name_size = NAME_BUF;
name = malloc(name_size * sizeof(*name));
path_size = PATH_BUF;
path = malloc(path_size * sizeof(*path));
 
i = 0;
side = 0;
match = FALSE;
total = 0;
 
#if defined ___AsyncIO___
while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
#else
while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) {
#endif
while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) {
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
PROGRAM_RUN = FALSE;
continue;
}
#endif
 
switch(c) {
case '\n':
++total;
if(match) {
fprintf(stdout, "%s\n", path);
match = FALSE;
}
if(name != NULL) {
free(name);
name_size = NAME_BUF;
name = malloc(name_size * sizeof(*name));
}
--side;
i = 0;
break;
case '\t':
// Case insensitive match.
if(compare(name, needle)) {
match = TRUE;
}
if(path != NULL) {
free(path);
path_size = PATH_BUF;
path = malloc(path_size * sizeof(*name));
}
++side;
i = 0;
break;
default:
switch(side) {
case 0:
if(strlen(name) == name_size) {
name_size = 1.5 * name_size;
name = realloc(name, name_size * sizeof(*name));
}
name[i] = c;
name[i + 1] = '\0';
break;
case 1:
if(strlen(path) == path_size) {
path_size = 1.5 * path_size;
path = realloc(path, path_size * sizeof(*path));
}
path[i] = c;
path[i + 1] = '\0';
break;
default:
fprintf(stderr, "Database corrupted.\n");
break;
}
++i;
break;
if((entry = CreateDatabaseEntry(line)) == NULL) {
fprintf(stderr, "Unable to create database entry.\n");
free(line);
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return;
}
 
if(StringMatch(entry->name, needle)) {
fprintf(stdout, "%s\n", entry->path);
}
 
free(entry->name);
free(entry->path);
free(line);
}
 
free(name);
free(path);
if(line != NULL) {
free(line);
}
 
#if defined ___AsyncIO___
CloseAsync(fp);
@@ -268,13 +155,13 @@
return 0;
case '?':
fprintf(stderr, "Invalid option %c.\n", optopt);;
return 1;
return 5;
}
}
 
if(optind >= argc) {
usage(argv[0]);
return 1;
return 5;
}
 
switch(GetFsType(dbFile)) {
@@ -281,7 +168,7 @@
case UNKNOWN:
case DIRECTORY:
fprintf(stderr, "'%s' is not a file.\n", dbFile);
return 1;
return 10;
case REGULAR:
Hunt(dbFile, argv[optind]);
break;