]>
git.ipfire.org Git - thirdparty/sarg.git/blob - stringbuffer.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 \brief Efficient? strings storage
29 Store strings in a globaly allocated memory to avoid memory waste and
33 #include "include/conf.h"
34 #include "include/stringbuffer.h"
36 //! Default size of the string buffer (I hope it fits inside one memory page).
37 #define STRINGBUFFER_SIZE (4096-sizeof(struct StringBufferStruct))
40 * \brief String storage data.
42 * Strings are concatenated in fixed size buffers. The buffers are linked
43 * in a list. New buffers are added as the previous buffers are filled.
45 struct StringBufferStruct
47 //! Next buffer in the chained list.
48 StringBufferObject Next
;
49 //! How many buffer bytes are left.
51 //! Where the strings are stored.
56 * Create an object to store constant strings.
58 * \return The created object or NULL if it failed.
60 StringBufferObject
StringBuffer_Create(void)
62 StringBufferObject SObj
;
64 SObj
=(StringBufferObject
)calloc(1,sizeof(*SObj
));
65 if (!SObj
) return(NULL
);
70 * Destroy the object created by StringBuffer_Create().
72 * Any string pointer to the destroyed object becomes invalid.
74 * \param SPtr A pointer to the object created by StringBuffer_Create().
75 * The pointer is reset to NULL before the functrion returns to prevent
76 * subsequent use of the freed pointer.
78 void StringBuffer_Destroy(StringBufferObject
*SPtr
)
80 StringBufferObject SObj
;
81 StringBufferObject Next
;
83 if (!SPtr
|| !*SPtr
) return;
90 if (SObj
->Buffer
) free(SObj
->Buffer
);
97 * Store a string in an existing buffer.
99 static char *StringBuffer_StoreInBuffer(StringBufferObject SObj
,const char *String
,int Length
)
105 Start
=STRINGBUFFER_SIZE
-SObj
->BytesLeft
;
107 else if (Length
>=STRINGBUFFER_SIZE
)
109 SObj
->BytesLeft
=Length
+1;
110 SObj
->Buffer
=malloc(SObj
->BytesLeft
);
114 SObj
->BytesLeft
=STRINGBUFFER_SIZE
;
115 SObj
->Buffer
=malloc(SObj
->BytesLeft
);
117 if (!SObj
->Buffer
) return(NULL
);
118 strncpy(SObj
->Buffer
+Start
,String
,Length
);
119 SObj
->Buffer
[Start
+Length
]='\0';
120 SObj
->BytesLeft
-=Length
+1;
121 return(SObj
->Buffer
+Start
);
125 * Add a string to the buffer. Duplicate strings are not merged.
126 * Each call to this function stores one copy of the string.
128 * \param SObj The string buffer object.
129 * \param String The string to store.
130 * \param Length The length of the string.
132 * \return The pointer to the stored string or NULL if the function
133 * failed. The returned string may be altered or truncated but not
136 char *StringBuffer_StoreLength(StringBufferObject SObj
,const char *String
,int Length
)
138 StringBufferObject SLast
;
141 if (!SObj
) return(NULL
);
143 // find a suitable buffer
147 if (!SObj
->Buffer
|| Length
<SObj
->BytesLeft
)
149 return(StringBuffer_StoreInBuffer(SObj
,String
,Length
));
155 // create a new buffer
156 SObj
=(StringBufferObject
)calloc(1,sizeof(*SObj
));
157 if (!SObj
) return(NULL
);
158 Ptr
=StringBuffer_StoreInBuffer(SObj
,String
,Length
);
169 * Add a string to the buffer. Duplicate strings are not merged.
170 * Each call to this function stores one copy of the string.
172 * \param SObj The string buffer object.
173 * \param String The string to store.
175 * \return The pointer to the stored string or NULL if the function
176 * failed. The returned string may be altered or truncated but not
179 char *StringBuffer_Store(StringBufferObject SObj
,const char *String
)
181 return(StringBuffer_StoreLength(SObj
,String
,strlen(String
)));