1 /*
2 Copyright (C) 2015-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: dk4error.ctr
12 */
13 
14 #ifndef DK4ERROR_H_INCLUDED
15 /** Avoid multiple inclusions. */
16 #define DK4ERROR_H_INCLUDED 1
17 
18 
19 /**	@file
20 	Return error information from
21 	called function to caller, more detailed than just an
22 	error code.
23 
24 	In addition to the error code we can save details, i.e.
25 	the errno or GetLastError() value or the element size
26 	and number of elements for a failed memory allocation...
27 	This allows the caller to produce detailed error messages
28 	if necessary.
29 
30 	Before processing a code section where errors can occur,
31 	use dk4error_init() to initialize an error structure.
32 	A value indicating ``no error occured'' is set in the structure.
33 
34 	Depending on the errors occuring, the code section might use
35 	one from the dk4error_set_...() functions.
36 
37 	At the end of the code section, check the error code in the
38 	dk4_er_t structure.
39 
40 	The address of one dk4_er_t can be passed to several function
41 	calls. Only the first error occured is noted in the structure,
42 	it is not overwritten by later errors.
43 
44 	The final number used by DK tools and libraries is
45 	DK4_E_HIGHEST_NUMBER.
46 
47 	In your applications you can define additional error codes
48 	starting at (DK4_E_HIGHEST_NUMBER+1).
49 
50 */
51 
52 #ifndef DK4CONF_H_INCLUDED
53 #if DK4_BUILDING_DKTOOLS4
54 #include "dk4conf.h"
55 #else
56 #include <dktools-4/dk4conf.h>
57 #endif
58 #endif
59 
60 #ifndef DK4TYPES_H_INCLUDED
61 #if DK4_BUILDING_DKTOOLS4
62 #include <libdk4base/dk4types.h>
63 #else
64 #include <dktools-4/dk4types.h>
65 #endif
66 #endif
67 
68 
69 /**	Error report.
70 */
71 typedef struct {
72   /**	Details, depending on error code.
73   */
74   union {
75     /**	Position within a file or data stream.
76     */
77     struct {
78       dk4_um_t	byteno;		/**< Byte number. */
79       dk4_um_t	lineno;		/**< Line number. */
80       dk4_um_t	charno;		/**< Character number. */
81       dk4_um_t	charinline;	/**< Character number within the line. */
82     }	fpos;
83     /**	Failed memory allocation.
84     */
85     struct {
86       size_t	elsize;		/**< Element size. */
87       size_t	nelem;		/**< Number of elements. */
88     }	mem;
89     long	lDetails1;	/**< Long details, i.e. from GetLastError(). */
90     int		iDetails1;	/**< Integer details, i.e. from errno. */
91   } dt;
92   int		ec;		/**< Error code. */
93 } dk4_er_t;
94 
95 
96 
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100 
101 /**	Initialize error report.
102 
103 	CRT on Windows: Optional.
104 	@param	erp	Error report to initialize.
105 */
106 void
107 dk4error_init(
108   dk4_er_t      *erp
109 );
110 
111 /**	Set simple error code.
112 
113 	CRT on Windows: Not used.
114 	@param	erp	Error report to modify, may be NULL.
115 	@param	ec	New error code.
116 */
117 void
118 dk4error_set_simple_error_code(
119   dk4_er_t      *erp,
120   int            ec
121 );
122 
123 /**	Set error code for dealing with a number of elements
124 	(memory allocation).
125 
126 	CRT on Windows: Not used.
127 	@param	erp	Error report to modify, may be NULL.
128 	@param	ec	New error code to set.
129 	@param	sz	Element size.
130 	@param	ne	Number of elements.
131 */
132 void
133 dk4error_set_elsize_nelem(
134   dk4_er_t      *erp,
135   int            ec,
136   size_t         sz,
137   size_t         ne
138 );
139 
140 /**	Set error code for error from a system function, save
141 	errno value in details.
142 
143 	CRT on Windows: Not used.
144 	@param	erp	Error report to modify, may be NULL.
145 	@param	ec	New error code to set.
146 	@param	errnval	Value of errno, stored in iDetails1.
147 */
148 void
149 dk4error_set_idetails(
150   dk4_er_t      *erp,
151   int            ec,
152   int            errnval
153 );
154 
155 /**	Set long error code for error from a Windows system function,
156 	save GetLastError() value in details.
157 
158 	CRT on Windows: Not used.
159 	@param	erp	Error report to modify, may be NULL.
160 	@param	ec	New error code to set.
161 	@param	errnval	Value obtained from GetLastError().
162 */
163 void
164 dk4error_set_ldetails(
165   dk4_er_t      *erp,
166   int            ec,
167   long           errnval
168 );
169 
170 /**	Set error code and keep file or data stream position.
171 
172 	CRT on Windows: Not used.
173 	@param	erp		Error report to modify, may be NULL.
174 	@param	ec		New error code to set.
175 	@param	byteno		Byte position in file.
176 	@param	lineno		Current line number.
177 	@param	charno		Character number in file.
178 	@param	charinline	Character position in line.
179 */
180 void
181 dk4error_set_with_position(
182   dk4_er_t	*erp,
183   int		 ec,
184   dk4_um_t	 byteno,
185   dk4_um_t	 lineno,
186   dk4_um_t	 charno,
187   dk4_um_t	 charinline
188 );
189 
190 /**	Copy error report if there was an error in the source report
191 	and no error yet in the destination report.
192 
193 	CRT on Windows: Optional.
194 	@param	dptr	Destination report.
195 	@param	sptr	Source report.
196 */
197 void
198 dk4error_copy(
199   dk4_er_t       *dptr,
200   const dk4_er_t *sptr
201 );
202 
203 #ifdef __cplusplus
204 }
205 #endif
206 
207 
208 
209 /**	Error codes.
210 */
211 enum {
212 
213 			/**	No error occured.
214 			*/
215   DK4_E_NONE	=	0,
216 
217 			/**	System error, the errno value is saved to
218 				det,syserr.
219 			*/
220   DK4_E_SYSTEM ,
221 
222 			/**	Something failed although it should not.
223 			*/
224   DK4_E_BUG ,
225 
226 			/**	Invalid arguments (in example NULL pointers,
227 				0 sizes) were passed to a function.
228 				No details are reported.
229 			*/
230   DK4_E_INVALID_ARGUMENTS ,
231 
232   			/**	A buffer was too small to take a text
233 				completely.
234   			*/
235   DK4_E_BUFFER_TOO_SMALL ,
236 
237 			/**	The requested information was not found.
238 			*/
239   DK4_E_NOT_FOUND ,
240 
241 			/**	Memory allocation failed. Element size and
242 				number of elements are stored in
243 				dt,mem,elsize and dt,mem,nelem.
244   			*/
245   DK4_E_MEMORY_ALLOCATION_FAILED ,
246 
247 			/**	Numeric overflow. For floating point numbers
248 				the absolute value exceeded DBL_MAX.
249 				For integer numbers the result is outside
250 				the range MIN...MAX.
251 				No details are reported.
252 			*/
253   DK4_E_MATH_OVERFLOW ,
254 
255 			/**	Numeric underflow.
256 				The underflow is reported if the absolute
257 				value of a floating point operation is too
258 				small.
259 				For integer numbers only overflow is reported
260 				even if the result is smaller than MIN.
261 				No details are reported.
262 			*/
263   DK4_E_MATH_UNDERFLOW ,
264 
265 			/**	Division by zero. No details are reported.
266 			*/
267   DK4_E_MATH_DIVZERO ,
268 
269 			/**	An invalid argument was passed to a
270 				mathematical function.
271 			*/
272   DK4_E_MATH_INVALID ,
273 
274 			/**	Mathematical result is inexact, cutting bits.
275 			*/
276   DK4_E_MATH_INEXACT ,
277 
278 			/**	Syntax error when processing text, illegal
279 				character when decoding or encoding.
280 			*/
281   DK4_E_SYNTAX ,
282 
283 			/**	Functionality not supported on this system.
284 			*/
285   DK4_E_NOT_SUPPORTED ,
286 
287 			/**	A write operation failed.
288 			*/
289   DK4_E_WRITE_FAILED ,
290 
291 			/**	A data flush operation failed.
292 			*/
293   DK4_E_FLUSH_FAILED ,
294 
295 			/**	Failed to read data.
296 			*/
297   DK4_E_READ_FAILED ,
298 			/**	A file or other stream close operation failed.
299 			*/
300   DK4_E_CLOSE_FAILED ,
301 
302 			/**	Additional security checks before opening a
303 				file failed.
304 			*/
305   DK4_E_SEC_CHECK ,
306 
307 			/**	Attempt to open a file failed.
308 			*/
309   DK4_E_OPEN_FAILED ,
310 
311 			/**	Failed to open file for reading.
312 			*/
313   DK4_E_OPEN_READ_FAILED ,
314 
315 			/**	Failed to open file for writing.
316 			*/
317   DK4_E_OPEN_WRITE_FAILED ,
318 
319 			/**	Failed to encode 32 bit character to
320 				output encoding.
321 			*/
322   DK4_E_ENCODING_FAILED ,
323 
324 			/**	Failed to decode 32 bit character from
325 				input encoding.
326 			*/
327   DK4_E_DECODING_FAILED ,
328 
329 			/**	Failed to create a directory.
330 			*/
331   DK4_E_MKDIR_FAILED ,
332 
333 			/**	Failed to change file or directory ownership.
334 			*/
335   DK4_E_CHOWN_FAILED ,
336 
337 			/**	Failed to change file or directory permissions.
338 			*/
339   DK4_E_CHMOD_FAILED ,
340 
341 			/**	Non-directory component in path name.
342 			*/
343   DK4_E_NON_DIR ,
344 
345 			/**	File exists, but is not a socket.
346 			*/
347   DK4_E_NON_SOCKET ,
348 
349 			/**	An unlink operation failed.
350 			*/
351   DK4_E_UNLINK_FAILED ,
352 
353 			/**	The DeleteFile() function on Windows failed.
354 			*/
355   DK4_E_DELETE_FILE_FAILED ,
356 
357 			/**	An rmdir operation failed.
358 			*/
359   DK4_E_RMDIR_FAILED ,
360 
361 			/**	The RemoveDirectory() function failed
362 				on Windows.
363 			*/
364   DK4_E_REMOVE_DIRECTORY_FAILED ,
365 
366 			/**	Opendir failed.
367 			*/
368   DK4_E_OPENDIR_FAILED ,
369 
370 			/**	The CreateFile() function failed on Windows.
371 				The result of GetLastError() is stored
372 				in dt.lDetails1.
373 			*/
374   DK4_E_CREATE_FILE_FAILED ,
375 
376 			/**	The CreateDirectory() function failed on
377 				Windows. The result of GetLastError() is
378 				stored in dt.lDetails1.
379 			*/
380   DK4_E_CREATE_DIR_FAILED ,
381 
382 			/**	The GetFileInformationByHandle() function
383 				failed on Windows. The result of
384 				GetLastError() is stored in dt.lDetails1.
385 			*/
386   DK4_E_FILE_INFORMATION_FAILED ,
387 
388 			/**	The FindFirstFile() function on Windows failed.
389 				The GetLastError() value is stored in lDetails1.
390 			*/
391   DK4_E_FINDFIRSTFILE_FAILED ,
392 
393 			/**	Timeout occured in socket operation,
394 				no details in iDetails1.
395 			*/
396   DK4_E_SOCKET_TIMEOUT ,
397 
398 			/**	The getaddrinfo() function failed to
399 				find the remote address.
400 				The function result is stored in iDetails1.
401 			*/
402   DK4_E_SOCKET_GETADDRINFO ,
403 
404 			/**	The getaddrinfo() function failed to
405 				find the local address.
406 				The function result is stored in iDetails1.
407 			*/
408   DK4_E_SOCKET_GETADDRLOC ,
409 
410 			/**	Failed to start up Windows sockets.
411 				The error code from WSAGetLastError() on
412 				Windows / errno on non-Windows is stored
413 				in iDetails1.
414 			*/
415   DK4_E_SOCKET_UP ,
416 
417 			/**	Failed to shut down Windows sockets.
418 				The error code from WSAGetLastError() on
419 				Windows / errno on non-Windows is stored
420 				in iDetails1.
421 			*/
422   DK4_E_SOCKET_DOWN ,
423 
424 			/**	The socket() function failed.
425 				The error code from WSAGetLastError() on
426 				Windows / errno on non-Windows is stored
427 				in iDetails1.
428 			*/
429   DK4_E_SOCKET_SOCKET ,
430 
431 			/**	The close() or closesocket() function failed.
432 				The error code from WSAGetLastError() on
433 				Windows / errno on non-Windows is stored
434 				in iDetails1.
435 			*/
436   DK4_E_SOCKET_CLOSE ,
437 
438 			/**	The bind() function failed.
439 				The error code from WSAGetLastError() on
440 				Windows / errno on non-Windows is stored
441 				in iDetails1.
442 			*/
443   DK4_E_SOCKET_BIND ,
444 
445 			/**	The connect() function failed.
446 				The error code from WSAGetLastError() on
447 				Windows / errno on non-Windows is stored
448 				in iDetails1.
449 			*/
450   DK4_E_SOCKET_CONNECT ,
451 
452 			/**	The listen() function failed.
453 				The error code from WSAGetLastError() on
454 				Windows / errno on non-Windows is stored
455 				in iDetails1.
456 			*/
457   DK4_E_SOCKET_LISTEN ,
458 
459 			/**	The accept() function failed.
460 				The error code from WSAGetLastError() on
461 				Windows / errno on non-Windows is stored
462 				in iDetails1.
463 			*/
464   DK4_E_SOCKET_ACCEPT ,
465 
466 			/**	The select() function failed.
467 				The error code from WSAGetLastError() on
468 				Windows / errno on non-Windows is stored
469 				in iDetails1.
470 			*/
471   DK4_E_SOCKET_SELECT ,
472 
473 			/**	The setsockopt() function failed.
474 				The error code from WSAGetLastError() on
475 				Windows / errno on non-Windows is stored
476 				in iDetails1.
477 			*/
478   DK4_E_SOCKET_SETSOCKOPT ,
479 
480   			/**	The getsockopt() function failed.
481 				The error code from WSAGetLastError() on
482 				Windows / errno on non-Windows is stored
483 				in iDetails1.
484 			*/
485   DK4_E_SOCKET_GETSOCKOPT ,
486 
487   			/**	The shutdown() function failed.
488 				The error code from WSAGetLastError() on
489 				Windows / errno on non-Windows is stored
490 				in iDetails1.
491 			*/
492   DK4_E_SOCKET_SHUTDOWN ,
493 
494   			/**	The send()/sendto() function failed.
495 				The error code from WSAGetLastError() on
496 				Windows / errno on non-Windows is stored
497 				in iDetails1.
498 			*/
499   DK4_E_SOCKET_SEND ,
500 
501   			/**	The recv()/recvfrom() function failed.
502 				The error code from WSAGetLastError() on
503 				Windows / errno on non-Windows is stored
504 				in iDetails1.
505 			*/
506   DK4_E_SOCKET_RECV ,
507 
508 			/**	The ioctlsocket() function failed.
509 				The error code from WSAGetLastError() on
510 				Windows is stored in iDetails1.
511 				On Windows only.
512 			*/
513   DK4_E_SOCKET_IOCTLSOCKET ,
514 
515 			/**	The fcntl() function failed.
516 				The error code from
517 				errno on non-Windows is stored
518 				in iDetails1.
519 				Not on Windows systems (ioctlsocket
520 				is used to switch between blocking
521 				and non-blocking).
522 			*/
523   DK4_E_SOCKET_FCNTL ,
524 
525 			/**	The getservbyname() function failed.
526 				The error code from WSAGetLastError() on
527 				Windows / errno on non-Windows is stored
528 				in iDetails1.
529 			*/
530   DK4_E_SOCKET_GETSERVBYNAME ,
531 
532 			/**	The gethostbyname() function failed.
533 				The error code from WSAGetLastError() on
534 				Windows / errno on non-Windows is stored
535 				in iDetails1.
536 			*/
537   DK4_E_SOCKET_GETHOSTBYNAME ,
538 
539 			/**	The inet_ntop() function failed.
540 				The error code from WSAGetLastError() on
541 				Windows / errno on non-Windows is stored
542 				in iDetails1.
543 			*/
544   DK4_E_SOCKET_INET_NTOP ,
545 
546 			/**	The inet_pton() function failed.
547 				The error code from WSAGetLastError() on
548 				Windows / errno on non-Windows is stored
549 				in iDetails1.
550 			*/
551   DK4_E_SOCKET_INET_PTON ,
552 
553 			/**	Maximum error number defined in dk4error
554 				module.
555 			*/
556   DK4_E_HIGHEST_NUMBER = DK4_E_SOCKET_INET_PTON
557 };
558 
559 
560 
561 
562 #endif
563