HuntnGather

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 30  →  ?path2? @ 31
/trunk/HuntnGather/Gather/Gather.c
@@ -24,12 +24,17 @@
#include <asyncio.h>
#endif
 
#include "StringStack.h"
 
#if !defined ___HAVE_GETOPT___
#include "getopt.h"
#endif
 
#include "StringStack.h"
 
// MemLib memory debugging.
#if defined MWDEBUG
#include "memwatch.h"
#endif
 
#define PROGRAM_VERSION "1.7.4"
 
#if defined ___AmigaOS___
@@ -93,12 +98,16 @@
* Used for sorting database lines.
*/
int QsortCompare(const void *a, const void *b) {
const char **p = (const char **)a;
const char **q = (const char **)b;
#if defined ___AmigaOS___
return StrnCmp(locale, (STRPTR)*p, (STRPTR)*q, -1, SC_ASCII);
return StrnCmp(
locale,
(STRPTR)(*(const char **)a),
(STRPTR)*((const char **)b),
-1,
SC_ASCII
);
#else
return strcmp(*p, *q);
return strcmp(*(const char **)a, *(const char **)b);
#endif
}
 
@@ -369,7 +378,8 @@
char *PeekLine(FILE *fp) {
char c;
#endif
char *line;
char *line = NULL;
char *real = NULL;
unsigned int size;
int i;
 
@@ -412,7 +422,13 @@
default:
if(strlen(line) == size) {
size = size * 1.5;
line = realloc(line, size * sizeof(*line));
real = realloc(line, size * sizeof(*line));
if(real == NULL) {
fprintf(stderr, "Memory reallocation failure.\n");
free(line);
return NULL;
}
line = real;
}
line[i] = c;
line[i + 1] = '\0';
@@ -421,6 +437,10 @@
++i;
}
 
if(line != NULL) {
free(line);
}
 
return NULL;
}
 
@@ -435,7 +455,8 @@
char *ReadLine(FILE *fp) {
char c;
#endif
char *line;
char *line = NULL;
char *real = NULL;
unsigned int size;
unsigned int i;
 
@@ -464,7 +485,13 @@
default:
if(strlen(line) == size) {
size = size * 1.5;
line = realloc(line, size * sizeof(*line));
real = realloc(line, size * sizeof(*line));
if(real == NULL) {
fprintf(stderr, "Memory reallocation failure.\n");
free(line);
return NULL;
}
line = real;
}
line[i] = c;
line[i + 1] = '\0';
@@ -473,6 +500,10 @@
++i;
}
 
if(line != NULL) {
free(line);
}
 
return NULL;
}
 
@@ -636,6 +667,10 @@
sprintf(rem, "%s", lines[i]);
}
 
if(rem != NULL) {
free(rem);
}
 
#if defined ___AsyncIO___
CloseAsync(fp);
#else
@@ -647,7 +682,7 @@
*
* Create a database entry from a line of text.
*/
dbEntry* CreateDataseEntry(char *line) {
dbEntry* CreateDatabaseEntry(char *line) {
dbEntry *entry;
char *ptr;
unsigned int side;
@@ -659,12 +694,12 @@
return NULL;
}
 
if((entry->name = malloc(strlen(line) * sizeof(*entry->name))) == NULL) {
if((entry->name = malloc((strlen(line) + 1) * sizeof(*entry->name))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
 
if((entry->path = malloc(strlen(line) * sizeof(*entry->path))) == NULL) {
if((entry->path = malloc((strlen(line) + 1) * sizeof(*entry->path))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
@@ -672,7 +707,7 @@
for(ptr = line, side = 0, i = 0, j = 0; run && *ptr != '\0'; ++ptr) {
#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) {
run = FALSE;
continue;
}
@@ -713,14 +748,10 @@
#endif
dbArray *array;
dbEntry *entry;
char *line;
char *line = NULL;
char *real = NULL;
unsigned int count;
 
if((array = malloc(1 * sizeof(*array))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
 
// Open database file for reading.
#if defined ___AsyncIO___
if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
@@ -731,6 +762,16 @@
return NULL;
}
 
if((array = malloc(1 * sizeof(*array))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return NULL;
}
 
count = 0;
while(run && (line = ReadLine(fp)) != NULL) {
#if defined ___AmigaOS___
@@ -740,8 +781,9 @@
continue;
}
#endif
if((entry = CreateDataseEntry(line)) == NULL) {
if((entry = CreateDatabaseEntry(line)) == NULL) {
fprintf(stderr, "Unable to create database entry.\n");
free(line);
#if defined ___AsyncIO___
CloseAsync(fp);
#else
@@ -751,9 +793,26 @@
}
 
// Load up the name and path into the database variable.
array->database = realloc(array->database, (count + 1) * sizeof(*array->database));
real = realloc(array->database, (count + 1) * sizeof(*array->database));
if(real == NULL) {
fprintf(stderr, "Memory reallocation failure.\n");
free(entry->name);
free(entry->path);
free(entry);
free(line);
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return NULL;
}
array->database = real;
 
if((array->database[count] = malloc((strlen(entry->name) + strlen(entry->path) + 1 + 1) * sizeof(*array->database[count]))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
free(entry->name);
free(entry->path);
free(entry);
free(line);
#if defined ___AsyncIO___
@@ -774,6 +833,10 @@
free(line);
}
 
if(line != NULL) {
free(line);
}
 
#if defined ___AsyncIO___
CloseAsync(fp);
#else
@@ -835,20 +898,11 @@
struct stat dirStat;
#endif
stringStack *stack;
stats *stats;
stats *stats = NULL;
int i;
char *path;
char *sub;
 
// Initialize metrics.
if((stats = malloc(sizeof(*stats))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
 
stats->dirs = 0;
stats->files = 0;
 
#if defined ___AsyncIO___
if((fp = OpenAsync(dbFile, MODE_APPEND, ASYNC_BUF)) == NULL) {
#else
@@ -855,7 +909,7 @@
if((fp = fopen(dbFile, "a")) == NULL) {
#endif
fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile);
return stats;
return NULL;
}
 
if(verbose) {
@@ -862,6 +916,20 @@
fprintf(stdout, "Collecting files...\r");
}
 
// Initialize metrics.
if((stats = malloc(sizeof(*stats))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return NULL;
}
 
stats->dirs = 0;
stats->files = 0;
 
// Push the first path onto the stack.
stack = stringStackCreate(count);
for(i = 0; run && i < count; ++i) {
@@ -906,10 +974,7 @@
// Check if CTRL+C was pressed and abort the program.
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
run = FALSE;
FreeDosObject(DOS_FIB, FIBp);
UnLock(lockp);
free(path);
break;
continue;
}
#else
 
@@ -1249,8 +1314,8 @@
FILE *fp;
FILE **tp;
#endif
int i;
int j;
unsigned int i;
unsigned int j;
char *tmp;
char *rem;
char *min;
@@ -1462,6 +1527,7 @@
FILE *tp;
#endif
char *line;
unsigned int lines;
int i;
dbEntry *entry;
 
@@ -1493,6 +1559,11 @@
return;
}
 
if(verbose) {
fprintf(stdout, "Removing lines...\r");
}
 
lines = 0;
while(run && (line = ReadLine(fp)) != NULL) {
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
@@ -1501,12 +1572,18 @@
continue;
}
#endif
if((entry = CreateDataseEntry(line)) == NULL) {
if((entry = CreateDatabaseEntry(line)) == NULL) {
fprintf(stderr, "Unable to create database entry.\n");
free(line);
continue;
}
 
for(i = 0; i < count; ++i) {
if(PathCompare(entry->path, paths[i]) == TRUE) {
++lines;
if(verbose) {
fprintf(stdout, "Removing lines: %d\r", lines);
}
continue;
}
#if defined ___AsyncIO___
@@ -1518,11 +1595,19 @@
break;
}
 
 
free(entry->name);
free(entry->path);
free(entry);
 
free(line);
}
 
if(verbose) {
fprintf(stdout, "\n");
}
 
 
#if defined ___AsyncIO___
CloseAsync(fp);
CloseAsync(tp);
@@ -1585,7 +1670,7 @@
WriteTemporaryFiles(dbFile, tmpNames, tmpFiles, tmpLines, dbLines);
 
// Sort the temporary files.
for(i = 0; run && i < tmpFiles; ++i) {
for(i = 0; i < tmpFiles; ++i) {
SortDatabase(tmpNames[i]);
}
 
@@ -1595,6 +1680,7 @@
// Remove all temporary files.
RemoveFiles(tmpNames, tmpFiles);
 
// Free temporary file names.
free(tmpNames);
}
 
@@ -1614,7 +1700,10 @@
CopyFile(tmpName, dbFile);
 
// Remove temporary file.
RemoveFile(tmpName);
if(RemoveFile(tmpName) == FALSE) {
fprintf(stderr, "Temporary file could not be removed.\n");
return;
}
}
 
void usage(char *name) {
@@ -1707,7 +1796,9 @@
fprintf(stderr, "Memory allocation failure.\n");
return 1;
}
for(i = optind, count = 0; i < argc; ++i, ++count) {
 
count = 0;
for(i = optind, count = 0; i < argc; ++i) {
if((path = PathToAbsolute(argv[i])) == NULL) {
fprintf(stderr, "Absolute path for '%s' failed to resolve.\n", argv[optind]);
continue;
@@ -1761,6 +1852,7 @@
}
 
sprintf(paths[count], "%s", path);
++count;
 
#if defined ___AmigaOS___
UnLock(lock);
@@ -1770,6 +1862,12 @@
 
}
 
if(count == 0) {
fprintf(stderr, "No valid paths are available.\n");
free(paths);
return 1;
}
 
if(verbose) {
fprintf(stdout, "Gathering to: '%s'\n", dbFile);
}
@@ -1778,13 +1876,15 @@
locale = OpenLocale(NULL);
#endif
 
// Gather.
switch(operation) {
case CREATE:
if(verbose) {
fprintf(stdout, "Removing '%s' and creating a new database.\n", dbFile);
}
RemoveFile(dbFile);
if(RemoveFile(dbFile) == FALSE) {
fprintf(stderr, "File '%s' could not be removed.\n", dbFile);
break;
}
case GATHER:
if(verbose) {
fprintf(stdout, "Gathering files to database...\n");
@@ -1798,6 +1898,7 @@
RemoveDatabaseFiles(dbFile, paths, count);
break;
default:
fprintf(stderr, "Unknown operation.\n");
break;
}
 
@@ -1807,5 +1908,10 @@
 
free(paths);
 
#if defined MWDEBUG
/* Generate a memory usage report */
MWReport("At end of main()", MWR_FULL);
#endif
 
return 0;
}