bstring  0.1.1
 All Data Structures Files Functions Macros Pages
bstraux.h
Go to the documentation of this file.
1 /* Copyright 2002-2010 Paul Hsieh
2  * This file is part of Bstrlib.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of bstrlib nor the names of its contributors may be
15  * used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Alternatively, the contents of this file may be used under the terms of
31  * GNU General Public License Version 2 (the "GPL").
32  */
33 
42 #ifndef BSTRAUX_H
43 #define BSTRAUX_H
44 
45 #include <time.h>
46 #include <bstrlib.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /* Safety mechanisms */
53 #define bstrDeclare(b) \
54  bstring(b) = NULL
55 
56 #define bstrFree(b) \
57 do { \
58  if ((b) != NULL && (b)->slen >= 0 && (b)->mlen >= (b)->slen) { \
59  bdestroy(b); \
60  (b) = NULL; \
61  } \
62 } while (0)
63 
64 /* Backward compatibilty with previous versions of Bstrlib */
65 #define bAssign(a, b) \
66  ((bassign)((a), (b)))
67 
68 #define bSubs(b, pos, len, a, c) \
69  ((breplace)((b),(pos),(len),(a),(unsigned char)(c)))
70 
71 #define bStrchr(b, c) \
72  ((bstrchr)((b), (c)))
73 
74 #define bStrchrFast(b, c) \
75  ((bstrchr)((b), (c)))
76 
77 #define bCatCstr(b, s) \
78  ((bcatcstr)((b), (s)))
79 
80 #define bCatBlk(b, s, len) \
81  ((bcatblk)((b), (s), (len)))
82 
83 #define bCatStatic(b, s) \
84  bCatBlk((b), ("" s ""), sizeof (s) - 1)
85 
86 #define bTrunc(b, n) \
87  ((btrunc)((b), (n)))
88 
89 #define bReplaceAll(b, find, repl, pos) \
90  ((bfindreplace)((b),(find),(repl),(pos)))
91 
92 #define bUppercase(b) \
93  ((btoupper)(b))
94 
95 #define bLowercase(b) \
96  ((btolower)(b))
97 
98 #define bCaselessCmp(a, b) \
99  ((bstricmp)((a), (b)))
100 
101 #define bCaselessNCmp(a, b, n) \
102  ((bstrnicmp)((a), (b), (n)))
103 
104 #define bBase64Decode(b) \
105  (bBase64DecodeEx((b), NULL))
106 
107 #define bUuDecode(b) \
108  (bUuDecodeEx((b), NULL))
109 
110 /* Unusual functions */
111 
117 BSTR_PUBLIC struct bStream *
118 bsFromBstr(const bstring b);
119 
123 BSTR_PUBLIC bstring
124 bTail(bstring b, int n);
125 
129 BSTR_PUBLIC bstring
130 bHead(bstring b, int n);
131 
140 BSTR_PUBLIC int
141 bSetCstrChar(bstring a, int pos, char c);
142 
150 BSTR_PUBLIC int
151 bSetChar(bstring b, int pos, char c);
152 
156 BSTR_PUBLIC int
157 bFill(bstring a, char c, int len);
158 
162 BSTR_PUBLIC int
163 bReplicate(bstring b, int n);
164 
168 BSTR_PUBLIC int
169 bReverse(bstring b);
170 
175 BSTR_PUBLIC int
176 bInsertChrs(bstring b, int pos, int len, unsigned char c, unsigned char fill);
177 
186 BSTR_PUBLIC bstring
187 bStrfTime(const char * fmt, const struct tm * timeptr);
188 
189 #define bAscTime(t) (bStrfTime ("%c\n", (t)))
190 
191 #define bCTime(t) ((t) ? bAscTime (localtime (t)) : NULL)
192 
193 /* Spacing formatting */
194 
198 BSTR_PUBLIC int
199 bJustifyLeft(bstring b, int space);
200 
204 BSTR_PUBLIC int
205 bJustifyRight(bstring b, int width, int space);
206 
213 BSTR_PUBLIC int
214 bJustifyMargin(bstring b, int width, int space);
215 
220 BSTR_PUBLIC int
221 bJustifyCenter(bstring b, int width, int space);
222 
223 /* Esoteric standards specific functions */
224 
236 BSTR_PUBLIC char *
237 bStr2NetStr(const bstring b);
238 
247 BSTR_PUBLIC bstring
248 bNetStr2Bstr(const char *buf);
249 
255 BSTR_PUBLIC bstring
256 bBase64Encode(const bstring b);
257 
265 BSTR_PUBLIC bstring
266 bBase64DecodeEx(const bstring b, int *boolTruncError);
267 
277 BSTR_PUBLIC struct bStream *
278 bsUuDecode(struct bStream *sInp, int *badlines);
279 
290 BSTR_PUBLIC bstring
291 bUuDecodeEx(const bstring src, int *badlines);
292 
298 BSTR_PUBLIC bstring
299 bUuEncode(const bstring src);
300 
308 BSTR_PUBLIC bstring
309 bYEncode(const bstring src);
310 
316 BSTR_PUBLIC bstring
317 bYDecode(const bstring src);
318 
319 /* Writable stream */
320 typedef int
321 (*bNwrite)(const void *buf, size_t elsize, size_t nelem, void *parm);
322 
328 BSTR_PUBLIC struct bwriteStream *
329 bwsOpen(bNwrite writeFn, void *parm);
330 
338 BSTR_PUBLIC int
339 bwsWriteBstr(struct bwriteStream *stream, const bstring b);
340 
346 BSTR_PUBLIC int
347 bwsWriteBlk(struct bwriteStream *stream, void *blk, int len);
348 
352 BSTR_PUBLIC int
353 bwsWriteFlush(struct bwriteStream *stream);
354 
359 BSTR_PUBLIC int
360 bwsIsEOF(const struct bwriteStream *stream);
361 
368 BSTR_PUBLIC int
369 bwsBuffLength(struct bwriteStream *stream, int sz);
370 
378 BSTR_PUBLIC void *
379 bwsClose(struct bwriteStream *stream);
380 
381 /* Security functions */
382 #define bSecureDestroy(b) \
383 do { \
384  if ((b) && (b)->mlen > 0 && (b)->data) { \
385  (void)memset((b)->data, 0, (size_t)(b)->mlen); \
386  (void)bdestroy((b)); \
387  } \
388 } while (0)
389 
390 #define bSecureWriteProtect(t) \
391 do { \
392  if ((t).mlen >= 0) { \
393  if ((t).mlen > (t).slen)) { \
394  (void)memset((t).data + (t).slen, 0, \
395  (size_t)(t).mlen - (t).slen); \
396  } \
397  (t).mlen = -1; \
398  } \
399 } while (0)
400 
410 BSTR_PUBLIC bstring
411 bSecureInput(int maxlen, int termchar, bNgetc vgetchar, void *vgcCtx);
412 
413 #ifdef __cplusplus
414 }
415 #endif
416 
417 #endif /* BSTRAUX_H */