python: Added PDM support in scgi-wsgi{,3}.
[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 18#define sizebuf(buf, wanted) (_sizebuf((struct buffer *)&(buf), (wanted), sizeof(*((buf).b))))
dee4e642 19#define bufdel(buf, i) (memmove((buf).b + (i), (buf).b + (i) + 1, (--((buf).d) - (i)) * sizeof(*((buf).b))))
f0bbedf7
FT
20#define bufadd(buf, new) \
21do { \
22 _sizebuf((struct buffer *)&(buf), (buf).d + 1, sizeof(*((buf).b))); \
23 (buf).b[(buf).d++] = (new); \
24} while(0)
25#define bufcat(buf, new, size) \
26do { \
27 size_t __bufcat_size__; \
28 __bufcat_size__ = (size); \
29 _sizebuf((struct buffer *)&(buf), (buf).d + __bufcat_size__, sizeof((buf).b)); \
30 memcpy((buf).b + (buf).d, (new), (__bufcat_size__) * sizeof(*((buf).b))); \
31 (buf).d += __bufcat_size__; \
32} while(0)
90f6e953 33#define bufcatstr(buf, str) \
f0bbedf7
FT
34do { \
35 char *__buf__; \
36 __buf__ = (str); \
37 bufcat((buf), __buf__, strlen(__buf__)); \
38} while(0)
90f6e953
FT
39#define bufcatstr2(buf, str) \
40do { \
41 char *__buf__; \
42 __buf__ = (str); \
43 bufcat((buf), __buf__, strlen(__buf__) + 1); \
44} while(0)
45#define bufeat(buf, len) memmove((buf).b, (buf).b + (len), (buf).d -= (len))
f0bbedf7
FT
46
47struct buffer {
48 void *b;
49 size_t s, d;
50};
51
52#define typedbuf(type) struct {type *b; size_t s, d;}
53
54struct charbuf {
55 char *b;
56 size_t s, d;
57};
58
59struct charvbuf {
60 char **b;
61 size_t s, d;
62};
63
59c94590
FT
64struct btree {
65 struct btree *l, *r;
66 int h;
67 void *d;
68};
69
f0bbedf7 70void _sizebuf(struct buffer *buf, size_t wanted, size_t el);
90f6e953
FT
71char *decstr(char **p, size_t *len);
72char *vsprintf2(char *format, va_list al);
73char *sprintf2(char *format, ...);
74char *sprintf3(char *format, ...);
9aee4cbc 75off_t atoo(char *n);
f2b26d44
FT
76char **tokenize(char *src);
77void freeca(char **ca);
d1bd343c 78int calen(char **a);
d422fdfd
FT
79void bvprintf(struct charbuf *buf, char *format, va_list al);
80void bprintf(struct charbuf *buf, char *format, ...);
5fc1bf9f 81void replstr(char **p, char *n);
be6e50bf
FT
82char *base64encode(char *data, size_t datalen);
83char *base64decode(char *data, size_t *datalen);
59c94590
FT
84int bbtreedel(struct btree **tree, void *item, int (*cmp)(void *, void *));
85void freebtree(struct btree **tree, void (*ffunc)(void *));
86int bbtreeput(struct btree **tree, void *item, int (*cmp)(void *, void *));
87void *btreeget(struct btree *tree, void *key, int (*cmp)(void *, void *));
f0bbedf7
FT
88
89#endif