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

  1. /*-- Rev Header - do NOT edit!
  2.  *
  3.  *  Filename : DemoSource.cc
  4.  *  Purpose  : Shows some features of my provided Classes etc.
  5.  *
  6.  *  Program  : TestExit
  7.  *  Author   : Gerhard Müller
  8.  *  Copyright: (c) by Gerhard Müller
  9.  *  Creation : Sun Sep 12 00:17:36 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.  *  12.09.93                           work started
  22.  *  25.10.93                           added a better new()
  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 <intuition/intuitionbase.h>
  48. #include <graphics/gfxbase.h>
  49. #include <inline/stubs.h>
  50. #include <clib/dos_protos.h>
  51. #ifdef __OPTIMIZE__
  52. #include <inline/exec.h>
  53. #include <inline/dos.h>
  54. #include <inline/intuition.h>
  55. #else
  56. #include <clib/exec_protos.h>
  57. #include <clib/dos_protos.h>
  58. #include <clib/intuition_protos.h>
  59. #endif
  60.  
  61. int     atexit(void (*)(void));
  62. }
  63.  
  64. #undef template
  65. #undef class
  66.  
  67.  
  68. extern "C" {
  69.  
  70. /* dont mangle our global symbols */
  71.  
  72. IntuitionBase    *IntuitionBase;
  73. GfxBase            *GfxBase;
  74. Library            *GadToolsBase;
  75. Library            *UtilityBase;
  76. Library            *WorkbenchBase;
  77. Library            *IconBase;
  78. Library            *CxBase;
  79. Library            *IFFParseBase;
  80. Library            *AslBase;
  81.  
  82. extern ExecBase    *SysBase;
  83.  
  84. extern BPTR stdin;
  85. extern BPTR stdout;
  86. }
  87.  
  88. BPTR stdin_WB = FALSE;
  89.  
  90.  
  91.     /*
  92.      * C++-Includes, C++-Definitionen
  93.      *
  94.      */
  95.  
  96. #include "add/baserel/add.h"        // prototypes for all new C-functions
  97. #include "add/baserel/couta.h"        // defines cout
  98. #include "add/baserel/cina.h"        // defines cin
  99. #include "add/baserel/OwnError.h"    // defines err
  100. #include "add/baserel/Editfile.h"    // makes it easier to read in a file
  101. #include "add/baserel/CED_Support.h"// allow communication with CED 3.5+
  102.  
  103.  
  104.  
  105.     /*
  106.      * our function-prototypes
  107.      *
  108.      */
  109.  
  110. void    OutputError(void);
  111. void    MyExit();
  112. BOOL    MyInit(int alen, char *aptr);
  113. int        main(int alen, char *aptr);
  114. void    OutputError(void);
  115. BOOL    ask_user_to_free_memory(ULONG len, struct Window *win=NULL);
  116.  
  117.  
  118. /* Version-String */
  119.  
  120. char *version = "\0$VER: Example 0.02 (" __DATE__ ")";
  121.  
  122.  
  123.  
  124. /* things for commandline-parsing */
  125.  
  126. #define TEMPLATE "STRING/A,INTEGER/N,TEST"
  127.  
  128. typedef struct { char *string; long *integer; char *test; } ARGS;
  129. #define DEFAULT { NULL, &integer, "test" }
  130.  
  131. long             integer    =    99L;
  132.  
  133. ARGS            args = DEFAULT;    /* here the arguments are stored */
  134.  
  135. struct RDArgs    *rda = NULL;    /* for CLI-startup */
  136. UBYTE            **ToolTypes;     /* for WB-startup */
  137.  
  138.  
  139.  
  140.  
  141.     /*
  142.      * void *operator new(unsigned long int anz)
  143.      *
  144.      * This overloads the standard new operator with a friendlier one.
  145.      * It won't terminate the program, just give zero as return if
  146.      * no memory is available, and asks the user to free memory.
  147.      *
  148.      * The user of new HAS to check if the variable is available!!
  149.      *
  150.      */
  151.  
  152.  
  153.  
  154. void *operator new(unsigned long int anz)
  155. {
  156.     char *ptr=NULL;
  157.  
  158.     while(1)
  159.     {
  160.         if(ptr=(char *)AllocVec(anz, 0))
  161.             return (ptr);
  162.         else
  163.         {
  164.             if(!IntuitionBase) return NULL;
  165.             if(!ask_user_to_free_memory(anz)) return NULL;
  166.         }
  167.     }
  168. }
  169.  
  170.  
  171.     /* this one is the same as above, but you can define which
  172.        kind of memory you need (MEMF_CHIP, MEMF_FAST, MEMF_CLEAR etc. */
  173.  
  174. void *operator new(unsigned long int anz, ULONG requirements)
  175. {
  176.     char *ptr=NULL;
  177.  
  178.     while(1)
  179.     {
  180.         if(ptr=(char *)AllocVec(anz, requirements))
  181.             return (ptr);
  182.         else
  183.         {
  184.             if(!IntuitionBase) return NULL;
  185.             if(!ask_user_to_free_memory(anz)) return NULL;
  186.         }
  187.     }
  188. }
  189.  
  190. void operator delete(void *mem)
  191. {
  192.     FreeVec(mem);
  193. }
  194.  
  195.  
  196.     /*
  197.      * Dummy Class, provided as an example only, has no real use
  198.      *
  199.      */
  200.  
  201. class xxxx {
  202.  public:
  203.     xxxx(ULONG mem_len=10000) : mem(0)            // initialise mem to 0, not necessary in our class, but...
  204.     {
  205.         // show where we are called
  206.         cout << "xxxx Constructor: file: " << __FILE__ << "  line: "<< __LINE__ << endl;
  207.  
  208.         mem=new (MEMF_CLEAR) char[mem_len];
  209.     }
  210.  
  211.     ~xxxx()
  212.     {
  213.         // show where we are called
  214.         cout << "xxxx Destructor: file: " << __FILE__ << "  line: "<< __LINE__ << endl;
  215.  
  216.         delete [] mem;
  217.     }
  218.  
  219.     char *getmem(){return mem;};
  220.  
  221. private:
  222.     char *mem;
  223.  
  224. };
  225.  
  226.  
  227.  
  228.  
  229. /* Code fragment for use within new */
  230.  
  231. /* Asks the user to free memory */
  232.  
  233. BOOL ask_user_to_free_memory(ULONG len, struct Window *win=NULL)
  234. {
  235.     BOOL return_ok            = FALSE;
  236.  
  237.     // Request User
  238.  
  239.     UBYTE ProgName[50];
  240.  
  241.     GetProgramName(ProgName,49);
  242.  
  243.     return_ok=ShowRequest(win, (UBYTE *)"We have memory trouble...",
  244.         (UBYTE *)"Program\n   %s\nneeds %ld Bytes of memory.\nSo much bytes are not free.\nPlease try to free some memory or give up\n",
  245.         (UBYTE *)"Try again|Give up",  ProgName, len);
  246.  
  247.     return return_ok;
  248. }
  249.  
  250.  
  251.     /*
  252.      * This function gets called by exit(), look im MyInit() for corresponding atexit() call
  253.      * frees all system-depended things
  254.      */
  255.  
  256.  
  257. void MyExit()
  258. {
  259.     cout << "MyExit() is called. file: " << __FILE__ << "  line: "<< __LINE__ <<endl;
  260.  
  261.  
  262.         /* Close WB-Window stream */
  263.  
  264.     if( stdin_WB )
  265.     {
  266.         stdin=0;            // set all things back to normal
  267.         stdout=0;            // just for security reasons
  268.         SelectInput(0);
  269.         SelectOutput(0);
  270.  
  271.         cin.attach(0);        // just for security, not really necessary
  272.         cout.attach(0);
  273.  
  274.         Close(stdin_WB);    // the window now can be closed (forever) by the user via CTRL-\ or via close-gadget
  275.     }
  276.  
  277.  
  278.  
  279.         /* free WB-arguments */
  280.  
  281.     if( ToolTypes )
  282.     {
  283.         ArgArrayDone();
  284.     }
  285.  
  286.  
  287.         /* Free arguments passed by commandline allocated by ReadArgs() */
  288.  
  289.     if( rda )
  290.     {
  291.         FreeArgs( rda );
  292.         FreeDosObject( DOS_RDARGS, rda );
  293.     }
  294.  
  295.  
  296.         /* close all our libraries */
  297.  
  298.         /* Close asl.library. */
  299.  
  300.     if(AslBase)
  301.         CloseLibrary(AslBase);
  302.  
  303.         /* Close iffparse.library. */
  304.  
  305.     if(IFFParseBase)
  306.         CloseLibrary(IFFParseBase);
  307.  
  308.         /* Close commodities.library. */
  309.  
  310.     if(CxBase)
  311.         CloseLibrary(CxBase);
  312.  
  313.         /* Close icon.library. */
  314.  
  315.     if(IconBase)
  316.         CloseLibrary(IconBase);
  317.  
  318.         /* Close workbench.library. */
  319.  
  320.     if(WorkbenchBase)
  321.         CloseLibrary(WorkbenchBase);
  322.  
  323.         /* Close utility.library. */
  324.  
  325.     if(UtilityBase)
  326.         CloseLibrary(UtilityBase);
  327.  
  328.         /* Close gadtools.library. */
  329.  
  330.     if(GadToolsBase)
  331.         CloseLibrary(GadToolsBase);
  332.  
  333.         /* Close graphics.library. */
  334.  
  335.     if(GfxBase)
  336.     {
  337.         // This construction gives the compiler to distinguish between
  338.         // the struct GfxBase {/*...*/};  and the variable GfxBase. If this
  339.         // is not done, the warning (and following error)
  340.         //
  341.         //    warning: name `IntuitionBase' could be type or expression; compiler assuming type
  342.         //    parse error before `;'
  343.         //
  344.         // were the consequence.
  345.  
  346.         struct Library *dummy=(struct Library *)GfxBase;
  347.         CloseLibrary(dummy);
  348.     }
  349.  
  350.         /* Close intuition.library. */
  351.  
  352.     if(IntuitionBase)
  353.     {
  354.         struct Library *dummy=(struct Library *)IntuitionBase;
  355.         CloseLibrary(dummy);
  356.     }
  357.  
  358. }
  359.  
  360.  
  361.     /*
  362.      * Open some libraries and make the commandline-parsing
  363.      *
  364.      */
  365.  
  366. BOOL MyInit(int alen, char *aptr)
  367. {
  368.     BOOL ok=FALSE;
  369.  
  370.     /* Are we running under control of Kickstart 2.0 or higher? */
  371.  
  372.     if(SysBase -> LibNode . lib_Version < 37)
  373.     {
  374.         err.NotifyError("needs Workbench & Kickstart Version 2.04 and above !\n");
  375.         return FALSE;
  376.     }
  377.  
  378.  
  379.     atexit(MyExit);            // the function MyExit should be called at exit()-time
  380.  
  381.  
  382.     /* open some libraries, not really needed in this programm, just to show how to do */
  383.  
  384.     if(IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library",37))
  385.     {
  386.         if(GfxBase = (struct GfxBase *)OpenLibrary((UBYTE *)"graphics.library",37))
  387.         {
  388.             if(GadToolsBase = OpenLibrary((UBYTE *)"gadtools.library",37))
  389.             {
  390.                 if(UtilityBase = OpenLibrary((UBYTE *)"utility.library",37))
  391.                 {
  392.                     if(WorkbenchBase = OpenLibrary((UBYTE *)"workbench.library",37))
  393.                     {
  394.                         if(IconBase = OpenLibrary((UBYTE *)"icon.library",37))
  395.                         {
  396.                             if(CxBase = OpenLibrary((UBYTE *)"commodities.library",37))
  397.                             {
  398.                                 if(IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library",37))
  399.                                 {
  400.                                     if(AslBase = OpenLibrary((UBYTE *)"asl.library",37))
  401.                                     {
  402.                                         // we have all our libraries
  403.                                         ok=TRUE;
  404.                                     }
  405.                                     else err.NotifyError("unable to open asl.library !");
  406.                                 }
  407.                                 else err.NotifyError("unable to open iffparse.library !");
  408.                             }
  409.                             else err.NotifyError("unable to open commodities.library !");
  410.                         }
  411.                         else err.NotifyError("unable to open icon.library !");
  412.                     }
  413.                     else err.NotifyError("unable to open workbench.library !");
  414.                 }
  415.                 else err.NotifyError("unable to open utility.library !");
  416.             }
  417.             else err.NotifyError("unable to open gadtools.library !");
  418.         }
  419.         else err.NotifyError("unable to open graphics.library !");
  420.     }
  421.     else err.NotifyError("unable to open intuition.library !");
  422.  
  423.     if(!ok) return FALSE;
  424.  
  425.  
  426.     /* normal startup-code.... */
  427.  
  428.     ok=FALSE;
  429.  
  430.     if(alen)
  431.     {
  432.         /* start from CLI */
  433.         /* parse arguements */
  434.  
  435.         if(rda = (RDArgs *)AllocDosObject( DOS_RDARGS, NULL ))
  436.         {
  437.             rda->RDA_ExtHelp = (UBYTE *)"This could be an extended help - here you get nothing :-)\n";
  438.  
  439.             if(ReadArgs( (UBYTE *)TEMPLATE, (LONG *) &args, rda ) )
  440.             {
  441.                 /* Args erfolgreich gelesen */
  442.  
  443.                 /* und ausgeben */
  444.  
  445.                 cout << "Readargs: "<< endl;
  446.  
  447.                 cout << "STRING: "  << args.string     << endl;
  448.                 cout << "INTEGER: " << *(args.integer) << endl;
  449.                 cout << "TEST: "    << args.test       << endl;
  450.  
  451.                 ok=TRUE;
  452.             }
  453.             else
  454.             {
  455.                 err.DosError(IoErr(), "Error parsing commandline");    /* prints the appropriate err message */
  456.             }
  457.         }
  458.         else
  459.             err.NotifyError("Out of memory error !");
  460.     }
  461.     else
  462.     {
  463.         /* Workbench-Start */
  464.  
  465.         ToolTypes = (UBYTE **)ArgArrayInit((LONG)alen,(UBYTE **)aptr);
  466.  
  467.         if(ToolTypes)
  468.         {
  469.             /* open a windows for Input() und Output()                  */
  470.             /* this is only sometimes useful. If you don't do something */
  471.             /* similar, you won't be able to output something to stdout */
  472.             /* (cout, cin)                                              */
  473.             /* necessary because startupcode initializes stdin=Input()  */
  474.             /* and stdout=Output(), which is both zero if called via WB */
  475.  
  476.             DisplayBeep(0);
  477.  
  478.             stdin=(BPTR)Open((STRPTR)ArgString(ToolTypes,(STRPTR)"WINDOW", (STRPTR) "CON:0/0/640/200/Startup Code demo window/AUTO/CLOSE/WAIT"),MODE_NEWFILE);
  479.             stdout=stdin;
  480.             stdin_WB=stdin;
  481.  
  482.             SelectInput(stdin);
  483.             SelectOutput(stdout);
  484.  
  485.             cin.attach(Input());    // let cin and cout
  486.             cout.attach(Output());    // notify our Input() and Output() changed
  487.  
  488.  
  489.             /* look here at all your supported tooltypes ! */
  490.             /* we take defaults if no arguements are given */
  491.             /* NOTE: this is different from CLI !          */
  492.  
  493.             args.string    =(char *)ArgString(ToolTypes, (STRPTR)"STRING",  (STRPTR)args.string);
  494.  
  495.             *(args.integer)=        ArgInt(   ToolTypes, (STRPTR)"INTEGER", (LONG)  99L);
  496.  
  497.             args.test      =(char *)ArgString(ToolTypes, (STRPTR)"TEST",    (STRPTR)args.test);
  498.  
  499.  
  500.             /* Just as an example, normally you would do some other things... */
  501.  
  502.             cout << "ArgArrayInit:" << endl;
  503.  
  504.             cout << "STRING: "  << args.string     << endl;
  505.             cout << "INTEGER: " << *(args.integer) << endl;
  506.             cout << "TEST: "    << args.test       << endl;
  507.  
  508.             ok=TRUE;
  509.         }
  510.     }
  511.  
  512.     cin.tie(Output());        // to flush output before input is done
  513.                             // do this before you get any input !
  514.                             // (but after we have stdout and stdin defined if called via wb)
  515.  
  516.     return ok;
  517. }
  518.  
  519.  
  520.  
  521.  
  522.     /*
  523.      * Here the testprogram starts, uses some misc features
  524.      *
  525.      */
  526.  
  527.  
  528. int main(int alen, char *aptr)
  529. {
  530.     if(MyInit(alen,aptr))        // all things went well
  531.     {
  532.         char String[100];        // in C++ is is possible to declare variables everywhere, not only at the top of a function
  533.  
  534.  
  535.         // demonstrate some in/outputs
  536.  
  537.         cout << "Please enter string: ";
  538.         cin.get(String,100);             // save variant
  539.  
  540.         cout << String << endl;
  541.  
  542.         cout << "Please enter secound string: " << flush;
  543.         cin >> String;                    // only 80 bytes are read in for security reasons
  544.  
  545.         cout << String << endl;
  546.  
  547.  
  548.  
  549.         // do the same with some integers
  550.  
  551.         LONG x_a=0, x_b=0, y_a=0, y_b=0;
  552.  
  553.  
  554.         ShowRequest(NULL, (UBYTE *)"Demo window",
  555.             (UBYTE *)"Note that you can write 0x... for hex numbers,\nand 0... for octal numers",
  556.             (UBYTE *)"Yes, my lord");
  557.  
  558.         cin.clear();            // we want to have a clear the state of cin
  559.  
  560.         while( (!cin) || (x_a < 2) || (x_a > 36) )
  561.         {
  562.             cin.clear();
  563.             cout << "Please enter base_from :";
  564.             cin >> x_a;
  565.             if(cin.good())
  566.                 cout << "= " << x_a << endl;
  567.             else
  568.                 cin.ignore(9999,'\n');    // skip all characters till EOF or '\n'
  569.         }
  570.  
  571.         while( (!cin) || (x_b < 2) || (x_b > 36) || (x_b < x_a) )
  572.         {
  573.             cin.clear();
  574.             cout << "Please enter base_to :";
  575.             cin >> x_b;
  576.             if(cin.good())
  577.                 cout << "= " << x_b << endl;
  578.             else
  579.                 cin.ignore(9999,'\n');
  580.         }
  581.  
  582.         do
  583.         {
  584.             cin.clear();
  585.             cout << "Please enter number_from :";
  586.             cin >> y_a;
  587.             if(cin.good())
  588.                 cout << "= " << y_a << endl;
  589.             else
  590.                 cin.ignore(9999,'\n');
  591.         }
  592.            while(!cin);
  593.  
  594.         do
  595.         {
  596.             cin.clear();
  597.             cout << "Please enter number_to :";
  598.             cin >> y_b;
  599.             if(cin.good())
  600.                 cout << "= " << y_b << endl;
  601.             else
  602.                 cin.ignore(9999,'\n');
  603.  
  604.         }
  605.         while( (!cin) || (y_b < y_a) );
  606.  
  607.  
  608.  
  609.         // allocate memory. I am sure you could do better. But remeber,
  610.         // this is only an example
  611.  
  612.         xxxx xyz;
  613.  
  614.  
  615.         // do some output
  616.  
  617.         if (xyz.getmem())
  618.         {
  619.             ULONG x;
  620.             BOOL stop=FALSE;
  621.  
  622.             for(x=x_a;(x<=x_b) && (!stop);x++)
  623.             {
  624.                 LONG y;
  625.                 for(y=y_a;(y<=y_b) && (!stop) ; y++)
  626.                 {
  627.                     cout << "Base:" << x << ' ' << ltostr(y,xyz.getmem(),1000,TRUE,x);
  628.                     cout << endl << flush;
  629.  
  630.  
  631.                     // if the user wants to abort the output, he can..
  632.  
  633.                     if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) /* Hit since last check? */
  634.                     {
  635.                          SetSignal(0L,SIGBREAKF_CTRL_C);    /* Clear old status */
  636.                         cout << "*** BREAK\n" << endl;
  637.                         stop=TRUE;
  638.                     }
  639.                 }
  640.             }
  641.  
  642.             // Show Clipboard
  643.  
  644.             ULONG len;
  645.  
  646.             if(len=LoadClip((UBYTE *)xyz.getmem(),10000))
  647.             {
  648.                 char *s=xyz.getmem();
  649.                 s[len]=0;
  650.                 ShowRequest(NULL, (UBYTE *)"Contents of the clipboard", (UBYTE *)xyz.getmem(),(UBYTE *)"OK, I saw the clipboard");
  651.             }
  652.  
  653.         }
  654.         else cout << "Out of memory error !" << endl;
  655.  
  656.  
  657.  
  658.         // demonstrate class editfile
  659.  
  660.         cout << "Please enter filename to read: ";
  661.         cin.get(String,100);
  662.  
  663.         err.Clear();
  664.  
  665.         editfile ef(String);
  666.  
  667.         if(err)
  668.         {
  669.             cout << "Error reading file \"" << String << "\"";
  670.             OutputError();
  671.         }
  672.         else
  673.         {
  674.             cout << String << " has " << ef.strings << " lines." << endl;
  675.  
  676.             cout << "The first three lines are:" << endl;
  677.  
  678.             for(ULONG x=0; (x<3) && (x < ef.strings); x++)
  679.                 cout << ef.index[x] << endl;
  680.         }
  681.  
  682.         cout << endl;
  683.  
  684.  
  685.  
  686.  
  687.         // demonstrate CED_Support
  688.  
  689.  
  690.         // look for CED
  691.  
  692.         MsgPort *CED_Port=FindPort((STRPTR)"rexx_ced");
  693.  
  694.         if(CED_Port)
  695.         {
  696.             cout << "You have CED in memory" << endl;
  697.             cout << "Is it Version 3.5 (or higer)? (y/n) ";
  698.  
  699.             // the functions in CED_support only work with CED 3.5+, so we have
  700.             // to ask the user
  701.  
  702.             char ok=0;
  703.  
  704.             while( (ok != 'n') && (ok != 'y'))
  705.             {
  706.                 cin >> ok;
  707.             }
  708.  
  709.             if(ok=='n')
  710.             {
  711.                 cout << "Upgrade as fast as possible" << endl;
  712.             }
  713.             else
  714.             {
  715.                 // Send some strings to CED
  716.  
  717.                 SendCygnusEdMessage("cedtofront");
  718.                 SendCygnusEdMessageGetReply("status restname"); /* Name der Datei im aktuellen View */
  719.                 char *s=GetStringFromArexx();            // allocates storage and copies the result into it
  720.  
  721.                 char *Buffer;
  722.                 if(Buffer=(char *)AllocVec(1024,MEMF_ANY))
  723.                 {
  724.                     sprintf((UBYTE *)Buffer,(UBYTE *)"okay1 You are vieving file %s\n",s);
  725.                     SendCygnusEdMessage("cedtofront");
  726.                     SendCygnusEdMessage(Buffer);
  727.                     FreeVec((UBYTE *)Buffer);
  728.                 }
  729.                 else
  730.                     err.NotifyError("Out of memory error !");
  731.  
  732.                 FreeVec(s);                    // free allocated memory by GetStringFromArexx()
  733.             }
  734.         }
  735.         else
  736.         {
  737.             cout << "You don't have CED in memory" << endl;
  738.         }
  739.  
  740.  
  741.         // try to allocate some memory
  742.  
  743.         char *a = new char [10000];
  744.  
  745.         char *b = new char [500000];
  746.  
  747.         if(b)
  748.             cout << "You had some memory free...\n";
  749.  
  750.         char *c = new char [1000000];
  751.         if(c)
  752.             cout << "Wow, you had even some more memory free...\n";
  753.  
  754.  
  755.         char *d= new(MEMF_CLEAR) char [10000000];
  756.         if(d)
  757.             cout << "Wow, you must have really a lot of ram...\n";
  758.  
  759.         delete a;
  760.         delete b;
  761.         delete c;
  762.         delete d;
  763.  
  764.     }    // MyInit()
  765.  
  766.  
  767.  
  768.     // if an error occurred...
  769.  
  770.     OutputError();
  771.  
  772.     cout << "end of demo program." << endl;
  773.  
  774.  
  775.     /* the Dos-Printf */
  776.  
  777.     Printf((UBYTE *)"Hello, world ! %ld %ld %ld %ld\n",1,2,3,4);
  778.  
  779.     return(0);                // exit is called if we fall through
  780. }
  781.  
  782.  
  783.  
  784.  
  785.     /*
  786.      * Output error-message if we have one
  787.      *
  788.      */
  789.  
  790.  
  791. void OutputError(void)
  792. {
  793.     if(err)
  794.     {
  795.         char *err_mem=0;
  796.         char *es;
  797.         LONG err_nr=err.SetErrorNumber(0);
  798.  
  799.         if(es=err.have_error_string())
  800.         {
  801.             cout <<    es << endl;
  802.         }
  803.         else
  804.         {
  805.             if(err_nr)
  806.             {
  807.                 cout <<    "Error-Number: " << err_nr << endl;
  808.             }
  809.         }
  810.     }
  811.  
  812.     err.Clear();
  813. }
  814.