Back to index

libcitadel  8.12
Functions
tokenizer Functions; Slow ones.
Stringbuffer, A class for manipulating strings with dynamic buffers

versatile tokenizer; random access to tokens, but slower; Prefer the Next Tokenizer More...

Collaboration diagram for tokenizer Functions; Slow ones.:

Functions

int StrBufReplaceToken (StrBuf *Buf, long where, long HowLong, const char *Repl, long ReplLen)
 Replace a token at a given place with a given length by another token with given length.
int StrBufNum_tokens (const StrBuf *source, char tok)
 Counts the numbmer of tokens in a buffer.
int StrBufRemove_token (StrBuf *Source, int parmnum, char separator)
 a string tokenizer
int StrBufExtract_token (StrBuf *dest, const StrBuf *Source, int parmnum, char separator)
 a string tokenizer
int StrBufExtract_int (const StrBuf *Source, int parmnum, char separator)
 a string tokenizer to fetch an integer
long StrBufExtract_long (const StrBuf *Source, int parmnum, char separator)
 a string tokenizer to fetch a long integer
unsigned long StrBufExtract_unsigned_long (const StrBuf *Source, int parmnum, char separator)
 a string tokenizer to fetch an unsigned long

Detailed Description

versatile tokenizer; random access to tokens, but slower; Prefer the Next Tokenizer


Function Documentation

int StrBufExtract_int ( const StrBuf Source,
int  parmnum,
char  separator 
)

a string tokenizer to fetch an integer

Parameters:
SourceString containing tokens
parmnumn'th Parameter to extract
separatortokenizer character
Returns:
0 if not found, else integer representation of the token

Definition at line 1442 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_token(&tmp, Source, parmnum, separator) > 0)
              return(atoi(buf));
       else
              return 0;
}

Here is the call graph for this function:

long StrBufExtract_long ( const StrBuf Source,
int  parmnum,
char  separator 
)

a string tokenizer to fetch a long integer

Parameters:
SourceString containing tokens
parmnumn'th Parameter to extract
separatortokenizer character
Returns:
0 if not found, else long integer representation of the token

Definition at line 1466 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_token(&tmp, Source, parmnum, separator) > 0)
              return(atoi(buf));
       else
              return 0;
}

Here is the call graph for this function:

int StrBufExtract_token ( StrBuf dest,
const StrBuf Source,
int  parmnum,
char  separator 
)

a string tokenizer

Parameters:
destDestination StringBuffer
SourceStringBuffer to read into
parmnumn'th Parameter to extract
separatortokenizer character
Returns:
-1 if not found, else length of token.

Definition at line 1375 of file stringbuf.c.

{
       const char *s, *e;          //* 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);
       }
       s = Source->buf;
       e = s + Source->BufUsed;

       //cit_backtrace();
       //lprintf (CTDL_DEBUG, "test >: n: %d sep: %c source: %s \n willi \n", parmnum, separator, source);

       while ((s < e) && !IsEmptyStr(s)) {
              if (*s == separator) {
                     ++current_token;
              }
              if (len >= dest->BufSize) {
                     dest->BufUsed = len;
                     if (IncreaseBuf(dest, 1, -1) < 0) {
                            dest->BufUsed --;
                            break;
                     }
              }
              if ( (current_token == parmnum) && 
                   (*s != separator)) {
                     dest->buf[len] = *s;
                     ++len;
              }
              else if (current_token > parmnum) {
                     break;
              }
              ++s;
       }
       
       dest->buf[len] = '\0';
       dest->BufUsed = len;
              
       if (current_token < parmnum) {
              //lprintf (CTDL_DEBUG,"test <!: %s\n", dest);
              return(-1);
       }
       //lprintf (CTDL_DEBUG,"test <: %d; %s\n", len, dest);
       return(len);
}

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned long StrBufExtract_unsigned_long ( const StrBuf Source,
int  parmnum,
char  separator 
)

a string tokenizer to fetch an unsigned long

Parameters:
SourceString containing tokens
parmnumn'th Parameter to extract
separatortokenizer character
Returns:
0 if not found, else unsigned long representation of the token

Definition at line 1491 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_token(&tmp, Source, parmnum, 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 StrBufNum_tokens ( const StrBuf source,
char  tok 
)

Counts the numbmer of tokens in a buffer.

Parameters:
sourceString to count tokens in
tokTokenizer char to count
Returns:
numbers of tokenizer chars found

Definition at line 1258 of file stringbuf.c.

{
       char *pch, *pche;
       long NTokens;
       if ((source == NULL) || (source->BufUsed == 0))
              return 0;
       if ((source->BufUsed == 1) && (*source->buf == tok))
              return 2;
       NTokens = 1;
       pch = source->buf;
       pche = pch + source->BufUsed;
       while (pch < pche)
       {
              if (*pch == tok)
                     NTokens ++;
              pch ++;
       }
       return NTokens;
}

Here is the caller graph for this function:

int StrBufRemove_token ( StrBuf Source,
int  parmnum,
char  separator 
)

a string tokenizer

Parameters:
SourceStringBuffer to read into
parmnumn'th Parameter to remove
separatortokenizer character
Returns:
-1 if not found, else length of token.

Definition at line 1286 of file stringbuf.c.

{
       int ReducedBy;
       char *d, *s, *end;          /* dest, source */
       int count = 0;

       /* Find desired @parameter */
       end = Source->buf + Source->BufUsed;
       d = Source->buf;
       while ((d <= end) && 
              (count < parmnum))
       {
              /* End of string, bail! */
              if (!*d) {
                     d = NULL;
                     break;
              }
              if (*d == separator) {
                     count++;
              }
              d++;
       }
       if ((d == NULL) || (d >= end))
              return 0;            /* @Parameter not found */

       /* Find next @parameter */
       s = d;
       while ((s <= end) && 
              (*s && *s != separator))
       {
              s++;
       }
       if (*s == separator)
              s++;
       ReducedBy = d - s;

       /* Hack and slash */
       if (s >= end) {
              return 0;
       }
       else if (*s) {
              memmove(d, s, Source->BufUsed - (s - Source->buf));
              Source->BufUsed += ReducedBy;
              Source->buf[Source->BufUsed] = '\0';
       }
       else if (d == Source->buf) {
              *d = 0;
              Source->BufUsed = 0;
       }
       else {
              *--d = '\0';
              Source->BufUsed += ReducedBy;
       }
       /*
       while (*s) {
              *d++ = *s++;
       }
       *d = 0;
       */
       return ReducedBy;
}

Here is the caller graph for this function:

int StrBufReplaceToken ( StrBuf Buf,
long  where,
long  HowLong,
const char *  Repl,
long  ReplLen 
)

Replace a token at a given place with a given length by another token with given length.

Parameters:
BufString where to work on
wherewhere inside of the Buf is the search-token
HowLongHow long is the token to be replaced
ReplToken to insert at 'where'
ReplLenLength of repl
Returns:
-1 if fail else length of resulting Buf

Definition at line 1226 of file stringbuf.c.

{

       if ((Buf == NULL) || 
           (where > Buf->BufUsed) ||
           (where + HowLong > Buf->BufUsed))
              return -1;

       if (where + ReplLen - HowLong > Buf->BufSize)
              if (IncreaseBuf(Buf, 1, Buf->BufUsed + ReplLen) < 0)
                     return -1;

       memmove(Buf->buf + where + ReplLen, 
              Buf->buf + where + HowLong,
              Buf->BufUsed - where - HowLong);
                                          
       memcpy(Buf->buf + where, 
              Repl, ReplLen);

       Buf->BufUsed += ReplLen - HowLong;

       return Buf->BufUsed;
}

Here is the call graph for this function: