1 /* 2 Copyright (c) by Valery Goryachev (Wal) 3 */ 4 5 #define _CRT_SECURE_NO_DEPRECATE 1 6 //#include "stdafx.h" 7 #ifdef MFC_VER 8 #include <afx.h> 9 #include <Afxwin.h> 10 #else 11 #if defined _WIN32 && !defined __GNUC__ 12 #include <new.h> 13 #else 14 #include <new> 15 using namespace std; 16 #endif 17 #endif 18 19 #include "wal.h" 20 #include <stdio.h> 21 #include <string.h> 22 #include <stdarg.h> 23 #include <errno.h> 24 25 26 namespace wal 27 { 28 29 30 #ifdef _WIN32 31 # define vsnprintf _vsnprintf 32 #endif 33 34 #if defined(_WIN32) && !defined( __GNUC__ ) my_new_handler(size_t)35 static int my_new_handler( size_t ) 36 { 37 throw_oom(); 38 return 0; 39 } 40 #else 41 static void my_new_handler( ) 42 { 43 throw_oom(); 44 } 45 #endif 46 47 init_exceptions()48 void init_exceptions() 49 { 50 #if defined _WIN32 && !defined __GNUC__ 51 _set_new_handler( my_new_handler ); 52 #else 53 set_new_handler( my_new_handler ); 54 #endif 55 } 56 57 static coom oom; 58 destroy()59 void cexception::destroy() 60 { 61 if ( this != &oom ) { delete this; } 62 } 63 message()64 const char* cexception::message() 65 { 66 return "exception"; 67 } 68 ~cexception()69 cexception::~cexception() { } 70 message()71 const char* coom::message() 72 { 73 return "out of memory"; 74 } 75 ~coom()76 coom::~coom() {} 77 message()78 const char* cstop_exception::message() { return "stop"; } ~cstop_exception()79 cstop_exception::~cstop_exception() {} 80 cmsg(const char * s)81 cmsg::cmsg( const char* s ) 82 { 83 if ( !s ) { return; } 84 85 _msg.resize( strlen( s ) + 1 ); 86 strcpy( _msg.data(), s ); 87 } 88 message()89 const char* cmsg::message() 90 { 91 return _msg.data() ? _msg.data() : "<?>"; 92 } 93 ~cmsg()94 cmsg::~cmsg() {} 95 throw_oom()96 void throw_oom() 97 { 98 throw ( cexception* )( &oom ); 99 } 100 throw_stop()101 void throw_stop() 102 { 103 throw new cstop_exception(); 104 } 105 106 107 #define MAXMSGSTR 4096 throw_msg(const char * format,...)108 void throw_msg( const char* format, ... ) 109 { 110 char buf[MAXMSGSTR]; 111 va_list ap; 112 va_start( ap, format ); 113 vsnprintf( buf, MAXMSGSTR, format, ap ); 114 va_end( ap ); 115 cmsg* p = new cmsg( buf ); 116 throw p; 117 } 118 csyserr(int _code,const char * msg)119 csyserr::csyserr( int _code, const char* msg ) 120 : cmsg( msg ) 121 { 122 code = _code; 123 } 124 ~csyserr()125 csyserr::~csyserr() {} 126 throw_syserr(int err,const char * format,...)127 void throw_syserr( int err, const char* format, ... ) 128 { 129 char buf[MAXMSGSTR * 2] = ""; 130 131 if ( err == 0 ) { err = get_sys_error(); } 132 133 if ( format != NULL ) 134 { 135 va_list ap; 136 va_start( ap, format ); 137 vsnprintf( buf, MAXMSGSTR, format, ap ); 138 va_end( ap ); 139 }; 140 141 strncat( buf, " (", MAXMSGSTR ); 142 143 strncat( buf, sys_error_utf8( err ).data(), MAXMSGSTR ); 144 145 strncat( buf, ")", MAXMSGSTR ); 146 147 buf[MAXMSGSTR - 1] = 0; 148 149 csyserr* p = new csyserr( err, buf ); 150 151 throw p; 152 } 153 154 155 }; //namespace wal 156