OpenFAST
Wind turbine multiphysics simulator
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 */
BSTR_PUBLIC int bFill(bstring a, char c, int len)
Fill a given bstring with the character in parameter c, for a length n.
Definition: bstraux.c:74
BSTR_PUBLIC bstring bSecureInput(int maxlen, int termchar, bNgetc vgetchar, void *vgcCtx)
Read input from an abstracted input interface, for a length of at most maxlen characters.
Definition: bstraux.c:973
C implementaion of bstring functions.
BSTR_PUBLIC bstring bBase64Encode(const bstring b)
Generate a base64 encoding.
Definition: bstraux.c:343
BSTR_PUBLIC bstring bYDecode(const bstring src)
Performs a YDecode of a block of data.
Definition: bstraux.c:846
BSTR_PUBLIC bstring bYEncode(const bstring src)
Performs a YEncode of a block of data.
Definition: bstraux.c:815
BSTR_PUBLIC int bReverse(bstring b)
Reverse the contents of b in place.
Definition: bstraux.c:90
BSTR_PUBLIC int bSetChar(bstring b, int pos, char c)
Sets the character at position pos to the character c in the bstring a.
Definition: bstraux.c:955
BSTR_PUBLIC char * bStr2NetStr(const bstring b)
Convert a bstring to a netstring.
Definition: bstraux.c:285
BSTR_PUBLIC int bwsWriteBstr(struct bwriteStream *stream, const bstring b)
Send a bstring to a bwriteStream.
Definition: bstraux.c:1079
BSTR_PUBLIC bstring bNetStr2Bstr(const char *buf)
Convert a netstring to a bstring.
Definition: bstraux.c:306
BSTR_PUBLIC int bReplicate(bstring b, int n)
Replicate the contents of b end to end n times and replace it in b.
Definition: bstraux.c:84
BSTR_PUBLIC bstring bStrfTime(const char *fmt, const struct tm *timeptr)
Takes a format string that is compatible with strftime and a struct tm pointer, formats the time acco...
Definition: bstraux.c:896
BSTR_PUBLIC bstring bHead(bstring b, int n)
Return with a string of the first n characters of b.
Definition: bstraux.c:62
BSTR_PUBLIC int bwsBuffLength(struct bwriteStream *stream, int sz)
Set the length of the buffer used by the bwsStream.
Definition: bstraux.c:1144
BSTR_PUBLIC int bSetCstrChar(bstring a, int pos, char c)
Sets the character at position pos to the character c in the bstring a.
Definition: bstraux.c:935
Definition: bstraux.c:1024
BSTR_PUBLIC int bwsIsEOF(const struct bwriteStream *stream)
Returns 0 if the stream is currently writable, 1 if the core stream has responded by not accepting th...
Definition: bstraux.c:1132
BSTR_PUBLIC int bwsWriteBlk(struct bwriteStream *stream, void *blk, int len)
Send a block of data a bwriteStream.
Definition: bstraux.c:1121
BSTR_PUBLIC struct bStream * bsUuDecode(struct bStream *sInp, int *badlines)
Creates a bStream which performs the UUDecode of an an input stream.
Definition: bstraux.c:740
BSTR_PUBLIC void * bwsClose(struct bwriteStream *stream)
Close the bwriteStream, and return the handle to the stream that was originally used to open the give...
Definition: bstraux.c:1158
BSTR_PUBLIC bstring bUuEncode(const bstring src)
Performs a UUEncode of a block of data.
Definition: bstraux.c:773
BSTR_PUBLIC struct bwriteStream * bwsOpen(bNwrite writeFn, void *parm)
Wrap a given open stream (described by a fwrite work-a-like function pointer and stream handle) into ...
Definition: bstraux.c:1033
BSTR_PUBLIC int bwsWriteFlush(struct bwriteStream *stream)
Force any pending data to be written to the core stream.
Definition: bstraux.c:1064
BSTR_PUBLIC struct bStream * bsFromBstr(const bstring b)
Create a bStream whose contents are a copy of the bstring passed in.
Definition: bstraux.c:243
Definition: bstrlib.c:1925
BSTR_PUBLIC int bJustifyLeft(bstring b, int space)
Left justify a string.
Definition: bstraux.c:137
BSTR_PUBLIC int bJustifyMargin(bstring b, int width, int space)
Stretch a string to flush against left and right margins by evenly distributing additional white spac...
Definition: bstraux.c:194
BSTR_PUBLIC int bInsertChrs(bstring b, int pos, int len, unsigned char c, unsigned char fill)
Insert a repeated sequence of a given character into the string at position pos for a length len...
Definition: bstraux.c:111
BSTR_PUBLIC int bJustifyRight(bstring b, int width, int space)
Right justify a string to within a given width.
Definition: bstraux.c:161
BSTR_PUBLIC bstring bTail(bstring b, int n)
Return with a string of the last n characters of b.
Definition: bstraux.c:50
Definition: bstrlib.h:80
BSTR_PUBLIC bstring bBase64DecodeEx(const bstring b, int *boolTruncError)
Decode a base64 block of data.
Definition: bstraux.c:430
BSTR_PUBLIC bstring bUuDecodeEx(const bstring src, int *badlines)
Performs a UUDecode of a block of data.
Definition: bstraux.c:635
BSTR_PUBLIC int bJustifyCenter(bstring b, int width, int space)
Center a string&#39;s non-white space characters to within a given width by inserting whitespaces at the ...
Definition: bstraux.c:177