From 2991c0b3560781344488680625954aa3ef0893c6 Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Sun, 26 Apr 2026 12:07:39 +0200 Subject: Create a "utils" module Also put configuration macros in a dedicated header. In the future the configuration will be read at execution time. --- src/utils.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/utils.h (limited to 'src/utils.h') 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 +#include +#include + +/* 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 -- cgit v1.2.3