home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / examples / Inc.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  5.8 KB  |  359 lines

  1. /*-- Rev Header - do NOT edit!
  2.  *
  3.  *  Filename : Inc.cc
  4.  *  Purpose  : Increments an environment variable
  5.  *
  6.  *  Program  : Inc
  7.  *  Author   : Gerhard Müller
  8.  *  Copyright: (c) by Gerhard Müller
  9.  *  Creation : Sun Nov 21 20:40:43 1993
  10.  *
  11.  *  compile  : makefile
  12.  *
  13.  *
  14.  *  Compile version  : 0.1
  15.  *  Ext. Version     : 0.1
  16.  *
  17.  *  REVISION HISTORY
  18.  *
  19.  *  Date       C-Version  E-Version    Comment
  20.  *  ---------  ---------  -----------  -------
  21.  *  21.11.93                           work started
  22.  *
  23.  *
  24.  *
  25.  *-- REV_END --
  26.  */
  27.  
  28.     /*
  29.      * C-Includes, C-Definitionen
  30.      *
  31.      */
  32.  
  33. #define class _class
  34. #define template _template
  35.  
  36. extern "C" {
  37. #include <exec/types.h>
  38. #include <exec/ports.h>
  39. #include <exec/memory.h>
  40. #include <exec/execbase.h>
  41. #include <dos/dos.h>
  42. #include <dos/dosextens.h>
  43. #include <dos/exall.h>
  44. #include <dos/rdargs.h>
  45. #include <clib/alib_protos.h>
  46. #include <clib/alib_stdio_protos.h>
  47. #include <inline/stubs.h>
  48. #include <clib/dos_protos.h>
  49. #ifdef __OPTIMIZE__
  50. #include <inline/exec.h>
  51. #include <inline/dos.h>
  52. #include <inline/utility.h>
  53. #else
  54. #include <clib/exec_protos.h>
  55. #include <clib/dos_protos.h>
  56. #include <clib/utility_protos.h>
  57. #endif
  58.  
  59. int     atexit(void (*)(void));
  60. }
  61.  
  62. #undef template
  63. #undef class
  64.  
  65.  
  66. extern "C" {
  67.  
  68. /* dont mangle our global symbols */
  69.  
  70. //Library            *UtilityBase;
  71.  
  72. extern ExecBase    *SysBase;
  73.  
  74. extern BPTR stdin;
  75. extern BPTR stdout;
  76. }
  77.  
  78.  
  79.     /*
  80.      * C++-Includes, C++-Definitionen
  81.      *
  82.      */
  83.  
  84. #include "add/baserel/add.h"        // prototypes for all new C-functions
  85. #include "add/baserel/OwnError.h"    // defines err
  86.  
  87.  
  88.     /*
  89.      * our function-prototypes
  90.      *
  91.      */
  92.  
  93. void    OutputError(void);
  94. void    MyExit();
  95. BOOL    MyInit(int alen, char *aptr);
  96. int        main(int alen, char *aptr);
  97.  
  98.  
  99. /* Version-String */
  100.  
  101. char *version = "\0$VER: Inc 0.01 (" __DATE__ ")";
  102.  
  103.  
  104.  
  105. /* things for commandline-parsing */
  106.  
  107. #define TEMPLATE "VARNAME,LOCAL/S,GLOBAL/S,FILE/K,OUTPUT/S"
  108.  
  109. typedef struct { UBYTE *varname; ULONG local; ULONG global; UBYTE *file; ULONG output;} ARGS;
  110. #define DEFAULT { NULL, FALSE, FALSE, NULL, FALSE }
  111.  
  112. ARGS            args = DEFAULT;    /* here the arguments are stored */
  113.  
  114. struct RDArgs    *rda = NULL;    /* for CLI-startup */
  115. UBYTE            **ToolTypes;     /* for WB-startup */
  116.  
  117.  
  118.  
  119.  
  120.  
  121.     /*
  122.      * This function gets called by exit(), look im MyInit() for corresponding atexit() call
  123.      * frees all system-depended things
  124.      */
  125.  
  126.  
  127. void MyExit()
  128. {
  129.         /* Free arguments passed by commandline allocated by ReadArgs() */
  130.  
  131.     if( rda )
  132.     {
  133.         FreeArgs( rda );
  134.         FreeDosObject( DOS_RDARGS, rda );
  135.     }
  136.  
  137.  
  138.         /* close all our libraries */
  139.  
  140. //    if(UtilityBase)
  141. //        CloseLibrary(UtilityBase);
  142.  
  143. }
  144.  
  145.  
  146.     /*
  147.      * Open some libraries and make the commandline-parsing
  148.      *
  149.      */
  150.  
  151. BOOL MyInit(int alen, char *aptr)
  152. {
  153.     BOOL ok=FALSE;
  154.  
  155.     /* Are we running under control of Kickstart 2.0 or higher? */
  156.  
  157.     if(SysBase -> LibNode . lib_Version < 37)
  158.     {
  159.         err.NotifyError("needs Workbench & Kickstart Version 2.04 and above !\n");
  160.         return FALSE;
  161.     }
  162.  
  163.  
  164.     atexit(MyExit);            // the function MyExit should be called at exit()-time
  165.  
  166.  
  167.     /* open some libraries, not really needed in this programm, just to show how to do */
  168.  
  169. //    if(UtilityBase = OpenLibrary((UBYTE *)"utility.library",37))
  170. //    {
  171. //        // we have all our libraries
  172. //            ok=TRUE;
  173. //    }
  174.  
  175. //    if(!ok) return FALSE;
  176.  
  177.  
  178.     /* normal startup-code.... */
  179.  
  180.     ok=FALSE;
  181.  
  182.     if(alen)
  183.     {
  184.         /* start from CLI */
  185.         /* parse arguements */
  186.  
  187.         if(rda = (RDArgs *)AllocDosObject( DOS_RDARGS, NULL ))
  188.         {
  189.             rda->RDA_ExtHelp = (UBYTE *)"Increments the contents of an environment variable or file\n";
  190.  
  191.             if(ReadArgs( (UBYTE *)TEMPLATE, (LONG *) &args, rda ) )
  192.             {
  193.                 /* Args erfolgreich gelesen */
  194.  
  195.                 ok=TRUE;
  196.             }
  197.             else
  198.             {
  199.                 err.DosError(IoErr(), "Error parsing commandline");    /* prints the appropriate err message */
  200.             }
  201.         }
  202.         else
  203.             err.NotifyError("Out of memory error !");
  204.     }
  205.     else
  206.     {
  207.         /* Workbench-Start */
  208.  
  209.         err.NotifyError("Start only from CLI !");
  210.  
  211.         ok=FALSE;
  212.     }
  213.  
  214.     return ok;
  215. }
  216.  
  217.  
  218.  
  219.     /*
  220.      * Here the testprogram starts, uses some misc features
  221.      *
  222.      */
  223.  
  224.  
  225. int main(int alen, char *aptr)
  226. {
  227.     if(MyInit(alen,aptr))        // all things went well
  228.     {
  229.         BPTR fh;
  230.  
  231.         if (args.varname)
  232.         {
  233.             ULONG flags=0;
  234.             if(args.local)
  235.             {
  236.                 flags=GVF_LOCAL_ONLY;
  237.             }
  238.             else
  239.             {
  240.                 if (args.global)
  241.                 {
  242.                     flags=GVF_GLOBAL_ONLY;
  243.                 }
  244.             }
  245.  
  246.             UBYTE Buffer[32];
  247.  
  248.             LONG len=GetVar(args.varname,Buffer,31,flags);
  249.  
  250.             Buffer[31]=0;
  251.  
  252.             if(len==-1)
  253.             {
  254.                 /* Error */
  255.                 err.NotifyError("Can't access variable!");
  256.             }
  257.             else
  258.             {
  259.                 volatile LONG zahl=0;
  260.                 StrToLong(Buffer, &zahl);
  261.  
  262.                 zahl=zahl+1;
  263.  
  264.                 sprintf(Buffer, (UBYTE *)"%ld\n", zahl);
  265.  
  266.                 SetVar(args.varname, Buffer, strlen((char *)Buffer), flags);
  267.  
  268.                 if(args.output) Printf((UBYTE *)"%ld\n",zahl);
  269.             }
  270.         }
  271.         else
  272.         {
  273.             /* Filename ? */
  274.  
  275.             if (args.file)
  276.             {
  277.                 if(fh=Open(args.file,MODE_OLDFILE))
  278.                 {
  279.                     UBYTE Buffer[32];
  280.  
  281.                     LONG len=Read(fh, Buffer, 31);
  282.  
  283.                     if(len>0 && len < 32)
  284.                     {
  285.                         Buffer[31]=0;
  286.  
  287.                         volatile LONG zahl=0;
  288.                         StrToLong(Buffer, &zahl);
  289.  
  290.                         zahl++;
  291.  
  292.                         sprintf(Buffer, (UBYTE *)"%ld\n", zahl);
  293.  
  294.                         Close(fh);
  295.  
  296.                         if(fh=Open(args.file,MODE_NEWFILE))
  297.                         {
  298.                             Write(fh, Buffer, strlen((char *)Buffer));
  299.  
  300.                             Close(fh);
  301.  
  302.                             if(args.output) Printf((UBYTE *)"%ld\n",zahl);
  303.  
  304.                         }
  305.                         else
  306.                             err.NotifyError("Can't open file for writing !");
  307.                     }
  308.                     else
  309.                         err.NotifyError("File contains no variable !");
  310.                 }
  311.                 else
  312.                     err.NotifyError("Can't open file for reading !");
  313.  
  314.             }
  315.             else
  316.                 err.NotifyError("No variable name or Filename given !");
  317.  
  318.         }
  319.     }    // MyInit()
  320.  
  321.  
  322.     // if an error occurred...
  323.  
  324.     OutputError();
  325.  
  326.     return(0);                // exit is called if we fall through
  327. }
  328.  
  329.  
  330.  
  331.  
  332.     /*
  333.      * Output error-message if we have one
  334.      *
  335.      */
  336.  
  337.  
  338. void OutputError(void)
  339. {
  340.     if(err)
  341.     {
  342.         char *err_mem=0;
  343.         char *es;
  344.         LONG err_nr=err.SetErrorNumber(0);
  345.  
  346.         if(es=err.have_error_string())
  347.         {
  348.             Printf((UBYTE *)"%s\n", es);
  349.         }
  350.         else
  351.         {
  352.             if(err_nr)
  353.             {
  354.                 Printf((UBYTE *)"Error-Number: %ld\n", err_nr);
  355.             }
  356.         }
  357.     }
  358. }
  359.