~kris/hacks

sbot

ref: 57c09c8c4fa2cd2479d9ff83a68b7fb447757a13 sbot/util.h -rw-r--r-- 1.6 KiB
57c09c8c — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* See LICENSE file for copyright and license details. */

#ifndef UTIL_H
#define UTIL_H

#include <stddef.h>

/* Memory allocation with error handling */
void *xmalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *s);

/* Base64 encoding */
char *base64_encode(const unsigned char *data, size_t input_len,
                    size_t *output_len);

/* String utilities */
int str_starts_with(const char *str, const char *prefix);
int str_ends_with(const char *str, const char *suffix);
char *str_tolower(char *str);
char *str_trim(char *str);

/* URL utilities */
char *url_resolve(const char *base, const char *relative);
char *url_get_domain(const char *url);
int url_same_domain(const char *url1, const char *url2);

/* Return pointer into url at the start of the path component (after domain).
 * Handles http:// https:// // and bare paths. Returns "/" if no path.
 * Never returns NULL. The returned pointer is inside the original string. */
const char *url_path(const char *url);

/* Compute a relative path from 'from' (current file path) to 'to' (target file path).
 * Both are paths relative to site root, e.g. "dir/page.htm" and "other/file.pdf".
 * Returns e.g. "../other/file.pdf" or "file.pdf" as needed for <a href> rewriting.
 * Caller must free the result. */
char *make_relative_path(const char *from, const char *to);

/* File utilities */
char *get_mime_type(const char *url);
char *sanitize_filename(const char *url);

/* Time utilities */
char *get_iso_date(void);

/* Error handling */
void die(const char *fmt, ...);
void warn(const char *fmt, ...);

#endif /* UTIL_H */