bstring 1.0.2
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#define bAssign(a, b) \
70 ((bassign)((a), (b)))
71
72#define bSubs(b, pos, len, a, c) \
73 ((breplace)((b),(pos),(len),(a),(unsigned char)(c)))
74
75#define bStrchr(b, c) \
76 ((bstrchr)((b), (c)))
77
78#define bStrchrFast(b, c) \
79 ((bstrchr)((b), (c)))
80
81#define bCatCstr(b, s) \
82 ((bcatcstr)((b), (s)))
83
84#define bCatBlk(b, s, len) \
85 ((bcatblk)((b), (s), (len)))
86
87#define bCatStatic(b, s) \
88 bCatBlk((b), ("" s ""), sizeof (s) - 1)
89
90#define bTrunc(b, n) \
91 ((btrunc)((b), (n)))
92
93#define bReplaceAll(b, find, repl, pos) \
94 ((bfindreplace)((b),(find),(repl),(pos)))
95
96#define bUppercase(b) \
97 ((btoupper)(b))
98
99#define bLowercase(b) \
100 ((btolower)(b))
101
102#define bCaselessCmp(a, b) \
103 ((bstricmp)((a), (b)))
104
105#define bCaselessNCmp(a, b, n) \
106 ((bstrnicmp)((a), (b), (n)))
107
108#define bBase64Decode(b) \
109 (bBase64DecodeEx((b), NULL))
110
111#define bUuDecode(b) \
112 (bUuDecodeEx((b), NULL))
113
114/* Unusual functions */
115
121BSTR_PUBLIC struct bStream *
122bsFromBstr(const bstring b);
123
127BSTR_PUBLIC bstring
128bTail(bstring b, int n);
129
133BSTR_PUBLIC bstring
134bHead(bstring b, int n);
135
144BSTR_PUBLIC int
145bSetCstrChar(bstring a, int pos, char c);
146
154BSTR_PUBLIC int
155bSetChar(bstring b, int pos, char c);
156
160BSTR_PUBLIC int
161bFill(bstring a, char c, int len);
162
166BSTR_PUBLIC int
167bReplicate(bstring b, int n);
168
172BSTR_PUBLIC int
173bReverse(bstring b);
174
179BSTR_PUBLIC int
180bInsertChrs(bstring b, int pos, int len, unsigned char c, unsigned char fill);
181
190BSTR_PUBLIC bstring
191bStrfTime(const char * fmt, const struct tm * timeptr);
192
193#define bAscTime(t) (bStrfTime ("%c\n", (t)))
194
195#define bCTime(t) ((t) ? bAscTime (localtime (t)) : NULL)
196
197/* Spacing formatting */
198
202BSTR_PUBLIC int
203bJustifyLeft(bstring b, int space);
204
208BSTR_PUBLIC int
209bJustifyRight(bstring b, int width, int space);
210
217BSTR_PUBLIC int
218bJustifyMargin(bstring b, int width, int space);
219
224BSTR_PUBLIC int
225bJustifyCenter(bstring b, int width, int space);
226
227/* Esoteric standards specific functions */
228
240BSTR_PUBLIC char *
241bStr2NetStr(const bstring b);
242
251BSTR_PUBLIC bstring
252bNetStr2Bstr(const char *buf);
253
259BSTR_PUBLIC bstring
260bBase64Encode(const bstring b);
261
269BSTR_PUBLIC bstring
270bBase64DecodeEx(const bstring b, int *boolTruncError);
271
281BSTR_PUBLIC struct bStream *
282bsUuDecode(struct bStream *sInp, int *badlines);
283
293
294BSTR_PUBLIC bstring
295bUuDecodeEx(const bstring src, int *badlines);
296
302BSTR_PUBLIC bstring
303bUuEncode(const bstring src);
304
312BSTR_PUBLIC bstring
313bYEncode(const bstring src);
314
320BSTR_PUBLIC bstring
321bYDecode(const bstring src);
322
323/* Writable stream */
324typedef int
325(*bNwrite)(const void *buf, size_t elsize, size_t nelem, void *parm);
326
332BSTR_PUBLIC struct bwriteStream *
333bwsOpen(bNwrite writeFn, void *parm);
334
342BSTR_PUBLIC int
343bwsWriteBstr(struct bwriteStream *stream, const bstring b);
344
350BSTR_PUBLIC int
351bwsWriteBlk(struct bwriteStream *stream, void *blk, int len);
352
356BSTR_PUBLIC int
357bwsWriteFlush(struct bwriteStream *stream);
358
363BSTR_PUBLIC int
364bwsIsEOF(const struct bwriteStream *stream);
365
372BSTR_PUBLIC int
373bwsBuffLength(struct bwriteStream *stream, int sz);
374
382BSTR_PUBLIC void *
383bwsClose(struct bwriteStream *stream);
384
385/* Security functions */
386#define bSecureDestroy(b) \
387do { \
388 if ((b) && (b)->mlen > 0 && (b)->data) { \
389 (void)memset((b)->data, 0, (size_t)(b)->mlen); \
390 } \
391 (void)bdestroy((b)); \
392} while (0)
393
394#define bSecureWriteProtect(t) \
395do { \
396 if ((t).mlen >= 0) { \
397 if ((t).mlen > (t).slen)) { \
398 (void)memset((t).data + (t).slen, 0, \
399 (size_t)(t).mlen - (t).slen); \
400 } \
401 (t).mlen = -1; \
402 } \
403} while (0)
404
414BSTR_PUBLIC bstring
415bSecureInput(int maxlen, int termchar, bNgetc vgetchar, void *vgcCtx);
416
417#ifdef __cplusplus
418}
419#endif
420
421#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 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.