| 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 | |
| 13 | char *vsprintf2(char *format, va_list al); |
| 14 | char *sprintf2(char *format, ...); |
| 15 | wchar_t *vswprintf2(wchar_t *format, va_list al); |
| 16 | wchar_t *swprintf2(wchar_t *format, ...); |
| 17 | wchar_t *icmbstowcs(char *mbs, char *charset); |
| 18 | wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def); |
| 19 | char *icwcstombs(wchar_t *wcs, char *charset); |
| 20 | char *icswcstombs(wchar_t *wcs, char *charset, char *def); |
| 21 | wchar_t *wcstolower(wchar_t *wcs); |
| 22 | void _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) \ |
| 27 | do { \ |
| 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) \ |
| 32 | do { \ |
| 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 |