Added the hubmgr.1 manpage.
[doldaconnect.git] / include / utils.h
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
302a2600 3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
d3372da9 4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19#ifndef _UTILS_H
20#define _UTILS_H
21
22#include <stdarg.h>
23#include <stdlib.h>
8b17e919 24#ifdef DAEMON
d3372da9 25#include "log.h"
8b17e919 26#endif
d3372da9 27
9ec790e8 28struct wcspair {
29 struct wcspair *next;
30 wchar_t *key;
31 wchar_t *val;
32};
33
ba087e49
FT
34struct strpair {
35 struct strpair *next;
36 char *key;
37 char *val;
38};
8b17e919 39
d3372da9 40/* "Safe" functions */
8b17e919 41#ifdef DAEMON
12e320a0 42#define LOGOOM(size) flog(LOG_CRIT, "%s (%s:%i): out of memory (alloc %zi)", __FUNCTION__, __FILE__, __LINE__, (size))
43#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM((ssize_t)(size)); abort(); (void *)0;}):__result__;})
44#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM((ssize_t)(size)); abort(); (void *)0;}):__result__;})
4344b8c8 45#define swcsdup(wcs) ({wchar_t *__eval__; __eval__ = (wcs); (wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(__eval__) + 1)), __eval__);})
d3372da9 46#define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
8b17e919 47#else
48#define LOGOOM(size)
49#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
50#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
51#define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
52#define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
53#endif
d3372da9 54
55#define CBCHAIN(name, args...) \
56struct cbchain_ ## name { \
57 struct cbchain_ ## name *next, *prev; \
58 int (*func)(args, void *data); \
59 void (*destroy)(void *data); \
60 void *data; \
61} * name
62
63#define GCBCHAIN(name, args...) \
64struct cbchain_ ## name * name = NULL
65
66#define EGCBCHAIN(name, args...) \
67extern struct cbchain_ ## name { \
68 struct cbchain_ ## name *next, *prev; \
69 int (*func)(args, void *data); \
70 void *data; \
71} * name
72
d3372da9 73char *vsprintf2(char *format, va_list al);
74char *sprintf2(char *format, ...)
735f4b1a 75#if defined(__GNUC__)
d3372da9 76 __attribute__ ((format (printf, 1, 2)))
77#endif
d3372da9 78;
79wchar_t *vswprintf2(wchar_t *format, va_list al);
80wchar_t *swprintf2(wchar_t *format, ...);
6a3ef546 81int havecharset(char *charset);
d3372da9 82wchar_t *icmbstowcs(char *mbs, char *charset);
83wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
84char *icwcstombs(wchar_t *wcs, char *charset);
85char *icswcstombs(wchar_t *wcs, char *charset, char *def);
86wchar_t *wcstolower(wchar_t *wcs);
87wchar_t ucptowc(int ucp);
88void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
89double ntime(void);
90int wcsexists(wchar_t *h, wchar_t *n);
91#ifndef HAVE_WCSCASECMP
92int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
93#endif
94char *hexencode(char *data, size_t datalen);
95char *hexdecode(char *data, size_t *len);
96char *base64encode(char *data, size_t datalen);
97char *base64decode(char *data, size_t *datalen);
98char *base32encode(char *data, size_t datalen);
99char *base32decode(char *data, size_t *datalen);
100void _freeparr(void **arr);
101int _parrlen(void **arr);
7a83517c 102char *findfile(char *name, char *homedir, int filldef);
ba087e49
FT
103struct strpair *newstrpair(char *key, char *val, struct strpair **list);
104void freestrpair(struct strpair *pair, struct strpair **list);
105char *spfind(struct strpair *list, char *key);
9ec790e8 106struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list);
107void freewcspair(struct wcspair *pair, struct wcspair **list);
14a9e5fd 108wchar_t *wpfind(struct wcspair *list, wchar_t *key);
d3372da9 109
e0cbc756
FT
110#define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(void *)(b), (bs), (rs), (es), (a))
111#define sizebuf2(b, rs, a) _sizebuf((void **)(void *)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
d3372da9 112#define addtobuf(b, c) \
113do { \
e0cbc756 114 _sizebuf((void **)(void *)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
d3372da9 115 (b)[(b ## data)++] = (c); \
116} while(0)
117#define bufcat(d, s, n) \
118do { \
119 size_t __bufcat_size__; \
120 __bufcat_size__ = (n); \
e0cbc756 121 _sizebuf((void **)(void *)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
d3372da9 122 memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
123 (d ## data) += __bufcat_size__; \
124} while (0)
c660fffa
FT
125#define bprintf(b, fmt...) \
126 do { \
127 char *__bprintf_dest__; \
128 __bprintf_dest__ = sprintf2(fmt); \
129 bufcat(b, __bprintf_dest__, strlen(__bprintf_dest__)); \
130 free(__bprintf_dest__); \
131 } while(0)
d3372da9 132
133#define freeparr(parr) _freeparr((void **)(parr))
134#define parrlen(parr) _parrlen((void **)(parr))
135
136#define CBREG(obj, name, funca, destroya, dataa) \
137do { \
138 struct cbchain_ ## name *__new_cb__; \
139 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
140 __new_cb__->func = funca; \
141 __new_cb__->destroy = destroya; \
142 __new_cb__->data = dataa; \
143 __new_cb__->prev = NULL; \
144 __new_cb__->next = (obj)->name; \
5c851d43 145 if((obj)->name != NULL) { \
146 (obj)->name->prev = __new_cb__; \
147 } \
d3372da9 148 (obj)->name = __new_cb__; \
149} while(0)
150
cab0b442 151#define CBUNREG(obj, name, dataa) \
152do { \
d3372da9 153 struct cbchain_ ## name *__cur__; \
154 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
cab0b442 155 if(__cur__->data == (dataa)) { \
d3372da9 156 if(__cur__->destroy != NULL) \
157 __cur__->destroy(__cur__->data); \
158 if(__cur__->prev != NULL) \
159 __cur__->prev->next = __cur__->next; \
160 if(__cur__->next != NULL) \
161 __cur__->next->prev = __cur__->prev; \
162 if(__cur__ == (obj)->name) \
163 (obj)->name = __cur__->next; \
164 free(__cur__); \
165 break; \
166 } \
167 } \
cab0b442 168} while(0)
d3372da9 169
170#define GCBREG(name, funca, dataa) \
171do { \
172 struct cbchain_ ## name *__new_cb__; \
173 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
174 __new_cb__->func = funca; \
175 __new_cb__->data = dataa; \
176 __new_cb__->prev = NULL; \
177 __new_cb__->next = name; \
5c851d43 178 if(name != NULL) { \
179 name->prev = __new_cb__; \
180 } \
d3372da9 181 name = __new_cb__; \
182} while(0)
183
184#define CBCHAININIT(obj, name) (obj)->name = NULL
185
186#define CBCHAINFREE(obj, name) \
187do { \
188 struct cbchain_ ## name *__cur__; \
189 while((__cur__ = (obj)->name) != NULL) { \
190 (obj)->name = __cur__->next; \
cab0b442 191 if(__cur__->destroy != NULL) \
192 __cur__->destroy(__cur__->data); \
193 free(__cur__); \
d3372da9 194 } \
195} while(0)
196
197#define CBCHAINDOCB(obj, name, args...) \
198do { \
199 struct cbchain_ ## name *__cur__, *__next__; \
200 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
201 __next__ = __cur__->next; \
cab0b442 202 if(__cur__->func(args, __cur__->data)) { \
d3372da9 203 if(__cur__->next != NULL) \
204 __cur__->next->prev = __cur__->prev; \
205 if(__cur__->prev != NULL) \
206 __cur__->prev->next = __cur__->next; \
207 if(__cur__ == (obj)->name) \
208 (obj)->name = __cur__->next; \
209 free(__cur__); \
210 } \
211 } \
212} while(0)
213
214#define GCBCHAINDOCB(name, args...) \
215({ \
216 struct cbchain_ ## name *__cur__; \
217 int __ret__; \
218 __ret__ = 0; \
219 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
220 if(__cur__->func(args, __cur__->data)) { \
221 __ret__ = 1; \
222 break; \
223 } \
224 } \
225 __ret__; \
226})
227
228#endif