2 $Id: bstradd.c,v 1.1.2.1 2010-02-01 10:56:08 franklahm Exp $
3 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
18 * Additional functions for bstrlib
28 #include <atalk/bstrlib.h>
30 /* Optionally include a mechanism for debugging memory */
32 #if defined(MEMORY_DEBUG) || defined(BSTRLIB_MEMORY_DEBUG)
37 #define bstr__alloc(x) malloc (x)
41 #define bstr__free(p) free (p)
45 #define bstr__realloc(p,x) realloc ((p), (x))
49 #define bstr__memcpy(d,s,l) memcpy ((d), (s), (l))
53 #define bstr__memmove(d,s,l) memmove ((d), (s), (l))
57 #define bstr__memset(d,c,l) memset ((d), (c), (l))
61 #define bstr__memcmp(d,c,l) memcmp ((d), (c), (l))
65 #define bstr__memchr(s,c,l) memchr ((s), (c), (l))
68 /*************************************************************************
69 * Stuff for making bstrings referencing c-strings
70 ************************************************************************/
73 * @brief Create a bstring referencing "str"
75 * This is usefull if a well know code path uses string, often doing strlen on string.
76 * By converting to bstring which carries the strlen, the repeated computation can be avoided.
78 bstring brefcstr (char *str) {
86 b = (bstring)bstr__alloc(sizeof(struct tagbstring));
92 b->data = (unsigned char *)str;
98 * @brief Free up the bstring, WITHOUT freeing the pointed to c-string!
100 int bunrefcstr (bstring b) {
101 if (b == NULL || b->slen < 0 || b->mlen > 0 || b->data == NULL)
104 /* In case there is any stale usage, there is one more chance to
105 notice this error. */
115 /*************************************************************************
117 ************************************************************************/
120 * @brief Create an empty list with preallocated storage for at least 'min' members
122 struct bstrList *bstrListCreateMin(int min)
124 struct bstrList *sl = NULL;
126 if ((sl = bstrListCreate()) == NULL)
129 if ((bstrListAlloc(sl, min)) != BSTR_OK) {
138 * @brief Push a bstring to the end of a list
140 int bstrListPush(struct bstrList *sl, bstring bs)
142 if (sl->qty == sl->mlen) {
143 if ((bstrListAlloc(sl, sl->qty + 1)) != BSTR_OK)
147 sl->entry[sl->qty] = bs;
153 * @brief Pop a bstring from the end of a list
155 bstring bstrListPop(struct bstrList *sl)
161 * @brief Inverse bjoin
163 bstring bjoinInv(const struct bstrList * bl, const_bstring sep) {
167 if (bl == NULL || bl->qty < 0)
169 if (sep != NULL && (sep->slen < 0 || sep->data == NULL))
172 for (i = 0, c = 1; i < bl->qty; i++) {
173 v = bl->entry[i]->slen;
175 return NULL;/* Invalid input */
178 return NULL;/* Wrap around ?? */
182 c += (bl->qty - 1) * sep->slen;
184 b = (bstring) bstr__alloc (sizeof (struct tagbstring));
186 return NULL; /* Out of memory */
187 b->data = (unsigned char *) bstr__alloc (c);
188 if (b->data == NULL) {
196 for (i = bl->qty - 1, c = 0, j = 0; i >= 0; i--, j++) {
197 if (j > 0 && sep != NULL) {
198 bstr__memcpy (b->data + c, sep->data, sep->slen);
201 v = bl->entry[i]->slen;
202 bstr__memcpy (b->data + c, bl->entry[i]->data, v);
205 b->data[c] = (unsigned char) '\0';