]> arthur.barton.de Git - netatalk.git/blob - include/atalk/bstrlib.h
Writing metadata xattr on directories with sticky bit set, FR#94
[netatalk.git] / include / atalk / bstrlib.h
1 /*
2  * This source file is part of the bstring string library.  This code was
3  * written by Paul Hsieh in 2002-2008, and is covered by the BSD open source 
4  * license and the GPL. Refer to the accompanying documentation for details 
5  * on usage and license.
6  */
7
8 /*!
9  * @file
10  * This file is the core module for implementing the bstring functions.
11  */
12
13 #ifndef BSTRLIB_INCLUDE
14 #define BSTRLIB_INCLUDE
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <stdarg.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <ctype.h>
24
25 #if !defined (BSTRLIB_VSNP_OK) && !defined (BSTRLIB_NOVSNP)
26 # if defined (__TURBOC__) && !defined (__BORLANDC__)
27 #  define BSTRLIB_NOVSNP
28 # endif
29 #endif
30
31 #define BSTR_ERR (-1)
32 #define BSTR_OK (0)
33 #define BSTR_BS_BUFF_LENGTH_GET (0)
34
35 typedef struct tagbstring * bstring;
36 typedef const struct tagbstring * const_bstring;
37
38 /* Copy functions */
39 #define cstr2bstr bfromcstr
40 extern bstring bfromcstr (const char * str);
41 extern bstring bfromcstralloc (int mlen, const char * str);
42 extern bstring blk2bstr (const void * blk, int len);
43 extern char * bstr2cstr (const_bstring s, char z);
44 extern int bcstrfree (char * s);
45 extern bstring bstrcpy (const_bstring b1);
46 extern int bassign (bstring a, const_bstring b);
47 extern int bassignmidstr (bstring a, const_bstring b, int left, int len);
48 extern int bassigncstr (bstring a, const char * str);
49 extern int bassignblk (bstring a, const void * s, int len);
50
51 /* Destroy function */
52 extern int bdestroy (bstring b);
53
54 /* Space allocation hinting functions */
55 extern int balloc (bstring s, int len);
56 extern int ballocmin (bstring b, int len);
57
58 /* Substring extraction */
59 extern bstring bmidstr (const_bstring b, int left, int len);
60
61 /* Various standard manipulations */
62 extern int bconcat (bstring b0, const_bstring b1);
63 extern int bconchar (bstring b0, char c);
64 extern int bcatcstr (bstring b, const char * s);
65 extern int bcatblk (bstring b, const void * s, int len);
66 extern int binsert (bstring s1, int pos, const_bstring s2, unsigned char fill);
67 extern int binsertch (bstring s1, int pos, int len, unsigned char fill);
68 extern int breplace (bstring b1, int pos, int len, const_bstring b2, unsigned char fill);
69 extern int bdelete (bstring s1, int pos, int len);
70 extern int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill);
71 extern int btrunc (bstring b, int n);
72
73 /* Scan/search functions */
74 extern int bstricmp (const_bstring b0, const_bstring b1);
75 extern int bstrnicmp (const_bstring b0, const_bstring b1, int n);
76 extern int biseqcaseless (const_bstring b0, const_bstring b1);
77 extern int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len);
78 extern int biseq (const_bstring b0, const_bstring b1);
79 extern int bisstemeqblk (const_bstring b0, const void * blk, int len);
80 extern int biseqcstr (const_bstring b, const char * s);
81 extern int biseqcstrcaseless (const_bstring b, const char * s);
82 extern int bstrcmp (const_bstring b0, const_bstring b1);
83 extern int bstrncmp (const_bstring b0, const_bstring b1, int n);
84 extern int binstr (const_bstring s1, int pos, const_bstring s2);
85 extern int binstrr (const_bstring s1, int pos, const_bstring s2);
86 extern int binstrcaseless (const_bstring s1, int pos, const_bstring s2);
87 extern int binstrrcaseless (const_bstring s1, int pos, const_bstring s2);
88 extern int bstrchrp (const_bstring b, int c, int pos);
89 extern int bstrrchrp (const_bstring b, int c, int pos);
90 #define bstrchr(b,c) bstrchrp ((b), (c), 0)
91 #define bstrrchr(b,c) bstrrchrp ((b), (c), blength(b)-1)
92 extern int binchr (const_bstring b0, int pos, const_bstring b1);
93 extern int binchrr (const_bstring b0, int pos, const_bstring b1);
94 extern int bninchr (const_bstring b0, int pos, const_bstring b1);
95 extern int bninchrr (const_bstring b0, int pos, const_bstring b1);
96 extern int bfindreplace (bstring b, const_bstring find, const_bstring repl, int pos);
97 extern int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, int pos);
98
99 /* List of string container functions */
100 struct bstrList {
101     int qty, mlen;
102     bstring * entry;
103 };
104 extern struct bstrList * bstrListCreate (void);
105 extern int bstrListDestroy (struct bstrList * sl);
106 extern int bstrListAlloc (struct bstrList * sl, int msz);
107 extern int bstrListAllocMin (struct bstrList * sl, int msz);
108
109 /* String split and join functions */
110 extern struct bstrList * bsplit (const_bstring str, unsigned char splitChar);
111 extern struct bstrList * bsplits (const_bstring str, const_bstring splitStr);
112 extern struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr);
113 extern bstring bjoin (const struct bstrList * bl, const_bstring sep);
114 extern int bsplitcb (const_bstring str, unsigned char splitChar, int pos,
115         int (* cb) (void * parm, int ofs, int len), void * parm);
116 extern int bsplitscb (const_bstring str, const_bstring splitStr, int pos,
117         int (* cb) (void * parm, int ofs, int len), void * parm);
118 extern int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos,
119         int (* cb) (void * parm, int ofs, int len), void * parm);
120
121 /* Miscellaneous functions */
122 extern int bpattern (bstring b, int len);
123 extern int btoupper (bstring b);
124 extern int btolower (bstring b);
125 extern int bltrimws (bstring b);
126 extern int brtrimws (bstring b);
127 extern int btrimws (bstring b);
128
129 #if !defined (BSTRLIB_NOVSNP)
130 extern bstring bformat (const char * fmt, ...);
131 extern int bformata (bstring b, const char * fmt, ...);
132 extern int bassignformat (bstring b, const char * fmt, ...);
133 extern int bvcformata (bstring b, int count, const char * fmt, va_list arglist);
134
135 #define bvformata(ret, b, fmt, lastarg) { \
136 bstring bstrtmp_b = (b); \
137 const char * bstrtmp_fmt = (fmt); \
138 int bstrtmp_r = BSTR_ERR, bstrtmp_sz = 16; \
139         for (;;) { \
140                 va_list bstrtmp_arglist; \
141                 va_start (bstrtmp_arglist, lastarg); \
142                 bstrtmp_r = bvcformata (bstrtmp_b, bstrtmp_sz, bstrtmp_fmt, bstrtmp_arglist); \
143                 va_end (bstrtmp_arglist); \
144                 if (bstrtmp_r >= 0) { /* Everything went ok */ \
145                         bstrtmp_r = BSTR_OK; \
146                         break; \
147                 } else if (-bstrtmp_r <= bstrtmp_sz) { /* A real error? */ \
148                         bstrtmp_r = BSTR_ERR; \
149                         break; \
150                 } \
151                 bstrtmp_sz = -bstrtmp_r; /* Doubled or target size */ \
152         } \
153         ret = bstrtmp_r; \
154 }
155
156 #endif
157
158 typedef int (*bNgetc) (void *parm);
159 typedef size_t (* bNread) (void *buff, size_t elsize, size_t nelem, void *parm);
160
161 /* Input functions */
162 extern bstring bgetstream (bNgetc getcPtr, void * parm, char terminator);
163 extern bstring bread (bNread readPtr, void * parm);
164 extern int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator);
165 extern int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator);
166 extern int breada (bstring b, bNread readPtr, void * parm);
167
168 /* Stream functions */
169 extern struct bStream * bsopen (bNread readPtr, void * parm);
170 extern void * bsclose (struct bStream * s);
171 extern int bsbufflength (struct bStream * s, int sz);
172 extern int bsreadln (bstring b, struct bStream * s, char terminator);
173 extern int bsreadlns (bstring r, struct bStream * s, const_bstring term);
174 extern int bsread (bstring b, struct bStream * s, int n);
175 extern int bsreadlna (bstring b, struct bStream * s, char terminator);
176 extern int bsreadlnsa (bstring r, struct bStream * s, const_bstring term);
177 extern int bsreada (bstring b, struct bStream * s, int n);
178 extern int bsunread (struct bStream * s, const_bstring b);
179 extern int bspeek (bstring r, const struct bStream * s);
180 extern int bssplitscb (struct bStream * s, const_bstring splitStr, 
181         int (* cb) (void * parm, int ofs, const_bstring entry), void * parm);
182 extern int bssplitstrcb (struct bStream * s, const_bstring splitStr, 
183         int (* cb) (void * parm, int ofs, const_bstring entry), void * parm);
184 extern int bseof (const struct bStream * s);
185
186 struct tagbstring {
187         int mlen;
188         int slen;
189         unsigned char * data;
190 };
191
192 /* Accessor macros */
193 #define blengthe(b, e)      (((b) == (void *)0 || (b)->slen < 0) ? (int)(e) : ((b)->slen))
194 #define blength(b)          (blengthe ((b), 0))
195 #define bdataofse(b, o, e)  (((b) == (void *)0 || (b)->data == (void*)0) ? (char *)(e) : ((char *)(b)->data) + (o))
196 #define bdataofs(b, o)      (bdataofse ((b), (o), (void *)0))
197 #define bdatae(b, e)        (bdataofse (b, 0, e))
198 #define bdata(b)            (bdataofs (b, 0))
199 #define bchare(b, p, e)     ((((unsigned)(p)) < (unsigned)blength(b)) ? ((b)->data[(p)]) : (e))
200 #define bchar(b, p)         bchare ((b), (p), '\0')
201
202 /* Static constant string initialization macro */
203 #define bsStaticMlen(q,m)   {(m), (int) sizeof(q)-1, (unsigned char *) ("" q "")}
204 #if defined(_MSC_VER)
205 # define bsStatic(q)        bsStaticMlen(q,-32)
206 #endif
207 #ifndef bsStatic
208 # define bsStatic(q)        bsStaticMlen(q,-__LINE__)
209 #endif
210
211 /* Static constant block parameter pair */
212 #define bsStaticBlkParms(q) ((void *)("" q "")), ((int) sizeof(q)-1)
213
214 /* Reference building macros */
215 #define cstr2tbstr btfromcstr
216 #define btfromcstr(t,s) {                                            \
217     (t).data = (unsigned char *) (s);                                \
218     (t).slen = ((t).data) ? ((int) (strlen) ((char *)(t).data)) : 0; \
219     (t).mlen = -1;                                                   \
220 }
221 #define blk2tbstr(t,s,l) {            \
222     (t).data = (unsigned char *) (s); \
223     (t).slen = l;                     \
224     (t).mlen = -1;                    \
225 }
226 #define btfromblk(t,s,l) blk2tbstr(t,s,l)
227 #define bmid2tbstr(t,b,p,l) {                                                \
228     const_bstring bstrtmp_s = (b);                                           \
229     if (bstrtmp_s && bstrtmp_s->data && bstrtmp_s->slen >= 0) {              \
230         int bstrtmp_left = (p);                                              \
231         int bstrtmp_len  = (l);                                              \
232         if (bstrtmp_left < 0) {                                              \
233             bstrtmp_len += bstrtmp_left;                                     \
234             bstrtmp_left = 0;                                                \
235         }                                                                    \
236         if (bstrtmp_len > bstrtmp_s->slen - bstrtmp_left)                    \
237             bstrtmp_len = bstrtmp_s->slen - bstrtmp_left;                    \
238         if (bstrtmp_len <= 0) {                                              \
239             (t).data = (unsigned char *)"";                                  \
240             (t).slen = 0;                                                    \
241         } else {                                                             \
242             (t).data = bstrtmp_s->data + bstrtmp_left;                       \
243             (t).slen = bstrtmp_len;                                          \
244         }                                                                    \
245     } else {                                                                 \
246         (t).data = (unsigned char *)"";                                      \
247         (t).slen = 0;                                                        \
248     }                                                                        \
249     (t).mlen = -__LINE__;                                                    \
250 }
251 #define btfromblkltrimws(t,s,l) {                                            \
252     int bstrtmp_idx = 0, bstrtmp_len = (l);                                  \
253     unsigned char * bstrtmp_s = (s);                                         \
254     if (bstrtmp_s && bstrtmp_len >= 0) {                                     \
255         for (; bstrtmp_idx < bstrtmp_len; bstrtmp_idx++) {                   \
256             if (!isspace (bstrtmp_s[bstrtmp_idx])) break;                    \
257         }                                                                    \
258     }                                                                        \
259     (t).data = bstrtmp_s + bstrtmp_idx;                                      \
260     (t).slen = bstrtmp_len - bstrtmp_idx;                                    \
261     (t).mlen = -__LINE__;                                                    \
262 }
263 #define btfromblkrtrimws(t,s,l) {                                            \
264     int bstrtmp_len = (l) - 1;                                               \
265     unsigned char * bstrtmp_s = (s);                                         \
266     if (bstrtmp_s && bstrtmp_len >= 0) {                                     \
267         for (; bstrtmp_len >= 0; bstrtmp_len--) {                            \
268             if (!isspace (bstrtmp_s[bstrtmp_len])) break;                    \
269         }                                                                    \
270     }                                                                        \
271     (t).data = bstrtmp_s;                                                    \
272     (t).slen = bstrtmp_len + 1;                                              \
273     (t).mlen = -__LINE__;                                                    \
274 }
275 #define btfromblktrimws(t,s,l) {                                             \
276     int bstrtmp_idx = 0, bstrtmp_len = (l) - 1;                              \
277     unsigned char * bstrtmp_s = (s);                                         \
278     if (bstrtmp_s && bstrtmp_len >= 0) {                                     \
279         for (; bstrtmp_idx <= bstrtmp_len; bstrtmp_idx++) {                  \
280             if (!isspace (bstrtmp_s[bstrtmp_idx])) break;                    \
281         }                                                                    \
282         for (; bstrtmp_len >= bstrtmp_idx; bstrtmp_len--) {                  \
283             if (!isspace (bstrtmp_s[bstrtmp_len])) break;                    \
284         }                                                                    \
285     }                                                                        \
286     (t).data = bstrtmp_s + bstrtmp_idx;                                      \
287     (t).slen = bstrtmp_len + 1 - bstrtmp_idx;                                \
288     (t).mlen = -__LINE__;                                                    \
289 }
290
291 /* Write protection macros */
292 #define bwriteprotect(t)     { if ((t).mlen >=  0) (t).mlen = -1; }
293 #define bwriteallow(t)       { if ((t).mlen == -1) (t).mlen = (t).slen + ((t).slen == 0); }
294 #define biswriteprotected(t) ((t).mlen <= 0)
295
296 #ifdef __cplusplus
297 }
298 #endif
299
300 #endif