~ubuntu-branches/ubuntu/lucid/rsyslog/lucid

« back to all changes in this revision

Viewing changes to stringbuf.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2007-10-19 17:21:49 UTC
  • Revision ID: james.westby@ubuntu.com-20071019172149-ie6ej2xve33mxiu7
Tags: upstream-1.19.10
ImportĀ upstreamĀ versionĀ 1.19.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*! \file stringbuf.h
 
2
 *  \brief The counted string object
 
3
 *
 
4
 * This is the byte-counted string class for rsyslog. It is a replacement
 
5
 * for classical \0 terminated string functions. We introduce it in
 
6
 * the hope it will make the program more secure, obtain some performance
 
7
 * and, most importantly, lay they foundation for syslog-protocol, which
 
8
 * requires strings to be able to handle embedded \0 characters.
 
9
 *
 
10
 * \author  Rainer Gerhards <rgerhards@adiscon.com>
 
11
 * \date    2005-09-07
 
12
 *          Initial version  begun.
 
13
 *
 
14
 * All functions in this "class" start with rsCStr (rsyslog Counted String).
 
15
 * Copyright 2005
 
16
 *     Rainer Gerhards and Adiscon GmbH. All Rights Reserved.
 
17
 *     This code is placed under the GPL.
 
18
 */
 
19
#ifndef _STRINGBUF_H_INCLUDED__
 
20
#define _STRINGBUF_H_INCLUDED__ 1
 
21
 
 
22
/** 
 
23
 * The dynamic string buffer object.
 
24
 */
 
25
struct rsCStrObject
 
26
{       
 
27
#ifndef NDEBUG
 
28
        rsObjID OID;            /**< object ID */
 
29
#endif
 
30
        uchar *pBuf;            /**< pointer to the string buffer, may be NULL if string is empty */
 
31
        uchar *pszBuf;          /**< pointer to the sz version of the string (after it has been created )*/
 
32
        size_t iBufSize;                /**< current maximum size of the string buffer */
 
33
        size_t iStrLen;         /**< length of the string in characters. */
 
34
        size_t iAllocIncrement; /**< the amount of bytes the string should be expanded if it needs to */
 
35
};
 
36
typedef struct rsCStrObject rsCStrObj;
 
37
 
 
38
 
 
39
/**
 
40
 * Construct a rsCStr object.
 
41
 */
 
42
rsCStrObj *rsCStrConstruct(void);
 
43
rsRetVal rsCStrConstructFromszStr(rsCStrObj **ppThis, uchar *sz);
 
44
rsRetVal rsCStrConstructFromCStr(rsCStrObj **ppThis, rsCStrObj *pFrom);
 
45
 
 
46
/**
 
47
 * Destruct the string buffer object.
 
48
 */
 
49
void rsCStrDestruct(rsCStrObj *pThis);
 
50
 
 
51
/**
 
52
 * Append a character to an existing string. If necessary, the
 
53
 * method expands the string buffer.
 
54
 *
 
55
 * \param c Character to append to string.
 
56
 */
 
57
rsRetVal rsCStrAppendChar(rsCStrObj *pThis, uchar c);
 
58
 
 
59
/**
 
60
 * Finish the string buffer dynamic allocation.
 
61
 */
 
62
rsRetVal rsCStrFinish(rsCStrObj *pThis);
 
63
 
 
64
/**
 
65
 * Truncate "n" number of characters from the end of the
 
66
 * string. The buffer remains unchanged, just the
 
67
 * string length is manipulated. This is for performance
 
68
 * reasons.
 
69
 */
 
70
rsRetVal rsCStrTruncate(rsCStrObj *pThis, size_t nTrunc);
 
71
 
 
72
rsRetVal rsCStrTrimTrailingWhiteSpace(rsCStrObj *pThis);
 
73
 
 
74
/**
 
75
 * Append a string to the buffer. For performance reasons,
 
76
 * use rsCStrAppenStrWithLen() if you know the length.
 
77
 *
 
78
 * \param psz pointer to string to be appended. Must not be NULL.
 
79
 */
 
80
rsRetVal rsCStrAppendStr(rsCStrObj *pThis, uchar* psz);
 
81
 
 
82
/**
 
83
 * Append a string to the buffer.
 
84
 *
 
85
 * \param psz pointer to string to be appended. Must not be NULL.
 
86
 * \param iStrLen the length of the string pointed to by psz
 
87
 */
 
88
rsRetVal rsCStrAppendStrWithLen(rsCStrObj *pThis, uchar* psz, size_t iStrLen);
 
89
 
 
90
/**
 
91
 * Set a new allocation incremet. This will influence
 
92
 * the allocation the next time the string will be expanded.
 
93
 * It can be set and changed at any time. If done immediately
 
94
 * after custructing the StrB object, this will also be
 
95
 * the inital allocation.
 
96
 *
 
97
 * \param iNewIncrement The new increment size
 
98
 *
 
99
 * \note It is possible to use a very low increment, e.g. 1 byte.
 
100
 *       This can generate a considerable overhead. We highly 
 
101
 *       advise not to use an increment below 32 bytes, except
 
102
 *       if you are very well aware why you are doing it ;)
 
103
 */
 
104
void rsCStrSetAllocIncrement(rsCStrObj *pThis, int iNewIncrement);
 
105
 
 
106
/**
 
107
 * Append an integer to the string. No special formatting is
 
108
 * done.
 
109
 */
 
110
rsRetVal rsCStrAppendInt(rsCStrObj *pThis, int i);
 
111
 
 
112
 
 
113
uchar*  rsCStrGetSzStr(rsCStrObj *pThis);
 
114
uchar*  rsCStrGetSzStrNoNULL(rsCStrObj *pThis);
 
115
rsRetVal rsCStrSetSzStr(rsCStrObj *pThis, uchar *pszNew);
 
116
rsRetVal rsCStrConvSzStrAndDestruct(rsCStrObj *pThis, uchar **ppSz, int bRetNULL);
 
117
int rsCStrCStrCmp(rsCStrObj *pCS1, rsCStrObj *pCS2);
 
118
int rsCStrSzStrCmp(rsCStrObj *pCS1, uchar *psz, size_t iLenSz);
 
119
int rsCStrOffsetSzStrCmp(rsCStrObj *pCS1, size_t iOffset, uchar *psz, size_t iLenSz);
 
120
int rsCStrLocateSzStr(rsCStrObj *pCStr, uchar *sz);
 
121
int rsCStrLocateInSzStr(rsCStrObj *pThis, uchar *sz);
 
122
int rsCStrStartsWithSzStr(rsCStrObj *pCS1, uchar *psz, size_t iLenSz);
 
123
int rsCStrSzStrStartsWithCStr(rsCStrObj *pCS1, uchar *psz, size_t iLenSz);
 
124
int rsCStrSzStrMatchRegex(rsCStrObj *pCS1, uchar *psz);
 
125
 
 
126
/* now come inline-like functions */
 
127
#ifdef NDEBUG
 
128
#       define rsCStrLen(x) ((int)((x)->iStrLen))
 
129
#else
 
130
        int rsCStrLen(rsCStrObj *pThis);
 
131
#endif
 
132
 
 
133
#define rsCStrGetBufBeg(x) ((x)->pBuf)
 
134
 
 
135
#endif /* single include */