home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / Classes / AppExceptions / AppExceptions.m < prev    next >
Text File  |  1992-07-09  |  2KB  |  81 lines

  1. /* AppExceptions
  2.  *
  3.  * A category of Application that provides methods for mapping
  4.  * ASCII names to integers for use in NeXTSTEP exception handling.
  5.  *
  6.  * Copyright 1991, 1992 Scott Hess.  This source code may be
  7.  * redistributed and modified without restriction.  Well, one
  8.  * restriction - do not claim that you wrote it.
  9.  *
  10.  * Scott Hess
  11.  * 12901 Upton Avenue South, #326
  12.  * Burnsville, MN  55337
  13.  * (612) 895-1208
  14.  * scott@gac.edu
  15.  * shess@ssesco.com
  16.  */
  17. #import "AppExceptions.h"
  18. #import <appkit/errors.h>
  19. #import <objc/HashTable.h>
  20.  
  21. @implementation Application (AppExceptions)
  22. int appExceptionsReference=0;
  23.     /* Hash exception names to numbers. */
  24. static HashTable *exceptionTable=nil;
  25.     /* Base for building exceptions. */
  26. static int exceptionBase=NX_APPBASE;
  27.     /* Reports errors for our exceptions. */
  28. static void errorReporter( NXHandler *h)
  29. {
  30.     const char *name=[NXApp nameForException:h->code];
  31.     if( name) {
  32.     fprintf( stderr, "Uncaught exception %s==%d.\n", name, h->code);
  33.     } else {
  34.     fprintf( stderr, "Unknown exception %d.\n", h->code);
  35.     }
  36. }
  37. -(int)exceptionFor:(const char *)name
  38. {
  39.     if( !name) {
  40.     int exception;
  41.     if( !exceptionTable) {
  42.         exceptionTable=[[HashTable alloc] initKeyDesc:"*" valueDesc:"i" capacity:32];
  43.         NXRegisterErrorReporter( NX_APPBASE, NX_APPBASE+999, errorReporter);
  44.     }
  45.     exception=(int)[exceptionTable valueForKey:name];
  46.     if( !exception) {
  47.         exception=exceptionBase++;
  48.         [exceptionTable insertKey:name value:(void *)exception];
  49.     }
  50.     return exception;
  51.     }
  52.     return -1;
  53. }
  54. -(const char *)nameForException:(int)exception
  55. {
  56.     const char *key;
  57.     int value;
  58.     NXHashState state=[exceptionTable initState];
  59.     while( [exceptionTable nextState:&state key:(const void **)&key
  60.                       value:(void **)&value]) {
  61.     if( exception==value) {
  62.         return key;
  63.     }
  64.     }
  65.     return NULL;
  66. }
  67. - raiseException:(const char *)name with:(const void *)data1 with:(const void *)data2
  68. {
  69.     NX_RAISE( [self exceptionFor:name], data1, data2);
  70. }
  71. - raiseException:(const char *)name with:(const void *)data1
  72. {
  73.     return [self raiseException:name with:data1 with:NULL];
  74. }
  75. - raiseException:(const char *)name
  76. {
  77.     return [self raiseException:name with:NULL with:NULL];
  78. }
  79.  
  80. @end
  81.