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