]> mj.ucw.cz Git - libucw.git/blob - lib/stkstring.h
75508fbb74cde3a64d210d6c890eff9a8d32b1b0
[libucw.git] / lib / stkstring.h
1 /*
2  *      UCW Library -- Strings Allocated on the Stack
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <alloca.h>
11 #include <string.h>
12
13 #define stk_strdup(s) ({ char *_s=(s); uns _l=strlen(_s)+1; char *_x=alloca(_l); memcpy(_x, _s, _l); _x; })
14 #define stk_strcat(s1,s2) ({ char *_s1=(s1); char *_s2=(s2); uns _l1=strlen(_s1); uns _l2=strlen(_s2); char *_x=alloca(_l1+_l2+1); memcpy(_x,_s1,_l1); memcpy(_x+_l1,_s2,_l2+1); _x; })
15 #define stk_strmulticat(s...) ({ char *_s[]={s}; char *_x=alloca(stk_array_len(_s, ARRAY_SIZE(_s)-1)); stk_array_copy(_x, _s, ARRAY_SIZE(_s)-1); _x; })
16 #define stk_strarraycat(s,n) ({ char **_s=(s); int _n=(n); char *_x=alloca(stk_array_len(_s,_n)); stk_array_copy(_x, _s, _n); _x; })
17 #define stk_printf(f...) ({ uns _l=stk_printf_internal(f); char *_x=alloca(_l); memcpy(_x, stk_printf_buf, _l); _x; })
18
19 uns stk_array_len(char **s, uns cnt);
20 void stk_array_copy(char *x, char **s, uns cnt);
21 uns stk_printf_internal(char *x, ...);
22
23 extern char *stk_printf_buf;