list.c
10.3 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* \file list.c
* \author Georg Steffers <georg@steffers.org>
* \brief Internal implementations for list handling.
*
* This implements the internals for the typesafe linked list
* implementation provided by \link scot/list.h list.h\endlink,
* \link scot/list_proto.h list_proto.h\endlink and
* \link scot/list_impl.h list_impl.h\endlink.
* The only thing one has to remember when one only wants to
* use linked lists within the own code is to link the code agains
* an object of this. This is normally done by linking agains
* libscot with an -lscot or similar compiler switch.
*
* Copyright (C)2006 Georg Steffers <georg@steffers.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <scot/list.h>
#include <scot/exception.h>
#include <scot/scot_exceptions.h>
#include <scot/memory.h>
#include <scot_common.h>
#include <scot/scot_types.h>
/**
* \internal
* \brief Error messages given.
*
* This holds all error messages that go either to the exception system
* or will be printed on stderr if an error occurs within a list function.
*/
const char *list_err_msg[] =
{
N_("[LIST]Failed to create new list."),
N_("[LIST]Failed to free list."),
N_("[LIST]Failed to check for begin of list."),
N_("[LIST]Failed to check for end of list."),
N_("[LIST]Failed to check for list emptiness."),
N_("[LIST]Failed to get first list node."),
N_("[LIST]Failed to get last list node."),
N_("[LIST]Failed to get next list node."),
N_("[LIST]Failed to get previous list node."),
N_("[LIST]Failed to find list node."),
N_("[LIST]Failed to find list anchor."),
N_("[LIST]Failed to retrive the node's value."),
N_("[LIST]Failed to set the node's value."),
N_("[LIST]Failed to insert node."),
N_("[LIST]Failed to delete node."),
N_("[LIST]Failed to concatanate the lists."),
N_("[LIST]Failed to count list nodes."),
N_("[LIST]Node is no anchor."),
N_("[LIST]Malformed list."),
NULL
};
/**
* \internal
* \brief Warning messages given.
*
* This holds all warning messages that go either to the exception system
* or will be printed on stderr if an error occurs within a list function.
*/
const char *list_wrn_msg[] =
{
N_("[LIST]tried to delete on an empyt list"),
NULL
};
/**
* \internal
* \param lvl either ERROR or WARNING
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
* \param id the error or warning id. (Used to pick the correct message)
*
* \pre None
* \returns Nothing
* \post An error or warning message printed to stderr. In case of an
* ERROR the program is aborted.
*
* \brief Print error or warning to stderr.
*/
static
void
list_ew_print (
const enum exclvl_t lvl,
const char *file,
const int line,
const int id)
{
bindtextdomain (PACKAGE, LOCALEDIR);
fprintf (
stderr,
"[%s(%s:%d)]\n(%d) %s\n",
(lvl == EXC_ERROR)?D_("ERROR"):D_("WARNING"),
file,
line,
id,
(lvl == EXC_ERROR)?D_(list_err_msg [id]):D_(list_wrn_msg [id]));
if (lvl == EXC_ERROR)
{
abort ();
}
}
/**
* \internal
* \param lvl either ERROR or WARNING
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
* \param id the error or warning id. (Used to pick the correct message)
*
* \pre An exception environment created by a previous call of TRY.
* \returns Nothing
* \post An exception thrown into the exception environment.
*
* \brief Throw error or warning in an exception environment.
*/
static
void
list_ew_throw (
enum exclvl_t lvl,
const char *file,
int line,
int id)
{
THROW (exc_new (lvl, file, line, id, D_(list_err_msg [id])));
}
/**
* \internal
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
* \param id the error id. (Used to pick the correct message)
*
* \pre None
* \returns Nothing
* \post An error message printed to stderr. The program is aborted.
*
* \brief Print error to stderr.
*/
void
list_error_print (const char *file, int line, const int id)
{
list_ew_print (EXC_ERROR, file, line, id);
}
/**
* \internal
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
* \param id the warning id. (Used to pick the correct message)
*
* \pre None
* \returns Nothing
* \post An warning message printed to stderr.
*
* \brief Print warning to stderr.
*/
void
list_warning_print (const char *file, int line, int id)
{
list_ew_print (EXC_WARNING, file, line, id);
}
/**
* \internal
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
* \param id the error id. (Used to pick the correct message)
*
* \pre An exception environment created by a previous call of TRY.
* \returns Nothing
* \post An exception thrown into the exception environment.
*
* \brief Throw error.
*/
void
list_error_throw (const char *file, int line, int id)
{
list_ew_throw (EXC_ERROR, file, line, id);
}
void
/**
* \internal
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
* \param id the warning id. (Used to pick the correct message)
*
* \pre An exception environment created by a previous call of TRY.
* \returns Nothing
* \post An exception thrown into the exception environment.
*
* \brief Throw warning.
*/
list_warning_throw (const char *file, int line, int id)
{
list_ew_throw (EXC_WARNING, file, line, id);
}
/**
* \internal
* \param size SIZE_T size of memory to be allocates (see man malloc).
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
*
* \pre None
* \returns A pointer to the memory location reserved by malloc.
* \post Either memory is allocated on the heap or an error messege is
* printed to stderr and the program aborted.
*
* \brief Malloc wrapper with error handling.
*
* This calls malloc and if it fails gives an error message to stderr.
* If this malloc fails it is assumed a serious error and thus the program
* is aborted in this case.
*/
void *
list_malloc_print (SIZE_T size, const char *file, int line)
{
void *a;
bindtextdomain (PACKAGE, LOCALEDIR);
a = SCOT_MEM_GET (size);
if (a == NULL)
{
fprintf (
stderr,
"[ERROR(%s:%d)]\n(%d) %s\n",
file,
line,
MALLOC_ERR,
D_(scot_err_msg [MALLOC_ERR]));
abort ();
}
}
/**
* \internal
* \param size SIZE_T size of memory to be allocates (see man malloc).
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
*
* \pre An exception environment created by a previous call of TRY.
* \returns A pointer to the memory location reserved by malloc.
* \post Either memory is allocated on the heap or an exception thrown
* into the exception environment.
*
* \brief Malloc wrapper with error handling.
*
* This calls malloc and if it fails throws an exception.
* This malloc fails it is assumed a serious error and thus an ERROR
* is thrown into the exception system causing the calling function to
* be aborted and goes to exception handling CATCH of the current
* exception environment.
*/
void *
list_malloc_throw (SIZE_T size, const char *file, int line)
{
return exc_malloc_fl (size, file, line);
}
/**
* \internal
* \param val Pointer to be checked.
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
*
* \pre None
* \returns Nothing.
* \post If val is NULL an error messege is
* printed to stderr and the program aborted.
*
* \brief Checks the given value for beeing NULL.
*
* This checks the given \a val for beeing NULL and if is gives an
* error message to stderr. If this check fails within the list
* code it is a serious error and thus the program is aborted.
*/
void
list_check_null_print (const void *val, const char *file, int line)
{
bindtextdomain (PACKAGE, LOCALEDIR);
if (val == NULL)
{
fprintf (
stderr,
"[ERROR(%s:%d)]\n(%d) %s\n",
file,
line,
NULL_PTR_ERR,
D_(scot_err_msg [NULL_PTR_ERR]));
abort ();
}
}
/**
* \internal
* \param val Pointer to be checked.
* \param file the filename of the source file from where this is called
* \param line the line in the source file from where this is called
*
* \pre An exception environment created by a previous call of TRY.
* \returns Nothing.
* \post If val is NULL an exception thrown
* into the exception environment.
*
* \brief Checks the given value for beeing NULL.
*
* This checks the given \a val for beeing NULL and if is throws
* an exception. If this check fails within the list code it is a
* serious error and thus an ERROR is throw into the exception system,
* causing the calling function to abort and go to exception handling
* CATCH of the actual exception environment.
*/
void
list_check_null_throw (const void *val, const char *file, int line)
{
check_null_fl (val, file, line);
}