home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / ehandler.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-30  |  8.8 KB  |  269 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: ehandler.cpp 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer  
  8. // File Creation Date: 02/14/1996  
  9. // Date Last Modified: 03/30/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The Ehandler class is used to catch program exceptions that
  32. occur at run-time. This implementation can be used with or
  33. without C++ built-in exception handling. If C++ exception
  34. handling is not enabled with the CPP_EXCEPTIONS macro, then
  35. the EHandler class can be used to display program errors,
  36. exit the program, or trap a program error with a user defined
  37. Action Function.
  38. */
  39. // ----------------------------------------------------------- // 
  40. #include <string.h>
  41. #include "ehandler.h"
  42.  
  43. #ifdef __wxWIN168B__
  44. #include "wxincs.h"  // Include files for wxWindows version 1.68b
  45. #endif
  46.  
  47. #ifdef __wxWIN201__
  48. #include "wx2incs.h"  // Include files for wxWindows version 2.0.1
  49. #endif
  50.  
  51. #ifdef __CONSOLE__
  52. #include <iostream.h>
  53. #include <stdlib.h>
  54. #endif
  55.  
  56. #ifdef __CURSES__
  57. #include <stdlib.h>
  58. #include "terminal.h"
  59. #endif
  60.  
  61. const char *MessageBoxString = "Caught Exception: ";
  62. const char *MessageBoxCaption = "Program Error";
  63.  
  64. EHandler ExceptionHandler;
  65. EHandler *Error = &ExceptionHandler; // Global exception handler pointer
  66.  
  67. const char *ExceptionMessage[MessageCount] = {
  68.     "No exception reported",                     // None
  69.     "File not ready (failed or closed file)",    // FileNotReady
  70.     "Could not write to file",                   // FileNotWriteable
  71.     "Trying to write to a read-only file",       // ReadOnlyFile
  72.     "Trying to use a closed file",               // FileNotOpenError
  73.     "Error creating file",                       // FileCreationError
  74.     "Error opening file",                        // FileOpenError
  75.     "Error closing file",                        // FileCloseError
  76.     "Error seeking in file",                     // FileSeekError
  77.     "Error reading from file",                   // FileReadError
  78.     "Error writing to file",                     // FileWriteError
  79.     "Unexpected end of file",                    // EOFError
  80.     "Wrong file type",                           // WrongFileType
  81.     "File corrupted",                            // FileCorrupt
  82.     "File already exists",                       // FileExists
  83.     "No such file exists",                       // NoFileExists
  84.     "Invalid path",                              // PathError
  85.     "Dangling 'reference counted pointer'",      // DanglingPtr
  86.     "Accessing a null pointer",                  // NullPtr
  87.     "Cache full",                                // CacheFull
  88.     "Stack full",                                // StkFull
  89.     "Stack empty",                               // StkEmpty
  90.     "Assertion failed",                          // AssertError
  91.     "No database open",                          // NoDatabaseOpen
  92.     "Object already exists",                     // ObjectExists
  93.     "Bad Object Address",                        // BadObjectAddress
  94.     "Bad Reference",                             // BadReference
  95.     "Divide By Zero Error",                      // DivideByZero
  96.     "Math overflow error",                       // OverFlow
  97.     "Math under-flow error",                     // UnderFlow
  98.     "Parse error",                               // ParseError         
  99.     "No objects exist",                          // NoObjectsExist
  100.     "Wrong object type",                         // BadClassID
  101.     "Synchronization Error",                     // SyncError
  102.     "Access Violation",                          // AccessViolation
  103.     "Checksum Error"                             // ChecksumError
  104. };
  105.  
  106. EHandler::~EHandler()
  107. {
  108.   // Destructor provided for virtuality
  109.  
  110. void EHandler::DisplayException()
  111. {
  112. #ifdef __wxWIN168B__
  113.   int len = strlen(MessageBoxString) + strlen(ExceptionMessage[ExceptionCode]);
  114.   char *comp = new char[len+1];
  115.   comp[len+1] = '\0';
  116.   strcpy(comp, MessageBoxString);
  117.   strcat(comp, ExceptionMessage[ExceptionCode]);
  118.   wxMessageBox(comp, (char *)MessageBoxCaption, wxOK|wxCENTRE);
  119.   delete comp;
  120. #endif
  121.  
  122. #ifdef __wxWIN201__
  123.   wxString message(MessageBoxString);
  124.   wxString caption(MessageBoxCaption);
  125.   message += ExceptionMessage[ExceptionCode];
  126.   wxMessageBox(message, caption, wxOK|wxCENTRE);
  127. #endif
  128.  
  129. #ifdef __CURSES__
  130.   terminal->ClearScreen();
  131.   terminal->Write("Caught Exception: ", 0, 0);
  132.   terminal->Write(ExceptionMessage[ExceptionCode]);
  133.   terminal->AnyKey(0, 2);
  134. #endif
  135.  
  136. #ifdef __CONSOLE__
  137.     cout << endl << "Caught Exception: "
  138.        << ExceptionMessage[ExceptionCode]
  139.        << endl;
  140. #endif
  141. }
  142.  
  143. void EHandler::DisplayException(int ECode)
  144. {
  145. #ifdef __wxWIN168B__
  146.   int len = strlen(MessageBoxString) + strlen(ExceptionMessage[ExceptionCode]);
  147.   char *comp = new char[len+1];
  148.   comp[len+1] = '\0';
  149.   strcpy(comp, MessageBoxString);
  150.   strcat(comp, ExceptionMessage[ECode]);
  151.   wxMessageBox(comp, (char *)MessageBoxCaption, wxOK|wxCENTRE);
  152.   delete comp;
  153. #endif
  154.  
  155. #ifdef __wxWIN201__
  156.   wxString message(MessageBoxString);
  157.   wxString caption(MessageBoxCaption);
  158.   message += ExceptionMessage[ECode];
  159.   wxMessageBox(message, caption, wxOK|wxCENTRE);
  160. #endif
  161.  
  162. #ifdef __CURESES__
  163.   terminal->ClearScreen();
  164.   terminal->Write("Caught Exception: ");
  165.   terminal->Write(ExceptionMessage[ECode]);
  166.   terminal->AnyKey(0, 2);
  167. #endif
  168.  
  169. #ifdef __CONSOLE__
  170.   cout << endl << "Caught Exception: "
  171.        << ExceptionMessage[ECode]
  172.        << endl;
  173. #endif
  174. }
  175.  
  176. void EHandler::SignalException(int ECode, int Level, int DisplayError)
  177. {
  178.   SetException(ECode); // Set the exception code
  179.  
  180.   // By default a message will be displayed
  181.   if(DisplayError) DisplayException(); 
  182.  
  183.   switch(Level)
  184.     {
  185.       case FATAL:    // Fatal error, terminate program immediately
  186.     Terminate();
  187.  
  188.       case DISPLAY: // Dummy handler that does nothing
  189.     return;
  190.  
  191.       default:
  192.     Terminate();
  193.     }
  194. }
  195.  
  196. void EHandler::Message
  197. (const char *mesg1, const char *mesg2, const char *mesg3)
  198. {
  199. #ifdef __wxWIN168B__
  200.   int len = strlen(mesg1) + strlen(mesg2) + strlen(mesg3);
  201.   char *comp = new char[len+1];
  202.   comp[len+1] = '\0';
  203.   strcpy(comp, mesg1);
  204.   strcat(comp, mesg2);
  205.   strcat(comp, mesg3);
  206.   wxMessageBox(comp, "Program Message", wxOK|wxCENTRE);
  207.   delete comp;
  208. #endif
  209.  
  210. #ifdef __wxWIN201__
  211.   wxString message(mesg1);
  212.   wxString caption("Program Message");
  213.   message += mesg2;
  214.   message += mesg3;
  215.   wxMessageBox(message, caption, wxOK|wxCENTRE);
  216. #endif
  217.  
  218.   // #ifdef __CURSES__
  219.   // Function not used in curses port
  220.   // #endif
  221.  
  222. #ifdef __CONSOLE__
  223.   cout << endl;
  224.   if(mesg1) cout << mesg1;
  225.   if(mesg2) cout << mesg2;
  226.   if(mesg3) cout << mesg3;
  227.   cout << endl;
  228. #endif
  229. }
  230.  
  231. void EHandler::Terminate()
  232. {
  233. #ifdef __wxWIN168B__ 
  234.   // Terminate the program using the wxExit() function.
  235.   wxExit();
  236.   // Exits application after calling wxApp::OnExit. Should only
  237.   // be used in an emergency: normally the top-level frame should
  238.   // be deleted (after deleting all other frames) to terminate the
  239.   // application. 
  240. #endif
  241.  
  242. #ifdef __wxWIN201__
  243.   // Terminate the program using the wxExit() function.
  244.   wxExit();
  245. #endif
  246.  
  247. #ifdef __CURSES__
  248.   // Terminate the program abnormally
  249.   terminal->finish();
  250.   exit(FatalErrorLevel);
  251. #endif
  252.  
  253. #ifdef __CONSOLE__
  254.   // Terminate the program abnormally
  255.   exit(FatalErrorLevel);
  256. #endif
  257. }
  258.  
  259. void EHandler::TrapException(AF ActionFunction)
  260. {
  261.   // Call the user defined routine
  262.   (*ActionFunction)();
  263. }
  264. // ----------------------------------------------------------- //
  265. // ------------------------------- //
  266. // --------- End of File --------- //
  267. // ------------------------------- //
  268.