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