home *** CD-ROM | disk | FTP | other *** search
- /*==================================================================
- File: MacOS_Exceptions.cpp
-
- Contains: Debugging code for use with Exceptions.h.
-
- Written by: Eric Traut
-
- Copyright: 1995-99 Connectix Corporation
- ==================================================================*/
-
- #include "MacOS_Exceptions.h"
-
- /*********************************************************************
- GLOBAL/STATIC VARIABLES
- *********************************************************************/
-
- static char sExcString[1024];
- static ExceptionCallBackProc sCallBackProc = NULL;
-
- const char * gFeatureIsUsed = "Feature Is Used";
- const char * gUnexpectedValue = "Unexpected Value";
- const char * gUnimplemented = "Unimplemented Feature";
- const char * gUntested = "Untested Feature";
-
-
- /*------------------------------------------------------------------
- InternalStrCpy
-
- This routine is here so we can remove a dependency on the
- standard C library.
- ------------------------------------------------------------------*/
-
- static char *
- InternalStrCpy(char * dest, const char * src)
- {
- while ((*dest++ = *src++) != 0) {};
- return dest - 1;
- }
-
-
- /*------------------------------------------------------------------
- InternalPrintInt
-
- This routine is here so we can remove a dependency on the
- standard C library.
- ------------------------------------------------------------------*/
-
- static char *
- InternalPrintInt(char * dest, unsigned int value)
- {
- unsigned int digitValue = 10000000;
- unsigned int digit;
- int started = false;
-
- if (value == 0)
- {
- *dest++ = '0';
- *dest = '\0';
- }
- else
- {
- while (digitValue > 0)
- {
- if (value >= digitValue || started)
- {
- digit = value / digitValue;
- value -= digit * digitValue;
- *dest++ = '0' + digit;
- started = true;
- }
-
- digitValue /= 10;
- }
-
- *dest = '\0';
- }
-
- return dest;
- }
-
-
- /*------------------------------------------------------------------
- AssertionFailed
- ------------------------------------------------------------------*/
-
- StringPtr
- AssertionFailed(const char * assertionString)
- {
- char * strPtr = sExcString + 1;
- unsigned int length;
-
- #if TARGET_API_MAC_CARBON
- strPtr = InternalStrCpy(strPtr, "[");
- strPtr = InternalStrCpy(strPtr, kProgramName);
- strPtr = InternalStrCpy(strPtr, "] ");
- #endif
-
- strPtr = InternalStrCpy(strPtr, "# Assertion \"");
- strPtr = InternalStrCpy(strPtr, assertionString);
- strPtr = InternalStrCpy(strPtr, "\" Failed");
-
- length = strPtr - sExcString - 1;
- if (length > 255)
- length = 255;
- sExcString[0] = length;
-
- return (StringPtr)&sExcString[0];
- }
-
-
- /*------------------------------------------------------------------
- AssertionFailedFileLine
- ------------------------------------------------------------------*/
-
- StringPtr
- AssertionFailedFileLine( const char * assertionString,
- const char * fileString,
- unsigned int line)
- {
- char * strPtr = sExcString + 1;
- unsigned int length;
-
- #if TARGET_API_MAC_CARBON
- strPtr = InternalStrCpy(strPtr, "[");
- strPtr = InternalStrCpy(strPtr, kProgramName);
- strPtr = InternalStrCpy(strPtr, "] ");
- #endif
-
- strPtr = InternalStrCpy(strPtr, "# Assertion \"");
- strPtr = InternalStrCpy(strPtr, assertionString);
- strPtr = InternalStrCpy(strPtr, "\" Failed file ");
- strPtr = InternalStrCpy(strPtr, fileString);
- strPtr = InternalStrCpy(strPtr, ", line ");
- strPtr = InternalPrintInt(strPtr, line);
-
- length = strPtr - sExcString - 1;
- if (length > 255)
- length = 255;
- sExcString[0] = length;
-
- if (sCallBackProc != NULL)
- (*sCallBackProc)((char *)&sExcString[1]);
-
- return (StringPtr)&sExcString[0];
- }
-
-
- /*------------------------------------------------------------------
- AssertionFailedExceptionRaised
- ------------------------------------------------------------------*/
-
- StringPtr
- AssertionFailedExceptionRaised( const char * assertionString,
- const char * exceptionString)
- {
- char * strPtr = sExcString + 1;
- unsigned int length;
-
- #if TARGET_API_MAC_CARBON
- strPtr = InternalStrCpy(strPtr, "[");
- strPtr = InternalStrCpy(strPtr, kProgramName);
- strPtr = InternalStrCpy(strPtr, "] ");
- #endif
-
- strPtr = InternalStrCpy(strPtr, "# Assertion \"");
- strPtr = InternalStrCpy(strPtr, assertionString);
- strPtr = InternalStrCpy(strPtr, "\" Failed # Exception \"");
- strPtr = InternalStrCpy(strPtr, exceptionString);
- strPtr = InternalStrCpy(strPtr, "\" Raised");
-
- length = strPtr - sExcString - 1;
- if (length > 255)
- length = 255;
- sExcString[0] = length;
-
- if (sCallBackProc != NULL)
- (*sCallBackProc)((char *)&sExcString[1]);
-
- return (StringPtr)&sExcString[0];
- }
-
-
- /*------------------------------------------------------------------
- AssertionFailedExceptionRaisedFileLine
- ------------------------------------------------------------------*/
-
- StringPtr
- AssertionFailedExceptionRaisedFileLine( const char * assertionString,
- const char * exceptionString,
- const char * fileString,
- unsigned int line)
- {
- char * strPtr = sExcString + 1;
- unsigned int length;
-
- #if TARGET_API_MAC_CARBON
- strPtr = InternalStrCpy(strPtr, "[");
- strPtr = InternalStrCpy(strPtr, kProgramName);
- strPtr = InternalStrCpy(strPtr, "] ");
- #endif
-
- strPtr = InternalStrCpy(strPtr, "# Assertion \"");
- strPtr = InternalStrCpy(strPtr, assertionString);
- strPtr = InternalStrCpy(strPtr, "\" Failed # Exception \"");
- strPtr = InternalStrCpy(strPtr, exceptionString);
- strPtr = InternalStrCpy(strPtr, "\" Raised file ");
- strPtr = InternalStrCpy(strPtr, fileString);
- strPtr = InternalStrCpy(strPtr, ", line ");
- strPtr = InternalPrintInt(strPtr, line);
-
- length = strPtr - sExcString - 1;
- if (length > 255)
- length = 255;
- sExcString[0] = length;
-
- if (sCallBackProc != NULL)
- (*sCallBackProc)((char *)&sExcString[1]);
-
- return (StringPtr)&sExcString[0];
- }
-
-
- /*------------------------------------------------------------------
- PrintDebugString
- ------------------------------------------------------------------*/
-
- StringPtr
- PrintDebugString(const char * debugString)
- {
- char * strPtr = sExcString + 1;
- unsigned int length;
-
- strPtr = InternalStrCpy(strPtr, debugString);
-
- length = strPtr - sExcString - 1;
- if (length > 255)
- length = 255;
- sExcString[0] = length;
-
- if (sCallBackProc != NULL)
- (*sCallBackProc)((char *)&sExcString[1]);
-
- return (StringPtr)&sExcString[0];
- }
-
-
- /*------------------------------------------------------------------
- PrintDebugStringFileLine
- ------------------------------------------------------------------*/
-
- StringPtr
- PrintDebugStringFileLine( const char * debugString,
- const char * fileString,
- unsigned int line)
- {
- char * strPtr = sExcString + 1;
- unsigned int length;
-
- strPtr = InternalStrCpy(strPtr, debugString);
- strPtr = InternalStrCpy(strPtr, "; file ");
- strPtr = InternalStrCpy(strPtr, fileString);
- strPtr = InternalStrCpy(strPtr, ", line ");
- strPtr = InternalPrintInt(strPtr, line);
-
- length = strPtr - sExcString - 1;
- if (length > 255)
- length = 255;
- sExcString[0] = length;
-
- if (sCallBackProc != NULL)
- (*sCallBackProc)((char *)&sExcString[1]);
-
- return (StringPtr)&sExcString[0];
- }
-
-
- /*------------------------------------------------------------------
- InstallExceptionCallBack
-
- This routine allows the client to install a "callback" routine
- for when assertions fire.
- ------------------------------------------------------------------*/
-
- void
- InstallExceptionCallBack(ExceptionCallBackProc inCallBackProc)
- {
- sCallBackProc = inCallBackProc;
- }
-
-
-
- /*==================================================================
- Change History (most recent first):
-
- $Log: MacOS_Exceptions.cpp,v $
- ==================================================================*/
-