home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_USR08B.LHA / gerlib / examples / add / normal / OwnError.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  3.1 KB  |  171 lines

  1. /*-- Rev Header - do NOT edit!
  2.  *
  3.  *  Filename : OwnError.h
  4.  *  Purpose  : Eine Klasse, die das Bearbeiten von schwerwiegenden Fehlern erleichtert,
  5.  *             die zum Abbruch des Programmes führen müssen
  6.  *
  7.  *  Program  : -
  8.  *  Author   : Gerhard Müller
  9.  *  Copyright: (c) by Gerhard Müller
  10.  *  Creation : Sun Sep 12 20:44:12 1993
  11.  *
  12.  *  compile  :
  13.  *
  14.  *  Compile version  : 0.1
  15.  *  Ext. Version     : 0.1
  16.  *
  17.  *  REVISION HISTORY
  18.  *
  19.  *  Date                     Comment
  20.  *  ------------------------ -------------------------------------------------
  21.  *  Fri Sep 17 00:56:20 1993 Created because I don't know how to use execptions
  22.  *
  23.  *-- REV_END --
  24.  */
  25.  
  26. #ifndef OWNERROR_H
  27. #define OWNERROR_H
  28.  
  29.     /*
  30.      * C-Includes, C-Definitionen
  31.      *
  32.      */
  33.  
  34. #define class _class
  35. #define template _template
  36.  
  37. extern "C" {
  38. #include <exec/types.h>
  39. #include <exec/memory.h>
  40. #include <clib/alib_protos.h>
  41. #include <clib/alib_stdio_protos.h>
  42. #include <inline/stubs.h>
  43. #include <dos/dos.h>
  44. #include <dos/dosextens.h>
  45. #ifdef __OPTIMIZE__
  46. #include <inline/exec.h>
  47. #include <inline/dos.h>
  48. #else
  49. #include <clib/exec_protos.h>
  50. #include <clib/dos_protos.h>
  51. #endif
  52. }
  53.  
  54. #undef template
  55. #undef class
  56.  
  57.  
  58.     /*
  59.      * C++-Includes, C++-Definitionen
  60.      *
  61.      */
  62.  
  63. typedef void (*OwnError_err_func)(char *, LONG);
  64.  
  65. class OwnError
  66. {
  67. public:
  68.     OwnError()
  69.     {
  70.         // nothing to do to construct, only clear our private things
  71.         error_func=0;
  72.         error_string=0;
  73.         error_number=RETURN_OK;
  74.     }
  75.  
  76.     void NotifyError(char *s,LONG nr=RETURN_FAIL, OwnError_err_func ef=0)
  77.     {
  78.         // we dont't have the ability to store more errors (yet), only
  79.         // the last one is memorized
  80.  
  81.         if(error_string)
  82.         {
  83.             FreeVec(error_string);
  84.         }
  85.  
  86.         error_func=ef;
  87.         error_string=(char *)AllocVec(strlen(s)+1,MEMF_CLEAR);
  88.         strcpy(error_string,s);
  89.         error_number=nr;
  90.     }
  91.  
  92.     LONG SetErrorNumber(LONG error)
  93.     {
  94.         LONG old_error=error_number;
  95.  
  96.         error_number=error;
  97.  
  98.         return old_error;
  99.     }
  100.  
  101.     OwnError_err_func SetErrorFunc(OwnError_err_func ef)
  102.     {
  103.         OwnError_err_func old_error = error_func;
  104.  
  105.         error_func=ef;
  106.  
  107.         return old_error;
  108.     }
  109.  
  110.  
  111.     void SetErrorString(char *new_error_string)
  112.     {
  113.         if(error_string) FreeVec(error_string);
  114.  
  115.         error_string = new_error_string;
  116.     }
  117.  
  118.     void Clear()
  119.     {
  120.         if(error_string) FreeVec(error_string);
  121.  
  122.         error_func=0;
  123.         error_string=0;
  124.         error_number=RETURN_OK;
  125.     }
  126.  
  127.     void DosError(ULONG Err,char *def="")  // we can use AmigaDos-Error-Strings
  128.     {
  129.         if(error_string) FreeVec(error_string);
  130.  
  131.         // get String with dos.Fault()
  132.  
  133.         error_string=(char *)AllocVec(200,MEMF_CLEAR);
  134.  
  135.         Fault(Err,(unsigned char *)def,(unsigned char *)error_string,200);
  136.     }
  137.  
  138.     ~OwnError()
  139.     {
  140.         if(error_func) OwnError_err_func(error_string,error_number);
  141.         if(error_string) FreeVec(error_string);
  142.     }
  143.  
  144.     char *have_error_string(void)        // did we have an Error ? Yes ? Give the Err-Str.
  145.     {
  146.         return(error_string);
  147.     }
  148.  
  149.     BOOL have_error(void)                // did we have an Error ? Yes ?
  150.     {
  151.         if(error_string || error_number) return(TRUE);
  152.         else return(FALSE);
  153.     }
  154.  
  155.     operator void*()                    // allows if(err) { }
  156.     {
  157.         return (void *)have_error();
  158.     }
  159.  
  160. private:
  161.  
  162.     OwnError_err_func error_func;
  163.     char    *error_string;
  164.     LONG    error_number;
  165. };
  166.  
  167.  
  168. extern OwnError err;
  169.  
  170. #endif
  171.