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