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 17extern "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 35typedef struct tagbstring * bstring; 36typedef const struct tagbstring * const_bstring; 37 38/* Copy functions */ 39#define cstr2bstr bfromcstr 40extern bstring bfromcstr (const char * str); 41extern bstring bfromcstralloc (int mlen, const char * str); 42extern bstring blk2bstr (const void * blk, int len); 43extern char * bstr2cstr (const_bstring s, char z); 44extern int bcstrfree (char * s); 45extern bstring bstrcpy (const_bstring b1); 46extern int bassign (bstring a, const_bstring b); 47extern int bassignmidstr (bstring a, const_bstring b, int left, int len); 48extern int bassigncstr (bstring a, const char * str); 49extern int bassignblk (bstring a, const void * s, int len); 50 51/* Destroy function */ 52extern int bdestroy (bstring b); 53 54/* Space allocation hinting functions */ 55extern int balloc (bstring s, int len); 56extern int ballocmin (bstring b, int len); 57 58/* Substring extraction */ 59extern bstring bmidstr (const_bstring b, int left, int len); 60 61/* Various standard manipulations */ 62extern int bconcat (bstring b0, const_bstring b1); 63extern int bconchar (bstring b0, char c); 64extern int bcatcstr (bstring b, const char * s); 65extern int bcatblk (bstring b, const void * s, int len); 66extern int binsert (bstring s1, int pos, const_bstring s2, unsigned char fill); 67extern int binsertch (bstring s1, int pos, int len, unsigned char fill); 68extern int breplace (bstring b1, int pos, int len, const_bstring b2, unsigned char fill); 69extern int bdelete (bstring s1, int pos, int len); 70extern int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill); 71extern int btrunc (bstring b, int n); 72 73/* Scan/search functions */ 74extern int bstricmp (const_bstring b0, const_bstring b1); 75extern int bstrnicmp (const_bstring b0, const_bstring b1, int n); 76extern int biseqcaseless (const_bstring b0, const_bstring b1); 77extern int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len); 78extern int biseq (const_bstring b0, const_bstring b1); 79extern int bisstemeqblk (const_bstring b0, const void * blk, int len); 80extern int biseqcstr (const_bstring b, const char * s); 81extern int biseqcstrcaseless (const_bstring b, const char * s); 82extern int bstrcmp (const_bstring b0, const_bstring b1); 83extern int bstrncmp (const_bstring b0, const_bstring b1, int n); 84extern int binstr (const_bstring s1, int pos, const_bstring s2); 85extern int binstrr (const_bstring s1, int pos, const_bstring s2); 86extern int binstrcaseless (const_bstring s1, int pos, const_bstring s2); 87extern int binstrrcaseless (const_bstring s1, int pos, const_bstring s2); 88extern int bstrchrp (const_bstring b, int c, int pos); 89extern 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) 92extern int binchr (const_bstring b0, int pos, const_bstring b1); 93extern int binchrr (const_bstring b0, int pos, const_bstring b1); 94extern int bninchr (const_bstring b0, int pos, const_bstring b1); 95extern int bninchrr (const_bstring b0, int pos, const_bstring b1); 96extern int bfindreplace (bstring b, const_bstring find, const_bstring repl, int pos); 97extern int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, int pos); 98 99/* List of string container functions */ 100struct bstrList { 101 int qty, mlen; 102 bstring * entry; 103}; 104extern struct bstrList * bstrListCreate (void); 105extern int bstrListDestroy (struct bstrList * sl); 106extern int bstrListAlloc (struct bstrList * sl, int msz); 107extern int bstrListAllocMin (struct bstrList * sl, int msz); 108 109/* String split and join functions */ 110extern struct bstrList * bsplit (const_bstring str, unsigned char splitChar); 111extern struct bstrList * bsplits (const_bstring str, const_bstring splitStr); 112extern struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr); 113extern bstring bjoin (const struct bstrList * bl, const_bstring sep); 114extern int bsplitcb (const_bstring str, unsigned char splitChar, int pos, 115 int (* cb) (void * parm, int ofs, int len), void * parm); 116extern int bsplitscb (const_bstring str, const_bstring splitStr, int pos, 117 int (* cb) (void * parm, int ofs, int len), void * parm); 118extern 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 */ 122extern int bpattern (bstring b, int len); 123extern int btoupper (bstring b); 124extern int btolower (bstring b); 125extern int bltrimws (bstring b); 126extern int brtrimws (bstring b); 127extern int btrimws (bstring b); 128 129#if !defined (BSTRLIB_NOVSNP) 130extern bstring bformat (const char * fmt, ...); 131extern int bformata (bstring b, const char * fmt, ...); 132extern int bassignformat (bstring b, const char * fmt, ...); 133extern int bvcformata (bstring b, int count, const char * fmt, va_list arglist); 134 135#define bvformata(ret, b, fmt, lastarg) { \ 136bstring bstrtmp_b = (b); \ 137const char * bstrtmp_fmt = (fmt); \ 138int 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 158typedef int (*bNgetc) (void *parm); 159typedef size_t (* bNread) (void *buff, size_t elsize, size_t nelem, void *parm); 160 161/* Input functions */ 162extern bstring bgetstream (bNgetc getcPtr, void * parm, char terminator); 163extern bstring bread (bNread readPtr, void * parm); 164extern int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator); 165extern int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator); 166extern int breada (bstring b, bNread readPtr, void * parm); 167 168/* Stream functions */ 169extern struct bStream * bsopen (bNread readPtr, void * parm); 170extern void * bsclose (struct bStream * s); 171extern int bsbufflength (struct bStream * s, int sz); 172extern int bsreadln (bstring b, struct bStream * s, char terminator); 173extern int bsreadlns (bstring r, struct bStream * s, const_bstring term); 174extern int bsread (bstring b, struct bStream * s, int n); 175extern int bsreadlna (bstring b, struct bStream * s, char terminator); 176extern int bsreadlnsa (bstring r, struct bStream * s, const_bstring term); 177extern int bsreada (bstring b, struct bStream * s, int n); 178extern int bsunread (struct bStream * s, const_bstring b); 179extern int bspeek (bstring r, const struct bStream * s); 180extern int bssplitscb (struct bStream * s, const_bstring splitStr, 181 int (* cb) (void * parm, int ofs, const_bstring entry), void * parm); 182extern int bssplitstrcb (struct bStream * s, const_bstring splitStr, 183 int (* cb) (void * parm, int ofs, const_bstring entry), void * parm); 184extern int bseof (const struct bStream * s); 185 186struct 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 301