home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Scheduling / Cassandra / Source / future / convert / EventDatabase.m < prev    next >
Encoding:
Text File  |  1990-06-10  |  6.8 KB  |  323 lines

  1. //
  2. // Database.m
  3. // Copyright (c) 1990 by Jiro Nakamura 
  4. // All rights reserved
  5. //
  6. //
  7. // RCS Information
  8. // Revision Number->    $Revision: 2.3 $
  9. // Last Revised->    $Date: 90/02/03 13:01:44 $
  10. //
  11. static char rcsid[] = "$Header: /user/jiro/Programming/Cassandra/src/RCS/Event.m,v 2.3 90/02/03 13:01:44 jiro Exp $";
  12.  
  13. #import "Database.h"
  14. #import <objc/objc.h>
  15. #import <strings.h>
  16. #import <appkit/Panel.h>       /* for NXRunAlertPanel */
  17. #import <appkit/Cursor.h>    // For NXWait
  18. #import "misc.h"
  19.  
  20.  
  21. @implementation Database 
  22.  
  23. + new
  24. {
  25. #ifdef DEBUG
  26.     fprintf(stderr,"Database.m:New\n");
  27. #endif
  28.  
  29.     self = [super new];
  30.     [self setDefaults];
  31.     return self;
  32. }
  33.  
  34. + newAt: (char *) filename;
  35. {
  36. #ifdef DEBUG
  37.     fprintf(stderr,"Database.m:NewAt %s\n", filename);
  38. #endif
  39.  
  40.     self = [super new];
  41.     present = next = previous = -1;
  42.     recordLength = 512;
  43.  
  44.     strcpy(dataFile, filename);
  45.     return self;
  46. }
  47.  
  48.  
  49. - free
  50. {
  51. #ifdef DEBUG
  52.     fprintf(stderr,"Database.m:Free ->Closing event!\n");
  53. #endif
  54.     [super free];
  55.     return nil;
  56. }
  57.  
  58.  
  59. - setDefaults
  60. {
  61.     present = next = previous = -1;
  62.     strcpy(dataFile, "-- File undefined --");
  63.     recordLength = 512;
  64.     return self;
  65. }
  66.  
  67.  
  68. - (int) readRecord: (RecordPointer) here    /* read record from data file */
  69. {
  70.     FILE * source;
  71.     static char errormsg[128], buf[512];
  72.     char *sPointer;
  73.         
  74.     #ifdef DEBUG
  75.         fprintf(stderr,    "------------------------\n"
  76.                 "[Database readRecord: %d]\n",here);
  77.     #endif
  78.     
  79.  
  80.     if( here < 0)
  81.         return ILLEGAL_RECORD_NUMBER;
  82.  
  83.     sprintf(errormsg, "Fatal error occured while trying to read database %s", dataFile);
  84.     source = fileOpen(dataFile, "r+",errormsg);
  85.  
  86.     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);
  87.     if( fileSeek(source, here, errormsg) == NULL)
  88.         {
  89.         present = here;
  90.         return SEEK_ERROR;
  91.         }
  92.  
  93.     for(;;)    
  94.         {
  95.         if( fgets( buf,512, source) == NULL)
  96.             break;
  97.         
  98.         if(strcmp(buf,"Finish.") == 0)
  99.             break;
  100.         
  101.         fprintf(stderr,"Buffer is %s.\n", buf);
  102.         
  103.         sPointer = index(buf,':') + 1;
  104.         *(sPointer - 1) = '\0';        
  105.         fprintf(stderr, 
  106.             "Buffer is now %s, sPointer is %s\n",buf,sPointer);
  107.     /*    [self @selector(buf2) with: value]; */
  108.         }
  109.     
  110.     
  111.     fclose(source);
  112.     return NO_ERROR;
  113. }
  114.                 
  115. - (int) firstRecord
  116. {
  117.     #ifdef DEBUG
  118.         fprintf(stderr,"Reading the first record.\n");
  119.     #endif
  120.  
  121.     [self readRecord : 0];
  122.     if( [self next] == 0)
  123.         return END_OF_RECORDS;
  124.     [self readRecord : [self next]];
  125.     return NO_ERROR;
  126. }    
  127.     
  128. - (int) writeRecord : (RecordPointer) here    
  129. /* write itself into the database using present as its index */
  130. {
  131.     FILE *source;
  132.     char errormsg[256];
  133.         
  134.     if( here < 0)
  135.         return ILLEGAL_RECORD_NUMBER;
  136.         
  137.     #ifdef DEBUG
  138.         fprintf(stderr,    "------------------------\n"
  139.                 "[Database writeRecord: %d]: Opening event with <%s>!\n",here, dataFile);
  140.     #endif
  141.  
  142.     sprintf(errormsg, "Fatal error when trying to write to database %s", dataFile);
  143.     source = fileOpen(dataFile, "r+",errormsg);
  144.     
  145.     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);
  146.     fileSeek(source, here, errormsg);
  147.     
  148.     present = here;
  149.     
  150.     fclose(source);
  151.     return NO_ERROR;
  152. }
  153.  
  154. - (RecordPointer) insertRecord
  155. {
  156.     return [self insertRecordFrom : 1];
  157. }
  158.  
  159.  
  160.  
  161. /* insert itself into the file with method indexCompare:with: as indexer */
  162. /* and starting from <here> in searching for a open deleted space */
  163. - (RecordPointer) insertRecordFrom : (RecordPointer) here
  164. {
  165.     id presentRecord, stepRecord;
  166.     int loop;
  167.     
  168.     // This may take a while, so use the system wait cursor
  169.     [NXWait push];
  170.  
  171.     presentRecord = [[self class] newAt: dataFile];
  172.     stepRecord = [[self class] newAt: dataFile];
  173.     
  174.     [presentRecord readRecord:0];
  175.     for( [presentRecord readRecord: [presentRecord next]]; 
  176.           [presentRecord present] != 0 && [self indexCompare: presentRecord with:self] < 1;
  177.           [presentRecord readRecord: [presentRecord next]])
  178.               {
  179.               #ifdef DEBUG
  180.             fprintf(stderr, "From %d looping onto %d\n",[presentRecord present],[presentRecord next]);
  181.         #endif
  182.         }
  183.     #ifdef DEBUG    
  184.         fprintf(stderr, "inserting between %d <- event -> %d.\n",[presentRecord previous], [presentRecord present]);
  185.     #endif
  186.     
  187.     previous = [presentRecord previous];
  188.     next = [presentRecord present];
  189.     
  190.     [stepRecord setNext: 0];
  191.     for( loop = here; !([stepRecord next] == -1 && [stepRecord previous] == -1);
  192.         [stepRecord readRecord: loop++])
  193.         {
  194.         #ifdef DEBUG
  195.             fprintf(stderr, "reading at %d.\n",loop);
  196.         #endif
  197.         }
  198.         
  199.     present = [stepRecord present];
  200.     
  201.     [self writeRecord: present];
  202.     
  203.     [stepRecord readRecord: previous];
  204.     [stepRecord setNext : present];
  205.     [stepRecord writeRecord:previous];
  206.     
  207.     [stepRecord readRecord: next];
  208.     [stepRecord setPrevious : present];
  209.     [stepRecord writeRecord:next];
  210.     
  211.     [stepRecord free];
  212.     [presentRecord free];
  213.     #ifdef DEBUG
  214.         fprintf(stderr,"successfully inserted %d <- %d -> %d\n\n",[self previous],[self present], [self next]);
  215.     #endif
  216.  
  217.  
  218.     // Finished, restore the cursor
  219.     NXPing();
  220.     [NXWait pop];
  221.     
  222.     return( [self present] );
  223. }
  224.  
  225. - deleteRecord: (RecordPointer) here        /* delete itself from the file with present as index */
  226. {
  227.     id previousRecord, nextRecord;
  228.     
  229.     #ifdef DEBUG
  230.         fprintf(stderr, "Deleting <%d>\n\n",here);
  231.     #endif
  232.     
  233.     if( here <= 0)
  234.         {
  235.             NXRunAlertPanel(NULL,"You cannot delete link 0 or below. OK?", "Shucks...",NULL,NULL);
  236.             fprintf(stderr,"Can't delete the flipping header, ok!  Here = %d.\n\n",here);
  237.             return nil;
  238.         }
  239.     
  240.     previousRecord= [[self class] newAt: dataFile];
  241.     nextRecord = [[self class] newAt: dataFile];
  242.     
  243.     [previousRecord readRecord  :here];
  244.     if( ( [previousRecord previous] == -1  )   ||  (  [previousRecord next]  == -1  ))
  245.         return nil;
  246.     
  247.     [nextRecord readRecord :[previousRecord next]];                /* Set the previous_link of the next link */
  248.     [nextRecord setPrevious  : [previousRecord previous]];            /* to the one of the previous one */
  249.     [nextRecord writeRecord  :[previousRecord next]];                /* confusing, eh? */
  250.     
  251.     [nextRecord readRecord  :[previousRecord previous] ];    
  252.     /* do the same confusing spiel for previous */
  253.     [nextRecord setNext  : [previousRecord next]];
  254.     [nextRecord writeRecord  :[previousRecord previous] ];
  255.     
  256.     [previousRecord setNext  : -1];    /* cancel out the header to finally kill it */
  257.     [previousRecord setPrevious  :-1];    
  258.     [previousRecord writeRecord  :here];    /* and write it out */
  259.     
  260.     #ifdef DEBUG
  261.         fprintf(stderr, "Record %d successfully deleted, O Leader.\n\n",here);
  262.     #endif
  263.     
  264.     [previousRecord free];
  265.     [nextRecord free];
  266.     return self;
  267. }
  268.  
  269.  
  270. - (int) indexCompare: (id) first  with: (id) second
  271. {
  272.     return 0;
  273. }
  274.  
  275.  
  276.  
  277.  
  278. - (RecordPointer) present
  279. {
  280.     return present;
  281. }
  282.  
  283. - (RecordPointer) previous
  284. {
  285.     return previous;
  286. }
  287.  
  288. - (RecordPointer) next
  289. {
  290.     return next;
  291. }
  292.  
  293. - setPresent : (RecordPointer) apresent
  294. {
  295.     present = apresent;
  296.     return self;
  297. }
  298. - setPrevious  : (RecordPointer) aprevious
  299. {
  300.     previous =aprevious;
  301.     return self;
  302. }
  303.  
  304. - setNext  : (RecordPointer) anext
  305. {
  306.     next = anext;
  307.     return self;
  308. }
  309.  
  310. - (char *) dataFile;
  311. {
  312.     return dataFile;
  313. }
  314.  
  315. - setDataFile: (char *) filename
  316. {
  317.     strcpy(dataFile, filename);
  318.     return self;
  319. }
  320. @end        
  321.  
  322.  
  323.