home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / APLocation / Sources / MacOS_Exceptions.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  7.4 KB  |  297 lines

  1. /*==================================================================
  2.     File:        MacOS_Exceptions.cpp
  3.  
  4.     Contains:    Debugging code for use with Exceptions.h.
  5.  
  6.     Written by:    Eric Traut
  7.  
  8.     Copyright:    1995-99 Connectix Corporation
  9. ==================================================================*/
  10.  
  11. #include "MacOS_Exceptions.h"
  12.  
  13. /*********************************************************************
  14.     GLOBAL/STATIC VARIABLES
  15. *********************************************************************/
  16.  
  17. static char                     sExcString[1024];
  18. static ExceptionCallBackProc    sCallBackProc = NULL;
  19.  
  20. const char *                    gFeatureIsUsed = "Feature Is Used";
  21. const char *                    gUnexpectedValue = "Unexpected Value";
  22. const char *                    gUnimplemented = "Unimplemented Feature";
  23. const char *                    gUntested = "Untested Feature";
  24.  
  25.  
  26. /*------------------------------------------------------------------
  27.     InternalStrCpy
  28.  
  29.     This routine is here so we can remove a dependency on the
  30.     standard C library.
  31. ------------------------------------------------------------------*/
  32.  
  33. static char *
  34. InternalStrCpy(char * dest, const char * src)
  35. {
  36.     while ((*dest++ = *src++) != 0) {};
  37.     return dest - 1;
  38. }
  39.  
  40.  
  41. /*------------------------------------------------------------------
  42.     InternalPrintInt
  43.  
  44.     This routine is here so we can remove a dependency on the
  45.     standard C library.
  46. ------------------------------------------------------------------*/
  47.  
  48. static char *
  49. InternalPrintInt(char * dest, unsigned int value)
  50. {
  51.     unsigned int digitValue = 10000000;
  52.     unsigned int digit;
  53.     int started = false;
  54.  
  55.     if (value == 0)
  56.     {
  57.         *dest++ = '0';
  58.         *dest = '\0';
  59.     }
  60.     else
  61.     {
  62.         while (digitValue > 0)
  63.         {
  64.             if (value >= digitValue || started)
  65.             {
  66.                 digit = value / digitValue;
  67.                 value -= digit * digitValue;
  68.                 *dest++ = '0' + digit;
  69.                 started = true;
  70.             }
  71.  
  72.             digitValue /= 10;
  73.         }
  74.  
  75.         *dest = '\0';
  76.     }
  77.  
  78.     return dest;
  79. }
  80.  
  81.  
  82. /*------------------------------------------------------------------
  83.     AssertionFailed
  84. ------------------------------------------------------------------*/
  85.  
  86. StringPtr
  87. AssertionFailed(const char * assertionString)
  88. {
  89.     char * strPtr = sExcString + 1;
  90.     unsigned int length;
  91.  
  92. #if TARGET_API_MAC_CARBON
  93.     strPtr = InternalStrCpy(strPtr, "[");
  94.     strPtr = InternalStrCpy(strPtr, kProgramName);
  95.     strPtr = InternalStrCpy(strPtr, "] ");
  96. #endif
  97.  
  98.     strPtr = InternalStrCpy(strPtr, "# Assertion \"");
  99.     strPtr = InternalStrCpy(strPtr, assertionString);
  100.     strPtr = InternalStrCpy(strPtr, "\" Failed");
  101.  
  102.     length = strPtr - sExcString - 1;
  103.     if (length > 255)
  104.         length = 255;
  105.     sExcString[0] = length;
  106.  
  107.     return (StringPtr)&sExcString[0];
  108. }
  109.  
  110.  
  111. /*------------------------------------------------------------------
  112.     AssertionFailedFileLine
  113. ------------------------------------------------------------------*/
  114.  
  115. StringPtr
  116. AssertionFailedFileLine(    const char *     assertionString,
  117.                             const char *     fileString,
  118.                             unsigned int    line)
  119. {
  120.     char * strPtr = sExcString + 1;
  121.     unsigned int length;
  122.  
  123. #if TARGET_API_MAC_CARBON
  124.     strPtr = InternalStrCpy(strPtr, "[");
  125.     strPtr = InternalStrCpy(strPtr, kProgramName);
  126.     strPtr = InternalStrCpy(strPtr, "] ");
  127. #endif
  128.  
  129.     strPtr = InternalStrCpy(strPtr, "# Assertion \"");
  130.     strPtr = InternalStrCpy(strPtr, assertionString);
  131.     strPtr = InternalStrCpy(strPtr, "\" Failed file ");
  132.     strPtr = InternalStrCpy(strPtr, fileString);
  133.     strPtr = InternalStrCpy(strPtr, ", line ");
  134.     strPtr = InternalPrintInt(strPtr, line);
  135.  
  136.     length = strPtr - sExcString - 1;
  137.     if (length > 255)
  138.         length = 255;
  139.     sExcString[0] = length;
  140.  
  141.     if (sCallBackProc != NULL)
  142.         (*sCallBackProc)((char *)&sExcString[1]);
  143.  
  144.     return (StringPtr)&sExcString[0];
  145. }
  146.  
  147.  
  148. /*------------------------------------------------------------------
  149.     AssertionFailedExceptionRaised
  150. ------------------------------------------------------------------*/
  151.  
  152. StringPtr
  153. AssertionFailedExceptionRaised(    const char *    assertionString,
  154.                                 const char *    exceptionString)
  155. {
  156.     char * strPtr = sExcString + 1;
  157.     unsigned int length;
  158.  
  159. #if TARGET_API_MAC_CARBON
  160.     strPtr = InternalStrCpy(strPtr, "[");
  161.     strPtr = InternalStrCpy(strPtr, kProgramName);
  162.     strPtr = InternalStrCpy(strPtr, "] ");
  163. #endif
  164.  
  165.     strPtr = InternalStrCpy(strPtr, "# Assertion \"");
  166.     strPtr = InternalStrCpy(strPtr, assertionString);
  167.     strPtr = InternalStrCpy(strPtr, "\" Failed # Exception \"");
  168.     strPtr = InternalStrCpy(strPtr, exceptionString);
  169.     strPtr = InternalStrCpy(strPtr, "\" Raised");
  170.  
  171.     length = strPtr - sExcString - 1;
  172.     if (length > 255)
  173.         length = 255;
  174.     sExcString[0] = length;
  175.  
  176.     if (sCallBackProc != NULL)
  177.         (*sCallBackProc)((char *)&sExcString[1]);
  178.  
  179.     return (StringPtr)&sExcString[0];
  180. }
  181.  
  182.  
  183. /*------------------------------------------------------------------
  184.     AssertionFailedExceptionRaisedFileLine
  185. ------------------------------------------------------------------*/
  186.  
  187. StringPtr
  188. AssertionFailedExceptionRaisedFileLine(    const char *    assertionString,
  189.                                         const char *    exceptionString,
  190.                                         const char *    fileString,
  191.                                         unsigned int    line)
  192. {
  193.     char * strPtr = sExcString + 1;
  194.     unsigned int length;
  195.  
  196. #if TARGET_API_MAC_CARBON
  197.     strPtr = InternalStrCpy(strPtr, "[");
  198.     strPtr = InternalStrCpy(strPtr, kProgramName);
  199.     strPtr = InternalStrCpy(strPtr, "] ");
  200. #endif
  201.  
  202.     strPtr = InternalStrCpy(strPtr, "# Assertion \"");
  203.     strPtr = InternalStrCpy(strPtr, assertionString);
  204.     strPtr = InternalStrCpy(strPtr, "\" Failed # Exception \"");
  205.     strPtr = InternalStrCpy(strPtr, exceptionString);
  206.     strPtr = InternalStrCpy(strPtr, "\" Raised file ");
  207.     strPtr = InternalStrCpy(strPtr, fileString);
  208.     strPtr = InternalStrCpy(strPtr, ", line ");
  209.     strPtr = InternalPrintInt(strPtr, line);
  210.  
  211.     length = strPtr - sExcString - 1;
  212.     if (length > 255)
  213.         length = 255;
  214.     sExcString[0] = length;
  215.  
  216.     if (sCallBackProc != NULL)
  217.         (*sCallBackProc)((char *)&sExcString[1]);
  218.  
  219.     return (StringPtr)&sExcString[0];
  220. }
  221.  
  222.  
  223. /*------------------------------------------------------------------
  224.     PrintDebugString
  225. ------------------------------------------------------------------*/
  226.  
  227. StringPtr
  228. PrintDebugString(const char * debugString)
  229. {
  230.     char * strPtr = sExcString + 1;
  231.     unsigned int length;
  232.  
  233.     strPtr = InternalStrCpy(strPtr, debugString);
  234.  
  235.     length = strPtr - sExcString - 1;
  236.     if (length > 255)
  237.         length = 255;
  238.     sExcString[0] = length;
  239.  
  240.     if (sCallBackProc != NULL)
  241.         (*sCallBackProc)((char *)&sExcString[1]);
  242.  
  243.     return (StringPtr)&sExcString[0];
  244. }
  245.  
  246.  
  247. /*------------------------------------------------------------------
  248.     PrintDebugStringFileLine
  249. ------------------------------------------------------------------*/
  250.  
  251. StringPtr
  252. PrintDebugStringFileLine(    const char *     debugString,
  253.                             const char *     fileString,
  254.                             unsigned int    line)
  255. {
  256.     char * strPtr = sExcString + 1;
  257.     unsigned int length;
  258.  
  259.     strPtr = InternalStrCpy(strPtr, debugString);
  260.     strPtr = InternalStrCpy(strPtr, "; file ");
  261.     strPtr = InternalStrCpy(strPtr, fileString);
  262.     strPtr = InternalStrCpy(strPtr, ", line ");
  263.     strPtr = InternalPrintInt(strPtr, line);
  264.  
  265.     length = strPtr - sExcString - 1;
  266.     if (length > 255)
  267.         length = 255;
  268.     sExcString[0] = length;
  269.  
  270.     if (sCallBackProc != NULL)
  271.         (*sCallBackProc)((char *)&sExcString[1]);
  272.  
  273.     return (StringPtr)&sExcString[0];
  274. }
  275.  
  276.  
  277. /*------------------------------------------------------------------
  278.     InstallExceptionCallBack
  279.  
  280.     This routine allows the client to install a "callback" routine
  281.     for when assertions fire.
  282. ------------------------------------------------------------------*/
  283.  
  284. void
  285. InstallExceptionCallBack(ExceptionCallBackProc inCallBackProc)
  286. {
  287.     sCallBackProc = inCallBackProc;
  288. }
  289.  
  290.  
  291.  
  292. /*==================================================================
  293.     Change History (most recent first):
  294.  
  295.     $Log: MacOS_Exceptions.cpp,v $
  296. ==================================================================*/
  297.