home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 640a.lha / Bump_v1.0 / Bump.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  8KB  |  309 lines

  1. /*-- AutoRev header do NOT edit!
  2. *
  3. *   Program         :   Bump.c
  4. *   Copyright       :   © Copyright 1992 Jaba Development
  5. *   Author          :   Jan van den Baard
  6. *   Creation Date   :   6-Feb-92
  7. *   Current version :   1.0
  8. *   Translator      :   DICE v2.06
  9. *
  10. *   REVISION HISTORY
  11. *
  12. *   Date          Version         Comment
  13. *   ---------     -------         ------------------------------------------
  14. *   21-Mar-92     1.0             Added "ONLYDATE" option.
  15. *   08-Feb-92     1.0             Added "QUIET" option.
  16. *   06-Feb-92     1.0             Version string updater.
  17. *
  18. *-- REV_END --*/
  19.  
  20. /*
  21.  * --- Compiling : dcc -r -mRR -proto Bump.c -o Bump
  22.  */
  23. #include <exec/types.h>
  24. #include <exec/memory.h>
  25. #include <dos/dos.h>
  26. #include <dos/rdargs.h>
  27. #include <clib/exec_protos.h>
  28. #include <clib/dos_protos.h>
  29. #include <clib/utility_protos.h>
  30. #include <ctype.h>
  31. #include <stdarg.h>
  32.  
  33. /*
  34.  * --- Some macros
  35.  */
  36. #define SKIP_BLANKS(p)      while(isspace(*p))  p++;
  37. #define SEEK_DIGIT(p)       while(!isdigit(*p)) p++;
  38. #define FIND_DOT(p)         while(*p++ != '.');
  39.  
  40. /*
  41.  * --- The version string
  42.  */
  43. static UBYTE     *version_string = "$VER: BUMP 37.15 (21.3.92)";
  44.  
  45. /*
  46.  * --- For the shell args
  47.  */
  48. UBYTE            *template  =   "Name/A,INCVER/S,INCREV/S,SETVER/K/N,SETREV/K/N,QUIET/S,ONLYDATE/S";
  49. ULONG             array[7]  = { 0L, 0L, 0L, 0L, 0L, 0L, 0L };
  50.  
  51. /*
  52.  * --- Some global data
  53.  */
  54. UBYTE            *header    =   "Error -";
  55. BPTR              stdout    =   NULL;
  56. UBYTE            *pointer   =   NULL;
  57. ULONG             filesize;
  58.  
  59. /*
  60.  * --- Function proto's
  61.  */
  62. extern ULONG atoi( UBYTE * );
  63. extern void exit( long );
  64.  
  65. ULONG MyFPrintf( BPTR, UBYTE *, ... );
  66. UBYTE *SeekVersion( void );
  67. LONG CheckFormat( UBYTE * );
  68. LONG ReadSourceFile( void );
  69. ULONG GetNum( UBYTE * );
  70. void DoDate( BPTR );
  71.  
  72. /*
  73.  * --- Perform formatted output
  74.  */
  75. ULONG MyFPrintf( BPTR fh, UBYTE *format, ... )
  76. {
  77.     va_list     args;
  78.     long        ret;
  79.  
  80.     va_start( args, format );
  81.  
  82.     ret = VFPrintf( fh, format, args );
  83.  
  84.     va_end( args );
  85.  
  86.     return( ret );
  87. }
  88.  
  89. /*
  90.  * --- Check the version string format.
  91.  */
  92. LONG CheckFormat( UBYTE *ptr )
  93. {
  94.     UBYTE d = 0, l = 0, r = 0;
  95.  
  96.     while( *ptr != 0x22 && *ptr != 0x27 ) {
  97.         switch( *ptr ) {
  98.             case    '.':
  99.                 d++;
  100.                 break;
  101.             case    '(':
  102.                 l++;
  103.                 break;
  104.             case    ')':
  105.                 r++;
  106.                 break;
  107.         }
  108.         ptr++;
  109.     }
  110.     if ( d != 3 || l != 1 || r != 1 )
  111.         return( FALSE );
  112.     return( TRUE );
  113. }
  114.  
  115. /*
  116.  * --- Search through the buffer for the version string
  117.  */
  118. UBYTE *SeekVersion( void )
  119. {
  120.     UBYTE           *ptr = pointer;
  121.     ULONG            num = 0L;
  122.  
  123.     while( 1 ) {
  124.         if( *ptr == '$' && ! Strnicmp( ptr + 1, "VER: ", 5 ))
  125.             return( ptr );
  126.         ptr++;
  127.         if ( num++ > ( filesize - 18 ))
  128.             return( 0L );
  129.     }
  130.     return( ptr );
  131. }
  132.  
  133. /*
  134.  * --- Read in the source file
  135.  */
  136. LONG ReadSourceFile( void )
  137. {
  138.     BPTR            file;
  139.  
  140.     stdout = Output();
  141.  
  142.     if ( file = Open(( UBYTE * )array[ 0 ], MODE_OLDFILE )) {
  143.                    Seek( file, 0L, OFFSET_END );
  144.         filesize = Seek( file, 0L, OFFSET_BEGINNING );
  145.         if ( pointer = ( UBYTE * )AllocMem( filesize, MEMF_PUBLIC )) {
  146.             if ( Read( file, pointer, filesize ) == filesize ) {
  147.                 Close( file );
  148.                 return( TRUE );
  149.             } else
  150.                 PrintFault( IoErr(), header );
  151.             FreeMem( pointer, filesize );
  152.         } else {
  153.             SetIoErr( ERROR_NO_FREE_STORE );
  154.             PrintFault( ERROR_NO_FREE_STORE, header );
  155.         }
  156.         Close( file );
  157.     } else {
  158.         MyFPrintf( stdout, "Can't open \"%s\" for input - ", array[ 0 ] );
  159.         PrintFault( IoErr(), NULL );
  160.     }
  161.     return( FALSE );
  162. }
  163.  
  164. /*
  165.  * --- Convert the numbers from the source
  166.  * --- into real numbers.
  167.  */
  168. ULONG GetNum( UBYTE *ptr )
  169. {
  170.     UBYTE       number[ 20 ], i = 0;
  171.  
  172.     while( isdigit( *ptr )) {
  173.         number[ i++ ] = *ptr++;
  174.     }
  175.  
  176.     number[ i ] = 0;
  177.  
  178.     return( atoi( number ));
  179. }
  180.  
  181. /*
  182.  * --- Ouput the current system date to the file.
  183.  */
  184. void DoDate( BPTR file )
  185. {
  186.     struct DateTime  dt;
  187.     char             date[10];
  188.     ULONG            day, month, year;
  189.  
  190.     DateStamp((struct DateStamp *)&dt);
  191.     dt.dat_Format  = FORMAT_CDN;
  192.     dt.dat_StrDate = &date[0];
  193.     dt.dat_Flags   = 0;
  194.     dt.dat_StrDay  = 0;
  195.     dt.dat_StrTime = 0;
  196.     DateToStr(&dt);
  197.  
  198.     day   = GetNum( &date[ 0 ] );
  199.     month = GetNum( &date[ 3 ] );
  200.     year  = GetNum( &date[ 6 ] );
  201.     MyFPrintf( file, "(%ld.%ld.%ld)", day, month, year );
  202. }
  203.  
  204. void _main( void )
  205. {
  206.     struct RDArgs   *cli_args;
  207.     BPTR             file;
  208.     UBYTE           *ptr, *ptr1;
  209.     ULONG            revision, version, i;
  210.  
  211.     stdout = Output();
  212.  
  213.     if ( cli_args = ReadArgs( template, &array[ 0 ], 0L )) {
  214.  
  215.         if ( ! array[ 1 ] && ! array[ 2 ] && ! array [ 3 ] && ! array[ 4 ] && ! array[ 6 ] ) {
  216.             array[ 2 ] = TRUE;
  217.             if ( ! array[ 5 ] )
  218.                 FPuts( stdout, "Defaulting to INCREV\n" );
  219.         }
  220.  
  221.         SetIoErr( NULL );
  222.  
  223.         if ( ReadSourceFile()) {
  224.             SetIoErr( 0L );
  225.  
  226.             if ( ! array[ 5 ] )
  227.                 MyFPrintf( stdout, "Processing \"%s\"\n", array[ 0 ] );
  228.  
  229.             if ( ptr = SeekVersion()) {
  230.  
  231.                 if ( CheckFormat( ptr )) {
  232.                     ptr1 = pointer;
  233.  
  234.                     if ( ! array[ 5 ] )
  235.                         MyFPrintf( stdout, "Found version string at offset %ld in \"%s\"\n", ptr - ptr1, array[ 0 ] );
  236.  
  237.                     ptr += 6;
  238.  
  239.                     if ( file = Open(( UBYTE * )array[ 0 ], MODE_NEWFILE )) {
  240.                         FWrite( file, ptr1, (ULONG)( ptr - ptr1 ), 1);
  241.  
  242.                         SKIP_BLANKS( ptr );
  243.  
  244.                         if ( ! array[ 5 ] )
  245.                             FPuts( stdout, "Program name \"" );
  246.  
  247.                         while( ! isspace( *ptr )) {
  248.                             FPutC( file, *ptr );
  249.                             if ( ! array[ 5 ] )
  250.                                 FPutC( stdout, *ptr );
  251.                             ptr++;
  252.                         }
  253.  
  254.                         if ( ! array[ 5 ] )
  255.                             FPuts( stdout, "\"\n" );
  256.  
  257.                         SEEK_DIGIT( ptr );
  258.  
  259.                         version = GetNum( ptr );
  260.  
  261.                         FIND_DOT( ptr );
  262.  
  263.                         revision = GetNum( ptr );
  264.  
  265.                         if ( ! array[ 5 ] ) {
  266.                             if ( ! array[ 6 ] )
  267.                                 MyFPrintf( stdout, "Old version : %ld.%ld\n", version, revision );
  268.                             else
  269.                                 FPuts( stdout, "Updating version date\n" );
  270.                         }
  271.  
  272.                         if ( ! array[ 6 ] ) {
  273.                             if ( array[ 2 ] )      revision++;
  274.                             else if ( array[ 4 ] ) revision = *((ULONG *)array[ 4 ] );
  275.                             if ( array[ 1 ] )      version++;
  276.                             else if ( array[ 3 ] ) version = *((ULONG *)array[ 3 ] );
  277.                         }
  278.  
  279.                         if ( ! array[ 5 ] ) {
  280.                             if ( ! array[ 6 ] )
  281.                                 MyFPrintf( stdout, "New version : %ld.%ld\n", version, revision );
  282.                         }
  283.  
  284.                         MyFPrintf( file, " %ld.%ld ", version, revision );
  285.  
  286.                         DoDate( file );
  287.  
  288.                         while( *ptr != 0x22 && *ptr != 0x27 ) ptr++;
  289.  
  290.                         FWrite( file, ptr, (ULONG)( filesize - (ULONG)( ptr - ptr1 )), 1 );
  291.                         Close( file );
  292.                         if ( IoErr())
  293.                             PrintFault( IoErr(), header );
  294.                         else if ( ! array[ 5 ] )
  295.                             FPuts( stdout, "Done.\n" );
  296.                     }
  297.                 } else
  298.                     FPuts( stdout, "Error -: Malformed version string\n" );
  299.             } else
  300.                 FPuts( stdout, "Error -: no version string found\n" );
  301.             FreeMem( pointer, filesize );
  302.         }
  303.         FreeArgs( cli_args );
  304.     } else
  305.         PrintFault( IoErr(), header );
  306.  
  307.     exit( IoErr());
  308. }
  309.