home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0766.lha / ISAM / examples / fileconvert.c < prev    next >
C/C++ Source or Header  |  1992-11-21  |  5KB  |  175 lines

  1.  
  2.  
  3. /* Here is a program template to use when you need to write a program to     */
  4. /* change an ISAM file's record length, or a field's offset or length (or,   */
  5. /* indeed, anything that cannot be done with ReIndex), and you already have  */
  6. /* lots of records stored and DON'T want to re-key all that data...          */
  7.  
  8. /* To do this, make a specs file for the new file information (different     */
  9. /* file names, please!), create the ISAM file, and use this program (suit-   */
  10. /* ably modified to work with your records and needs) to read records from   */
  11. /* the old file, transfer info from the old record to the new record, and    */
  12. /* store the new record in the new file.                                     */
  13.  
  14. /* NOTE: we do not lock the records read from the old file or stored in the  */
  15. /* new file, as we have locked both files with exclusive locks.              */
  16.  
  17. #include <MYLIB:ISAMLibPROTO.c>
  18.  
  19. void main ( int argc, char *(argv[]) );
  20.  
  21. #define OLDSPECSFILENAME   /* insert OLD specs file name here in quotes */
  22. #define NEWSPECSFILENAME   /* insert NEW specs file name here in quotes */
  23.  
  24.  
  25. struct OldRecord {
  26. }
  27.  
  28. struct NewRecord {
  29. }
  30.  
  31. ULONG OldISAMHandle = 0L, NewISAMHandle = 0L;
  32. long ErrorCode;
  33. struct Library *ISAMBase = NULL;
  34.  
  35.  
  36. /*---------------------------------- StartUp --------------------------------*/
  37. BOOL StartUp ( void )
  38. {
  39.   if ( (ISAMBase = OpenLibrary ( "isam.library", 0 )) == NULL )
  40.     {
  41.       printf ( "\nCouldn't open ISAM library.\n\n" );
  42.       return ( FALSE );
  43.     }
  44.   if (( ErrorCode = OpenISAMFile ( OLDSPECSFILENAME,
  45.                                    TRUE, 'W', FALSE, &OldISAMHandle )) != OK )
  46.     {
  47.       printf ( "Error %ld encountered opening old file.\n", ErrorCode );
  48.       return ( FALSE );
  49.     }
  50.   if (( ErrorCode = OpenISAMFile ( NEWSPECSFILENAME,
  51.                                    TRUE, 'W', FALSE, &NewISAMHandle )) != OK )
  52.     {
  53.       printf ( "Error %ld encountered opening new file.\n", ErrorCode );
  54.       return ( FALSE );
  55.     }
  56.  
  57.   return (TRUE );
  58. }
  59.  
  60.  
  61. /*---------------------------------- ShutDown -------------------------------*/
  62. void ShutDown ( void )
  63. {
  64.  
  65.   if ( ISAMBase && ISAM )
  66.     {
  67.       if ( OldISAMHandle != 0L )
  68.         {
  69.           (void) UnLockISAMFile ( OldISAMHandle );
  70.           (void) CloseISAMFile  ( OldISAMHandle );
  71.         }
  72.       if ( NewISAMHandle != 0L )
  73.         {
  74.           (void) UnLockISAMFile ( NewISAMHandle );
  75.           (void) CloseISAMFile  ( NewISAMHandle );
  76.         }
  77.     }
  78.  
  79.   if ( ISAMBase )
  80.     CloseLibrary ( ISAMBase );
  81.  
  82.   return;
  83. }
  84.  
  85.  
  86. /************************************ main ************************************/
  87. void main ( argc, argv )
  88. int argc;
  89. char * (argv []);
  90.  
  91. {
  92.   struct OldRecord OldRec;
  93.   struct NewRecord NewRec;
  94.   ULONG OldRecNo, NewRecNo, ul;
  95.   
  96.  
  97. /* modify the following to handle whatever arguments you wish. */
  98.  
  99.   switch ( argc )
  100.   {
  101.     case 0 : 
  102.  
  103.     case 2 : if ( argv[1][0] != '?' )
  104.                break;
  105.  
  106.     default: printf ( "\nFORMAT: '%s'\n\n", argv[0] );
  107.              exit ( 5 );
  108.   }
  109.  
  110.   if ( !StartUp () )
  111.     {
  112.       ShutDown ();
  113.       exit ( 5 );
  114.     }
  115.  
  116.   printf ( "\n\nBeginning File Conversion...\n\n" );
  117.  
  118.   for ( ul = 0L; ; ul++ )
  119.   {
  120.     printf ( "Processing Record #%lu.\r", ul );
  121.  
  122.     ErrorCode = ReadISAMRecord ( OldISAMHandle, ul, FALSE, ' ', &OldRec );
  123.  
  124.     switch ( ErrorCode )
  125.     {
  126.       case ERROR_DELETED_RECORD
  127.               : continue;
  128.  
  129.       case ERROR_RECORD_TOO_HIGH
  130.               : break;
  131.  
  132.       case OK : if ( some_quality_we_don't_want )
  133.                   continue;
  134.                 break;
  135.  
  136.       default : printf ( "Error %ld encountered reading old record #%lu.\n",
  137.                          ErrorCode, ul );
  138.                 break;
  139.     }
  140.     if ( ErrorCode != OK )
  141.       break;
  142.  
  143.         /* OK, we've got the old record, now transfer the contents    */
  144.         /* to the new record, making any changes/additions/deletions  */
  145.         /* you want to get the new record just right.                 */
  146.         /* Keep in mind that if you are changing a repeatable key to  */
  147.         /* a unique key, that you'll have to add something here to    */
  148.         /* keep from attempting to store more than one with the same  */
  149.         /* key value.   Of course, if you want to just eliminate all  */
  150.         /* but the first value encountered, you could just add a case */
  151.         /* ERROR_RECORD_ALREADY_EXISTS : continue to the above switch.*/
  152.  
  153.     {
  154.        /* ...stuff to put old record stuff into new record... */
  155.     }
  156.  
  157.     if (( ErrorCode = StoreISAMRecord ( NewISAMHandle, &NewRec,
  158.                                         FALSE, ' ', &NewRecNo )) != OK )
  159.       {
  160.         printf ( "Error %ld encountered storing changed old record #%lu.\n",
  161.                   ErrorCode, ul );
  162.         break;
  163.       }
  164.  
  165.     continue;    
  166.   }
  167.  
  168.  
  169.   printf ( "\n\nFile conversion complete.\n\n" );
  170.  
  171.   ShutDown ();
  172.  
  173.   exit ( 0 );
  174. }
  175.