include/Strings/BString/bstrlib.h

Go to the documentation of this file.
00001 /*
00002  * This source file is part of the bstring string library.  This code was
00003  * written by Paul Hsieh in 2002-2006, and is covered by the BSD open source 
00004  * license. Refer to the accompanying documentation for details on usage and 
00005  * license.
00006  */
00007 
00008 /*
00009  * bstrlib.c
00010  *
00011  * This file is the core module for implementing the bstring functions.
00012  */
00013 
00014 #ifndef BSTRLIB_INCLUDE
00015 #define BSTRLIB_INCLUDE
00016 
00017 #ifdef __cplusplus
00018 extern "C" {
00019 #endif
00020 
00021 #include <stdarg.h>
00022 #include <string.h>
00023 #include <limits.h>
00024 
00025 #if !defined (BSTRLIB_VSNP_OK) && !defined (BSTRLIB_NOVSNP)
00026 # if defined (__TURBOC__) && !defined (__BORLANDC__)
00027 #  define BSTRLIB_NOVSNP
00028 # endif
00029 #endif
00030 
00031 #define BSTR_ERR (-1)
00032 #define BSTR_OK (0)
00033 #define BSTR_BS_BUFF_LENGTH_GET (0)
00034 
00035 typedef struct tagbstring * bstring;
00036 
00037 /* Copy functions */
00038 #define cstr2bstr bfromcstr
00039 extern bstring bfromcstr (const char * str);
00040 extern bstring bfromcstralloc (int mlen, const char * str);
00041 extern bstring blk2bstr (const void * blk, int len);
00042 extern char * bstr2cstr (const bstring s, char z);
00043 extern int bcstrfree (char * s);
00044 extern bstring bstrcpy (const bstring b1);
00045 extern int bassign (bstring a, const bstring b);
00046 extern int bassignmidstr (bstring a, const bstring b, int left, int len);
00047 
00048 /* Destroy function */
00049 extern int bdestroy (bstring b);
00050 
00051 /* Space allocation hinting function */
00052 extern int balloc (bstring s, int len);
00053 
00054 /* Substring extraction */
00055 extern bstring bmidstr (const bstring b, int left, int len);
00056 
00057 /* Various standard manipulations */
00058 extern int bconcat (bstring b0, const bstring b1);
00059 extern int bconchar (bstring b0, char c);
00060 extern int bcatcstr (bstring b, const char * s);
00061 extern int bcatblk (bstring b, const unsigned char * s, int len);
00062 extern int binsert (bstring s1, int pos, const bstring s2, unsigned char fill);
00063 extern int binsertch (bstring s1, int pos, int len, unsigned char fill);
00064 extern int breplace (bstring b1, int pos, int len, const bstring b2, unsigned char fill);
00065 extern int bdelete (bstring s1, int pos, int len);
00066 extern int bsetstr (bstring b0, int pos, const bstring b1, unsigned char fill);
00067 extern int btrunc (bstring b, int n);
00068 
00069 /* Scan/search functions */
00070 extern int bstricmp (const bstring b0, const bstring b1);
00071 extern int bstrnicmp (const bstring b0, const bstring b1, int n);
00072 extern int biseqcaseless (const bstring b0, const bstring b1);
00073 extern int bisstemeqcaselessblk (const bstring b0, const void * blk, int len);
00074 extern int biseq (const bstring b0, const bstring b1);
00075 extern int bisstemeqblk (const bstring b0, const void * blk, int len);
00076 extern int biseqcstr (const bstring b, const char * s);
00077 extern int biseqcstrcaseless (const bstring b, const char * s);
00078 extern int bstrcmp (const bstring b0, const bstring b1);
00079 extern int bstrncmp (const bstring b0, const bstring b1, int n);
00080 extern int binstr (const bstring s1, int pos, const bstring s2);
00081 extern int binstrr (const bstring s1, int pos, const bstring s2);
00082 extern int binstrcaseless (const bstring s1, int pos, const bstring s2);
00083 extern int binstrrcaseless (const bstring s1, int pos, const bstring s2);
00084 extern int bstrchrp (const bstring b, int c, int pos);
00085 extern int bstrrchrp (const bstring b, int c, int pos);
00086 #define bstrchr(b,c) bstrchrp ((b), (c), 0)
00087 #define bstrrchr(b,c) bstrrchrp ((b), (c), blength(b)-1)
00088 extern int binchr (const bstring b0, int pos, const bstring b1);
00089 extern int binchrr (const bstring b0, int pos, const bstring b1);
00090 extern int bninchr (const bstring b0, int pos, const bstring b1);
00091 extern int bninchrr (const bstring b0, int pos, const bstring b1);
00092 extern int bfindreplace (bstring b, const bstring find, const bstring repl, int pos);
00093 extern int bfindreplacecaseless (bstring b, const bstring find, const bstring repl, int pos);
00094 
00095 struct bstrList {
00096     int qty;
00097     bstring entry[1];
00098 };
00099 
00100 /* String split and join functions */
00101 extern struct bstrList * bsplit (const bstring str, unsigned char splitChar);
00102 extern struct bstrList * bsplits (const bstring str, const bstring splitStr);
00103 extern bstring bjoin (const struct bstrList * bl, const bstring sep);
00104 extern int bstrListDestroy (struct bstrList * sl);
00105 extern int bsplitcb (const bstring str, unsigned char splitChar, int pos,
00106     int (* cb) (void * parm, int ofs, int len), void * parm);
00107 extern int bsplitscb (const bstring str, const bstring splitStr, int pos,
00108     int (* cb) (void * parm, int ofs, int len), void * parm);
00109 
00110 /* Miscellaneous functions */
00111 extern int bpattern (bstring b, int len);
00112 extern int btoupper (bstring b);
00113 extern int btolower (bstring b);
00114 extern int bltrimws (bstring b);
00115 extern int brtrimws (bstring b);
00116 extern int btrimws (bstring b);
00117 
00118 #if !defined (BSTRLIB_NOVSNP)
00119 extern bstring bformat (const char * fmt, ...);
00120 extern int bformata (bstring b, const char * fmt, ...);
00121 extern int bassignformat (bstring b, const char * fmt, ...);
00122 #endif
00123 
00124 typedef int (*bNgetc) (void *parm);
00125 typedef size_t (* bNread) (void *buff, size_t elsize, size_t nelem, void *parm);
00126 
00127 /* Input functions */
00128 extern bstring bgets (bNgetc getcPtr, void * parm, char terminator);
00129 extern bstring bread (bNread readPtr, void * parm);
00130 extern int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator);
00131 extern int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator);
00132 extern int breada (bstring b, bNread readPtr, void * parm);
00133 
00134 /* Stream functions */
00135 extern struct bStream * bsopen (bNread readPtr, void * parm);
00136 extern void * bsclose (struct bStream * s);
00137 extern int bsbufflength (struct bStream * s, int sz);
00138 extern int bsreadln (bstring b, struct bStream * s, char terminator);
00139 extern int bsreadlns (bstring r, struct bStream * s, const bstring term);
00140 extern int bsread (bstring b, struct bStream * s, int n);
00141 extern int bsreadlna (bstring b, struct bStream * s, char terminator);
00142 extern int bsreadlnsa (bstring r, struct bStream * s, const bstring term);
00143 extern int bsreada (bstring b, struct bStream * s, int n);
00144 extern int bsunread (struct bStream * s, const bstring b);
00145 extern int bspeek (bstring r, const struct bStream * s);
00146 extern int bssplitscb (struct bStream * s, const bstring splitStr, 
00147     int (* cb) (void * parm, int ofs, const bstring entry), void * parm);
00148 extern int bseof (const struct bStream * s);
00149 
00150 struct tagbstring {
00151     int mlen;
00152     int slen;
00153     unsigned char * data;
00154 };
00155 
00156 /* Accessor macros */
00157 #define blengthe(b, e)      (((b) == (void *)0 || (b)->slen < 0) ? (int)(e) : ((b)->slen))
00158 #define blength(b)          (blengthe ((b), 0))
00159 #define bdataofse(b, o, e)  (((b) == (void *)0 || (b)->data == (void*)0) ? (unsigned char *)(e) : ((b)->data) + (o))
00160 #define bdataofs(b, o)      (bdataofse ((b), (o), (void *)0))
00161 #define bdatae(b, e)        (bdataofse (b, 0, e))
00162 #define bdata(b)            (bdataofs (b, 0))
00163 #define bchare(b, p, e)     ((((unsigned)(p)) < (unsigned)blength(b)) ? ((b)->data[(p)]) : (e))
00164 #define bchar(b, p)         bchare ((b), (p), '\0')
00165 
00166 /* Static constant string initialization macro */
00167 #if defined(_MSC_VER) && defined(_DEBUG)
00168 # if _MSC_VER <= 1310
00169 #  define bsStatic(q)       {-32, (int) sizeof(q)-1, (unsigned char *) ("" q "")}
00170 # endif
00171 #endif
00172 #ifndef bsStatic
00173 # define bsStatic(q)        {-__LINE__, (int) sizeof(q)-1, (unsigned char *) ("" q "")}
00174 #endif
00175 
00176 /* Static constant block parameter pair */
00177 #define bsStaticBlkParms(q) ((void *)("" q "")), ((int) sizeof(q)-1)
00178 
00179 /* Reference building macros */
00180 #define cstr2tbstr btfromcstr
00181 #define btfromcstr(t,s) {                                            \
00182     (t).data = (unsigned char *) (s);                                \
00183     (t).slen = ((t).data) ? ((int) (strlen) ((char *)(t).data)) : 0; \
00184     (t).mlen = -1;                                                   \
00185 }
00186 #define blk2tbstr(t,s,l) {            \
00187     (t).slen = l;                     \
00188     (t).mlen = -1;                    \
00189     (t).data = (unsigned char *) (s); \
00190 }
00191 #define bmid2tbstr(t,b,p,l) {                                                \
00192     bstring bstrtmp_s = (b);                                                 \
00193     if (bstrtmp_s && bstrtmp_s->data && bstrtmp_s->slen >= 0) {              \
00194         int bstrtmp_left = (p);                                              \
00195         int bstrtmp_len  = (l);                                              \
00196         if (bstrtmp_left < 0) {                                              \
00197             bstrtmp_len += bstrtmp_left;                                     \
00198             bstrtmp_left = 0;                                                \
00199         }                                                                    \
00200         if (bstrtmp_len > bstrtmp_s->slen - bstrtmp_left)                    \
00201             bstrtmp_len = bstrtmp_s->slen - bstrtmp_left;                    \
00202         if (bstrtmp_len <= 0) {                                              \
00203             (t).data = (unsigned char *)"";                                  \
00204             (t).slen = 0;                                                    \
00205         } else {                                                             \
00206             (t).data = bstrtmp_s->data + bstrtmp_left;                       \
00207             (t).slen = bstrtmp_len;                                          \
00208         }                                                                    \
00209     } else {                                                                 \
00210         (t).data = (unsigned char *)"";                                      \
00211         (t).slen = 0;                                                        \
00212     }                                                                        \
00213     (t).mlen = -__LINE__;                                                    \
00214 }
00215 
00216 /* Write protection macros */
00217 #define bwriteprotect(t)     { if ((t).mlen >=  0) (t).mlen = -1; }
00218 #define bwriteallow(t)       { if ((t).mlen == -1) (t).mlen = (t).slen + ((t).slen == 0); }
00219 #define biswriteprotected(t) ((t).mlen <= 0)
00220 
00221 #ifdef __cplusplus
00222 }
00223 #endif
00224 
00225 #endif

(C) An X-Ryl669 project 2007

This document describes Unlimited Zooming Interface source code. UZI stands for Unlimited Zooming Interface, and source code license is