-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patherror_handlers.c
140 lines (117 loc) · 3.33 KB
/
error_handlers.c
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
/* Author: Ram Samudrala ([email protected])
* March 1, 1996.
* modified Hong Hung Sept 2006 2012
*/
#include "lite.h"
/******************************************************************/
int check_atomp(atom *atomp, const char routine_name[])
{
if (atomp == ((atom *) NULL))
{
fprintf(stderr, "%s(): atomp is NULL when it shouldn't be!\n", routine_name);
exit(FALSE);
}
return TRUE;
}
/******************************************************************/
int check_eof(int status, const char routine_name[])
{
if (status == EOF)
fprintf(stderr, "check_eof(): EOF returned in %s()!\n", routine_name);
return TRUE;
}
/******************************************************************/
int check_malloc(void *pointer, const char pointer_string[], const char routine_name[])
{
if (pointer == ((void *) NULL))
{
fprintf(stderr, "%s(): unable to allocate memory for %s!\n", routine_name, pointer_string);
exit(FALSE);
}
return TRUE;
}
/******************************************************************/
int check_maximum_value(int value, int maximum_value, const char routine_name[])
{
if (value >= maximum_value)
{
fprintf(stderr, "%s(): value, %d, exceeded maximum_value (%d)!\n",
routine_name, value, maximum_value);
exit(FALSE);
}
return TRUE;
}
/******************************************************************/
int check_null(void *pointer, const char routine_name[])
{
if (pointer == ((void *) NULL))
{
fprintf(stderr, "%s(): NULL encountered unexpectedly!\n", routine_name);
exit(FALSE);
}
return TRUE;
}
/******************************************************************/
int open_file(FILE **fp, const char filename[], const char status[], const char routine_name[])
{
char buf[20];
if (strcmp(filename, STDIN_FILENAME) == 0)
{
*fp = stdin;
return TRUE;
}
if (strcmp(filename, STDOUT_FILENAME) == 0)
{
*fp = stdout;
return TRUE;
}
if (strcmp(filename, STDERR_FILENAME) == 0)
{
*fp = stderr;
return TRUE;
}
switch (status[0])
{
case 'r':
strcpy(buf, "reading");
break;
case 'a':
strcpy(buf, "appending");
break;
case 'w':
strcpy(buf, "writing");
break;
default:
fprintf(stderr, "open_file(): unknown status (%s) encountered!\n", status);
break;
}
if ((*fp = fopen(filename, status)) == NULL)
{
fprintf(stderr, "%s(): couldn't open %s for %s!\n", routine_name, filename, buf);
exit(FALSE);
}
else if(routine_name)
fprintf(stderr, "%s(): opening %s for %s...\n", routine_name, filename, buf);
return TRUE;
}
/******************************************************************/
int close_file(FILE **fp, const char filename[], const char routine_name[])
{
if ((strcmp(filename, STDOUT_FILENAME) != 0) && (strcmp(filename, STDIN_FILENAME) != 0) &&
(strcmp(filename, STDERR_FILENAME) != 0))
{
if(routine_name) fprintf(stderr, "%s(): closing %s.\n", routine_name, filename);
fclose(*fp);
}
return TRUE;
}
/******************************************************************/
int myisnan_(int *nanflag, double *value)
{
if (isnan(*value) == 0)
*nanflag = 0;
else
*nanflag = 1;
return TRUE;
}
/******************************************************************/