home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iv26_w_3.zip / EXAMPLES / IDRAW / ERRHANDL.C < prev    next >
C/C++ Source or Header  |  1992-01-17  |  3KB  |  107 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. // $Header: errhandler.c,v 1.10 89/10/09 14:47:59 linton Exp $
  24. // implements class ErrHandler.
  25.  
  26. #include "editor.h"
  27. #include "errhandler.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30.  
  31. // Declare imported functions.
  32.  
  33. extern NewHandler* set_new_handler(NewHandler*);
  34.  
  35. ErrHandler* ErrHandler::_err_handler;
  36.  
  37. // out_of_memory relays the out of memory exception to the current
  38. // instance of ErrHandler.
  39.  
  40. void out_of_memory () {
  41.     ErrHandler::_err_handler->OutOfMemory();
  42. }
  43.  
  44. // ErrHandler doesn't have an Editor yet.
  45.  
  46. ErrHandler::ErrHandler () {
  47.     editor = nil;
  48.     olderr = nil;
  49.     oldnew = nil;
  50. }
  51.  
  52. // ~ErrHandler restores the previous handlers for out of memory exceptions.
  53.  
  54. ErrHandler::~ErrHandler () {
  55.     SetErrHandler(olderr);
  56.     set_new_handler(oldnew);
  57. }
  58.  
  59. // SetEditor sets the Editor which ErrHandler calls upon.
  60.  
  61. void ErrHandler::SetEditor (Editor* e) {
  62.     editor = e;
  63. }
  64.  
  65. // Install installs this instance of ErrHandler as the handler for
  66. // both X protocol request errors and out of memory exceptions.
  67.  
  68. ReqErr* ErrHandler::Install () {
  69.     olderr = SetErrHandler(this);
  70.     oldnew = set_new_handler(&out_of_memory);
  71.  
  72.     return ReqErr::Install();
  73. }
  74.  
  75. // Error prints the X error, checkpoints the current drawing, and
  76. // terminates the program's execution.
  77.  
  78. void ErrHandler::Error () {
  79.     fprintf(stderr, "X Error: %s\n", message);
  80.     fprintf(stderr, "         Request code: %d\n", request);
  81.     fprintf(stderr, "         Request function: %d\n", detail);
  82.     fprintf(stderr, "         Request window 0x%x\n", id);
  83.     fprintf(stderr, "         Error Serial #%d\n", msgid);
  84.     if (editor != nil) {
  85. //    editor->Checkpoint();
  86.     }
  87.     const int ERROR = 1;
  88.     exit(ERROR);
  89. }
  90.  
  91. // SetErrHandler updates the static variable read by out_of_memory
  92.  
  93. ErrHandler* ErrHandler::SetErrHandler (ErrHandler* n) {
  94.     ErrHandler* o = _err_handler;
  95.     _err_handler = n;
  96.     return o;
  97. }
  98.  
  99. // OutOfMemory aborts the program.  I wish it could checkpoint the
  100. // drawing, but writing the drawing consumes a significant amount of
  101. // memory.
  102.  
  103. void ErrHandler::OutOfMemory () {
  104.     fprintf(stderr, "operator new failed: out of memory\n");
  105.     abort();
  106. }
  107.