HuntnGather

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 37  →  ?path2? @ 38
/trunk/HuntnGather/shared/utilities.c
@@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#if !defined ___AmigaOS___
#include <dirent.h>
#include <sys/stat.h>
@@ -38,7 +39,7 @@
#else
int StrlenMax(char *a, char *b) {
int q = strlen(a);
int p = strien(b);
int p = strlen(b);
#endif
return p > q ? p : q;
}
@@ -52,9 +53,9 @@
LONG p = strlen(a);
LONG q = strlen(b);
#else
int StrlenMax(char *a, char *b) {
int StrlenMin(char *a, char *b) {
int q = strlen(a);
int p = strien(b);
int p = strlen(b);
#endif
return p > q ? q : p;
}
@@ -102,6 +103,7 @@
fprintf(stderr, "Path '%s' could not be examined.\n", path);
UnLock(lock);
FreeDosObject(DOS_FIB, FIB);
FIB = NULL;
return UNKNOWN;
}
 
@@ -108,6 +110,7 @@
if(FIB->fib_DirEntryType < 0) {
UnLock(lock);
FreeDosObject(DOS_FIB, FIB);
FIB = NULL;
return REGULAR;
#else
stat(path, &dirStat);
@@ -119,6 +122,7 @@
#if defined ___AmigaOS___
UnLock(lock);
FreeDosObject(DOS_FIB, FIB);
FIB = NULL;
#endif
 
return DIRECTORY;
@@ -155,7 +159,7 @@
#if defined ___AsyncIO___
while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
#else
while(run && fscanf(fp, "%c", &c) == 1) {
while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) {
#endif
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
@@ -266,21 +270,25 @@
*
* Create multiple temporary files and return their names.
*/
char **CreateTemporaryFiles(int files) {
char **tmpNames;
int count;
VECTOR *CreateTemporaryFiles(int files) {
VECTOR *tmpNames;
 
if((tmpNames = malloc(files * sizeof(*tmpNames))) == NULL) {
if((tmpNames = malloc(1 * sizeof(*tmpNames))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
 
if((tmpNames->array = malloc(files * sizeof(*tmpNames->array))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
 
if(PROGRAM_VERBOSE) {
fprintf(stdout, "Creating temporary files...\r");
}
 
count = files;
while(PROGRAM_RUN && --count > -1) {
tmpNames->length = 0;
while(PROGRAM_RUN && --files > -1) {
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
@@ -288,7 +296,8 @@
continue;
}
#endif
tmpNames[count] = CreateTemporaryFile();
tmpNames[files] = CreateTemporaryFile();
++tmpNames->length;
 
if(PROGRAM_VERBOSE) {
fprintf(stdout, "Creating temporary files: %d%%.\r", 100 - (int)(((float)count / files) * 100.0));
@@ -329,29 +338,58 @@
}
}
 
/*
*
* Peeks for the next line in a file.
*/
#if defined ___AsyncIO___
dbLine *PeekLine(struct AsyncFile *fp) {
#else
dbLine *PeekLine(FILE *fp) {
#endif
dbLine *line;
 
// Read the next line.
if((line = ReadLine(fp)) == NULL) {
return NULL;
}
 
// Rewind the file by the number of read characters.
#if defined ___AsyncIO___
if(SeekAsync(fp, -(line->length + 1), MODE_CURRENT) == -1) {
fprintf(stderr, "Could not seek in file.\n");
free(line->string);
free(line);
line = NULL;
return NULL;
}
#else
if(fseek(fp, -(line->length + 1), SEEK_CUR) != 0) {
fprintf(stderr, "Could not seek in file.\n");
free(line->string);
free(line);
line = NULL;
return NULL;
}
#endif
 
return line;
}
 
/*
*
* Peeks at a line from a file.
* Reads the next line in a file.
*/
#if defined ___AsyncIO___
char *PeekLine(struct AsyncFile *fp) {
dbLine *ReadLine(struct AsyncFile *fp) {
LONG c;
#else
char *PeekLine(FILE *fp) {
dbLine *ReadLine(FILE *fp) {
char c;
#endif
char *line = NULL;
char *real = NULL;
int size;
int i;
dbLine *line;
 
size = LINE_BUF;
if((line = malloc(size * sizeof(*line))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
 
i = 0;
#if defined ___AsyncIO___
while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
@@ -371,103 +409,61 @@
#if defined ___AsyncIO___
if(SeekAsync(fp, -(i + 1), MODE_CURRENT) == -1) {
fprintf(stderr, "Could not seek in file.\n");
free(line);
fprintf(stderr, "index at: %d\n", i);
return NULL;
}
#else
if(fseek(fp, -(i + 1), SEEK_CUR) != 0) {
fprintf(stderr, "Could not seek in file.\n");
free(line);
return NULL;
}
#endif
return line;
default:
if(strlen(line) == size) {
size = size * 1.5;
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';
break;
goto LINE;
}
 
++i;
}
 
if(line != NULL) {
LINE:
 
if(i == 0) {
return NULL;
}
 
if((line = malloc(1 * sizeof(*line))) == NULL) {
fprintf(stderr, "Memory allocation error.\n");
return NULL;
}
 
if((line->string = malloc((i + 1) * sizeof(*line->string))) == NULL) {
fprintf(stderr, "Memory allocation error.\n");
free(line);
line = NULL;
return NULL;
}
 
return NULL;
}
 
/*
*
* Read a line from a file.
*/
#if defined ___AsyncIO___
char *ReadLine(struct AsyncFile *fp) {
LONG c;
if(ReadAsync(fp, line->string, i) != i) {
#else
char *ReadLine(FILE *fp) {
char c;
if(fread(line->string, sizeof(char), i, fp) != i) {
#endif
char *line = NULL;
char *real = NULL;
int size;
int i;
 
size = LINE_BUF;
if((line = malloc(size * sizeof(*line))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
fprintf(stderr, "Unable to read line.\n");
free(line->string);
free(line);
line = NULL;
return NULL;
}
 
i = 0;
// Clear newline.
#if defined ___AsyncIO___
while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
ReadCharAsync(fp);
#else
while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) {
fgetc(fp);
#endif
#if defined ___AmigaOS___
// Check if CTRL+C was pressed and abort the program.
if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
PROGRAM_RUN = FALSE;
continue;
}
#endif
switch(c) {
case '\n':
return line;
default:
if(strlen(line) == size) {
size = size * 1.5;
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';
break;
}
++i;
}
 
if(line != NULL) {
free(line);
}
line->length = i;
 
return NULL;
return line;
}
 
/*
@@ -573,12 +569,11 @@
#endif
}
 
 
/*
*
* Create a database entry from a line of text.
*/
dbEntry* CreateDatabaseEntry(char *line) {
dbEntry* CreateDatabaseEntry(dbLine *line) {
dbEntry *entry;
char *ptr;
int side;
@@ -590,17 +585,17 @@
return NULL;
}
 
if((entry->name = malloc((strlen(line) + 1) * sizeof(*entry->name))) == NULL) {
if((entry->name = malloc((line->length + 1) * sizeof(*entry->name))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
 
if((entry->path = malloc((strlen(line) + 1) * sizeof(*entry->path))) == NULL) {
if((entry->path = malloc((line->length + 1) * sizeof(*entry->path))) == NULL) {
fprintf(stderr, "Memory allocation failure.\n");
return NULL;
}
 
for(ptr = line, side = 0, i = 0, j = 0; PROGRAM_RUN && *ptr != '\0'; ++ptr) {
for(ptr = line->string, side = 0, i = 0, j = 0; PROGRAM_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) {
@@ -632,7 +627,6 @@
return entry;
}
 
 
/*
* Compare two strings.
*/
@@ -668,6 +662,7 @@
}
 
FreeVec(pattern);
pattern = NULL;
}
 
return success;
@@ -679,8 +674,8 @@
success = FALSE;
 
#if defined ___NOCASE_FS___
e = StrUpr(e);
n = StrUpr(n);
StrUpr(e);
StrUpr(n);
#endif
 
// search for substring
/trunk/HuntnGather/shared/utilities.h
@@ -15,6 +15,11 @@
#define LINE_BUF 256
#define DEFAULT_DATABASE_FILE "S:gather.db"
 
typedef struct {
void **array;
int length;
} VECTOR;
 
typedef enum FS_TYPE {
UNKNOWN,
REGULAR,
@@ -21,7 +26,6 @@
DIRECTORY
} FS_TYPE;
 
 
typedef struct {
int dirs;
int files;
@@ -34,6 +38,11 @@
char *path;
} dbEntry;
 
typedef struct {
char *string;
int length;
} dbLine;
 
typedef enum OPERATION {
NONE,
GATHER,
@@ -58,12 +67,12 @@
 
#if defined ___AsyncIO___
extern void SkipLine(struct AsyncFile *fp);
extern char *PeekLine(struct AsyncFile *fp);
extern char *ReadLine(struct AsyncFile *fp);
extern dbLine *PeekLine(struct AsyncFile *fp);
extern dbLine *ReadLine(struct AsyncFile *fp);
#else
extern void SkipLine(FILE *fp);
extern char *PeekLine(FILE *fp);
extern char *ReadLine(FILE *fp);
extern dbLine *PeekLine(FILE *fp);
extern dbLine *ReadLine(FILE *fp);
#endif
 
extern void StrUpr(char *);
@@ -75,4 +84,4 @@
extern void RemoveFiles(char **names, int count);
extern void CopyFile(char *a, char *b);
 
extern dbEntry* CreateDatabaseEntry(char *line);
extern dbEntry* CreateDatabaseEntry(dbLine *line);