/trunk/HuntnGather/Gather/Gather.c |
@@ -25,7 +25,7 @@ |
/* Version string used for querrying the program version. */ |
/*************************************************************************/ |
TEXT version_string[] = |
"\0$VER: Gather 1.5 "__DATE__" by Wizardry and Steamworks"; |
"\0$VER: Gather 1.7 "__DATE__" by Wizardry and Steamworks"; |
#endif |
|
#if !defined TRUE |
@@ -37,6 +37,9 @@ |
#endif |
|
#define MAX_MEM 262144 |
#define NAME_BUF 32 |
#define PATH_BUF 128 |
#define LINE_BUF 256 |
#define DEFAULT_DATABASE_FILE "S:gather.db" |
|
typedef struct { |
@@ -71,6 +74,8 @@ |
int i; |
int side; |
unsigned int line; |
int name_size; |
int path_size; |
|
// Open database file for reading. |
if((fp = fopen(dbFile, "r")) == NULL) { |
@@ -78,9 +83,11 @@ |
return; |
} |
|
database = (char **) malloc(sizeof(char *)); |
name = (char *) malloc(sizeof(char)); |
path = (char *) malloc(sizeof(char)); |
database = malloc(sizeof(char *)); |
name_size = NAME_BUF; |
name = malloc(name_size * sizeof(char)); |
path_size = PATH_BUF; |
path = malloc(path_size * sizeof(char)); |
line = 0; |
side = 0; |
i = 0; |
@@ -100,13 +107,14 @@ |
switch(c) { |
case '\n': |
// Load up the name and path into the database variable. |
database = (char **) realloc(database, (line + 1) * sizeof(char *)); |
database[line] = (char *) malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(char)); |
database = realloc(database, (line + 1) * sizeof(char *)); |
database[line] = malloc((strlen(name) + strlen(path) + 1 + 1) * sizeof(char)); |
sprintf(database[line], "%s\t%s", name, path); |
++line; |
|
free(name); |
name = (char *) malloc(sizeof(char)); |
name_size = NAME_BUF; |
name = malloc(name_size * sizeof(char)); |
--side; |
i = 0; |
|
@@ -113,7 +121,8 @@ |
break; |
case '\t': |
free(path); |
path = (char *) malloc(sizeof(char)); |
path_size = PATH_BUF; |
path = malloc(path_size * sizeof(char)); |
++side; |
i = 0; |
break; |
@@ -120,12 +129,20 @@ |
default: |
switch(side) { |
case 0: |
name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); |
if(strlen(name) == name_size) { |
name_size = name_size * 1.5; |
name = realloc(name, name_size * sizeof(char)); |
} |
//name = realloc(name, (i + 1 + 1) * sizeof(char)); |
name[i] = c; |
name[i + 1] = '\0'; |
break; |
case 1: |
path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); |
if(strlen(path) == path_size) { |
path_size = path_size * 1.5; |
path = realloc(path, path_size * sizeof(char)); |
} |
//path = realloc(path, (i + 1 + 1) * sizeof(char)); |
path[i] = c; |
path[i + 1] = '\0'; |
break; |
@@ -201,11 +218,11 @@ |
switch(path[strlen(path) - 1]) { |
case '/': |
case ':': // This is a drive path. |
subPath = (char *) malloc(size); |
subPath = malloc(size); |
sprintf(subPath, "%s%s", path, dirEntry->d_name); |
break; |
default: |
subPath = (char *) malloc(size + 1); |
subPath = malloc(size + 1); |
sprintf(subPath, "%s/%s", path, dirEntry->d_name); |
break; |
} |
@@ -311,7 +328,7 @@ |
char **tmpNames; |
int count; |
|
tmpNames = (char **) malloc(files * sizeof(char *)); |
tmpNames = malloc(files * sizeof(char *)); |
|
if(verbose) { |
fprintf(stdout, "Creating temporary files.\r"); |
@@ -447,11 +464,13 @@ |
char *ReadDatabaseLine(FILE *fp) { |
char c; |
char *line; |
int chars; |
int line_size; |
int i; |
|
line = (char *) malloc(sizeof(char)); |
line_size = LINE_BUF; |
line = malloc(line_size * sizeof(char)); |
|
chars = 0; |
i = 0; |
while(run && fscanf(fp, "%c", &c) == 1) { |
#if defined ___AmigaOS___ |
// Check if CTRL+C was pressed and abort the program. |
@@ -462,15 +481,19 @@ |
switch(c) { |
case '\n': |
// Rewind the file by the number of read characters. |
fseek(fp, -(chars + 1), SEEK_CUR); |
fseek(fp, -(i + 1), SEEK_CUR); |
return line; |
default: |
line = (char *) realloc(line, (chars + 1 + 1) * sizeof(char)); |
line[chars] = c; |
line[chars + 1] = '\0'; |
if(strlen(line) == line_size) { |
line_size = line_size * 1.5; |
line = realloc(line, line_size * sizeof(char)); |
} |
//line = realloc(line, (chars + 1 + 1) * sizeof(char)); |
line[i] = c; |
line[i + 1] = '\0'; |
break; |
} |
++chars; |
++i; |
} |
|
return NULL; |
@@ -495,7 +518,7 @@ |
} |
|
// Allocate as many file pointers as temporary files. |
tp = (FILE **) malloc(files * sizeof(FILE *)); |
tp = malloc(files * sizeof(FILE *)); |
|
// Open all temporary files for reading. |
for(i = 0; i < files; ++i) { |
@@ -540,7 +563,7 @@ |
// Free previous instance. |
free(tmpMin); |
} |
tmpMin = (char *) malloc((strlen(tmp) + 1) * sizeof(char)); |
tmpMin = malloc((strlen(tmp) + 1) * sizeof(char)); |
sprintf(tmpMin, "%s", tmp); |
// Remember the index of the file where the smallest entry has been found. |
idxMin = i; |
@@ -637,6 +660,19 @@ |
MergeDatabase(dbFile, tmpNames, tmpFiles, dbLines); |
} |
|
void usage(char *name) { |
fprintf(stdout, "Hunt & Gather - %s, a file index generating tool. \n", name); |
fprintf(stdout, " \n"); |
fprintf(stdout, "SYNTAX: %s [-q] DATABASE \n", name); |
fprintf(stdout, " \n"); |
fprintf(stdout, " -q Do not print out any messages. \n"); |
fprintf(stdout, " \n"); |
fprintf(stdout, "DATABASE is a path to where the indexed results will be \n"); |
fprintf(stdout, "stored for searching with the Hunt tool. \n"); |
fprintf(stdout, " \n"); |
fprintf(stdout, "(c) 2021 Wizardry and Steamworks, MIT. \n"); |
} |
|
/* |
* |
* Main entry point. |
@@ -659,11 +695,10 @@ |
verbose = FALSE; |
break; |
case 'h': |
fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); |
usage(argv[0]); |
return 0; |
case '?': |
fprintf(stderr, "Invalid option %ct.\n", optopt); |
fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); |
return 1; |
} |
} |
@@ -670,7 +705,7 @@ |
|
|
if(optind >= argc) { |
fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); |
usage(argv[0]); |
return 1; |
} |
|
@@ -677,7 +712,6 @@ |
stat(argv[optind], &dirStat); |
if(!S_ISDIR(dirStat.st_mode)) { |
fprintf(stderr, "Path '%s' is not a directory.\n", argv[optind]); |
fprintf(stdout, "SYNTAX: %s [-q] [-d DATABASE] DIRECTORY\n", argv[0]); |
return 1; |
} |
|