HuntnGather

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 23  →  ?path2? @ 24
/trunk/HuntnGather/Gather/Gather.c
@@ -96,7 +96,7 @@
#else
if((fp = fopen(dbFile, "r")) == NULL) {
#endif
fprintf(stderr, "Unable to open gather database for reading.\n");
fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile);
return;
}
 
@@ -111,8 +111,9 @@
i = 0;
 
if(verbose) {
fprintf(stdout, "Sorting temporary database file: '%s'\n", dbFile);
fprintf(stdout, "Sorting '%s'\n", dbFile);
}
 
#if defined ___AsyncIO___
while(run && (c = ReadCharAsync(fp)) != -1) {
#else
@@ -168,7 +169,7 @@
path[i + 1] = '\0';
break;
default:
fprintf(stderr, "Database corrupted: %d\n", side);
fprintf(stderr, "File '%s' is corrupted.\n", dbFile);
break;
}
++i;
@@ -191,7 +192,7 @@
#else
if((fp = fopen(dbFile, "w")) == NULL) {
#endif
fprintf(stderr, "Unable to open gather database for writing.\n");
fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile);
return;
}
 
@@ -224,7 +225,7 @@
*
* Updates a database file "dbFile".
*/
void UpdateDatabase(char *dbFile, stringStack *dirStack, stats *stats) {
void CollectFiles(char *dbFile, stringStack *dirStack, stats *stats) {
#if defined ___AsyncIO___
struct AsyncFile *fp;
#else
@@ -242,10 +243,14 @@
#else
if((fp = fopen(dbFile, "w")) == NULL) {
#endif
fprintf(stderr, "Unable to open gather database for writing.\n");
fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile);
return;
}
 
if(verbose) {
fprintf(stdout, "Collecting files...\r");
}
 
while(run && !stringStackIsEmpty(dirStack)) {
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
@@ -259,6 +264,7 @@
}
 
if((dir = opendir(path)) == NULL) {
fprintf(stderr, "Unable to open '%s' for reading.\n", path);
return;
}
 
@@ -359,7 +365,7 @@
#else
if((fp = fopen(dbFile, "r")) == NULL) {
#endif
fprintf(stderr, "Unable to open gather database for reading.\n");
fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile);
return 0;
}
 
@@ -384,7 +390,7 @@
*
* Counts the lines of a file.
*/
int GetFileLines(char *dbFile) {
int CountFileLines(char *dbFile) {
#if defined ___AsyncIO___
struct AsyncFile *fp;
LONG c;
@@ -399,7 +405,7 @@
#else
if((fp = fopen(dbFile, "r")) == NULL) {
#endif
fprintf(stderr, "Unable to open gather database for reading.\n");
fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile);
return 0;
}
 
@@ -443,7 +449,7 @@
tmpNames = malloc(files * sizeof(*tmpNames));
 
if(verbose) {
fprintf(stdout, "Creating temporary files.\r");
fprintf(stdout, "Creating temporary files...\r");
}
 
count = files;
@@ -457,6 +463,9 @@
#endif
tmpNames[count] = tmpnam(NULL);
 
// Remove file to ensure that the file is empty.
remove(tmpNames[count]);
 
if(verbose) {
fprintf(stdout, "Creating temporary files: %d%%\r", 100 - (int)(((float)count / files) * 100.0));
}
@@ -489,7 +498,7 @@
#else
if((fp = fopen(dbFile, "r")) == NULL) {
#endif
fprintf(stderr, "Unable to open gather database '%s' for reading.\n", dbFile);
fprintf(stderr, "Unable to open '%s' for reading.\n", dbFile);
return;
}
 
@@ -498,12 +507,17 @@
#else
if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) {
#endif
fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]);
fprintf(stderr, "Unable to open '%s' for writing.\n", tmpNames[tmpFiles]);
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return;
}
 
if(verbose) {
fprintf(stdout, "Writing to temporary files.\r");
fprintf(stdout, "Writing to temporary files...\r");
}
 
linesWritten = 0;
@@ -535,7 +549,7 @@
#else
if(fprintf(tp, "%c", c) != 1) {
#endif
fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]);
fprintf(stderr, "Unable to write to '%s'.\n", tmpNames[tmpFiles]);
#if defined ___AsyncIO___
CloseAsync(tp);
CloseAsync(fp);
@@ -560,14 +574,13 @@
fclose(tp);
if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) {
#endif
fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]);
fprintf(stderr, "Unable to open '%s' for writing.\n", tmpNames[tmpFiles]);
#if defined ___AsyncIO___
CloseAsync(tp);
CloseAsync(fp);
#else
fclose(tp);
fclose(fp);
#endif
return;
}
lines = 0;
break;
@@ -579,7 +592,7 @@
#else
if(fprintf(tp, "%c", c) != 1) {
#endif
fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]);
fprintf(stderr, "Unable to write to '%s'.\n", tmpNames[tmpFiles]);
#if defined ___AsyncIO___
CloseAsync(tp);
CloseAsync(fp);
@@ -593,7 +606,9 @@
}
}
 
fprintf(stdout, "\n");
if(verbose) {
fprintf(stdout, "\n");
}
 
#if defined ___AsyncIO___
CloseAsync(tp);
@@ -606,7 +621,7 @@
 
/*
*
* Skips a line in a database file "fp".
* Skips a line in a file.
*/
 
#if defined ___AsyncIO___
@@ -634,7 +649,7 @@
 
/*
*
* Reads a line from the database file "fp".
* Reads a line from a file.
*/
#if defined ___AsyncIO___
char *ReadLine(struct AsyncFile *fp) {
@@ -695,7 +710,7 @@
*
* Merges temporary files "tmpNames" into a database "dbFile".
*/
void MergeDatabase(char *dbFile, char **tmpNames, int files, int lines) {
void MergeTemporaryFiles(char *dbFile, char **tmpNames, int files, int lines) {
#if defined ___AsyncIO___
struct AsyncFile *fp;
struct AsyncFile **tp;
@@ -714,7 +729,7 @@
#else
if((fp = fopen(dbFile, "w")) == NULL) {
#endif
fprintf(stderr, "Unable to open gather database for writing.\n");
fprintf(stderr, "Unable to open '%s' for writing.\n", dbFile);
return;
}
 
@@ -728,10 +743,9 @@
#else
if((tp[i] = fopen(tmpNames[i], "r")) == NULL) {
#endif
fprintf(stderr, "Unable to open temporary file '%s' for reading.\n", tmpNames[i]);
fprintf(stderr, "Unable to open '%s' for reading.\n", tmpNames[i]);
// Close all temporary files.
--i;
while(i >= 0) {
while(--i > -1) {
#if defined ___AsyncIO___
CloseAsync(tp[i]);
#else
@@ -738,12 +752,17 @@
fclose(tp[i]);
#endif
}
#if defined ___AsyncIO___
CloseAsync(fp);
#else
fclose(fp);
#endif
return;
}
}
 
if(verbose) {
fprintf(stdout, "Merging all database lines in temporary files.\r");
fprintf(stdout, "Merging all files...\r");
}
 
count = lines;
@@ -758,7 +777,7 @@
#endif
// Find the smallest line in all temporary files.
if(verbose) {
fprintf(stdout, "Merging all database lines in temporary files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0));
fprintf(stdout, "Merging all files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0));
}
 
min = NULL;
@@ -798,7 +817,14 @@
}
 
// Write out any remaining contents from the temporary files.
for(i = 0; i < files; ++i) {
for(i = 0; run && i < files; ++i) {
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
run = FALSE;
continue;
}
#endif
tmp = ReadLine(tp[i]);
if(tmp == NULL) {
continue;
@@ -812,7 +838,7 @@
free(tmp);
}
 
// Close and delete all temporary files.
// Close all temporary files.
for(i = 0; i < files; ++i) {
#if defined ___AsyncIO___
CloseAsync(tp[i]);
@@ -819,8 +845,6 @@
#else
fclose(tp[i]);
#endif
// Delete temporary file.
remove(tmpNames[i]);
}
 
if(verbose) {
@@ -836,6 +860,26 @@
 
/*
*
* Deletes temporary files.
*/
void DeleteTemporaryFiles(char **tmpNames, int tmpFiles) {
int i = tmpFiles;
 
if(verbose) {
fprintf(stdout, "Deleting temporary files...\r");
}
 
do {
remove(tmpNames[i]);
} while(--i > -1);
 
if(verbose) {
fprintf(stdout, "\n");
}
}
 
/*
*
* Indexes a "path" by creating a database "dbFile".
*/
void Gather(char *dbFile, char *path) {
@@ -853,11 +897,11 @@
stringStackPush(stack, path);
 
// Generate the database file.
UpdateDatabase(dbFile, stack, stats);
CollectFiles(dbFile, stack, stats);
 
// Get the database metrics.
dbSize = GetFileSize(dbFile);
dbLines = GetFileLines(dbFile);
dbLines = CountFileLines(dbFile);
 
// Compute the amount of temporary files needed.
tmpFiles = dbSize / MAX_MEM;
@@ -877,15 +921,19 @@
return;
}
 
// Write "tmpLines" to temporary files in "tmpFiles" from "dbFile".
// Write "tmpLines" to temporary files in "tmpNames" from "dbFile".
WriteTemporaryFiles(dbFile, tmpNames, tmpFiles, tmpLines, dbLines);
 
// Sort the temporary files.
for(i = 0; i < tmpFiles; ++i) {
for(i = 0; run && i < tmpFiles; ++i) {
SortDatabase(tmpNames[i]);
}
 
MergeDatabase(dbFile, tmpNames, tmpFiles, dbLines);
// Merge all the temporary files to the database file.
MergeTemporaryFiles(dbFile, tmpNames, tmpFiles, dbLines);
 
// Remove all temporary files.
DeleteTemporaryFiles(tmpNames, tmpFiles);
}
 
void usage(char *name) {
@@ -958,7 +1006,7 @@
}
 
if(verbose) {
fprintf(stdout, "Gathering to database file: %s\n", dbFile);
fprintf(stdout, "Gathering to %s\n", dbFile);
}
 
// Gather.