Merge branch 'master' of git.dolda2000.com:/srv/git/r/ashd
[ashd.git] / lib / utils.h
CommitLineData
f0bbedf7
FT
1#ifndef _UTILS_H
2#define _UTILS_H
3
90f6e953 4#include <stdarg.h>
04883ca4 5#include <sys/types.h>
90f6e953 6
b16800fa
FT
7#define max(a, b) (((b) > (a))?(b):(a))
8#define min(a, b) (((b) < (a))?(b):(a))
9
f0bbedf7
FT
10#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
11#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
12#define szmalloc(size) memset(smalloc(size), 0, size)
90f6e953 13#define sstrdup(str) ({const char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);})
f0bbedf7
FT
14#define omalloc(o) ((o) = szmalloc(sizeof(*(o))))
15
16#define bufinit(buf) memset(&(buf), 0, sizeof(buf))
5fc1bf9f 17#define buffree(buf) do { if((buf).b != NULL) {free((buf).b);} bufinit(buf); } while(0)
f0bbedf7
FT
18#define sizebuf(buf, wanted) (_sizebuf((struct buffer *)&(buf), (wanted), sizeof(*((buf).b))))
19#define bufadd(buf, new) \
20do { \
21 _sizebuf((struct buffer *)&(buf), (buf).d + 1, sizeof(*((buf).b))); \
22 (buf).b[(buf).d++] = (new); \
23} while(0)
24#define bufcat(buf, new, size) \
25do { \
26 size_t __bufcat_size__; \
27 __bufcat_size__ = (size); \
28 _sizebuf((struct buffer *)&(buf), (buf).d + __bufcat_size__, sizeof((buf).b)); \
29 memcpy((buf).b + (buf).d, (new), (__bufcat_size__) * sizeof(*((buf).b))); \
30 (buf).d += __bufcat_size__; \
31} while(0)
90f6e953 32#define bufcatstr(buf, str) \
f0bbedf7
FT
33do { \
34 char *__buf__; \
35 __buf__ = (str); \
36 bufcat((buf), __buf__, strlen(__buf__)); \
37} while(0)
90f6e953
FT
38#define bufcatstr2(buf, str) \
39do { \
40 char *__buf__; \
41 __buf__ = (str); \
42 bufcat((buf), __buf__, strlen(__buf__) + 1); \
43} while(0)
44#define bufeat(buf, len) memmove((buf).b, (buf).b + (len), (buf).d -= (len))
f0bbedf7
FT
45
46struct buffer {
47 void *b;
48 size_t s, d;
49};
50
51#define typedbuf(type) struct {type *b; size_t s, d;}
52
53struct charbuf {
54 char *b;
55 size_t s, d;
56};
57
58struct charvbuf {
59 char **b;
60 size_t s, d;
61};
62
59c94590
FT
63struct btree {
64 struct btree *l, *r;
65 int h;
66 void *d;
67};
68
f0bbedf7 69void _sizebuf(struct buffer *buf, size_t wanted, size_t el);
90f6e953
FT
70char *decstr(char **p, size_t *len);
71char *vsprintf2(char *format, va_list al);
72char *sprintf2(char *format, ...);
73char *sprintf3(char *format, ...);
9aee4cbc 74off_t atoo(char *n);
f2b26d44
FT
75char **tokenize(char *src);
76void freeca(char **ca);
d1bd343c 77int calen(char **a);
d422fdfd
FT
78void bvprintf(struct charbuf *buf, char *format, va_list al);
79void bprintf(struct charbuf *buf, char *format, ...);
5fc1bf9f 80void replstr(char **p, char *n);
be6e50bf
FT
81char *base64encode(char *data, size_t datalen);
82char *base64decode(char *data, size_t *datalen);
59c94590
FT
83int bbtreedel(struct btree **tree, void *item, int (*cmp)(void *, void *));
84void freebtree(struct btree **tree, void (*ffunc)(void *));
85int bbtreeput(struct btree **tree, void *item, int (*cmp)(void *, void *));
86void *btreeget(struct btree *tree, void *key, int (*cmp)(void *, void *));
f0bbedf7
FT
87
88#endif