- (int) readRecord: (RecordPointer) here /* read record from data file */
{
FILE * source;
static char errormsg[128], buf[512];
char *sPointer;
#ifdef DEBUG
fprintf(stderr, "------------------------\n"
"[Database readRecord: %d]\n",here);
#endif
if( here < 0)
return ILLEGAL_RECORD_NUMBER;
sprintf(errormsg, "Fatal error occured while trying to read database %s", dataFile);
source = fileOpen(dataFile, "r+",errormsg);
sprintf(errormsg, "An fatal error occured when we were trying to seek to record #%d in database %s.\n This program will crash.", here, dataFile);
if( fileSeek(source, here, errormsg) == NULL)
{
present = here;
return SEEK_ERROR;
}
for(;;)
{
if( fgets( buf,512, source) == NULL)
break;
if(strcmp(buf,"Finish.") == 0)
break;
fprintf(stderr,"Buffer is %s.\n", buf);
sPointer = index(buf,':') + 1;
*(sPointer - 1) = '\0';
fprintf(stderr,
"Buffer is now %s, sPointer is %s\n",buf,sPointer);
/* [self @selector(buf2) with: value]; */
}
fclose(source);
return NO_ERROR;
}
- (int) firstRecord
{
#ifdef DEBUG
fprintf(stderr,"Reading the first record.\n");
#endif
[self readRecord : 0];
if( [self next] == 0)
return END_OF_RECORDS;
[self readRecord : [self next]];
return NO_ERROR;
}
- (int) writeRecord : (RecordPointer) here
/* write itself into the database using present as its index */
{
FILE *source;
char errormsg[256];
if( here < 0)
return ILLEGAL_RECORD_NUMBER;
#ifdef DEBUG
fprintf(stderr, "------------------------\n"
"[Database writeRecord: %d]: Opening event with <%s>!\n",here, dataFile);
#endif
sprintf(errormsg, "Fatal error when trying to write to database %s", dataFile);
source = fileOpen(dataFile, "r+",errormsg);
sprintf(errormsg, "An fatal error occured when we were trying to seek to record #%d for writing in database %s.\n This program has crashed.", here, dataFile);
fileSeek(source, here, errormsg);
present = here;
fclose(source);
return NO_ERROR;
}
- (RecordPointer) insertRecord
{
return [self insertRecordFrom : 1];
}
/* insert itself into the file with method indexCompare:with: as indexer */
/* and starting from <here> in searching for a open deleted space */
- (RecordPointer) insertRecordFrom : (RecordPointer) here
{
id presentRecord, stepRecord;
int loop;
// This may take a while, so use the system wait cursor