Debian Codegraph @

runtime/stringbuf.h

raw
1 /* stringbuf.h
2 * The counted string object
3 *
4 * \author Rainer Gerhards <[email protected]>
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 * Contract notes:
36 * - The buffer stores counted bytes and may keep `pBuf == NULL` for empty
37 * strings.
38 * - Constructors and mutating helpers do not guarantee a trailing NUL.
39 * Callers must invoke `cstrFinalize()` before using any API that exposes the
40 * buffer as a classic C string.
41 * - `cstrFinalize()` writes the trailing NUL but is not a hard state change in
42 * release builds; appending after finalize remains invalid API usage even if
43 * some call paths appear to tolerate it.
44 */
45 typedef struct cstr_s {
46 #ifndef NDEBUG
47 rsObjID OID; /**< object ID */
48 sbool isFinalized;
49 #endif
50 uchar *pBuf; /**< pointer to the string buffer, may be NULL if string is empty */
51 size_t iBufSize; /**< current maximum size of the string buffer */
52 size_t iStrLen; /**< length of the string in characters. */
53 } cstr_t;
54
55
56 /**
57 * Construct an empty counted string.
58 *
59 * The resulting object is not finalized. Call `cstrFinalize()` before passing
60 * it to any consumer that expects a NUL-terminated string view.
61 */
62 rsRetVal cstrConstruct(cstr_t **ppThis);
63 #define rsCStrConstruct(x) cstrConstruct((x))
64 rsRetVal cstrConstructFromESStr(cstr_t **ppThis, es_str_t *str);
65 rsRetVal rsCStrConstructFromszStr(cstr_t **ppThis, const uchar *sz);
66 rsRetVal rsCStrConstructFromCStr(cstr_t **ppThis, const cstr_t *pFrom);
67 rsRetVal rsCStrConstructFromszStrf(cstr_t **ppThis, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
68
69 /**
70 * Destruct the string buffer object.
71 */
72 void rsCStrDestruct(cstr_t **ppThis);
73 #define cstrDestruct(x) rsCStrDestruct((x))
74
75
76 /* Append a character to the current string object. This may only be done until
77 * cstrFinalize() is called.
78 * rgerhards, 2009-06-16
79 */
80 rsRetVal cstrAppendChar(cstr_t *pThis, const uchar c);
81
82 /* Finalize the string object.
83 *
84 * This writes the trailing NUL expected by `cstrGetSzStrNoNULL()`, regex
85 * matching, and other C-string consumers. Call it after the last mutation and
86 * before any read that depends on classic NUL-terminated semantics.
87 * rgerhards, 2009-06-16
88 */
89 #ifdef NDEBUG
90 #define cstrFinalize(pThis) \
91 { \
92 if ((pThis)->iStrLen > 0) (pThis)->pBuf[(pThis)->iStrLen] = '\0'; /* space is always reserved for this */ \
93 }
94 #else
95 #define cstrFinalize(pThis) \
96 { \
97 if ((pThis)->iStrLen > 0) (pThis)->pBuf[(pThis)->iStrLen] = '\0'; /* space is always reserved for this */ \
98 (pThis)->isFinalized = 1; \
99 }
100 #endif
101
102
103 /**
104 * Truncate "n" number of characters from the end of the
105 * string. The buffer remains unchanged, just the
106 * string length is manipulated. This is for performance
107 * reasons.
108 */
109 rsRetVal rsCStrTruncate(cstr_t *pThis, size_t nTrunc);
110
111 void cstrTrimTrailingWhiteSpace(cstr_t *pThis);
112
113 /**
114 * Append a string to the buffer. For performance reasons,
115 * use rsCStrAppenStrWithLen() if you know the length.
116 *
117 * \param psz pointer to string to be appended. Must not be NULL.
118 *
119 * The object remains non-finalized after this call.
120 */
121 rsRetVal rsCStrAppendStr(cstr_t *pThis, const uchar *psz);
122
123 /**
124 * Append a string to the buffer.
125 *
126 * \param psz pointer to string to be appended. Must not be NULL.
127 * \param iStrLen the length of the string pointed to by psz
128 *
129 * The object remains non-finalized after this call.
130 */
131 rsRetVal rsCStrAppendStrWithLen(cstr_t *pThis, const uchar *psz, size_t iStrLen);
132
133 /**
134 * Append a printf-style formated string to the buffer.
135 *
136 * \param fmt pointer to the format string (see man 3 printf for details). Must not be NULL.
137 *
138 * The object remains non-finalized after this call.
139 */
140 rsRetVal rsCStrAppendStrf(cstr_t *pThis, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
141
142 /**
143 * Append an integer to the string. No special formatting is
144 * done.
145 */
146 rsRetVal rsCStrAppendInt(cstr_t *pThis, long i);
147
148
149 rsRetVal strExit(void);
150 /* Return a borrowed NUL-terminated view of the internal buffer.
151 *
152 * The object must already be finalized. The returned pointer is owned by the
153 * cstr object and remains valid only until the next mutation or destruction.
154 * Empty strings are returned as `""`, never as NULL.
155 */
156 uchar *cstrGetSzStrNoNULL(cstr_t *pThis);
157 #define rsCStrGetSzStrNoNULL(x) cstrGetSzStrNoNULL(x)
158 /* Replace the current contents from a classic NUL-terminated string.
159 *
160 * Passing `NULL` resets the object to an empty string and may leave
161 * `pBuf == NULL`. The setter does not finalize the object.
162 */
163 rsRetVal rsCStrSetSzStr(cstr_t *pThis, uchar *pszNew);
164 int rsCStrCStrCmp(cstr_t *pCS1, cstr_t *pCS2);
165 int rsCStrSzStrCmp(cstr_t *pCS1, uchar *psz, size_t iLenSz);
166 int rsCStrOffsetSzStrCmp(cstr_t *pCS1, size_t iOffset, uchar *psz, size_t iLenSz);
167 int rsCStrLocateSzStr(cstr_t *pCStr, uchar *sz);
168 int rsCStrLocateInSzStr(cstr_t *pThis, uchar *sz);
169 int rsCStrSzStrStartsWithCStr(cstr_t *pCS1, uchar *psz, size_t iLenSz);
170 int rsCStrSzStrEndsWithCStr(cstr_t *pCS1, uchar *psz, size_t iLenSz);
171 rsRetVal rsCStrSzStrMatchRegex(cstr_t *pCS1, uchar *psz, int iType, void *cache);
172 void rsCStrRegexDestruct(void *rc);
173
174 /* Convert to an owned C string and destroy the counted-string object.
175 *
176 * Ownership of the returned buffer transfers to the caller, which must
177 * `free()` it unless `bRetNULL == 1` and the string was empty. `*ppThis` is
178 * always cleared on return.
179 */
180 rsRetVal cstrConvSzStrAndDestruct(cstr_t **pThis, uchar **ppSz, int bRetNULL);
181 rsRetVal cstrAppendCStr(cstr_t *pThis, cstr_t *pstrAppend);
182
183 /* now come inline-like functions */
184 #ifdef NDEBUG
185 #define cstrLen(x) ((size_t)((x)->iStrLen))
186 #else
187 size_t cstrLen(cstr_t *pThis);
188 #endif
189 #define rsCStrLen(s) cstrLen((s))
190
191 #define rsCStrGetBufBeg(x) ((x)->pBuf)
192
193 rsRetVal strInit(void);
194
195 #endif /* single include */