home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / odtlktv4.zip / ODTLKT / TOOLKIT / BETA / SAMPLES / OPENDOC / PUBUTILS / EXCEPT.CPP < prev    next >
Text File  |  1995-12-04  |  7KB  |  235 lines

  1. /********************************************************************/
  2. /*  Licensed Materials - Property of IBM                            */
  3. /*                                                                  */
  4. /*                                                                  */
  5. /* Copyright (C) International Business Machines Corp., 1994.       */
  6. /* Copyright (C) Apple Computer, Inc., 1994                         */
  7. /*                                                                  */
  8. /*  US Government Users Restricted Rights -                         */
  9. /*  Use, duplication, or disclosure restricted                      */
  10. /*  by GSA ADP Schedule Contract with IBM Corp.                     */
  11. /*                                                                  */
  12. /*  File:    Except.cpp                                             */
  13. /*                                                                  */
  14. /*  Contains:  Exception-handling, assertion and debugging utilities*/
  15. /*                                                                  */
  16. /********************************************************************/
  17.  
  18. #include <builtin.h>
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #ifndef __EXCEPT__
  25. #include "Except.h"
  26. #endif
  27.  
  28. #ifndef somapi_h
  29. #include <somapi.h>
  30. #endif
  31.  
  32. #ifndef _PLATFORM_OS2_PPC_
  33. #define BREAK(msg)  _interrupt(3)
  34. #else
  35. #define BREAK(msg)  // How do you break into debugger on PPC???
  36. #endif
  37.  
  38. #include "ErrStrs.h"
  39.  
  40. extern "C" {
  41. static int _Optlink CompareErrors(const void *element1, const void* element2);
  42. }
  43.  
  44. const char* ODGetErrStringRep(ODError error)
  45. {
  46.    /* Search the array for the specified error.*/
  47.    /* Use a binary search technique since the  */
  48.    /* array is sorted by error code.           */
  49.  
  50.    int numerrors = sizeof(ErrorStrings) / sizeof(ODErrorString);
  51.    ODErrorString key = {error, NULL};
  52.    ODErrorString* match = (ODErrorString*)
  53.            bsearch(&key, ErrorStrings, numerrors, sizeof(ODErrorString),
  54.                    CompareErrors);
  55.    return (match ? match->str : "Unknown Error");
  56. }
  57.  
  58. int CompareErrors(const void *element1, const void* element2)
  59. {
  60.    return ((ODErrorString*)element1)->err - ((ODErrorString*)element2)->err;
  61. }
  62.  
  63.  
  64.  
  65. ODNativeException::ODNativeException(ODError error, const char* msg)
  66. {
  67.    fEv = 0;
  68.    fError = error;
  69.    if (!msg)
  70.       msg = "";
  71.  
  72.    fMessage = (char*)SOMMalloc(strlen(msg)+1);
  73.    if (fMessage)
  74.       strcpy(fMessage, msg);
  75. }
  76.  
  77. ODNativeException::ODNativeException(const ODNativeException& x)
  78. {
  79.    fEv = 0;
  80.    fError = x.fError;
  81.    fMessage = (char *)SOMMalloc(strlen(x.fMessage)+1);
  82.    if (fMessage)
  83.       strcpy(fMessage, x.fMessage);
  84. }
  85.  
  86. ODNativeException::~ODNativeException()
  87. {
  88.    if (fEv) {
  89.       ODSetSOMException(fEv, *this);
  90.    }
  91.    if (fMessage)
  92.       SOMFree(fMessage);
  93. }
  94.  
  95.  
  96. //===================================================================================
  97. // THROW, et al
  98. //===================================================================================
  99.  
  100. static const char fmtstring[] = "%s%sException %d:%s raised by %s in %s(%d).\n";
  101.  
  102. void THROWproc( ODError error, const char* msg, int linenum, const char*functionname, const char*filename )
  103. {
  104.   if (linenum)
  105.   {
  106.      /* Append debug info to message */
  107.  
  108.      const char* errstr = ODGetErrStringRep(error);
  109.  
  110.      int strsize = sizeof(fmtstring) + strlen(functionname) + strlen(errstr) +
  111.                    strlen(filename) + 22 + (msg ? (strlen(msg)+4) : 0);
  112.  
  113.      char* buffer = (char*)_alloca(strsize);
  114.  
  115.      if (buffer) {
  116.         sprintf( buffer,fmtstring, msg ? msg : "", msg ? "\n\n" : "",
  117.                  error, errstr, functionname, filename, linenum);
  118.         msg = buffer;
  119.      }
  120.      somPrintf("%s",msg);
  121.   }
  122.   else
  123.   {
  124.     if( msg )
  125.       somPrintf("** THROW(%d) called\n", error);
  126.     else
  127.       somPrintf("** THROW(%d,%s) called\n", error,msg);
  128.   }
  129.   if( getenv("ODBREAKONTHROW") )
  130.      BREAK(msg);
  131.  
  132.   ODNativeException x(error, msg);
  133.   throw(x);
  134. }
  135.  
  136. void RAISEproc(Environment* ev, ODError error, const char* msg, int linenum, const char* functionname, const char* filename)
  137. {
  138.    if (linenum)
  139.    {
  140.       const char* errstr = ODGetErrStringRep(error);
  141.  
  142.       int strsize = sizeof(fmtstring) + strlen(functionname) + strlen(errstr) +
  143.                     strlen(filename) + 22 + (msg ? (strlen(msg)+4) : 0);
  144.  
  145.       char* buffer = (char*)_alloca(strsize);
  146.  
  147.       if (buffer) {
  148.          sprintf( buffer,fmtstring, msg ? msg : "", msg ? "\n\n" : "",
  149.                   error, errstr, functionname, filename, linenum);
  150.  
  151.          msg = buffer;
  152.       }
  153.       somPrintf("%s", msg);
  154.    }
  155.    else
  156.    {
  157.      if( msg )
  158.        somPrintf("** RAISE(%d) called\n", error);
  159.      else
  160.        somPrintf("** RAISE(%d,%s) called\n", error,msg);
  161.    }
  162.    if( getenv("ODBREAKONTHROW") )
  163.       BREAK(msg);
  164.  
  165.    ODSetSOMException(ev, error, msg);
  166. }
  167.  
  168.  
  169. //===================================================================================
  170. // SOM EXCEPTIONS
  171. //===================================================================================
  172.  
  173.  
  174. void
  175. ODSetSOMException( Environment *ev, ODError error, const char *message /*=NULL*/ )
  176. {
  177.   if( error ) {
  178.     ODException *x = (ODException*) SOMMalloc(sizeof(ODException));
  179.     x->error = error;
  180.     if( message )
  181.       strcpy(x->message,message);
  182.     else
  183.       x->message[0] = '\0';
  184.     somSetException(ev,USER_EXCEPTION,ex_ODException,x);
  185.   } else {
  186.     somExceptionFree(ev);
  187.     ev->_major = NO_EXCEPTION;
  188.   }
  189. }
  190.  
  191.  
  192. void
  193. ODSetSOMException( Environment *ev, ODNativeException& except )
  194. {
  195.   ODSetSOMException(ev,except.error(), except.message());
  196. }
  197.  
  198.  
  199. ODError
  200. ODGetSOMException( Environment *ev )
  201. {
  202.   if( ev->_major ) {
  203.     const char *excpName = somExceptionId(ev);
  204.     if( strcmp(excpName,ex_ODException) == 0 ) {
  205.       ODException *x = (ODException*)somExceptionValue(ev);
  206.       return x->error;
  207.     } else {
  208.       somPrintf("Env has non-OpenDoc err: %s",excpName);
  209.       return kODErrSOMException;
  210.     }
  211.   } else
  212.     return kODNoError;
  213. }
  214.  
  215.  
  216. void CHECK_ENV( Environment *ev )
  217. {
  218.   if( ev->_major ) {
  219.     const char *excpName = somExceptionId(ev);
  220.     if( strcmp(excpName,ex_ODException) == 0 ) {
  221.       ODException x = *(ODException*)somExceptionValue(ev);
  222.       somExceptionFree(ev);
  223.       ev->_major = NO_EXCEPTION;
  224.       ODNativeException ex(x.error, x.message[0] ? x.message : kODNULL);
  225.       throw(ex);
  226.     } else {
  227.       somPrintf("Env has non-OpenDoc err: %s",excpName);
  228.       somExceptionFree(ev);
  229.       ev->_major = NO_EXCEPTION;
  230.       ODNativeException ex(kODErrSOMException, "Unknown SOM exception");
  231.       throw(ex);
  232.     }
  233.   }
  234. }
  235.