-
Notifications
You must be signed in to change notification settings - Fork 0
/
gregg_shared.c
215 lines (202 loc) · 6.81 KB
/
gregg_shared.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
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
/*============================[[ beg-code ]]============================*/
#include "gregg.h"
/*====================------------------------------------====================*/
/*===---- memory allccation ----===*/
/*====================------------------------------------====================*/
static void o___MEMORY__________o () { return; }
char
SHARED_new (char a_abbr, char a_force, short a_size, char *a_wiper (void *), void **r_new)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
void *x_new = NULL;
int x_tries = 0;
/*---(header)-------------------------*/
DEBUG_DATA yLOG_senter (__FUNCTION__);
DEBUG_DATA yLOG_schar (a_abbr);
/*---(check return)-------------------*/
DEBUG_DATA yLOG_spoint (r_new);
--rce; if (r_new == NULL) {
DEBUG_DATA yLOG_sexitr (__FUNCTION__, rce);
return rce;
}
DEBUG_DATA yLOG_spoint (*r_new);
--rce; if (a_force != 'y' && *r_new != NULL) {
DEBUG_DATA yLOG_snote ("already set");
DEBUG_DATA yLOG_sexitr (__FUNCTION__, rce);
return rce;
}
/*---(default)------------------------*/
*r_new = NULL;
/*---(check type)---------------------*/
--rce; switch (a_abbr) {
case B_BASE : break;
case B_DICT : break;
default :
DEBUG_DATA yLOG_snote ("unknown btree abbr");
DEBUG_DATA yLOG_sexitr (__FUNCTION__, rce);
return rce;
break;
}
/*---(check size)---------------------*/
DEBUG_DATA yLOG_sint (a_size);
--rce; if (a_size <= 0) {
DEBUG_DATA yLOG_sexitr (__FUNCTION__, rce);
return rce;
}
/*---(allocate)-----------------------*/
while (x_new == NULL) {
++x_tries;
x_new = malloc (a_size);
if (x_tries > 3) break;
}
DEBUG_DATA yLOG_sint (x_tries);
DEBUG_DATA yLOG_spoint (x_new);
--rce; if (x_new == NULL) {
DEBUG_DATA yLOG_sexitr (__FUNCTION__, rce);
return rce;
}
/*---(wipe)---------------------------*/
rc = 0;
if (a_wiper != NULL) rc = a_wiper (x_new);
/*---(save return)--------------------*/
*r_new = x_new;
/*---(complete)-----------------------*/
DEBUG_DATA yLOG_sexit (__FUNCTION__);
return rc;
}
char
SHARED_free (char a_abbr, void **r_old)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
/*---(header)-------------------------*/
DEBUG_DATA yLOG_senter (__FUNCTION__);
DEBUG_DATA yLOG_sint (a_abbr);
/*---(check return)-------------------*/
DEBUG_DATA yLOG_spoint (r_old);
--rce; if (r_old == NULL) {
DEBUG_DATA yLOG_sexitr (__FUNCTION__, rce);
return rce;
}
DEBUG_DATA yLOG_spoint (*r_old);
--rce; if (*r_old == NULL) {
DEBUG_DATA yLOG_snote ("never set");
DEBUG_DATA yLOG_sexitr (__FUNCTION__, rce);
return rce;
}
/*---(clear and return)---------------*/
free (*r_old);
/*---(save return)--------------------*/
*r_old = NULL;
/*---(complete)-----------------------*/
DEBUG_DATA yLOG_sexit (__FUNCTION__);
return 0;
}
/*====================------------------------------------====================*/
/*===---- file control ----===*/
/*====================------------------------------------====================*/
static void o___FILE____________o () { return; }
char
SHARED_open (char a_name [LEN_PATH], char a_mode, FILE **r_file)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char x_mode [LEN_TERSE] = "";
FILE *f = NULL;
/*---(header)-------------------------*/
DEBUG_FILE yLOG_enter (__FUNCTION__);
/*---(defense)------------------------*/
DEBUG_FILE yLOG_point ("a_name" , a_name);
--rce; if (a_name == NULL) {
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
DEBUG_FILE yLOG_info ("a_name" , a_name);
DEBUG_FILE yLOG_char ("a_mode" , a_mode);
--rce; if (a_mode == 0 || strchr ("rRwW", a_mode) == NULL) {
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
DEBUG_FILE yLOG_point ("r_file" , r_file);
--rce; if (r_file == NULL) {
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
DEBUG_INPT yLOG_point ("*r_file" , *r_file);
--rce; if (*r_file != NULL) {
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(set mode)-----------------------*/
DEBUG_FILE yLOG_char ("a_mode" , a_mode);
--rce; switch (a_mode) {
case 'r' :
strlcpy (x_mode, "rt", LEN_TERSE);
yURG_msg ('-', "reading text file å%sæ", a_name);
break;
case 'R' :
strlcpy (x_mode, "rb", LEN_TERSE);
yURG_msg ('-', "reading database å%sæ", a_name);
break;
case 'w' :
strlcpy (x_mode, "wt", LEN_TERSE);
yURG_msg ('-', "writing text file å%sæ", a_name);
break;
case 'W' :
strlcpy (x_mode, "wb", LEN_TERSE);
yURG_msg ('-', "writing database å%sæ", a_name);
break;
default :
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
DEBUG_FILE yLOG_info ("x_mode" , x_mode);
/*---(open)---------------------------*/
DEBUG_FILE yLOG_info ("a_name" , a_name);
f = fopen (a_name, x_mode);
DEBUG_FILE yLOG_point ("f" , f);
--rce; if (f == NULL) {
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(save-back)----------------------*/
if (r_file != NULL) *r_file = f;
/*---(complete)-----------------------*/
DEBUG_FILE yLOG_exit (__FUNCTION__);
return 0;
}
char
SHARED_close (FILE **b_file)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10;
char rc = 0;
/*---(header)-------------------------*/
DEBUG_FILE yLOG_enter (__FUNCTION__);
/*---(defense)------------------------*/
DEBUG_INPT yLOG_point ("b_file" , b_file);
--rce; if (b_file == NULL) {
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
DEBUG_INPT yLOG_point ("*b_file" , *b_file);
--rce; if (*b_file == NULL) {
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(close)--------------------------*/
rc = fclose (*b_file);
DEBUG_OUTP yLOG_value ("close" , rc);
--rce; if (rc < 0) {
DEBUG_FILE yLOG_exitr (__FUNCTION__, rce);
return rce;
}
/*---(ground pointer)-----------------*/
*b_file = NULL;
DEBUG_INPT yLOG_point ("*b_file" , *b_file);
/*---(complete)-----------------------*/
DEBUG_FILE yLOG_exit (__FUNCTION__);
return 0;
}