]> mj.ucw.cz Git - libucw.git/blob - ucw/stkstring.c
e501ba20fabe4d47f04e892d47a2d7b4c45e0214
[libucw.git] / ucw / stkstring.c
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 #include "ucw/lib.h"
13 #include "ucw/stkstring.h"
14 #include "ucw/string.h"
15
16 #include <stdio.h>
17
18 #ifdef CONFIG_DARWIN
19 uns
20 strnlen(const char *str, uns n)
21 {
22   const char *end = str + n;
23   const char *c;
24   for (c = str; *c && c < end; c++);
25   return c - str;
26 }
27 #endif
28
29 uns
30 stk_array_len(char **s, uns cnt)
31 {
32   uns l = 1;
33   while (cnt--)
34     l += strlen(*s++);
35   return l;
36 }
37
38 void
39 stk_array_join(char *x, char **s, uns cnt, uns sep)
40 {
41   while (cnt--)
42     {
43       uns l = strlen(*s);
44       memcpy(x, *s, l);
45       x += l;
46       s++;
47       if (sep && cnt)
48         *x++ = sep;
49     }
50   *x = 0;
51 }
52
53 uns
54 stk_printf_internal(const char *fmt, ...)
55 {
56   uns len = 256;
57   char *buf = alloca(len);
58   va_list args, args2;
59   va_start(args, fmt);
60   for (;;)
61     {
62       va_copy(args2, args);
63       int l = vsnprintf(buf, len, fmt, args2);
64       va_end(args2);
65       if (l < 0)
66         len *= 2;
67       else
68         {
69           va_end(args);
70           return l+1;
71         }
72       buf = alloca(len);
73     }
74 }
75
76 uns
77 stk_vprintf_internal(const char *fmt, va_list args)
78 {
79   uns len = 256;
80   char *buf = alloca(len);
81   va_list args2;
82   for (;;)
83     {
84       va_copy(args2, args);
85       int l = vsnprintf(buf, len, fmt, args2);
86       va_end(args2);
87       if (l < 0)
88         len *= 2;
89       else
90         {
91           va_end(args);
92           return l+1;
93         }
94       buf = alloca(len);
95     }
96 }
97
98 void
99 stk_hexdump_internal(char *dst, const byte *src, uns n)
100 {
101   mem_to_hex(dst, src, n, ' ');
102 }
103
104 void
105 stk_fsize_internal(char *buf, u64 x)
106 {
107   if (x < 1<<10)
108     sprintf(buf, "%dB", (int)x);
109   else if (x < 10<<10)
110     sprintf(buf, "%.1fK", (double)x/(1<<10));
111   else if (x < 1<<20)
112     sprintf(buf, "%dK", (int)(x/(1<<10)));
113   else if (x < 10<<20)
114     sprintf(buf, "%.1fM", (double)x/(1<<20));
115   else if (x < 1<<30)
116     sprintf(buf, "%dM", (int)(x/(1<<20)));
117   else if (x < (u64)10<<30)
118     sprintf(buf, "%.1fG", (double)x/(1<<30));
119   else if (x != ~(u64)0)
120     sprintf(buf, "%dG", (int)(x/(1<<30)));
121   else
122     strcpy(buf, "unknown");
123 }
124
125 #ifdef TEST
126
127 int main(void)
128 {
129   char *a = stk_strndup("are!",3);
130   a = stk_strcat(a, " the ");
131   a = stk_strmulticat(a, stk_strdup("Jabberwock, "), "my", NULL);
132   char *arr[] = { a, " son" };
133   a = stk_strarraycat(arr, 2);
134   a = stk_printf("Bew%s!", a);
135   puts(a);
136   puts(stk_hexdump(a, 3));
137   char *ary[] = { "The", "jaws", "that", "bite" };
138   puts(stk_strjoin(ary, 4, ' '));
139   puts(stk_fsize(1234567));
140   return 0;
141 }
142
143 #endif