summaryrefslogtreecommitdiff
path: root/src/shared.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared.h')
-rw-r--r--src/shared.h191
1 files changed, 0 insertions, 191 deletions
diff --git a/src/shared.h b/src/shared.h
deleted file mode 100644
index 94c6f06..0000000
--- a/src/shared.h
+++ /dev/null
@@ -1,191 +0,0 @@
1/**
2 * @file shared.h
3 * @author syxhe (https://t.me/syxhe)
4 * @brief A collection of functions and macros shared between files, or without a better place
5 * @version 0.1
6 * @date 2025-06-09
7 *
8 * @copyright Copyright (c) 2025
9 *
10 */
11
12#ifndef __VXGG_REWRITE___SHARED_H___3880294315821___
13#define __VXGG_REWRITE___SHARED_H___3880294315821___ 1
14
15#include <stddef.h>
16
17typedef int (*gcallback)(void*); //!< Generic callback signature
18typedef void (*fcallback)(void*); //!< free()-like callback signature
19
20//! Get the size of a statically/vla defined array. Doesn't work on arrays allocated via `malloc()`
21#define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0]))
22
23//! Sets errno to `errval`, then returns `retval`. Defined as a macro because it is meant for use inside functions, not as a function itself
24#define ERRRET(errval, retval) do {\
25 errno = (errval);\
26 return (retval);\
27} while (0)
28
29/// Defines how `x___alloc()` functions should exit. `___VXGG___XALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions
30/// registered with `atexit()` and `on_exit()`. `___VXGG___XALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `x___alloc()`
31/// type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type
32#define ___VXGG___XALLOC_EXIT_ON_ERROR___ 1
33
34/// Defines whether vxgg functions that can error print out a short warning of the error when one is encountered.
35/// `___VXGG___VERBOSE_ERRORS___ > 0` will print diagnostic error messages, and will do nothing otherwise
36#define ___VXGG___VERBOSE_ERRORS___ 1
37
38//! Macro to exit on an alloc error instead of doing the terrible nested if statement that was being used previously
39#define XALLOC_EXIT(msg, ...) do {\
40 if(!___VXGG___XALLOC_EXIT_ON_ERROR___)\
41 abort();\
42 if(!___VXGG___VERBOSE_ERRORS___)\
43 exit(EXIT_FAILURE);\
44 error(EXIT_FAILURE, errno, (msg)__VA_ARGS__);\
45 exit(EXIT_FAILURE); /* Makes gcc happy */\
46} while (0)
47
48
49//! Holdover macro because I'm lazy. Used to call either malloc or xmalloc, but the xalloc functions were a bad idea, so I removed them
50#define VALLOC(nmemb, size) malloc((nmemb) * (size))
51
52//! Error macro that gcc will not complain about
53#define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0)
54//! Spit out a warning using `error`
55#define WARN(errnum, format, ...) do {error(0, (errnum), (format)__VA_ARGS__);} while (0)
56
57
58
59/**
60 * @brief Read the entire contents of a file descriptor into a malloc()'ed buffer
61 *
62 * @param str Pointer to a string. Will be replaced with the malloc()'ed string
63 * @param initsize The initial size of the malloc()'ed string
64 * @param fd A file descriptor to read from
65 * @retval (int)[-1, strlen(str)] Returns the size of the array on success, or -1 on error
66 * @note The allocated buffer will expand and contract as necessary, but it's recommended to set `initsize` to some value close to or equal to the size of the file being read to reduce the number of resizes
67 */
68int rwbuf(char **str, unsigned long int initsize, int fd);
69
70/**
71 * @brief Write the entire contents of a buffer into a file descriptor
72 *
73 * @param fd The file descriptor to write to
74 * @param buf The buffer to write from
75 * @param len The length of the buffer
76 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
77 */
78int wwbuf(int fd, const unsigned char *buf, int len);
79
80
81
82/**
83 * @brief `dirname()` reimplementation that returns a malloc()'ed string
84 *
85 * @param path The filepath to be inspected
86 * @retval (char*)[NULL, char*] Returns a null-terminated string on success, or `null` on error
87 */
88char * vxdirname(const char * const path);
89
90
91
92/**
93 * @brief A locally defined structure designed for easier function cleanup
94 *
95 */
96typedef struct cl {
97 fcallback *callbacks; //!< An array of free()-like callbacks. Actual Type: fcallback callbacks[]
98 void * *arguments; //!< An array of void pointers. Actual Type: void *arguments[]
99 int size; //!< The size of each array
100 int used; //!< The current number of used elements in each array
101} cleanup;
102
103/**
104 * @brief Initialize a cleanup object
105 *
106 * @param loc The cleanup object to be initialized
107 * @param callbacks An array of free()-like callbacks. Must be `size` elements long
108 * @param arguments An array of void pointers. Must be `size` elements long
109 * @param size The number of elements the callbacks and arguments array are long
110 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
111 */
112int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size);
113
114/**
115 * @brief Register a new callback and argument onto a cleanup stack
116 *
117 * @param loc The cleanup object to modify
118 * @param cb A free()-like callback to run
119 * @param arg A piece of data for the callback to run
120 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
121 */
122int cleanup_register(cleanup *loc, fcallback cb, void *arg);
123
124/**
125 * @brief Conditionally register a callback and argument
126 *
127 * @param loc The cleanup object to modify
128 * @param cb A free()-like callback to run
129 * @param arg A piece of data for the callback to run
130 * @param flag Whether or not the register should take place. Will not run if `flag` is non-zero
131 * @retval (int)[-1, 0] Returns 0 on success or skip, -1 on error
132 */
133int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag);
134
135/**
136 * @brief Clear a cleanup object
137 * @attention Does not free any registered callbacks or arguments, just marks them as available space
138 *
139 * @param loc The cleanup object to modify
140 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
141 */
142int cleanup_clear(cleanup *loc);
143
144/**
145 * @brief Fires all the registered callbacks and arguments in a cleanup object in FIFO (stack) order
146 *
147 * @param loc The cleanup object to fire
148 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
149 */
150int cleanup_fire(cleanup *loc);
151
152/**
153 * @brief Conditionally fires a cleanup object
154 *
155 * @param loc The cleanup object in question
156 * @param flag Whether the object should be fired. Will skip firing if non-zero
157 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
158 */
159int cleanup_cndfire(cleanup *loc, unsigned char flag);
160
161/**
162 * @brief Initializes a set of variables suitable for use in the cleanup macros
163 * @param size The number of elements long each array should be
164 */
165#define cleanup_CREATE(size) \
166cleanup __CLEANUP; \
167fcallback __CLEANUP_FUNCS[(size)]; \
168void *__CLEANUP_ARGS[(size)]; \
169unsigned char __FLAG = 0; \
170cleanup_init(&__CLEANUP, __CLEANUP_FUNCS, __CLEANUP_ARGS, (size))
171
172//! Register a callback-argument pair using the local cleanup object
173#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg))
174//! Conditionally register a callback-argument pair using the local cleanup object
175#define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, (cb), (arg), __FLAG)
176//! Clean the local cleanup object
177#define cleanup_CLEAR() cleanup_clear(&__CLEANUP)
178//! Fire the local cleanup object
179#define cleanup_FIRE() cleanup_fire(&__CLEANUP)
180//! Conditionally fire the local cleanup object
181#define cleanup_CNDFIRE() cleanup_cndfire(&__CLEANUP, __FLAG)
182//! Set the local cleanup flag to a non-zero number
183#define cleanup_MARK() (__FLAG = 1)
184//! Set the local cleanup flag to zero
185#define cleanup_UNMARK() (__FLAG = 0)
186//! Check if the local cleanup flag is non-zero
187#define cleanup_ERRORFLAGGED (__FLAG != 0)
188//! Conditionally execute some `code` if the local cleanup flag has not been marked
189#define cleanup_CNDEXEC(code) while(!cleanup_ERRORFLAGGED) {code; break;}
190
191#endif \ No newline at end of file