]> mj.ucw.cz Git - libucw.git/blob - ucw/stkstring.h
Released as 6.5.16.
[libucw.git] / ucw / stkstring.h
1 /*
2  *      UCW Library -- Strings Allocated on the Stack
3  *
4  *      (c) 2005--2007 Martin Mares <mj@ucw.cz>
5  *      (c) 2005 Tomas Valla <tom@ucw.cz>
6  *      (c) 2008 Pavel Charvat <pchar@ucw.cz>
7  *
8  *      This software may be freely distributed and used according to the terms
9  *      of the GNU Lesser General Public License.
10  */
11
12 #ifndef _UCW_STKSTRING_H
13 #define _UCW_STKSTRING_H
14
15 #include <alloca.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <ucw/string.h>
19
20 #ifdef CONFIG_UCW_CLEAN_ABI
21 #define stk_array_join ucw_stk_array_join
22 #define stk_array_len ucw_stk_array_len
23 #define stk_fsize_internal ucw_stk_fsize_internal
24 #define stk_hexdump_internal ucw_stk_hexdump_internal
25 #define stk_printf_internal ucw_stk_printf_internal
26 #define stk_vprintf_internal ucw_stk_vprintf_internal
27 #endif
28
29 #define stk_strdup(s) ({ const char *_s=(s); uint _l=strlen(_s)+1; char *_x=alloca(_l); memcpy(_x, _s, _l); _x; })
30 #define stk_strndup(s,n) ({ const char *_s=(s); uint _l=strnlen(_s,(n)); char *_x=alloca(_l+1); memcpy(_x, _s, _l); _x[_l]=0; _x; })
31 #define stk_strcat(s1,s2) ({ const char *_s1=(s1); const char *_s2=(s2); uint _l1=strlen(_s1); uint _l2=strlen(_s2); char *_x=alloca(_l1+_l2+1); memcpy(_x,_s1,_l1); memcpy(_x+_l1,_s2,_l2+1); _x; })
32 #define stk_strmulticat(s...) ({ char *_s[]={s}; char *_x=alloca(stk_array_len(_s, ARRAY_SIZE(_s)-1)); stk_array_join(_x, _s, ARRAY_SIZE(_s)-1, 0); _x; })
33 #define stk_strarraycat(s,n) ({ char **_s=(s); int _n=(n); char *_x=alloca(stk_array_len(_s,_n)); stk_array_join(_x, _s, _n, 0); _x; })
34 #define stk_strjoin(s,n,sep) ({ char **_s=(s); int _n=(n); char *_x=alloca(stk_array_len(_s,_n)+_n-1); stk_array_join(_x, _s, _n, (sep)); _x; })
35 #define stk_printf(f...) ({ uint _l=stk_printf_internal(f); char *_x=alloca(_l); sprintf(_x, f); _x; })
36 #define stk_vprintf(f, args) ({ uint _l=stk_vprintf_internal(f, args); char *_x=alloca(_l); vsprintf(_x, f, args); _x; })
37 #define stk_hexdump(s,n) ({ uint _n=(n); char *_x=alloca(3*_n+1); stk_hexdump_internal(_x,(char*)(s),_n); _x; })
38 #define stk_str_unesc(s) ({ const char *_s=(s); char *_d=alloca(strlen(_s)+1); str_unesc(_d, _s); _d; })
39 #define stk_fsize(n) ({ char *_s=alloca(16); stk_fsize_internal(_s, n); _s; })
40
41 uint stk_array_len(char **s, uint cnt);
42 void stk_array_join(char *x, char **s, uint cnt, uint sep);
43 uint stk_printf_internal(const char *x, ...) FORMAT_CHECK(printf,1,2);
44 uint stk_vprintf_internal(const char *x, va_list args);
45 void stk_hexdump_internal(char *dst, const byte *src, uint n);
46 void stk_fsize_internal(char *dst, u64 size);
47
48 #endif