OERROR.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OERR.CPP
  21. //Description : Object Error Handling
  22. #include <new>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <windows.h>
  27. #include <OSYS.h>
  28. #include <OBOX.h>
  29. #include <OVGA.h>
  30. #include <ALL.h>
  31. //------------------------------------------------//
  32. //
  33. // There are several types of errors :
  34. //
  35. // 1. Internal Errors caused by bugs of the program
  36. // 2. Runtime Errors caused by using up of resources (memory) or
  37. // unexpected environment errors. (disk error)
  38. //
  39. //------------------------------------------------//
  40. static void new_func_handler();
  41. //---------- define static variable ----------//
  42. static char error_flag=0; // prevent error message dead loop
  43. //------- Begin of function Error::Error ------------//
  44. //
  45. // Set new() operator error handler, new_handler() is called when
  46. // new cannot allocate sufficient memory required.
  47. //
  48. Error::Error()
  49. {
  50. std::set_new_handler(new_func_handler); // set_new_handler() is a C++ function
  51. extra_handler = NULL;
  52. }
  53. //-------- End of function Error::Error --------------//
  54. //------- Begin of function new_func_handler ------------//
  55. //
  56. static void new_func_handler()
  57. {
  58. err.mem();
  59. }
  60. //-------- End of function new_func_handler --------------//
  61. //------- BEGIN OF FUNCTION Error::internal -----------//
  62. //
  63. // sample error message :
  64. //
  65. // Exit : Insufficient Memory
  66. // File : ODYNARR.CPP
  67. // Line : 453
  68. //
  69. // Continue ?
  70. //
  71. // <char*> errMsg - the error message
  72. // <char*> fileName - the file name of the CPP function cause error
  73. // usually is __FILE__
  74. // <int> lineNum - the line number of program cause error
  75. // usually is __LINE__
  76. //
  77. void Error::internal(char* errMsg,char* fileName,int lineNum)
  78. {
  79. if( error_flag ) // prevent error message dead loop
  80. return;
  81. error_flag=1;
  82. //-------------------------------------------------//
  83. char strBuf[100];
  84. if( extra_handler ) // all the extra error handler first
  85. (*extra_handler)();
  86. if( errMsg )
  87. sprintf(strBuf, "Error : %s\nFile : %s\nLine : %d\n", errMsg,fileName,lineNum );
  88. else
  89. sprintf(strBuf, "Error on File : %s\nLine : %d\n",fileName,lineNum );
  90. //-------- display error message -------//
  91. OutputDebugString( strBuf );
  92. if( vga.is_inited() )
  93. box.msg( strBuf, 0 );
  94. sys.deinit_directx();
  95. exit( -2 );
  96. }
  97. //--------- END OF FUNCTION Error::internal ----------//
  98. //------- BEGIN OF FUNCTION Error::mem -----------//
  99. //
  100. // There is no memory left to save the screen, so don't use v_pop.ask(),
  101. // direct output to screen.
  102. //
  103. void Error::mem()
  104. {
  105. if( error_flag ) // prevent error message dead loop
  106. return;
  107. error_flag=1;
  108. //-------------------------------------------------//
  109. if( extra_handler )
  110. (*extra_handler)();
  111. char* strBuf = "Insufficient Memory, execution interrupt.";
  112. //-------- display error message -------//
  113. OutputDebugString( strBuf );
  114. if( vga.is_inited() )
  115. box.msg( strBuf, 0 );
  116. sys.deinit_directx();
  117. exit( -2 );
  118. }
  119. //--------- END OF FUNCTION Error::mem ----------//
  120. //------- BEGIN OF FUNCTION Error::msg -----------//
  121. //
  122. // <char*> formated erorr message with % argument
  123. // <....> the argument list
  124. //
  125. void Error::msg( char *format, ... )
  126. {
  127. if( error_flag ) // prevent error message dead loop
  128. return;
  129. error_flag=1;
  130. //-------------------------------------------------//
  131. //---- translate the message and the arguments into one message ----//
  132. char strBuf[100];
  133. va_list argPtr; // the argument list structure
  134. va_start( argPtr, format );
  135. vsprintf( strBuf, format, argPtr );
  136. va_end( argPtr );
  137. //-------- display error message -------//
  138. OutputDebugString( strBuf );
  139. if( vga.is_inited() )
  140. box.msg( strBuf, 0 );
  141. sys.deinit_directx();
  142. error_flag = 0; // this error does not exit program
  143. }
  144. //--------- END OF FUNCTION Error::msg ----------//
  145. //------- BEGIN OF FUNCTION Error::run --------//
  146. //
  147. // <char*> formated erorr message with % argument
  148. // <....> the argument list
  149. //
  150. void Error::run( char *format, ... )
  151. {
  152. if( error_flag ) // prevent error message dead loop
  153. return;
  154. error_flag=1;
  155. //-------------------------------------------------//
  156. if( extra_handler )
  157. (*extra_handler)();
  158. //---- translate the message and the arguments into one message ----//
  159. char strBuf[100];
  160. va_list argPtr; // the argument list structure
  161. va_start( argPtr, format );
  162. vsprintf( strBuf, format, argPtr );
  163. va_end( argPtr );
  164. //-------- display error message -------//
  165. OutputDebugString( strBuf );
  166. if( vga.is_inited() )
  167. box.msg( strBuf, 0 );
  168. sys.deinit_directx();
  169. exit( -2 );
  170. }
  171. //---------- END OF FUNCTION Error::run -----------//