+ add next token tokenizer
authorWilfried Göesgens <willi@citadel.org>
Sun, 15 Feb 2009 21:59:55 +0000 (21:59 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 15 Feb 2009 21:59:55 +0000 (21:59 +0000)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c

index 32fbd6eb966ff5a80ce197d64f348aa52e011f65..330184cabe12939113bceba8de32863d9649cb0d 100644 (file)
@@ -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, ...);
index 1c5a4d0c93a29aaad4c191e4fa35865a63e01838..f22011cc7f502a26f0540e58afddb46cf8870a72 100644 (file)
@@ -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<EndBuffer) && !IsEmptyStr(s)) {
+               if (*s == separator) {
+                       ++current_token;
+               }
+               if (len >= 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 <!: %s\n", dest);
+       //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
+       return(len);
+}
+
+
+/**
+ * \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 StrBufSkip_NTokenS(const StrBuf *Source, const char **pStart, char separator, int nTokens)
+{
+       const char *s, *EndBuffer;      //* source * /
+       int len = 0;                    //* running total length of extracted string * /
+       int current_token = 0;          //* token currently being processed * /
+
+       if ((Source == NULL) || 
+           (Source->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<EndBuffer) && !IsEmptyStr(s)) {
+               if (*s == separator) {
+                       ++current_token;
+               }
+               if (current_token >= 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