7 #include "libcitadel.h"
19 inline const char *ChrPtr(StrBuf *Str)
26 inline int StrLength(StrBuf *Str)
31 StrBuf* NewStrBuf(void)
35 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
36 NewBuf->buf = (char*) malloc(SIZ);
37 NewBuf->buf[0] = '\0';
38 NewBuf->BufSize = SIZ;
45 StrBuf* NewStrBufPlain(const char* ptr, int nChars)
51 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
53 CopySize = strlen(ptr);
57 while (Siz <= CopySize)
60 NewBuf->buf = (char*) malloc(Siz);
61 memcpy(NewBuf->buf, ptr, CopySize);
62 NewBuf->buf[CopySize] = '\0';
63 NewBuf->BufSize = Siz;
64 NewBuf->BufUsed = CopySize;
69 StrBuf* _NewConstStrBuf(const char* StringConstant, size_t SizeOfStrConstant)
73 NewBuf = (StrBuf*) malloc(sizeof(StrBuf));
74 NewBuf->buf = (char*) StringConstant;
75 NewBuf->BufSize = SizeOfStrConstant;
76 NewBuf->BufUsed = SizeOfStrConstant;
82 static int IncreaseBuf(StrBuf *Buf, int KeepOriginal, int DestSize)
85 size_t NewSize = Buf->BufSize * 2;
91 while (NewSize < DestSize)
94 NewBuf= (char*) malloc(NewSize);
97 memcpy(NewBuf, Buf->buf, Buf->BufUsed);
110 int FlushStrBuf(StrBuf *buf)
119 void FreeStrBuf (StrBuf **FreeMe)
121 if (!(*FreeMe)->ConstBuf)
122 free((*FreeMe)->buf);
127 void HFreeStrBuf (void *VFreeMe)
129 StrBuf *FreeMe = (StrBuf*)VFreeMe;
130 if (!FreeMe->ConstBuf)
135 long StrTol(StrBuf *Buf)
138 return atol(Buf->buf);
144 void StrBufAppendBuf(StrBuf *Buf, StrBuf *AppendBuf, size_t Offset)
146 if ((AppendBuf == NULL) || (Buf == NULL))
148 if (Buf->BufSize - Offset < AppendBuf->BufUsed)
149 IncreaseBuf(Buf, (Buf->BufUsed > 0), AppendBuf->BufUsed);
150 memcpy(Buf->buf + Buf->BufUsed - 1,
151 AppendBuf->buf + Offset,
152 AppendBuf->BufUsed - Offset);
153 Buf->BufUsed += AppendBuf->BufUsed - Offset;
154 Buf->buf[Buf->BufUsed] = '\0';
158 inline int StrBufNum_tokens(const StrBuf *source, char tok)
160 return num_tokens(source->buf, tok);
164 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
167 if (Offset > Source->BufUsed)
172 if (Offset + nChars < Source->BufUsed)
174 if (nChars < dest->BufSize)
175 IncreaseBuf(dest, 0, nChars + 1);
176 memcpy(dest->buf, Source->buf + Offset, nChars);
177 dest->BufUsed = nChars + 1;
178 dest->buf[dest->BufUsed] = '\0';
181 NCharsRemain = Source->BufUsed - Offset;
182 if (NCharsRemain < dest->BufSize)
183 IncreaseBuf(dest, 0, NCharsRemain + 1);
184 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
185 dest->BufUsed = NCharsRemain + 1;
186 dest->buf[dest->BufUsed] = '\0';
190 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
192 size_t nWritten = Buf->BufSize + 1;
195 while (nWritten >= Buf->BufSize) {
196 va_start(arg_ptr, format);
197 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
199 Buf->BufUsed = nWritten ;
200 if (nWritten >= Buf->BufSize)
201 IncreaseBuf(Buf, 0, 0);
207 * \brief a string tokenizer
208 * \param dest Destination StringBuffer
209 * \param Source StringBuffer to read into
210 * \param separator tokenizer param
211 * \returns -1 if not found, else length of token.
213 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
215 const char *s; //* source * /
216 int len = 0; //* running total length of extracted string * /
217 int current_token = 0; //* token currently being processed * /
219 if ((Source == NULL) || (Source->BufUsed ==0)) {
229 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
234 if (*s == separator) {
237 if (len >= dest->BufSize)
238 if (!IncreaseBuf(dest, 1, -1))
240 if ( (current_token == parmnum) &&
245 else if (current_token > parmnum) {
251 dest->buf[len] = '\0';
254 if (current_token < parmnum) {
255 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
258 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
264 * extract_int() - extract an int parm w/o supplying a buffer
266 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
275 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
282 * extract_long() - extract an long parm w/o supplying a buffer
284 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
293 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
301 * extract_unsigned_long() - extract an unsigned long parm
303 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
312 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
321 * \brief Input binary data from socket
322 * \param buf the buffer to get the input to
323 * \param bytes the maximal number of bytes to read
325 int StrBufTCP_read_line(StrBuf *buf, int fd, int append, const char **Error)
332 slen = len = buf->BufUsed;
333 while (buf->buf[len] != '\n') {
334 rlen = read(fd, &buf->buf[len], 1);
336 *Error = strerror(errno);
342 if (buf->buf[len] == '\n')
344 if (buf->buf[len] != '\r')
346 if (!(len < buf->BufSize)) {
348 buf->buf[len+1] = '\0';
349 IncreaseBuf(buf, 1, -1);
353 buf->buf[len] = '\0';
357 void StrBufCutLeft(StrBuf *Buf, int nChars)
359 if (nChars >= Buf->BufUsed) {
363 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
364 Buf->BufUsed -= nChars;
367 void StrBufCutRight(StrBuf *Buf, int nChars)
369 if (nChars >= Buf->BufUsed) {
373 Buf->BufUsed -= nChars;
374 Buf->buf[Buf->BufUsed] = '\0';
379 * string conversion function
381 void StrBufEUid_unescapize(StrBuf *target, StrBuf *source)
385 int target_length = 0;
390 if (source == NULL ||target == NULL)
395 len = source->BufUsed;
396 for (a = 0; a < len; ++a) {
397 if (target_length >= target->BufSize)
398 IncreaseBuf(target, 1, -1);
400 if (source->buf[a] == '=') {
401 hex[0] = source->buf[a + 1];
402 hex[1] = source->buf[a + 2];
405 sscanf(hex, "%02x", &b);
406 target->buf[target_length] = b;
407 target->buf[++target_length] = 0;
411 target->buf[target_length] = source->buf[a];
412 target->buf[++target_length] = 0;