From: Wilfried Göesgens Date: Sun, 15 Feb 2009 21:59:55 +0000 (+0000) Subject: + add next token tokenizer X-Git-Tag: v7.86~1452 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=029bea4f206b00ba0850a1ec131fedf6ad7ee524 + add next token tokenizer --- diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 32fbd6eb9..330184cab 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -245,12 +245,20 @@ int StrBufTCP_read_buffered_line(StrBuf *Line, int StrBufSipLine(StrBuf *LineBuf, StrBuf *Buf, const char **Ptr); int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char separator); int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars); + unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, char separator); long StrBufExtract_long(const StrBuf* Source, int parmnum, char separator); int StrBufExtract_int(const StrBuf* Source, int parmnum, char separator); inline int StrBufNum_tokens(const StrBuf *source, char tok); int StrBufRemove_token(StrBuf *Source, int parmnum, char separator); +int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator); +int StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens); +unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator); +long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator); +int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator); + + void StrBufAppendBufPlain(StrBuf *Buf, const char *AppendBuf, long AppendSize, unsigned long Offset); void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset); void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...); diff --git a/libcitadel/lib/stringbuf.c b/libcitadel/lib/stringbuf.c index 1c5a4d0c9..f22011cc7 100644 --- a/libcitadel/lib/stringbuf.c +++ b/libcitadel/lib/stringbuf.c @@ -936,6 +936,9 @@ int StrBufExtract_token(StrBuf *dest, const StrBuf *Source, int parmnum, char se } + + + /** * \brief a string tokenizer to fetch an integer * \param dest Destination StringBuffer @@ -1013,6 +1016,207 @@ unsigned long StrBufExtract_unsigned_long(const StrBuf* Source, int parmnum, cha +/** + * \brief a string tokenizer + * \param dest Destination StringBuffer + * \param Source StringBuffer to read into + * \param pStart pointer to the end of the last token. Feed with NULL. + * \param separator tokenizer param + * \returns -1 if not found, else length of token. + */ +int StrBufExtract_NextToken(StrBuf *dest, const StrBuf *Source, const char **pStart, char separator) +{ + const char *s, *EndBuffer; //* source * / + int len = 0; //* running total length of extracted string * / + int current_token = 0; //* token currently being processed * / + + if (dest != NULL) { + dest->buf[0] = '\0'; + dest->BufUsed = 0; + } + else + return(-1); + + if ((Source == NULL) || + (Source->BufUsed ==0)) { + return(-1); + } + if (*pStart == NULL) + *pStart = Source->buf; + + EndBuffer = Source->buf + Source->BufUsed; + + if ((*pStart < Source->buf) || + (*pStart > EndBuffer)) { + return (-1); + } + + + s = *pStart; + + //cit_backtrace(); + //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source); + + while ((s= dest->BufSize) + if (!IncreaseBuf(dest, 1, -1)) { + *pStart = EndBuffer + 1; + break; + } + if ( (current_token == 0) && + (*s != separator)) { + dest->buf[len] = *s; + ++len; + } + else if (current_token > 0) { + *pStart = s; + break; + } + ++s; + } + *pStart = s; + (*pStart) ++; + + dest->buf[len] = '\0'; + dest->BufUsed = len; + //lprintf (CTDL_DEBUG,"test BufUsed ==0)) { + return(-1); + } + if (nTokens == 0) + return Source->BufUsed; + + if (*pStart == NULL) + *pStart = Source->buf; + + EndBuffer = Source->buf + Source->BufUsed; + + if ((*pStart < Source->buf) || + (*pStart > EndBuffer)) { + return (-1); + } + + + s = *pStart; + + //cit_backtrace(); + //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source); + + while ((s= nTokens) { + break; + } + ++s; + } + *pStart = s; + (*pStart) ++; + + return(len); +} + +/** + * \brief a string tokenizer to fetch an integer + * \param dest Destination StringBuffer + * \param parmnum n'th parameter to extract + * \param separator tokenizer param + * \returns 0 if not found, else integer representation of the token + */ +int StrBufExtractNext_int(const StrBuf* Source, const char **pStart, char separator) +{ + StrBuf tmp; + char buf[64]; + + tmp.buf = buf; + buf[0] = '\0'; + tmp.BufSize = 64; + tmp.BufUsed = 0; + tmp.ConstBuf = 1; + if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0) + return(atoi(buf)); + else + return 0; +} + +/** + * \brief a string tokenizer to fetch a long integer + * \param dest Destination StringBuffer + * \param parmnum n'th parameter to extract + * \param separator tokenizer param + * \returns 0 if not found, else long integer representation of the token + */ +long StrBufExtractNext_long(const StrBuf* Source, const char **pStart, char separator) +{ + StrBuf tmp; + char buf[64]; + + tmp.buf = buf; + buf[0] = '\0'; + tmp.BufSize = 64; + tmp.BufUsed = 0; + tmp.ConstBuf = 1; + if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0) + return(atoi(buf)); + else + return 0; +} + + +/** + * \brief a string tokenizer to fetch an unsigned long + * \param dest Destination StringBuffer + * \param parmnum n'th parameter to extract + * \param separator tokenizer param + * \returns 0 if not found, else unsigned long representation of the token + */ +unsigned long StrBufExtractNext_unsigned_long(const StrBuf* Source, const char **pStart, char separator) +{ + StrBuf tmp; + char buf[64]; + char *pnum; + + tmp.buf = buf; + buf[0] = '\0'; + tmp.BufSize = 64; + tmp.BufUsed = 0; + tmp.ConstBuf = 1; + if (StrBufExtract_NextToken(&tmp, Source, pStart, separator) > 0) { + pnum = &buf[0]; + if (*pnum == '-') + pnum ++; + return (unsigned long) atol(pnum); + } + else + return 0; +} + + + /** * \brief Read a line from socket * flushes and closes the FD on error