Autoupdated.
[doldaconnect.git] / include / doldaconnect / utils.h
... / ...
CommitLineData
1#ifndef _UTILS_H
2#define _UTILS_H
3
4#include <stdarg.h>
5#include <stdlib.h>
6
7/* "Safe" functions */
8#define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
9#define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
10#define swcsdup(wcs) ((wchar_t *)wcscpy(smalloc(sizeof(wchar_t) * (wcslen(wcs) + 1)), (wcs)))
11#define sstrdup(str) ((char *)strcpy(smalloc(strlen(str) + 1), (str)))
12
13char *vsprintf2(char *format, va_list al);
14char *sprintf2(char *format, ...);
15wchar_t *vswprintf2(wchar_t *format, va_list al);
16wchar_t *swprintf2(wchar_t *format, ...);
17wchar_t *icmbstowcs(char *mbs, char *charset);
18wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def);
19char *icwcstombs(wchar_t *wcs, char *charset);
20char *icswcstombs(wchar_t *wcs, char *charset, char *def);
21wchar_t *wcstolower(wchar_t *wcs);
22void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo);
23
24#define sizebuf(b, bs, rs, es, a) _sizebuf((void **)(b), (bs), (rs), (es), (a))
25#define sizebuf2(b, rs, a) _sizebuf((void **)(&(b)), &(b ## size), (rs), sizeof(*(b)), (a))
26#define addtobuf(b, c) \
27do { \
28 _sizebuf((void **)(&(b)), &(b ## size), (b ## data) + 1, sizeof(*(b)), 1); \
29 (b)[(b ## data)++] = (c); \
30} while(0)
31#define bufcat(d, s, n) \
32do { \
33 size_t __bufcat_size__; \
34 __bufcat_size__ = (n); \
35 _sizebuf((void **)(&(d)), &(d ## size), (d ## data) + __bufcat_size__, sizeof(*(d)), 1); \
36 memcpy((d) + (d ## data), (s), sizeof(*(d)) * __bufcat_size__); \
37 (d ## data) += __bufcat_size__; \
38} while (0)
39
40#endif