home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / misc / Bump24.lha / Bump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-26  |  10.0 KB  |  351 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. *   Changes         :   © Copyright 1997 Software Industry & General Hardware
  11. *   Author          :   Clark Williams
  12. *   Changes Date    :   Version 2.0 & 2.1
  13. *
  14. *   REVISION HISTORY
  15. *
  16. *   Date          Version         Comment
  17. *   ---------     -------         ------------------------------------------
  18. *   26-Feb-97     2.4             Added the version string to the output.
  19. *   26-Feb-97     2.3             Added the full century to the date string.
  20. *   25-Feb-97     2.2             Changed date to always put out leading zeros.
  21. *   25-Feb-97     2.1             Added the European date format as standard
  22. *                                 Added the USA/S switch to output dd.mm.yy
  23. *   25-Feb-97     2.0             Converted to SAS C/C++ Version 6.56
  24. *   21-Mar-92     1.0             Added "ONLYDATE" option.
  25. *   08-Feb-92     1.0             Added "QUIET" option.
  26. *   06-Feb-92     1.0             Version string updater.
  27. *
  28. *-- REV_END --*/
  29.  
  30. /*
  31.  * --- Compiling : dcc -r -mRR -proto Bump.c -o Bump
  32.  */
  33.  
  34. #include <exec/types.h>
  35. #include <exec/memory.h>
  36. #include <dos/dos.h>
  37. #include <dos/rdargs.h>
  38. #include <clib/exec_protos.h>
  39. #include <clib/dos_protos.h>
  40. #include <clib/utility_protos.h>
  41. #include <ctype.h>
  42. #include <stdarg.h>
  43.  
  44. /*
  45.  * --- Some macros
  46.  */
  47.  
  48. #define SKIP_BLANKS(p)      while(isspace(*p))  p++;
  49. #define SEEK_DIGIT(p)       while(!isdigit(*p)) p++;
  50. #define FIND_DOT(p)         while(*p++ != '.');
  51.  
  52. /*
  53.  * --- The version string
  54.  */
  55.  
  56. static UBYTE *version_string = "$VER: BUMP 2.4 (1997.02.26)";
  57.  
  58. /*
  59.  * --- For the shell args
  60.  */
  61.  
  62. #define           TEMPLATE_MEMBERS 9
  63. UBYTE *template = "Name/A,INCVER/S,INCREV/S,SETVER/K/N,SETREV/K/N,QUIET/S,ONLYDATE/S,USADATE/S,NOCENT/S";
  64. ULONG  array [ TEMPLATE_MEMBERS ] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
  65.  
  66. /*
  67.  * --- Some global data
  68.  */
  69.  
  70. UBYTE  *header    =   "Error -";
  71. BPTR    stdout    =   NULL;
  72. UBYTE  *pointer   =   NULL;
  73. ULONG   filesize;
  74.  
  75. /*
  76.  * --- Function proto's
  77.  */
  78. extern ULONG atoi ( UBYTE * );
  79. extern void  exit ( long    );
  80.  
  81. ULONG  MyFPrintf      ( BPTR, UBYTE *, ... );
  82. UBYTE *SeekVersion    ( void               );
  83. LONG   CheckFormat    ( UBYTE *            );
  84. LONG   ReadSourceFile ( void               );
  85. ULONG  GetNum         ( UBYTE *            );
  86. void   DoDate         ( BPTR, BOOL, BOOL   );
  87.  
  88. /*
  89.  * --- Perform formatted output
  90.  */
  91. ULONG MyFPrintf ( BPTR fh, UBYTE *format, ... )
  92. {
  93.     va_list     args;
  94.     long        ret;
  95.  
  96.     va_start ( args, format );
  97.  
  98.     ret = VFPrintf ( fh, format, args );
  99.  
  100.     va_end ( args );
  101.  
  102.     return ( ( ULONG ) ret );
  103. }
  104.  
  105. /*
  106.  * --- Check the version string format.
  107.  */
  108. LONG CheckFormat ( UBYTE *ptr )
  109. {
  110.     UBYTE d = 0, l = 0, r = 0;
  111.  
  112.     while ( *ptr != 0x22 && *ptr != 0x27 )
  113.     {
  114.         switch( *ptr )
  115.         {
  116.             case    '.': d++;
  117.                          break;
  118.             case    '(': l++;
  119.                          break;
  120.             case    ')': r++;
  121.                          break;
  122.         }
  123.         ptr++;
  124.     }
  125.     if ( d != 3 || l != 1 || r != 1 ) return( FALSE );
  126.     return ( TRUE );
  127. }
  128.  
  129. /*
  130.  * --- Search through the buffer for the version string
  131.  */
  132.  
  133. UBYTE *SeekVersion ( void )
  134. {
  135.     UBYTE           *ptr = pointer;
  136.     ULONG            num = 0L;
  137.  
  138.     while ( 1 )
  139.     {
  140.         if ( *ptr == '$' && ! Strnicmp ( ptr + 1, "VER: ", 5 ) ) return ( ptr );
  141.         ptr++;
  142.         if ( num++ > ( filesize - 18 ) ) return ( 0L );
  143.     }
  144.     return ( ptr );
  145. }
  146.  
  147. /*
  148.  * --- Read in the source file
  149.  */
  150. LONG ReadSourceFile( void )
  151. {
  152.   BPTR            file;
  153.  
  154.   stdout = Output();
  155.  
  156.   if ( file = Open ( ( UBYTE * ) array [ 0 ], MODE_OLDFILE ) )
  157.      {
  158.        Seek ( file, 0L, OFFSET_END );
  159.        filesize = Seek ( file, 0L, OFFSET_BEGINNING );
  160.        if ( pointer = ( UBYTE * ) AllocMem( filesize, MEMF_PUBLIC ) )
  161.           {
  162.             if ( Read ( file, pointer, filesize ) == filesize )
  163.                {
  164.                  Close  ( file );
  165.                  return ( TRUE );
  166.                }
  167.                else PrintFault ( IoErr(), header );
  168.             FreeMem ( pointer, filesize );
  169.           }
  170.           else
  171.           {
  172.             SetIoErr ( ERROR_NO_FREE_STORE );
  173.             PrintFault ( ERROR_NO_FREE_STORE, header );
  174.           }
  175.        Close ( file );
  176.      }
  177.      else
  178.      {
  179.        MyFPrintf ( stdout, "Can't open \"%s\" for input - ", array[ 0 ] );
  180.        PrintFault ( IoErr(), NULL );
  181.      }
  182.   return ( FALSE );
  183. }
  184.  
  185. /*
  186.  * --- Convert the numbers from the source
  187.  * --- into real numbers.
  188.  */
  189.  
  190. ULONG GetNum( UBYTE *ptr )
  191. {
  192.   UBYTE number[ 20 ];
  193.   UBYTE i = 0;
  194.  
  195.   while ( isdigit( *ptr ) ) number[ i++ ] = *ptr++;
  196.   number [ i ] = 0;
  197.   return ( atoi ( number ) );
  198. }
  199.  
  200. /*
  201.  * --- Ouput the current system date to the file.
  202.  */
  203.  
  204. void DoDate ( BPTR file, BOOL UsaDate, BOOL NoCentury )
  205. {
  206.   struct DateTime  dt;
  207.   char             date [ 12 ];
  208.   ULONG            day, month, year;
  209.  
  210.   DateStamp ( (struct DateStamp * ) &dt );
  211.   dt.dat_Format  = FORMAT_CDN;
  212.   dt.dat_StrDate = &date [ 0 ];
  213.   dt.dat_Flags   = 0;
  214.   dt.dat_StrDay  = 0;
  215.   dt.dat_StrTime = 0;
  216.   DateToStr(&dt);
  217.  
  218.   day   = GetNum ( &date[ 0 ] );
  219.   month = GetNum ( &date[ 3 ] );
  220.   year  = GetNum ( &date[ 6 ] );
  221.  
  222.   /* Add the Century to the year */
  223.   if ( ! NoCentury )
  224.      {
  225.        if ( year < 78 ) year += 2000;
  226.           else          year += 1900;
  227.      }
  228.  
  229.   /* Force out leading zeros. -- S.I.G.H. (BCW) */
  230.   if ( UsaDate )
  231.        if ( NoCentury ) MyFPrintf ( file, "(%02ld.%02ld.%02ld)", day, month, year );
  232.           else          MyFPrintf ( file, "(%02ld.%02ld.%4ld)",  day, month, year );
  233.      else
  234.        if ( NoCentury ) MyFPrintf ( file, "(%02ld.%02ld.%02ld)", year, month, day );
  235.           else          MyFPrintf ( file, "(%4ld.%02ld.%02ld)",  year, month, day );
  236. }
  237.  
  238. void main ( int argc, char *argv[] )
  239. {
  240.     struct RDArgs   *cli_args;
  241.     BPTR             file;
  242.     UBYTE           *ptr;
  243.     UBYTE           *ptr1;
  244.     ULONG            revision;
  245.     ULONG            version;
  246.     BOOL             UsaDate   = FALSE;  /* Use USA date format. */
  247.     BOOL             NoCentury = FALSE;  /* Do not include century in output of date. */
  248.  
  249.     stdout = Output();
  250.  
  251.     if ( cli_args = ReadArgs( template, (long *) &array[ 0 ], 0L ) )
  252.        {
  253.         if ( ! array [ 1 ] && ! array [ 2 ] && ! array [ 3 ] && ! array [ 4 ] && ! array [ 6 ] )
  254.            {
  255.              array [ 2 ] = TRUE;
  256.              if ( ! array [ 5 ] ) FPuts ( stdout, "Defaulting to INCREV\n" );
  257.            }
  258.  
  259.         SetIoErr ( NULL );
  260.  
  261.         if ( ReadSourceFile() )
  262.            {
  263.             SetIoErr ( 0L );
  264.  
  265.             if ( ! array [ 5 ] ) MyFPrintf ( stdout, "%s\n",                version_string );
  266.             if ( ! array [ 5 ] ) MyFPrintf ( stdout, "Processing \"%s\"\n", array[ 0 ]     );
  267.  
  268.             UsaDate   = array [ 7 ]; /* Does User want a USA date format?  */
  269.             NoCentury = array [ 8 ]; /* Whether to output century in date. */
  270.  
  271.             if ( ptr = SeekVersion())
  272.                {
  273.                  if ( CheckFormat( ptr ))
  274.                     {
  275.                       ptr1 = pointer;
  276.  
  277.                       if ( ! array [ 5 ] )
  278.                            MyFPrintf ( stdout, "Found version string at %ld in \"%s\"\n", ptr - ptr1, array[ 0 ] );
  279.  
  280.                       ptr += 6;
  281.  
  282.                       if ( file = Open ( ( UBYTE * ) array [ 0 ], MODE_NEWFILE ) )
  283.                          {
  284.                            FWrite ( file, ptr1, (ULONG) ( ptr - ptr1 ), 1 );
  285.  
  286.                            SKIP_BLANKS ( ptr );
  287.  
  288.                            if ( ! array [ 5 ] ) FPuts ( stdout, "Program name \"" );
  289.  
  290.                            while ( ! isspace ( *ptr ) )
  291.                            {
  292.                               FPutC ( file, *ptr );
  293.                               if ( ! array [ 5 ] ) FPutC ( stdout, *ptr );
  294.                               ptr++;
  295.                            }
  296.  
  297.                            if ( ! array[ 5 ] ) FPuts ( stdout, "\"\n" );
  298.  
  299.                            SEEK_DIGIT ( ptr );
  300.  
  301.                            version = GetNum ( ptr );
  302.  
  303.                            FIND_DOT ( ptr );
  304.  
  305.                            revision = GetNum ( ptr );
  306.  
  307.                            if ( ! array [ 5 ] )
  308.                               {
  309.                                 if ( ! array [ 6 ] )
  310.                                      MyFPrintf ( stdout, "Old version : %ld.%ld\n", version, revision );
  311.                                    else FPuts( stdout, "Updating version date\n" );
  312.                               }
  313.  
  314.                            if ( ! array [ 6 ] )
  315.                               {
  316.                                 if ( array [ 2 ] ) revision++;
  317.                                    else
  318.                                      if ( array [ 4 ] ) revision = *( (ULONG *) array [ 4 ] );
  319.                                 if ( array [ 1 ] ) version++;
  320.                                    else
  321.                                      if ( array [ 3 ] ) version  = *( (ULONG *) array [ 3 ] );
  322.                               }
  323.  
  324.                            if ( ! array [ 5 ] )
  325.                               if ( ! array [ 6 ] ) MyFPrintf ( stdout, "New version : %ld.%ld\n", version, revision );
  326.  
  327.                            MyFPrintf ( file, " %ld.%ld ", version, revision );
  328.  
  329.                            DoDate ( file, UsaDate, NoCentury );
  330.  
  331.                            while ( *ptr != 0x22 && *ptr != 0x27 ) ptr++;
  332.  
  333.                            FWrite ( file, ptr, (ULONG) ( filesize - (ULONG) ( ptr - ptr1 ) ), 1 );
  334.  
  335.                            Close ( file );
  336.  
  337.                            if ( IoErr() ) PrintFault ( IoErr(), header );
  338.                               else if ( ! array [ 5 ] ) FPuts ( stdout, "Done.\n" );
  339.                          }
  340.                     }
  341.                   else FPuts ( stdout, "Error -: Malformed version string\n" );
  342.                }
  343.               else FPuts ( stdout, "Error -: no version string found\n" );
  344.             FreeMem ( pointer, filesize );
  345.            }
  346.          FreeArgs ( cli_args );
  347.        }
  348.      else PrintFault ( IoErr(), header );
  349.     exit ( IoErr() );
  350. }
  351.