diff options
| author | Tristan Riehs <tristan.riehs@inria.fr> | 2026-04-26 12:07:39 +0200 |
|---|---|---|
| committer | Tristan Riehs <tristan.riehs@inria.fr> | 2026-04-26 12:07:39 +0200 |
| commit | 2991c0b3560781344488680625954aa3ef0893c6 (patch) | |
| tree | b5bc2048e83453e8c7b5a2d2677161c4cc4847f2 /src/utils.h | |
| parent | d2fb6a8aac6abe5bfe4b4ea7f2528d119afbc8c6 (diff) | |
Create a "utils" module
Also put configuration macros in a dedicated header. In the future the
configuration will be read at execution time.
Diffstat (limited to 'src/utils.h')
| -rw-r--r-- | src/utils.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..e382ace --- /dev/null +++ b/src/utils.h @@ -0,0 +1,39 @@ +#ifndef UTILS_H +#define UTILS_H + +/* Utility functions for manipulating strings, time, and error codes. */ + +#include <stdio.h> +#include <stdint.h> +#include <time.h> + +/* Internals of strbuild. */ +void __strbuild(char *buf, int size, + const char *file, int line, + const char *fmt, ...); + +/* Safely create a formatted string and write it to BUF. BUF shall be a buffer + * of size at least SIZE. BUF can be stack-allocated. If the formatting cannot + * be performed, exit(3) is called. This function is not meant to be called + * directly: the macro strbuild should be used. The code is adapted from the + * make_message function of the vsnprintf(3) manual page, section "examples". */ +#define strbuild(buf, fmt, ...) \ + __strbuild(buf, sizeof(buf), __FILE__, __LINE__, fmt, __VA_ARGS__) + +#define strbuild_with_size(buf, size, fmt, ...) \ + __strbuild(buf, size, __FILE__, __LINE__, fmt, __VA_ARGS__) + +int str_has_suffix(const char *str, const char *suffix); + +/* Abort if the database does not exist. */ +void assert_db_exists(void); + +/* Compute something that identifies the content of FILE. Algorithm by Dan + * Bernstein from http://www.cse.yorku.ca/~oz/hash.html via + * https://stackoverflow.com/a/7666577/20138083. */ +uint32_t sum(const char *file); + +/* Return an integer timestamp from a date string of the format YYYY-MM-DD. */ +time_t time_from_str(const char *str); + +#endif |
