2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 * Additional functions for bstrlib
27 #include <atalk/bstrlib.h>
29 /* Optionally include a mechanism for debugging memory */
31 #if defined(MEMORY_DEBUG) || defined(BSTRLIB_MEMORY_DEBUG)
36 #define bstr__alloc(x) malloc (x)
40 #define bstr__free(p) free (p)
44 #define bstr__realloc(p,x) realloc ((p), (x))
48 #define bstr__memcpy(d,s,l) memcpy ((d), (s), (l))
52 #define bstr__memmove(d,s,l) memmove ((d), (s), (l))
56 #define bstr__memset(d,c,l) memset ((d), (c), (l))
60 #define bstr__memcmp(d,c,l) memcmp ((d), (c), (l))
64 #define bstr__memchr(s,c,l) memchr ((s), (c), (l))
67 /*************************************************************************
68 * Stuff for making bstrings referencing c-strings
69 ************************************************************************/
72 * @brief Create a bstring referencing "str"
74 * This is usefull if a well know code path uses string, often doing strlen on string.
75 * By converting to bstring which carries the strlen, the repeated computation can be avoided.
77 bstring brefcstr (char *str) {
85 b = (bstring)bstr__alloc(sizeof(struct tagbstring));
91 b->data = (unsigned char *)str;
97 * @brief Free up the bstring, WITHOUT freeing the pointed to c-string!
99 int bunrefcstr (bstring b) {
100 if (b == NULL || b->slen < 0 || b->mlen > 0 || b->data == NULL)
103 /* In case there is any stale usage, there is one more chance to
104 notice this error. */
114 /*************************************************************************
116 ************************************************************************/
119 * @brief Create an empty list with preallocated storage for at least 'min' members
121 struct bstrList *bstrListCreateMin(int min)
123 struct bstrList *sl = NULL;
125 if ((sl = bstrListCreate()) == NULL)
128 if ((bstrListAlloc(sl, min)) != BSTR_OK) {
137 * @brief Push a bstring to the end of a list
139 int bstrListPush(struct bstrList *sl, bstring bs)
141 if (sl->qty == sl->mlen) {
142 if ((bstrListAlloc(sl, sl->qty + 1)) != BSTR_OK)
146 sl->entry[sl->qty] = bs;
152 * @brief Pop a bstring from the end of a list
154 bstring bstrListPop(struct bstrList *sl)
160 * @brief Inverse bjoin
162 bstring bjoinInv(const struct bstrList * bl, const_bstring sep) {
166 if (bl == NULL || bl->qty < 0)
168 if (sep != NULL && (sep->slen < 0 || sep->data == NULL))
171 for (i = 0, c = 1; i < bl->qty; i++) {
172 v = bl->entry[i]->slen;
174 return NULL;/* Invalid input */
177 return NULL;/* Wrap around ?? */
181 c += (bl->qty - 1) * sep->slen;
183 b = (bstring) bstr__alloc (sizeof (struct tagbstring));
185 return NULL; /* Out of memory */
186 b->data = (unsigned char *) bstr__alloc (c);
187 if (b->data == NULL) {
195 for (i = bl->qty - 1, c = 0, j = 0; i >= 0; i--, j++) {
196 if (j > 0 && sep != NULL) {
197 bstr__memcpy (b->data + c, sep->data, sep->slen);
200 v = bl->entry[i]->slen;
201 bstr__memcpy (b->data + c, bl->entry[i]->data, v);
204 b->data[c] = (unsigned char) '\0';