1 /* stringbuf.h
2  * The counted string object
3  *
4  * \author  Rainer Gerhards <rgerhards@adiscon.com>
5  * \date    2005-09-07
6  *          Initial version  begun.
7  *
8  * Copyright 2005-2016 Adiscon GmbH. All Rights Reserved.
9  *
10  * This file is part of the rsyslog runtime library.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *       http://www.apache.org/licenses/LICENSE-2.0
17  *       -or-
18  *       see COPYING.ASL20 in the source distribution
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  */
26 #ifndef _STRINGBUF_H_INCLUDED__
27 #define _STRINGBUF_H_INCLUDED__ 1
28 
29 #include <assert.h>
30 #include <libestr.h>
31 
32 /**
33  * The dynamic string buffer object.
34  */
35 typedef struct cstr_s
36 {
37 #ifndef	NDEBUG
38 	rsObjID OID;		/**< object ID */
39 	sbool isFinalized;
40 #endif
41 	uchar *pBuf;		/**< pointer to the string buffer, may be NULL if string is empty */
42 	size_t iBufSize;	/**< current maximum size of the string buffer */
43 	size_t iStrLen;		/**< length of the string in characters. */
44 } cstr_t;
45 
46 
47 /**
48  * Construct a rsCStr object.
49  */
50 rsRetVal cstrConstruct(cstr_t **ppThis);
51 #define rsCStrConstruct(x) cstrConstruct((x))
52 rsRetVal cstrConstructFromESStr(cstr_t **ppThis, es_str_t *str);
53 rsRetVal rsCStrConstructFromszStr(cstr_t **ppThis, const uchar *sz);
54 rsRetVal rsCStrConstructFromCStr(cstr_t **ppThis, const cstr_t *pFrom);
55 rsRetVal rsCStrConstructFromszStrf(cstr_t **ppThis, const char *fmt, ...) __attribute__((format(printf,2, 3)));
56 
57 /**
58  * Destruct the string buffer object.
59  */
60 void rsCStrDestruct(cstr_t **ppThis);
61 #define cstrDestruct(x) rsCStrDestruct((x))
62 
63 
64 /* Append a character to the current string object. This may only be done until
65  * cstrFinalize() is called.
66  * rgerhards, 2009-06-16
67  */
68 rsRetVal cstrAppendChar(cstr_t *pThis, const uchar c);
69 
70 /* Finalize the string object. This must be called after all data is added to it
71  * but before that data is used.
72  * rgerhards, 2009-06-16
73  */
74 #ifdef NDEBUG
75 #define cstrFinalize(pThis) { \
76 	if((pThis)->iStrLen > 0) \
77 		(pThis)->pBuf[(pThis)->iStrLen] = '\0'; /* space is always reserved for this */ \
78 }
79 #else
80 #define cstrFinalize(pThis) { \
81 	if((pThis)->iStrLen > 0) \
82 		(pThis)->pBuf[(pThis)->iStrLen] = '\0'; /* space is always reserved for this */ \
83 	(pThis)->isFinalized = 1; \
84 }
85 #endif
86 
87 
88 /**
89  * Truncate "n" number of characters from the end of the
90  * string. The buffer remains unchanged, just the
91  * string length is manipulated. This is for performance
92  * reasons.
93  */
94 rsRetVal rsCStrTruncate(cstr_t *pThis, size_t nTrunc);
95 
96 void cstrTrimTrailingWhiteSpace(cstr_t *pThis);
97 
98 /**
99  * Append a string to the buffer. For performance reasons,
100  * use rsCStrAppenStrWithLen() if you know the length.
101  *
102  * \param psz pointer to string to be appended. Must not be NULL.
103  */
104 rsRetVal rsCStrAppendStr(cstr_t *pThis, const uchar* psz);
105 
106 /**
107  * Append a string to the buffer.
108  *
109  * \param psz pointer to string to be appended. Must not be NULL.
110  * \param iStrLen the length of the string pointed to by psz
111  */
112 rsRetVal rsCStrAppendStrWithLen(cstr_t *pThis, const uchar* psz, size_t iStrLen);
113 
114 /**
115  * Append a printf-style formated string to the buffer.
116  *
117  * \param fmt pointer to the format string (see man 3 printf for details). Must not be NULL.
118  */
119 rsRetVal rsCStrAppendStrf(cstr_t *pThis, const char *fmt, ...) __attribute__((format(printf,2, 3)));
120 
121 /**
122  * Append an integer to the string. No special formatting is
123  * done.
124  */
125 rsRetVal rsCStrAppendInt(cstr_t *pThis, long i);
126 
127 
128 rsRetVal strExit(void);
129 uchar*  cstrGetSzStrNoNULL(cstr_t *pThis);
130 #define rsCStrGetSzStrNoNULL(x) cstrGetSzStrNoNULL(x)
131 rsRetVal rsCStrSetSzStr(cstr_t *pThis, uchar *pszNew);
132 int rsCStrCStrCmp(cstr_t *pCS1, cstr_t *pCS2);
133 int rsCStrSzStrCmp(cstr_t *pCS1, uchar *psz, size_t iLenSz);
134 int rsCStrOffsetSzStrCmp(cstr_t *pCS1, size_t iOffset, uchar *psz, size_t iLenSz);
135 int rsCStrLocateSzStr(cstr_t *pCStr, uchar *sz);
136 int rsCStrLocateInSzStr(cstr_t *pThis, uchar *sz);
137 int rsCStrSzStrStartsWithCStr(cstr_t *pCS1, uchar *psz, size_t iLenSz);
138 rsRetVal rsCStrSzStrMatchRegex(cstr_t *pCS1, uchar *psz, int iType, void *cache);
139 void rsCStrRegexDestruct(void *rc);
140 
141 /* new calling interface */
142 rsRetVal cstrConvSzStrAndDestruct(cstr_t **pThis, uchar **ppSz, int bRetNULL);
143 rsRetVal cstrAppendCStr(cstr_t *pThis, cstr_t *pstrAppend);
144 
145 /* now come inline-like functions */
146 #ifdef NDEBUG
147 #	define cstrLen(x) ((int)((x)->iStrLen))
148 #else
149 	int cstrLen(cstr_t *pThis);
150 #endif
151 #define rsCStrLen(s) cstrLen((s))
152 
153 #define rsCStrGetBufBeg(x) ((x)->pBuf)
154 
155 rsRetVal strInit(void);
156 
157 #endif /* single include */
158