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