]> icculus.org git repositories - btb/d2x.git/blob - misc/error.c
use the orientation parameter of g3_draw_bitmap
[btb/d2x.git] / misc / error.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13 /*
14  *
15  * Error handling/printing/exiting code
16  *
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <conf.h>
21 #endif
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27
28 #include "pstypes.h"
29 #include "console.h"
30 #include "mono.h"
31 #include "dxxerror.h"
32
33 #define MAX_MSG_LEN 256
34
35 //edited 05/17/99 Matt Mueller added err_ prefix to prevent conflicts with statically linking SDL
36 int err_initialized=0;
37 //end edit -MM
38
39 static void (*ErrorPrintFunc)(char *);
40
41 char exit_message[MAX_MSG_LEN]="";
42 char warn_message[MAX_MSG_LEN];
43
44 //takes string in register, calls printf with string on stack
45 void warn_printf(char *s)
46 {
47         con_printf(CON_URGENT, "%s\n",s);
48 }
49
50 void (*warn_func)(char *s)=warn_printf;
51
52 //provides a function to call with warning messages
53 void set_warn_func(void (*f)(char *s))
54 {
55         warn_func = f;
56 }
57
58 //uninstall warning function - install default printf
59 void clear_warn_func(void (*f)(char *s))
60 {
61         warn_func = warn_printf;
62 }
63
64 void set_exit_message(char *fmt,...)
65 {
66         va_list arglist;
67         int len;
68
69         va_start(arglist,fmt);
70         len = vsprintf(exit_message,fmt,arglist);
71         va_end(arglist);
72
73         if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in set_exit_message (len=%d, max=%d)",len,MAX_MSG_LEN);
74
75 }
76
77 void _Assert(int expr,char *expr_text,char *filename,int linenum)
78 {
79         Int3();
80         if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
81 }
82
83 void print_exit_message(void)
84 {
85         if (*exit_message)
86         {
87                 if (ErrorPrintFunc)
88                 {
89                         (*ErrorPrintFunc)(exit_message);
90                 }
91                 else
92                 {
93 #if (defined(MACINTOSH) && defined(NDEBUG) && defined(RELEASE))
94                         c2pstr(exit_message);
95                         ShowCursor();
96                         ParamText(exit_message, "\p", "\p", "\p");
97                         StopAlert(ERROR_ALERT, nil);
98 #else
99                         con_printf(CON_CRITICAL, "%s\n",exit_message);
100 #endif
101                 }
102         }
103 }
104
105 //terminates with error code 1, printing message
106 void Error(const char *fmt,...)
107 {
108         va_list arglist;
109
110 #if (defined(MACINTOSH) && defined(NDEBUG) && defined(RELEASE))
111         strcpy(exit_message,"Error: "); // don't put the new line in for dialog output
112 #else
113         strcpy(exit_message,"\nError: ");
114 #endif
115         va_start(arglist,fmt);
116         vsprintf(exit_message+strlen(exit_message),fmt,arglist);
117         va_end(arglist);
118
119         if (!err_initialized) print_exit_message();
120         Int3();
121         exit(1);
122 }
123
124 //print out warning message to user
125 void Warning(char *fmt,...)
126 {
127         va_list arglist;
128
129         if (warn_func == NULL)
130                 return;
131
132         strcpy(warn_message,"Warning: ");
133
134         va_start(arglist,fmt);
135         vsprintf(warn_message+strlen(warn_message),fmt,arglist);
136         va_end(arglist);
137
138         mprintf((0, "%s\n", warn_message));
139         (*warn_func)(warn_message);
140
141 }
142
143 //initialize error handling system, and set default message. returns 0=ok
144 int error_init(void (*func)(char *), char *fmt, ...)
145 {
146         va_list arglist;
147         int len;
148
149         atexit(print_exit_message);             //last thing at exit is print message
150
151         ErrorPrintFunc = func;          // Set Error Print Functions
152
153         if (fmt != NULL) {
154                 va_start(arglist,fmt);
155                 len = vsprintf(exit_message,fmt,arglist);
156                 va_end(arglist);
157                 if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in error_init (len=%d, max=%d)",len,MAX_MSG_LEN);
158         }
159
160         err_initialized=1;
161
162         return 0;
163 }