diff options
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 |
