General utilities - Standalone hedaer.
More...
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Go to the source code of this file.
|
#define | QUOTE(name) #name |
| Quote.
|
|
#define | STR(macro) QUOTE(macro) |
| Stringify.
|
|
#define | CAT(x, y) CAT_(x, y) |
| Concatenate.
|
|
#define | CAT_(x, y) x##y |
| Token-paste.
|
|
#define | buffer_size(format, ...) (snprintf(NULL, 0, (format), __VA_ARGS__) + 1) |
| Gets the necessary buffer size for a sprintf operation.
|
|
#define | streq(x, y) (strcmp((x), (y)) == 0) |
| Compares two strings for equality.
|
|
#define | strneq(x, y, n) (strncmp((x), (y), (n)) == 0) |
| Compares two strings for equality up to a given length.
|
|
#define | MAX(a, b) ((a) > (b) ? (a) : (b)) |
| Returns the maximum of two values.
|
|
#define | MIN(a, b) ((a) < (b) ? (a) : (b)) |
| Returns the minimum of two values.
|
|
#define | COALESCE(a, b) ((a == NULL) ? (b) : (a)) |
| Returns the first non-null argument.
|
|
#define | errno_exit(of) |
| Exits the program with an error message and failure status.
|
|
#define | array_len(array) (sizeof(array) / sizeof((array)[0])) |
| Calculates the length of an array.
|
|
#define | ATTR_FLAG_ENUM |
|
#define | ATTR_FORMAT(archetype, string_index, first_to_check) |
|
#define | unreachable() abort() |
|
|
char * | strfmt (const char *fmt,...) ATTR_FORMAT(printf |
| Formats a string using a printf-style format string and arguments.
|
|
char char * | vstrfmt (const char *fmt, va_list ap) |
| Formats a string using a printf-style format string and a va_list of arguments.
|
|
char * | fslurp (FILE *fp) |
| Slurps a stream into a dynamically allocated string.
|
|
General utilities - Standalone hedaer.
- Author
- Raphaƫl
- Date
- 23/01/2025
Definition in file util.h.
◆ array_len
#define array_len |
( |
|
array | ) |
(sizeof(array) / sizeof((array)[0])) |
Calculates the length of an array.
This macro takes an array as its argument and returns the number of elements in the array.
- Parameters
-
array | The array to get the length of. |
- Returns
- The number of elements in the array.
Definition at line 91 of file util.h.
◆ ATTR_FLAG_ENUM
◆ ATTR_FORMAT
#define ATTR_FORMAT |
( |
|
archetype, |
|
|
|
string_index, |
|
|
|
first_to_check |
|
) |
| |
◆ buffer_size
#define buffer_size |
( |
|
format, |
|
|
|
... |
|
) |
| (snprintf(NULL, 0, (format), __VA_ARGS__) + 1) |
Gets the necessary buffer size for a sprintf operation.
Definition at line 25 of file util.h.
◆ CAT
#define CAT |
( |
|
x, |
|
|
|
y |
|
) |
| CAT_(x, y) |
Concatenate.
Definition at line 20 of file util.h.
◆ CAT_
#define CAT_ |
( |
|
x, |
|
|
|
y |
|
) |
| x##y |
Token-paste.
Definition at line 22 of file util.h.
◆ COALESCE
#define COALESCE |
( |
|
a, |
|
|
|
b |
|
) |
| ((a == NULL) ? (b) : (a)) |
Returns the first non-null argument.
This macro takes two arguments a
and b
, and returns a
if it is not null, otherwise it returns b
.
- Parameters
-
a | The first argument to check. |
b | The second argument to use if a is null. |
- Returns
a
if it is not null, otherwise b
.
Definition at line 74 of file util.h.
◆ errno_exit
Value: do { \
perror(of); \
exit(EXIT_FAILURE); \
} while (0)
Exits the program with an error message and failure status.
This macro is used to exit the program with an error message and failure status when an error occurs. It calls perror()
to print the error message and then calls exit()
with the EXIT_FAILURE
status.
Definition at line 79 of file util.h.
80 { \
81 perror(of); \
82 exit(EXIT_FAILURE); \
83 } while (0)
◆ MAX
#define MAX |
( |
|
a, |
|
|
|
b |
|
) |
| ((a) > (b) ? (a) : (b)) |
Returns the maximum of two values.
This macro returns the larger of the two arguments a
and b
.
- Parameters
-
a | The first value to compare. |
b | The second value to compare. |
- Returns
- The larger of
a
and b
.
Definition at line 54 of file util.h.
◆ MIN
#define MIN |
( |
|
a, |
|
|
|
b |
|
) |
| ((a) < (b) ? (a) : (b)) |
Returns the minimum of two values.
This macro returns the smaller of the two arguments a
and b
.
- Parameters
-
a | The first value to compare. |
b | The second value to compare. |
- Returns
- The smaller of
a
and b
.
Definition at line 64 of file util.h.
◆ QUOTE
#define QUOTE |
( |
|
name | ) |
#name |
Quote.
Definition at line 15 of file util.h.
◆ STR
#define STR |
( |
|
macro | ) |
QUOTE(macro) |
Stringify.
Definition at line 17 of file util.h.
◆ streq
#define streq |
( |
|
x, |
|
|
|
y |
|
) |
| (strcmp((x), (y)) == 0) |
Compares two strings for equality.
This macro compares the two null-terminated strings x
and y
for equality. It returns 1 if the strings are equal, and 0 otherwise.
- Parameters
-
x | The first string to compare. |
y | The second string to compare. |
- Returns
- 1 if the strings are equal, 0 otherwise.
Definition at line 34 of file util.h.
◆ strneq
#define strneq |
( |
|
x, |
|
|
|
y, |
|
|
|
n |
|
) |
| (strncmp((x), (y), (n)) == 0) |
Compares two strings for equality up to a given length.
This macro compares the first n
characters of the null-terminated strings x
and y
for equality. It returns 1 if the strings are equal up to the first n
characters, and 0 otherwise.
- Parameters
-
x | The first string to compare. |
y | The second string to compare. |
n | The number of characters to compare. |
- Returns
- 1 if the strings are equal up to the first
n
characters, 0 otherwise.
Definition at line 44 of file util.h.
◆ unreachable
#define unreachable |
( |
| ) |
abort() |
◆ fslurp()
char * fslurp |
( |
FILE * |
fp | ) |
|
Slurps a stream into a dynamically allocated string.
The caller is responsible for freeing the returned string.
- Parameters
-
- Returns
- A dynamically allocated string containing the stream's contents.
◆ strfmt()
char * strfmt |
( |
const char * |
fmt, |
|
|
|
... |
|
) |
| |
Formats a string using a printf-style format string and arguments.
This function takes a format string and a variable number of arguments, and returns a dynamically allocated string that contains the formatted result. The caller is responsible for freeing the returned string.
- Parameters
-
fmt | The printf-style format string. |
... | The arguments to be formatted. |
- Returns
- A dynamically allocated string containing the formatted result.
◆ vstrfmt()
char char * vstrfmt |
( |
const char * |
fmt, |
|
|
va_list |
ap |
|
) |
| |
Formats a string using a printf-style format string and a va_list of arguments.
This function takes a format string and a va_list of arguments, and returns a dynamically allocated string that contains the formatted result. The caller is responsible for freeing the returned string.
- Parameters
-
fmt | The printf-style format string. |
ap | The va_list of arguments to be formatted. |
- Returns
- A dynamically allocated string containing the formatted result.