home *** CD-ROM | disk | FTP | other *** search
/ HomeWare 14 / HOMEWARE14.bin / prog / kfs.arj / KFSSAMP.C < prev    next >
Text File  |  1994-02-16  |  9KB  |  204 lines

  1. /************************************************************************/
  2. /* This is a sample program for the Keyed File System. It will create a */
  3. /* keyed file from a sequential input file and perform various          */
  4. /* operations on the keyed file.                                        */
  5. /************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. #include <kfs.h>                /* Need this for KFS                    */
  12.  
  13. KFS_FILEINFO file1;             /* Keyed file declaration               */
  14. FILE *infile;                   /* Normal C file declaration            */
  15.  
  16. struct TR {                     /* A data structure for the keyed file  */
  17.   char fld1[4];
  18.   char keyfld[15];              /* The key field of the keyed file      */
  19.   char fld2;
  20.   short inputseq;
  21.   char desc[40];
  22.   char rest[18];
  23.   } tr;
  24.  
  25. char inarea[80];
  26. /* These internal routines will show you some of the information in the */
  27. /* KFS_FILEINFO structure "file1" and print the keyed file.             */
  28. static void prtKFS(KFS_FILEINFO *, char *);
  29. static void prttr(struct TR, char *);
  30.  
  31. void main(argc, argv, envp)
  32.   int argc;
  33.   char *argv[];
  34.   char *envp[];
  35.   {
  36.     int i;
  37.  
  38. /* These five statements needed for a KFS_Create                        */
  39.     file1.KFS_keypos = 4;
  40.     file1.KFS_keylen = 15;
  41.     file1.KFS_recsize = sizeof(tr);
  42.     file1.KFS_flags = KFS_Normal_PTR;
  43.     strcpy(file1.KFS_filename, "keyfile.001");
  44.  
  45. /* Create a new file named KEYFILE.001 in the current directory         */
  46.     KFS_Create(&file1);
  47.     prtKFS(&file1, "Print after KFS_Create");
  48.  
  49. /* This is how you check the return code from a KFS function            */
  50.     if (file1.KFS_rc == KFS_File_Already_Exists) {                     
  51.       printf("Keyed file already exists, program terminated!\n");      
  52.       return;                                                          
  53.     } /* endif */                                                      
  54.     KFS_Close(&file1);                                                 
  55.     prtKFS(&file1, "Print after KFS_Close");                           
  56.                                                                        
  57. /* To open an existing file, specify only the file name and path        */
  58.     strcpy(file1.KFS_filename, "keyfile.001");
  59.     KFS_Open(&file1);
  60.     prtKFS(&file1, "Print after KFS_Open");
  61.     if (file1.KFS_rc != KFS_OK) {
  62.       printf("Error opening new keyed file, program terminated!\n");
  63.       return;
  64.     } /* endif */
  65.  
  66. /* Open the sequential input file we'll use to build the keyed file     */
  67.     if ((infile = fopen("kfssamp.dat", "r")) == NULL)
  68.       {
  69.        printf("Cannot open input file!\n");
  70.        return;
  71.       }
  72.  
  73. /* Just put some stuff in the keyed record for all records              */
  74.     strncpy(tr.fld1, "REC", sizeof(tr.fld1));                          
  75.     strcpy(tr.rest, "End of record !");                                
  76.     tr.fld2 = ' ';                                                     
  77.     tr.inputseq = 0;
  78.     fgets(inarea, 80, infile);                                         
  79.                                                                        
  80. /* Read all the input records and build the keyed file                  */
  81.     while (!feof(infile)) {
  82.       tr.inputseq++;            /* This shows what sequence record added*/
  83.  
  84. /* Insure key field has no garbage (not really necessary here)          */
  85.       memset(tr.keyfld, 0, sizeof(tr.keyfld));                       
  86.                                                                      
  87. /* Copy in key from sequential file                                     */
  88.       strncpy(tr.keyfld, inarea, sizeof(tr.keyfld));
  89.  
  90.       strcpy(tr.desc, &inarea[sizeof(tr.keyfld)]);
  91.       KFS_Add(&file1, &tr);     /* Add each new record to the file      */
  92.       if (file1.KFS_rc != KFS_OK)
  93.          if (file1.KFS_rc == KFS_No_Space_On_Disk) {
  94.             printf("Not enough space to add key %s\n",
  95.                file1.KFS_rc, tr.keyfld);
  96.          } else {
  97.             printf("Bad return code from KFS_Add, rc = %d, key = %s\n",
  98.                file1.KFS_rc, tr.keyfld);
  99.          } /* endif */
  100.       fgets(inarea, 80, infile);
  101.     } /* endwhile */
  102.  
  103. /* Read a record by key                                                 */
  104.     strncpy(tr.keyfld, "Dmiddle key    ", 15);
  105.     KFS_Read(&file1, &tr);
  106.     switch (file1.KFS_rc) {     /* We could check KFS_rc like this      */
  107.     case KFS_Key_Not_Found:
  108.        printf("Key not found error\n");
  109.        break;
  110.     case KFS_File_Error:
  111.        printf("Error reading file, file damaged?\n");
  112.        break;
  113.     } /* endswitch */
  114.     prtKFS(&file1, "Print after KFS_Read"); /* Show the file structure  */
  115.     prttr(tr, "TR structure after read");   /* Show the data area       */
  116.  
  117. /* Now read the first record in the file                                */
  118.     KFS_ReadFirst(&file1, &tr);
  119.     prtKFS(&file1, "Print after KFS_ReadFirst");
  120.     prttr(tr, "TR structure after read");
  121.  
  122. /* Now read the second record in the file                               */
  123.     KFS_ReadNext(&file1, &tr);                                         
  124.     prtKFS(&file1, "Reading sequentially");                            
  125.                                                                        
  126. /* Now read the rest of the file                                        */
  127.     while (file1.KFS_rc != KFS_EOF) {                                  
  128.       prttr(tr, "TR structure after read");                            
  129.       KFS_ReadNext(&file1, &tr);                                       
  130.     } /* endwhile */                                                   
  131.     printf("End of file reached\n");                                   
  132.                                                                        
  133. /* Delete a record (notice we didn't read it first)                     */
  134.     strncpy(tr.keyfld, "Ni   key       ",15);
  135.     KFS_Delete(&file1, &tr);                                           
  136.     prtKFS(&file1, "Print after KFS_Delete");                          
  137.                                                                        
  138. /* Notice if we do a ReadNext that we get an error (Prev key must exist)*/
  139.     KFS_ReadNext(&file1, &tr);
  140.     prtKFS(&file1, "Print after KFS_ReadNext");
  141.     if (file1.KFS_rc == KFS_Prior_Key_Not_Found) {
  142.        printf("We (correctly) cant do a ReadNext when prior key is gone\n");
  143.     prttr(tr, "TR structure still contains old information (except key)");
  144.     } /* endif */
  145.                                                                        
  146. /* But a ReadGen will work to give us the next record                   */
  147.     KFS_ReadGen(&file1, &tr, 15);
  148.     prtKFS(&file1, "Print after KFS_ReadGen");
  149.     if (file1.KFS_rc == KFS_Key_Not_Found) {
  150.        printf("We couldnt find the key but still got the next record\n");
  151.     } /* endif */
  152.     prttr(tr, "TR structure now contains next record");
  153.                                                                        
  154. /* Update and replace an existing record                                */
  155.     strncpy(tr.keyfld, "Dsecond key    ",15);                          
  156.     strcpy(tr.fld1, "REP");                                            
  157.     strcpy(tr.desc, "This record was replaced\n");                     
  158.     KFS_Replace(&file1, &tr);                                          
  159.     prtKFS(&file1, "Print after KFS_Replace");                         
  160.     KFS_Read(&file1, &tr);                                             
  161.     prtKFS(&file1, "Verifying replace by reading");
  162.                                                                        
  163. /* Read the complete file again                                         */
  164.     KFS_ReadFirst(&file1, &tr);
  165.     prtKFS(&file1, "Print after KFS_ReadFirst");
  166.     prttr(tr, "TR structure after read");
  167.  
  168.     KFS_ReadNext(&file1, &tr);
  169.     prtKFS(&file1, "Reading sequentially");
  170.     while (file1.KFS_rc == KFS_OK) {
  171.       prttr(tr, "TR structure after read");
  172.       KFS_ReadNext(&file1, &tr);
  173.     } /* endwhile */
  174.     if (file1.KFS_rc == KFS_EOF)
  175.       printf("\n\nEnd of file reached!!\n");
  176.     else
  177.       printf("Some other error ocurred\n");
  178.  
  179. /* Always close the file when finished                                  */
  180.     KFS_Close(&file1);
  181.   }
  182.  
  183. void prtKFS(KFS_FILEINFO *f, char *header)
  184.   {
  185.     printf("\n%s\n", header);
  186.     printf("KFS_rc = %d\n", f->KFS_rc);
  187.     printf("KFS_keypos = %d\n", f->KFS_keypos);
  188.     printf("KFS_keylen = %d\n", f->KFS_keylen);
  189.     printf("KFS_recsize = %d\n", f->KFS_recsize);
  190.     printf("KFS_filename = %s\n", f->KFS_filename);
  191.  
  192.   }
  193.  
  194. void prttr(struct TR tr, char *header)
  195.   {
  196.     char cwork[40];
  197.     printf("\n%s\n", header);
  198.     strncpy(cwork, tr.keyfld, sizeof(tr.keyfld));
  199.     cwork[sizeof(tr.keyfld)] = '\0';
  200.     printf("tr.keyfld = %s\n", cwork);
  201.     printf("tr.inputseq = %d\n", tr.inputseq);
  202.     printf("tr.desc = %s\n", tr.desc);
  203.   }
  204.