| 1 | #ifndef _UTILS_H |
| 2 | #define _UTILS_H |
| 3 | |
| 4 | #include <sys/types.h> |
| 5 | #include <stdint.h> |
| 6 | #include <stdarg.h> |
| 7 | |
| 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 szmalloc(size) memset(smalloc(size), 0, size) |
| 11 | #define sstrdup(str) ({char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);}) |
| 12 | #define omalloc(o) ((o) = szmalloc(sizeof(*(o)))) |
| 13 | |
| 14 | #define bufinit(buf) memset(&(buf), 0, sizeof(buf)) |
| 15 | #define buffree(buf) do { if((buf).b != NULL) {free((buf).b);} } while(0) |
| 16 | #define bufdupc(buf) memcpy(smalloc((buf).d * sizeof(*((buf).b))), (buf).b, (buf).d * sizeof(*((buf).b))) |
| 17 | #define sizebuf(buf, wanted) (_sizebuf((struct buffer *)&(buf), (wanted), sizeof(*((buf).b)))) |
| 18 | #define sizebuf2(buf, wanted) (_sizebuf2((void **)&(buf), &(buf ## size), (wanted), sizeof(*(buf)))) |
| 19 | #define bufdel(buf, i) (memmove((buf).b + (i), (buf).b + (i) + 1, (--((buf).d) - (i)) * sizeof(*((buf).b)))) |
| 20 | #define bufadd(buf, new) \ |
| 21 | do { \ |
| 22 | _sizebuf((struct buffer *)&(buf), (buf).d + 1, sizeof(*((buf).b))); \ |
| 23 | (buf).b[(buf).d++] = (new); \ |
| 24 | } while(0) |
| 25 | #define bufcat(buf, new, size) \ |
| 26 | do { \ |
| 27 | size_t __bufcat_size__; \ |
| 28 | __bufcat_size__ = (size); \ |
| 29 | _sizebuf((struct buffer *)&(buf), (buf).d + __bufcat_size__, sizeof((buf).b)); \ |
| 30 | memcpy((buf).b + (buf).d, (new), (__bufcat_size__) * sizeof(*((buf).b))); \ |
| 31 | (buf).d += __bufcat_size__; \ |
| 32 | } while(0) |
| 33 | #define bufcatnum(buf, num, type) \ |
| 34 | do { \ |
| 35 | type __buf__; \ |
| 36 | __buf__ = (num); \ |
| 37 | bufcat((buf), &__buf__, sizeof(type)); \ |
| 38 | } while(0) |
| 39 | #define bufcatuid(buf, uid) \ |
| 40 | do { \ |
| 41 | uniqid_t __buf__; \ |
| 42 | __buf__ = (uid); \ |
| 43 | bufcat((buf), &__buf__, sizeof(__buf__)); \ |
| 44 | } while(0) |
| 45 | #define bufcatstr(buf, str) \ |
| 46 | do { \ |
| 47 | char *__buf__; \ |
| 48 | __buf__ = (str); \ |
| 49 | bufcat((buf), __buf__, strlen(__buf__) + 1); \ |
| 50 | } while(0) |
| 51 | #define bufcatstr2(buf, str) \ |
| 52 | do { \ |
| 53 | char *__buf__; \ |
| 54 | __buf__ = (str); \ |
| 55 | bufcat((buf), __buf__, strlen(__buf__)); \ |
| 56 | } while(0) |
| 57 | #define bufcats(buf, d) bufcat((buf), &(d), sizeof(d)) |
| 58 | #define bufeat(buf, len) \ |
| 59 | do { \ |
| 60 | size_t __bufeat_size__; \ |
| 61 | __bufeat_size__ = (len); \ |
| 62 | memmove((buf).b, (buf).b + __bufeat_size__, (buf).d -= __bufeat_size__); \ |
| 63 | } while(0) |
| 64 | |
| 65 | struct buffer { |
| 66 | void *b; |
| 67 | size_t s, d; |
| 68 | }; |
| 69 | |
| 70 | struct intbuf { |
| 71 | int *b; |
| 72 | size_t s, d; |
| 73 | }; |
| 74 | |
| 75 | struct charbuf { |
| 76 | char *b; |
| 77 | size_t s, d; |
| 78 | }; |
| 79 | |
| 80 | struct charvbuf { |
| 81 | char **b; |
| 82 | size_t s, d; |
| 83 | }; |
| 84 | |
| 85 | struct ptrbuf { |
| 86 | void **b; |
| 87 | size_t s, d; |
| 88 | }; |
| 89 | |
| 90 | void _sizebuf(struct buffer *buf, size_t wanted, size_t el); |
| 91 | void _sizebuf2(void **buf, size_t *sz, size_t wanted, size_t el); |
| 92 | char *vsprintf2(char *format, va_list al); |
| 93 | char *sprintf2(char *format, ...); |
| 94 | |
| 95 | #endif |