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