Back to index

libcitadel  8.12
Functions
Fast tokenizer to pull tokens in sequence
Stringbuffer, A class for manipulating strings with dynamic buffers

Quick tokenizer; demands of the user to pull its tokens in sequence. More...

Collaboration diagram for Fast tokenizer to pull tokens in sequence:

Functions

int StrBufHaveNextToken (const StrBuf *Source, const char **pStart)
 a string tokenizer; Bounds checker function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.
int StrBufExtract_NextToken (StrBuf *dest, const StrBuf *Source, const char **pStart, char separator)
 a string tokenizer
int StrBufSkip_NTokenS (const StrBuf *Source, const char **pStart, char separator, int nTokens)
 a string tokenizer
int StrBufExtractNext_int (const StrBuf *Source, const char **pStart, char separator)
 a string tokenizer to fetch an integer
long StrBufExtractNext_long (const StrBuf *Source, const char **pStart, char separator)
 a string tokenizer to fetch a long integer
unsigned long StrBufExtractNext_unsigned_long (const StrBuf *Source, const char **pStart, char separator)
 a string tokenizer to fetch an unsigned long

Detailed Description

Quick tokenizer; demands of the user to pull its tokens in sequence.


Function Documentation

int StrBufExtract_NextToken ( StrBuf dest,
const StrBuf Source,
const char **  pStart,
char  separator 
)

a string tokenizer

Parameters:
destDestination StringBuffer
SourceStringBuffer to read into
pStartpointer to the end of the last token. Feed with NULL on start.
separatortokenizer
Returns:
-1 if not found, else length of token.

Definition at line 1555 of file stringbuf.c.

{
       const char *s;          /* source */
       const char *EndBuffer;  /* end stop of source buffer */
       int current_token = 0;      /* token currently being processed */
       int len = 0;         /* running total length of extracted string */

       if ((Source          == NULL) || 
           (Source->BufUsed == 0)      ) 
       {
              *pStart = StrBufNOTNULL;
              if (dest != NULL)
                     FlushStrBuf(dest);
              return -1;
       }
        
       EndBuffer = Source->buf + Source->BufUsed;

       if (dest != NULL) 
       {
              dest->buf[0] = '\0';
              dest->BufUsed = 0;
       }
       else
       {
              *pStart = EndBuffer + 1;
              return -1;
       }

       if (*pStart == NULL)
       {
              *pStart = Source->buf; /* we're starting to examine this buffer. */
       }
       else if ((*pStart < Source->buf) || 
               (*pStart > EndBuffer  )   ) 
       {
              return -1; /* no more tokens to find. */
       }

       s = *pStart;
       /* start to find the next token */
       while ((s <= EndBuffer)      && 
              (current_token == 0) ) 
       {
              if (*s == separator) 
              {
                     /* we found the next token */
                     ++current_token;
              }

              if (len >= dest->BufSize) 
              {
                     /* our Dest-buffer isn't big enough, increase it. */
                     dest->BufUsed = len;

                     if (IncreaseBuf(dest, 1, -1) < 0) {
                            /* WHUT? no more mem? bail out. */
                            s = EndBuffer;
                            dest->BufUsed --;
                            break;
                     }
              }

              if ( (current_token == 0 ) &&   /* are we in our target token? */
                   (!IsEmptyStr(s)     ) &&
                   (separator     != *s)    ) /* don't copy the token itself */
              {
                     dest->buf[len] = *s;    /* Copy the payload */
                     ++len;                  /* remember the bigger size. */
              }

              ++s;
       }

       /* did we reach the end? */
       if ((s > EndBuffer)) {
              EndBuffer = StrBufNOTNULL;
              *pStart = EndBuffer;
       }
       else {
              *pStart = s;  /* remember the position for the next run */
       }

       /* sanitize our extracted token */
       dest->buf[len] = '\0';
       dest->BufUsed  = len;

       return (len);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int StrBufExtractNext_int ( const StrBuf Source,
const char **  pStart,
char  separator 
)

a string tokenizer to fetch an integer

Parameters:
SourceStringBuffer to read from
pStartCursor on the tokenstring
separatortokenizer character
Returns:
0 if not found, else integer representation of the token

Definition at line 1707 of file stringbuf.c.

{
       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;
}

Here is the call graph for this function:

long StrBufExtractNext_long ( const StrBuf Source,
const char **  pStart,
char  separator 
)

a string tokenizer to fetch a long integer

Parameters:
SourceStringBuffer to read from
pStartCursor on the tokenstring
separatortokenizer character
Returns:
0 if not found, else long integer representation of the token

Definition at line 1731 of file stringbuf.c.

{
       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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned long StrBufExtractNext_unsigned_long ( const StrBuf Source,
const char **  pStart,
char  separator 
)

a string tokenizer to fetch an unsigned long

Parameters:
SourceStringBuffer to read from
pStartCursor on the tokenstring
separatortokenizer character
Returns:
0 if not found, else unsigned long representation of the token

Definition at line 1756 of file stringbuf.c.

{
       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;
}

Here is the call graph for this function:

int StrBufHaveNextToken ( const StrBuf Source,
const char **  pStart 
)

a string tokenizer; Bounds checker function to make shure whether StrBufExtract_NextToken and friends have reached the end of the string.

Parameters:
Sourceour tokenbuffer
pStartthe token iterator pointer to inspect
Returns:
whether the revolving pointer is inside of the search range

Definition at line 1522 of file stringbuf.c.

{
       if ((Source == NULL) || 
           (*pStart == StrBufNOTNULL) ||
           (Source->BufUsed == 0))
       {
              return 0;
       }
       if (*pStart == NULL)
       {
              return 1;
       }
       else if (*pStart > Source->buf + Source->BufUsed)
       {
              return 0;
       }
       else if (*pStart <= Source->buf)
       {
              return 0;
       }

       return 1;
}

Here is the caller graph for this function:

int StrBufSkip_NTokenS ( const StrBuf Source,
const char **  pStart,
char  separator,
int  nTokens 
)

a string tokenizer

Parameters:
SourceStringBuffer to read from
pStartpointer to the end of the last token. Feed with NULL.
separatortokenizer character
nTokensnumber of tokens to fastforward over
Returns:
-1 if not found, else length of token.

Definition at line 1655 of file stringbuf.c.

{
       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);
}