1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
//-----------------------------------------------------------------------------
// BSPLIB MODULE: SystemIO.cpp
//
// Copyright (c) 1997-1998 by Markus Hadwiger
// All Rights Reserved.
//-----------------------------------------------------------------------------
// bsplib header files
#include "SystemIO.h"
BSPLIB_NAMESPACE_BEGIN
// define bsplib version string -----------------------------------------------
//
#define BSPLIB_VERSION "1.48"
// set program's name which is an optional part of error messages -------------
//
void SystemIO::SetProgramName( const char *name )
{
program_name = name;
}
// set callback for info message output function ------------------------------
//
void SystemIO::SetInfoMessageCallback( void (*callb)(const char*) )
{
infomessage_callback = callb;
}
// set callback for error message output function -----------------------------
//
void SystemIO::SetErrorMessageCallback( void (*callb)(const char*) )
{
errormessage_callback = callb;
}
// set callback for critical error handler ------------------------------------
//
void SystemIO::SetCriticalErrorCallback( int (*callb)(int) )
{
criticalerror_callback = callb;
}
// display informational message (system dependent code encapsulated here) ----
//
void SystemIO::InfoMessage( const char *message )
{
if ( infomessage_callback != NULL ) {
// call user supplied handler function
infomessage_callback( message );
} else {
#if defined( _WINDOWS ) || defined( WIN32 )
MessageBox( hwnd_main, message, "BspLib Info", MB_OK | MB_ICONINFORMATION );
#else
printf( "%s", message );
fflush( stdout );
#endif
}
}
// display error message (system dependent code encapsulated here) ------------
//
void SystemIO::ErrorMessage( const char *message )
{
if ( errormessage_callback != NULL ) {
// call user supplied handler function
errormessage_callback( message );
} else {
#if defined( _WINDOWS ) || defined( WIN32 )
MessageBox( hwnd_main, message, "BspLib Error", MB_OK | MB_ICONERROR );
#else
fflush( stdout );
fprintf( stderr, "%s: %s\n", program_name, message );
fflush( stderr );
#endif
}
}
// reaction to critical error -------------------------------------------------
//
void SystemIO::HandleCriticalError( int flags )
{
int handled = 0;
if ( criticalerror_callback != NULL ) {
// give user a chance to handle critical error
handled = criticalerror_callback( flags );
}
// if user failed to handle error exit from program
if ( !handled || ( ( flags & CRITERR_ALLOW_RET ) == 0 ) )
exit( EXIT_FAILURE );
}
// open file via FileAccess (resource acquisition is initialization) ----------
//
SystemIO::FileAccess::FileAccess( const char *fname, const char *mode, int flags )
: SystemIO::FilePtr( fname, mode )
{
filename = new char[ strlen( fname ) + 1 ];
strcpy( filename, fname );
status = ( fp != NULL ) ? SYSTEM_IO_OK : FILE_NOT_FOUND;
if ( ( flags & CHECK_ERRORS ) && ( status != SYSTEM_IO_OK ) )
IOError( status );
}
// close FileAccess (resource acquisition is initialization) ------------------
//
SystemIO::FileAccess::~FileAccess()
{
delete filename;
}
// reopen file ----------------------------------------------------------------
//
int SystemIO::FileAccess::ReOpen( const char *fname, const char *mode, int flags )
{
if ( status == FILE_CLOSED ) {
status = ( fopen( fname, mode ) != NULL ) ? SYSTEM_IO_OK : FILE_NOT_FOUND;
if ( ( flags & CHECK_ERRORS ) && ( status != SYSTEM_IO_OK ) )
IOError( status );
return status;
} else {
return FILE_ALREADY_OPEN;
}
}
// close file -----------------------------------------------------------------
//
int SystemIO::FileAccess::Close()
{
status = ( fclose( fp ) == 0 ) ? FILE_CLOSED : SYSTEM_IO_ERROR;
return ( status == FILE_CLOSED );
}
// read from file -------------------------------------------------------------
//
int SystemIO::FileAccess::Read( void *buffer, size_t size, size_t count, int flags )
{
if ( status == SYSTEM_IO_OK ) {
if ( fread( buffer, size, count, fp ) == count )
status = SYSTEM_IO_OK;
else
status = feof( fp ) ? END_OF_FILE : FILE_READ_ERROR;
if ( ( flags & CHECK_ERRORS ) && ( status != SYSTEM_IO_OK ) )
IOError( status );
} else {
if ( flags & CHECK_ERRORS ) IOError( status );
}
return status;
}
// write to file --------------------------------------------------------------
//
int SystemIO::FileAccess::Write( void *buffer, size_t size, size_t count, int flags )
{
if ( status == SYSTEM_IO_OK ) {
status = ( fwrite( buffer, size, count, fp ) == count ) ? SYSTEM_IO_OK : FILE_WRITE_ERROR;
if ( ( flags & CHECK_ERRORS ) && ( status != SYSTEM_IO_OK ) )
IOError( status );
} else {
if ( flags & CHECK_ERRORS ) IOError( status );
}
return status;
}
// read text line from file ---------------------------------------------------
//
char *SystemIO::FileAccess::ReadLine( char *line, int maxlen, int flags )
{
if ( status == SYSTEM_IO_OK ) {
if ( fgets( line, maxlen, fp ) != NULL )
status = SYSTEM_IO_OK;
else
status = feof( fp ) ? END_OF_FILE : FILE_READ_ERROR;
if ( ( flags & CHECK_ERRORS ) && ( status != SYSTEM_IO_OK ) )
IOError( status );
return ( status == SYSTEM_IO_OK ) ? line : NULL;
} else {
if ( flags & CHECK_ERRORS ) IOError( status );
return NULL;
}
}
// write text line to file ----------------------------------------------------
//
const char *SystemIO::FileAccess::WriteLine( const char *line, int flags )
{
if ( status == SYSTEM_IO_OK ) {
status = ( fprintf( fp, "%s", line ) != -1 ) ? SYSTEM_IO_OK : FILE_WRITE_ERROR;
if ( ( flags & CHECK_ERRORS ) && ( status != SYSTEM_IO_OK ) )
IOError( status );
return ( status == SYSTEM_IO_OK ) ? line : NULL;
} else {
if ( flags & CHECK_ERRORS ) IOError( status );
return NULL;
}
}
// I/O error handler ----------------------------------------------------------
//
void SystemIO::FileAccess::IOError( int errorcode )
{
{
StrScratch errtext;
sprintf( errtext, "%s: %s", filename, strerror( errno ) );
ErrorMessage( errtext );
}
HandleCriticalError();
}
// construct String with init string ------------------------------------------
//
String::String( const char *src )
{
if ( src != NULL ) {
data = new char[ strlen( src ) + 1 ];
strcpy( data, src );
} else {
data = NULL;
}
}
// copy constructor for String ------------------------------------------------
//
String::String( const String& copyobj )
{
if ( copyobj.data != NULL ) {
data = new char[ strlen( copyobj.data ) + 1 ];
strcpy( data, copyobj.data );
} else {
data = NULL;
}
}
// assignment operator for String ---------------------------------------------
//
String& String::operator =( const String& copyobj )
{
if ( ©obj != this ) {
delete data;
if ( copyobj.data != NULL ) {
data = new char[ strlen( copyobj.data ) + 1 ];
strcpy( data, copyobj.data );
} else {
data = NULL;
}
}
return *this;
}
// pointer to global string containing the program's name ---------------------
//
const char *SystemIO::program_name = NULL;
// pointers to user supplied callback functions -------------------------------
//
void (*SystemIO::infomessage_callback)(const char*) = NULL;
void (*SystemIO::errormessage_callback)(const char*) = NULL;
int (*SystemIO::criticalerror_callback)(int) = NULL;
// handle of main window (used only when system is Win32) ---------------------
//
#if defined( _WINDOWS ) || defined( WIN32 )
HWND SystemIO::hwnd_main = NULL;
#endif
// bsplib version string ------------------------------------------------------
//
const char SystemIO::version_str[] = BSPLIB_VERSION;
BSPLIB_NAMESPACE_END
//-----------------------------------------------------------------------------
|