]> icculus.org git repositories - btb/d2x.git/blob - unused/bios/error.c
move PhysicsFS initialisation, search path setup and argument reading to physfsx.h
[btb/d2x.git] / unused / bios / 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-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #pragma off (unreferenced)
15 static char rcsid[] = "$Id: error.c,v 1.1.1.1 2001-01-19 03:30:14 bradleyb Exp $";
16 #pragma on (unreferenced)
17
18 #if defined(__NT__)
19 #define WIN32_LEAN_AND_MEAN
20 #include <windows.h>
21 #endif
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27
28 #include "mono.h"
29 #include "error.h"
30 #include "args.h"
31
32 #ifdef MACINTOSH
33         #include <Types.h>
34         #include <Strings.h>
35         #include <LowMem.h>
36         
37         #include "resource.h"
38 #endif
39
40 #define MAX_MSG_LEN 256
41
42 int initialized=0;
43
44 static void (*ErrorPrintFunc)(char *);
45
46 char exit_message[MAX_MSG_LEN]="";
47 char warn_message[MAX_MSG_LEN];
48
49 //takes string in register, calls printf with string on stack
50 void warn_printf(char *s)
51 {
52         printf("%s\n",s);
53 }
54
55 void (*warn_func)(char *s)=warn_printf;
56
57 //provides a function to call with warning messages
58 void set_warn_func(void (*f)(char *s))
59 {
60         warn_func = f;
61 }
62
63 //uninstall warning function - install default printf
64 void clear_warn_func(void (*f)(char *s))
65 {
66         warn_func = warn_printf;
67 }
68
69 void set_exit_message(char *fmt,...)
70 {
71         va_list arglist;
72         int len;
73
74         va_start(arglist,fmt);
75         len = vsprintf(exit_message,fmt,arglist);
76         va_end(arglist);
77
78         if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in set_exit_message (len=%d, max=%d)",len,MAX_MSG_LEN);
79
80 }
81
82 #ifdef __NT__
83 void _Assert(int expr, char *expr_text, char *filename, int linenum)
84 {
85         if (!(expr)) {
86         #ifndef NDEBUG
87                 if (FindArg("-debugmode")) DebugBreak();
88         #endif
89                 Error("Assertion failed: %s, file %s, line %d", expr_text, filename, linenum);
90         }
91 }
92 #else
93 void _Assert(int expr,char *expr_text,char *filename,int linenum)
94 {
95         if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
96
97 }
98 #endif
99
100 void print_exit_message()
101 {
102         if (*exit_message)
103         {
104                 if (ErrorPrintFunc)
105                 {
106                         (*ErrorPrintFunc)(exit_message);
107                 }
108                 else
109                 {
110                         #if (defined(MACINTOSH) && defined(NDEBUG) && defined(RELEASE))
111                                 
112                                 c2pstr(exit_message);
113                                 ShowCursor();
114                                 ParamText(exit_message, "\p", "\p", "\p");
115                                 StopAlert(ERROR_ALERT, nil);
116                                 
117                         #else
118                                 
119                                 printf("%s\n",exit_message);
120                         
121                         #endif
122                 }
123         }
124 }
125
126 //terminates with error code 1, printing message
127 void Error(char *fmt,...)
128 {
129         va_list arglist;
130
131         #if (defined(MACINTOSH) && defined(NDEBUG) && defined(RELEASE))
132                 strcpy(exit_message,"Error: ");         // don't put the new line in for dialog output
133         #else
134                 strcpy(exit_message,"\nError: ");
135         #endif
136         va_start(arglist,fmt);
137         vsprintf(exit_message+strlen(exit_message),fmt,arglist);
138         va_end(arglist);
139
140         Int3();
141
142         if (!initialized) print_exit_message();
143
144         exit(1);
145 }
146
147 //print out warning message to user
148 void Warning(char *fmt,...)
149 {
150         va_list arglist;
151
152         if (warn_func == NULL)
153                 return;
154
155         strcpy(warn_message,"Warning: ");
156
157         va_start(arglist,fmt);
158         vsprintf(warn_message+strlen(warn_message),fmt,arglist);
159         va_end(arglist);
160
161         mprintf((0, "%s\n", warn_message));
162         (*warn_func)(warn_message);
163
164 }
165
166 //initialize error handling system, and set default message. returns 0=ok
167 int error_init(void (*func)(char *), char *fmt,...)
168 {
169         va_list arglist;
170         int len;
171
172         atexit(print_exit_message);             //last thing at exit is print message
173
174         ErrorPrintFunc = func;                          // Set Error Print Functions
175
176         if (fmt != NULL) {
177                 va_start(arglist,fmt);
178                 len = vsprintf(exit_message,fmt,arglist);
179                 va_end(arglist);
180                 if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in error_init (len=%d, max=%d)",len,MAX_MSG_LEN);
181         }
182
183         initialized=1;
184
185         return 0;
186 }
187
188 #ifdef MACINTOSH
189
190 int MacEnableInt3 = 1;
191
192 void MacAssert(int expr, char *expr_text, char *filename, int linenum)
193 {
194         if (!(expr)) {
195                 Int3();
196 //              Error("Assertion failed: %s, file %s, line %d", expr_text, filename, linenum);
197         }
198 }
199
200 #endif
201
202 #if defined(__NT__)
203
204 int WinEnableInt3 = 1;
205
206 void WinInt3()
207 {
208         if (WinEnableInt3) 
209                 DebugBreak();
210 }
211 #endif