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;
132 if (!FreeMe->ConstBuf)
137 long StrTol(StrBuf *Buf)
140 return atol(Buf->buf);
146 void StrBufAppendBuf(StrBuf *Buf, StrBuf *AppendBuf, size_t Offset)
148 if ((AppendBuf == NULL) || (Buf == NULL))
150 if (Buf->BufSize - Offset < AppendBuf->BufUsed)
151 IncreaseBuf(Buf, (Buf->BufUsed > 0), AppendBuf->BufUsed);
152 memcpy(Buf->buf + Buf->BufUsed,
153 AppendBuf->buf + Offset,
154 AppendBuf->BufUsed - Offset);
155 Buf->BufUsed += AppendBuf->BufUsed - Offset;
156 Buf->buf[Buf->BufUsed] = '\0';
160 inline int StrBufNum_tokens(const StrBuf *source, char tok)
162 return num_tokens(source->buf, tok);
166 int StrBufSub(StrBuf *dest, const StrBuf *Source, size_t Offset, size_t nChars)
169 if (Offset > Source->BufUsed)
174 if (Offset + nChars < Source->BufUsed)
176 if (nChars > dest->BufSize)
177 IncreaseBuf(dest, 0, nChars + 1);
178 memcpy(dest->buf, Source->buf + Offset, nChars);
179 dest->BufUsed = nChars;
180 dest->buf[dest->BufUsed] = '\0';
183 NCharsRemain = Source->BufUsed - Offset;
184 if (NCharsRemain > dest->BufSize)
185 IncreaseBuf(dest, 0, NCharsRemain + 1);
186 memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
187 dest->BufUsed = NCharsRemain;
188 dest->buf[dest->BufUsed] = '\0';
192 void StrBufPrintf(StrBuf *Buf, const char *format, ...)
194 size_t nWritten = Buf->BufSize + 1;
197 while (nWritten >= Buf->BufSize) {
198 va_start(arg_ptr, format);
199 nWritten = vsnprintf(Buf->buf, Buf->BufSize, format, arg_ptr);
201 Buf->BufUsed = nWritten ;
202 if (nWritten >= Buf->BufSize)
203 IncreaseBuf(Buf, 0, 0);
209 * \brief a string tokenizer
210 * \param dest Destination StringBuffer
211 * \param Source StringBuffer to read into
212 * \param separator tokenizer param
213 * \returns -1 if not found, else length of token.
215 int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
217 const char *s, *e; //* source * /
218 int len = 0; //* running total length of extracted string * /
219 int current_token = 0; //* token currently being processed * /
221 if ((Source == NULL) || (Source->BufUsed ==0)) {
225 e = s + Source->BufUsed;
231 //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);
235 while ((s<e) && !IsEmptyStr(s)) {
236 if (*s == separator) {
239 if (len >= dest->BufSize)
240 if (!IncreaseBuf(dest, 1, -1))
242 if ( (current_token == parmnum) &&
247 else if (current_token > parmnum) {
253 dest->buf[len] = '\0';
256 if (current_token < parmnum) {
257 //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
260 //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
266 * extract_int() - extract an int parm w/o supplying a buffer
268 int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator)
277 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
284 * extract_long() - extract an long parm w/o supplying a buffer
286 long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator)
295 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
303 * extract_unsigned_long() - extract an unsigned long parm
305 unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator)
314 if (StrBufExtract_token(&tmp, Source, parmnum, separator) > 0)
323 * \brief Input binary data from socket
324 * \param buf the buffer to get the input to
325 * \param bytes the maximal number of bytes to read
327 int StrBufTCP_read_line(StrBuf *buf, int fd, int append, const char **Error)
334 slen = len = buf->BufUsed;
336 rlen = read(fd, &buf->buf[len], 1);
338 *Error = strerror(errno);
344 if (buf->buf[len] == '\n')
346 if (buf->buf[len] != '\r')
348 if (!(len < buf->BufSize)) {
350 buf->buf[len+1] = '\0';
351 IncreaseBuf(buf, 1, -1);
355 buf->buf[len] = '\0';
359 void StrBufCutLeft(StrBuf *Buf, int nChars)
361 if (nChars >= Buf->BufUsed) {
365 memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
366 Buf->BufUsed -= nChars;
369 void StrBufCutRight(StrBuf *Buf, int nChars)
371 if (nChars >= Buf->BufUsed) {
375 Buf->BufUsed -= nChars;
376 Buf->buf[Buf->BufUsed] = '\0';
381 * string conversion function
383 void StrBufEUid_unescapize(StrBuf *target, StrBuf *source)
391 if (source == NULL ||target == NULL)
396 len = source->BufUsed;
397 for (a = 0; a < len; ++a) {
398 if (target->BufUsed >= target->BufSize)
399 IncreaseBuf(target, 1, -1);
401 if (source->buf[a] == '=') {
402 hex[0] = source->buf[a + 1];
403 hex[1] = source->buf[a + 2];
406 sscanf(hex, "%02x", &b);
407 target->buf[target->BufUsed] = b;
408 target->buf[++target->BufUsed] = 0;
412 target->buf[target->BufUsed] = source->buf[a];
413 target->buf[++target->BufUsed] = 0;
420 * string conversion function
422 void StrBufEUid_escapize(StrBuf *target, StrBuf *source)
429 if (source == NULL ||target == NULL)
434 len = source->BufUsed;
435 for (i=0; i<len; ++i) {
436 if (target->BufUsed + 4 >= target->BufSize)
437 IncreaseBuf(target, 1, -1);
438 if ( (isalnum(source->buf[i])) ||
439 (source->buf[i]=='-') ||
440 (source->buf[i]=='_') ) {
441 target->buf[target->BufUsed++] = source->buf[i];
444 sprintf(&target->buf[target->BufUsed],
446 (0xFF &source->buf[i]));
447 target->BufUsed += 3;
450 target->buf[target->BufUsed + 1] = '\0';