Remove check for libdl.
[doldaconnect.git] / include / utils.h
CommitLineData
d3372da9 1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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>
24#include <malloc.h>
8b17e919 25#ifdef DAEMON
d3372da9 26#include "log.h"
8b17e919 27#endif
d3372da9 28
9ec790e8 29struct wcspair {
30 struct wcspair *next;
31 wchar_t *key;
32 wchar_t *val;
33};
34
8b17e919 35
d3372da9 36/* "Safe" functions */
8b17e919 37#ifdef DAEMON
38#define LOGOOM(size) flog(LOG_CRIT, "%s (%s:%i): out of memory (alloc %i)", __FUNCTION__, __FILE__, __LINE__, (size))
d3372da9 39#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
40#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({LOGOOM(size); abort(); (void *)0;}):__result__;})
41#define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
42#define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
8b17e919 43#else
44#define LOGOOM(size)
45#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
46#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
47#define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
48#define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
49#endif
d3372da9 50
51#define CBCHAIN(name, args...) \
52struct cbchain_ ## name { \
53 struct cbchain_ ## name *next, *prev; \
54 int (*func)(args, void *data); \
55 void (*destroy)(void *data); \
56 void *data; \
57} * name
58
59#define GCBCHAIN(name, args...) \
60struct cbchain_ ## name * name = NULL
61
62#define EGCBCHAIN(name, args...) \
63extern struct cbchain_ ## name { \
64 struct cbchain_ ## name *next, *prev; \
65 int (*func)(args, void *data); \
66 void *data; \
67} * name
68
69extern int vswprintf (wchar_t *__restrict __s, size_t __n,
70 __const wchar_t *__restrict __format,
71 __gnuc_va_list __arg);
72extern int swprintf (wchar_t *__restrict __s, size_t __n,
73 __const wchar_t *__restrict __format, ...);
74
75char *vsprintf2(char *format, va_list al);
76char *sprintf2(char *format, ...)
77#if defined(__GNUC__) && 0
78 __attribute__ ((format (printf, 1, 2)))
79#endif
d3372da9 80;
81wchar_t *vswprintf2(wchar_t *format, va_list al);
82wchar_t *swprintf2(wchar_t *format, ...);
6a3ef546 83int havecharset(char *charset);
d3372da9 84wchar_t *icmbstowcs(char *mbs, char *charset);
85wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
86char *icwcstombs(wchar_t *wcs, char *charset);
87char *icswcstombs(wchar_t *wcs, char *charset, char *def);
88wchar_t *wcstolower(wchar_t *wcs);
89wchar_t ucptowc(int ucp);
90void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
91double ntime(void);
92int wcsexists(wchar_t *h, wchar_t *n);
93#ifndef HAVE_WCSCASECMP
94int wcscasecmp(const wchar_t *s1, const wchar_t *s2);
95#endif
96char *hexencode(char *data, size_t datalen);
97char *hexdecode(char *data, size_t *len);
98char *base64encode(char *data, size_t datalen);
99char *base64decode(char *data, size_t *datalen);
100char *base32encode(char *data, size_t datalen);
101char *base32decode(char *data, size_t *datalen);
102void _freeparr(void **arr);
103int _parrlen(void **arr);
7a83517c 104char *findfile(char *name, char *homedir, int filldef);
9ec790e8 105struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list);
106void freewcspair(struct wcspair *pair, struct wcspair **list);
14a9e5fd 107wchar_t *wpfind(struct wcspair *list, wchar_t *key);
d3372da9 108
109#define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(b), (bs), (rs), (es), (a))
110#define sizebuf2(b, rs, a) _sizebuf((void **)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
111#define addtobuf(b, c) \
112do { \
113 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
114 (b)[(b ## data)++] = (c); \
115} while(0)
116#define bufcat(d, s, n) \
117do { \
118 size_t __bufcat_size__; \
119 __bufcat_size__ = (n); \
120 _sizebuf((void **)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
121 memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
122 (d ## data) += __bufcat_size__; \
123} while (0)
124
125#define freeparr(parr) _freeparr((void **)(parr))
126#define parrlen(parr) _parrlen((void **)(parr))
127
128#define CBREG(obj, name, funca, destroya, dataa) \
129do { \
130 struct cbchain_ ## name *__new_cb__; \
131 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
132 __new_cb__->func = funca; \
133 __new_cb__->destroy = destroya; \
134 __new_cb__->data = dataa; \
135 __new_cb__->prev = NULL; \
136 __new_cb__->next = (obj)->name; \
5c851d43 137 if((obj)->name != NULL) { \
138 (obj)->name->prev = __new_cb__; \
139 } \
d3372da9 140 (obj)->name = __new_cb__; \
141} while(0)
142
cab0b442 143#define CBUNREG(obj, name, dataa) \
144do { \
d3372da9 145 struct cbchain_ ## name *__cur__; \
146 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __cur__->next) { \
cab0b442 147 if(__cur__->data == (dataa)) { \
d3372da9 148 if(__cur__->destroy != NULL) \
149 __cur__->destroy(__cur__->data); \
150 if(__cur__->prev != NULL) \
151 __cur__->prev->next = __cur__->next; \
152 if(__cur__->next != NULL) \
153 __cur__->next->prev = __cur__->prev; \
154 if(__cur__ == (obj)->name) \
155 (obj)->name = __cur__->next; \
156 free(__cur__); \
157 break; \
158 } \
159 } \
cab0b442 160} while(0)
d3372da9 161
162#define GCBREG(name, funca, dataa) \
163do { \
164 struct cbchain_ ## name *__new_cb__; \
165 __new_cb__ = smalloc(sizeof(*__new_cb__)); \
166 __new_cb__->func = funca; \
167 __new_cb__->data = dataa; \
168 __new_cb__->prev = NULL; \
169 __new_cb__->next = name; \
5c851d43 170 if(name != NULL) { \
171 name->prev = __new_cb__; \
172 } \
d3372da9 173 name = __new_cb__; \
174} while(0)
175
176#define CBCHAININIT(obj, name) (obj)->name = NULL
177
178#define CBCHAINFREE(obj, name) \
179do { \
180 struct cbchain_ ## name *__cur__; \
181 while((__cur__ = (obj)->name) != NULL) { \
182 (obj)->name = __cur__->next; \
cab0b442 183 if(__cur__->destroy != NULL) \
184 __cur__->destroy(__cur__->data); \
185 free(__cur__); \
d3372da9 186 } \
187} while(0)
188
189#define CBCHAINDOCB(obj, name, args...) \
190do { \
191 struct cbchain_ ## name *__cur__, *__next__; \
192 for(__cur__ = (obj)->name; __cur__ != NULL; __cur__ = __next__) { \
193 __next__ = __cur__->next; \
cab0b442 194 if(__cur__->func(args, __cur__->data)) { \
d3372da9 195 if(__cur__->next != NULL) \
196 __cur__->next->prev = __cur__->prev; \
197 if(__cur__->prev != NULL) \
198 __cur__->prev->next = __cur__->next; \
199 if(__cur__ == (obj)->name) \
200 (obj)->name = __cur__->next; \
201 free(__cur__); \
202 } \
203 } \
204} while(0)
205
206#define GCBCHAINDOCB(name, args...) \
207({ \
208 struct cbchain_ ## name *__cur__; \
209 int __ret__; \
210 __ret__ = 0; \
211 for(__cur__ = name; __cur__ != NULL; __cur__ = __cur__->next) { \
212 if(__cur__->func(args, __cur__->data)) { \
213 __ret__ = 1; \
214 break; \
215 } \
216 } \
217 __ret__; \
218})
219
220#endif