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) {
87 b = (bstring)bstr__alloc(sizeof(struct tagbstring));
99 * @brief Free up the bstring, WITHOUT freeing the pointed to c-string!
101 int bunrefcstr (bstring b) {
102 if (b == NULL || b->slen < 0 || b->mlen > 0 || b->data == NULL)
105 /* In case there is any stale usage, there is one more chance to
106 notice this error. */
116 /*************************************************************************
118 ************************************************************************/
121 * @brief Create an empty list with preallocated storage for at least 'min' members
123 struct bstrList *bstrListCreateMin(int min)
125 struct bstrList *sl = NULL;
127 if ((sl = bstrListCreate()) == NULL)
130 if ((bstrListAlloc(sl, min)) != BSTR_OK) {
139 * @brief Push a bstring to the end of a list
141 int bstrListPush(struct bstrList *sl, bstring bs)
143 if (sl->qty == sl->mlen) {
144 if ((bstrListAlloc(sl, sl->qty + 1)) != BSTR_OK)
148 sl->entry[sl->qty] = bs;
154 * @brief Pop a bstring from the end of a list
156 bstring bstrListPop(struct bstrList *sl)
162 * @brief Inverse bjoin
164 bstring bjoinInv(const struct bstrList * bl, const_bstring sep) {
168 if (bl == NULL || bl->qty < 0)
170 if (sep != NULL && (sep->slen < 0 || sep->data == NULL))
173 for (i = 0, c = 1; i < bl->qty; i++) {
174 v = bl->entry[i]->slen;
176 return NULL;/* Invalid input */
179 return NULL;/* Wrap around ?? */
183 c += (bl->qty - 1) * sep->slen;
185 b = (bstring) bstr__alloc (sizeof (struct tagbstring));
187 return NULL; /* Out of memory */
188 b->data = (unsigned char *) bstr__alloc (c);
189 if (b->data == NULL) {
197 for (i = bl->qty - 1, c = 0, j = 0; i >= 0; i--, j++) {
198 if (j > 0 && sep != NULL) {
199 bstr__memcpy (b->data + c, sep->data, sep->slen);
202 v = bl->entry[i]->slen;
203 bstr__memcpy (b->data + c, bl->entry[i]->data, v);
206 b->data[c] = (unsigned char) '\0';