bstring 1.1.0
Loading...
Searching...
No Matches
bstraux.h
Go to the documentation of this file.
1/* Copyright 2002-2015 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
41
42#ifndef BSTRAUX_H
43#define BSTRAUX_H
44
45#ifdef HAVE_CONFIG_H
46#include <config.h>
47#endif
48
49#include <time.h>
50#include <bstrlib.h>
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/* Safety mechanisms */
57#define bstrDeclare(b) \
58 bstring(b) = NULL
59
60#define bstrFree(b) \
61do { \
62 if ((b) != NULL && (b)->slen >= 0 && (b)->mlen >= (b)->slen) { \
63 bdestroy(b); \
64 (b) = NULL; \
65 } \
66} while (0)
67
68/* Backward compatibilty with previous versions of Bstrlib */
69#if !defined(BSTRLIB_REDUCE_NAMESPACE_POLLUTION)
70#define bAssign(a, b) \
71 ((bassign)((a), (b)))
72
73#define bSubs(b, pos, len, a, c) \
74 ((breplace)((b),(pos),(len),(a),(unsigned char)(c)))
75
76#define bStrchr(b, c) \
77 ((bstrchr)((b), (c)))
78
79#define bStrchrFast(b, c) \
80 ((bstrchr)((b), (c)))
81
82#define bCatCstr(b, s) \
83 ((bcatcstr)((b), (s)))
84
85#define bCatBlk(b, s, len) \
86 ((bcatblk)((b), (s), (len)))
87
88#define bCatStatic(b, s) \
89 bcatStatic(b, s)
90
91#define bTrunc(b, n) \
92 ((btrunc)((b), (n)))
93
94#define bReplaceAll(b, find, repl, pos) \
95 ((bfindreplace)((b),(find),(repl),(pos)))
96
97#define bUppercase(b) \
98 ((btoupper)(b))
99
100#define bLowercase(b) \
101 ((btolower)(b))
102
103#define bCaselessCmp(a, b) \
104 ((bstricmp)((a), (b)))
105
106#define bCaselessNCmp(a, b, n) \
107 ((bstrnicmp)((a), (b), (n)))
108
109#define bBase64Decode(b) \
110 (bBase64DecodeEx((b), NULL))
111
112#define bUuDecode(b) \
113 (bUuDecodeEx((b), NULL))
114#endif
115
116/* Unusual functions */
117
123BSTR_PUBLIC struct bStream *
124bsFromBstr(const bstring b);
125
129BSTR_PUBLIC bstring
130bTail(bstring b, int n);
131
135BSTR_PUBLIC bstring
136bHead(bstring b, int n);
137
146BSTR_PUBLIC int
147bSetCstrChar(bstring a, int pos, char c);
148
156BSTR_PUBLIC int
157bSetChar(bstring b, int pos, char c);
158
162BSTR_PUBLIC int
163bFill(bstring a, char c, int len);
164
168BSTR_PUBLIC int
169bReplicate(bstring b, int n);
170
174BSTR_PUBLIC int
175bReverse(bstring b);
176
181BSTR_PUBLIC int
182bInsertChrs(bstring b, int pos, int len, unsigned char c, unsigned char fill);
183
192BSTR_PUBLIC bstring
193bStrfTime(const char * fmt, const struct tm * timeptr);
194
195#define bAscTime(t) (bStrfTime ("%c\n", (t)))
196
197#define bCTime(t) ((t) ? bAscTime (localtime (t)) : NULL)
198
199/* Spacing formatting */
200
204BSTR_PUBLIC int
205bJustifyLeft(bstring b, int space);
206
210BSTR_PUBLIC int
211bJustifyRight(bstring b, int width, int space);
212
219BSTR_PUBLIC int
220bJustifyMargin(bstring b, int width, int space);
221
226BSTR_PUBLIC int
227bJustifyCenter(bstring b, int width, int space);
228
229/* Esoteric standards specific functions */
230
242BSTR_PUBLIC char *
243bStr2NetStr(const bstring b);
244
253BSTR_PUBLIC bstring
254bNetStr2Bstr(const char *buf);
255
261BSTR_PUBLIC bstring
262bBase64Encode(const bstring b);
263
271BSTR_PUBLIC bstring
272bBase64DecodeEx(const bstring b, int *boolTruncError);
273
283BSTR_PUBLIC struct bStream *
284bsUuDecode(struct bStream *sInp, int *badlines);
285
295
296BSTR_PUBLIC bstring
297bUuDecodeEx(const bstring src, int *badlines);
298
304BSTR_PUBLIC bstring
305bUuEncode(const bstring src);
306
314BSTR_PUBLIC bstring
315bYEncode(const bstring src);
316
322BSTR_PUBLIC bstring
323bYDecode(const bstring src);
324
332BSTR_PUBLIC int
333bSGMLEncode(bstring b);
334
335/* Writable stream */
336typedef int
337(*bNwrite)(const void *buf, size_t elsize, size_t nelem, void *parm);
338
344BSTR_PUBLIC struct bwriteStream *
345bwsOpen(bNwrite writeFn, void *parm);
346
354BSTR_PUBLIC int
355bwsWriteBstr(struct bwriteStream *stream, const bstring b);
356
362BSTR_PUBLIC int
363bwsWriteBlk(struct bwriteStream *stream, void *blk, int len);
364
368BSTR_PUBLIC int
369bwsWriteFlush(struct bwriteStream *stream);
370
375BSTR_PUBLIC int
376bwsIsEOF(const struct bwriteStream *stream);
377
384BSTR_PUBLIC int
385bwsBuffLength(struct bwriteStream *stream, int sz);
386
394BSTR_PUBLIC void *
395bwsClose(struct bwriteStream *stream);
396
397/* Security functions */
398#define bSecureDestroy(b) \
399do { \
400 if ((b) && (b)->mlen > 0 && (b)->data) { \
401 (void)memset((b)->data, 0, (size_t)(b)->mlen); \
402 } \
403 (void)bdestroy((b)); \
404} while (0)
405
406#define bSecureWriteProtect(t) \
407do { \
408 if ((t).mlen >= 0) { \
409 if ((t).mlen > (t).slen)) { \
410 (void)memset((t).data + (t).slen, 0, \
411 (size_t)(t).mlen - (t).slen); \
412 } \
413 (t).mlen = -1; \
414 } \
415} while (0)
416
426BSTR_PUBLIC bstring
427bSecureInput(int maxlen, int termchar, bNgetc vgetchar, void *vgcCtx);
428
429#ifdef __cplusplus
430}
431#endif
432
433#endif /* BSTRAUX_H */
BSTR_PUBLIC void * bwsClose(struct bwriteStream *stream)
BSTR_PUBLIC int bwsWriteFlush(struct bwriteStream *stream)
BSTR_PUBLIC int bReplicate(bstring b, int n)
BSTR_PUBLIC bstring bYEncode(const bstring src)
BSTR_PUBLIC bstring bBase64DecodeEx(const bstring b, int *boolTruncError)
BSTR_PUBLIC bstring bHead(bstring b, int n)
BSTR_PUBLIC int bReverse(bstring b)
BSTR_PUBLIC struct bwriteStream * bwsOpen(bNwrite writeFn, void *parm)
BSTR_PUBLIC bstring bYDecode(const bstring src)
BSTR_PUBLIC int bSetChar(bstring b, int pos, char c)
BSTR_PUBLIC int bSetCstrChar(bstring a, int pos, char c)
BSTR_PUBLIC char * bStr2NetStr(const bstring b)
BSTR_PUBLIC int bwsWriteBstr(struct bwriteStream *stream, const bstring b)
BSTR_PUBLIC bstring bStrfTime(const char *fmt, const struct tm *timeptr)
BSTR_PUBLIC int bFill(bstring a, char c, int len)
BSTR_PUBLIC int bJustifyRight(bstring b, int width, int space)
BSTR_PUBLIC bstring bUuDecodeEx(const bstring src, int *badlines)
BSTR_PUBLIC int bJustifyCenter(bstring b, int width, int space)
BSTR_PUBLIC struct bStream * bsUuDecode(struct bStream *sInp, int *badlines)
BSTR_PUBLIC int bJustifyMargin(bstring b, int width, int space)
BSTR_PUBLIC int bwsWriteBlk(struct bwriteStream *stream, void *blk, int len)
BSTR_PUBLIC int bwsBuffLength(struct bwriteStream *stream, int sz)
BSTR_PUBLIC int bSGMLEncode(bstring b)
BSTR_PUBLIC struct bStream * bsFromBstr(const bstring b)
BSTR_PUBLIC bstring bBase64Encode(const bstring b)
BSTR_PUBLIC int bInsertChrs(bstring b, int pos, int len, unsigned char c, unsigned char fill)
BSTR_PUBLIC bstring bSecureInput(int maxlen, int termchar, bNgetc vgetchar, void *vgcCtx)
BSTR_PUBLIC bstring bTail(bstring b, int n)
BSTR_PUBLIC int bwsIsEOF(const struct bwriteStream *stream)
BSTR_PUBLIC bstring bNetStr2Bstr(const char *buf)
BSTR_PUBLIC int bJustifyLeft(bstring b, int space)
BSTR_PUBLIC bstring bUuEncode(const bstring src)
C implementaion of bstring functions.