deps/kb_meme/meme-5.0.1/src/buffer.c
/*
 * buf_get_token
 * returns a string which ends at (ie. does not include) the first character 
 * that is_delim designates is a delimeter or the end of the buffer 
 * if the done flag is set. If it reaches the end of the buffer and it 
 * doesn't find a delimiter and input isn't done it returns NULL. 
 * The parameter strlen will be set to the length of the found string or -1 
 * if the string is not found (in which case the buffer position is not changed).
 * The returned string is always null terminated.
 *
 * Allocation
 * When target is specified it will use it as a destination for the token,
 * it will be filled with up to size -1 characters from the buffer.
 * When target is NULL it will allocate a string to fit up to size -1
 * characters. If size is 0, any length is allowed to be allocated. 
 *
 * Errors
 * If the token doesn't fit in the maxlen -1 space then NULL is returned 
 * and strlen is set to the unreturned token length (so anything allocating
 * memory would need to add one). 
 *
 */
buf_get_token
returns a string which ends at  the first character
that is_delim designates is a delimeter or the end of the buffer
if the done flag is set. If it reaches the end of the buffer and it
doesn't find a delimiter and input isn't done it returns NULL.
The parameter strlen will be set to the length of the found string or -1
if the string is not found (in which case the buffer position is not changed).
The returned string is always null terminated.

Allocation
When target is specified it will use it as a destination for the token,
it will be filled with up to size -1 characters from the buffer.
When target is NULL it will allocate a string to fit up to size -1
characters. If size is 0, any length is allowed to be allocated.

Errors
If the token doesn't fit in the maxlen -1 space then NULL is returned
and strlen is set to the unreturned token length (so anything allocating
memory would need to add one).
buf_get_token returns a string which ends at the first character that is_delim designates is a delimeter or the end of the buffer if the done flag is set . If it reaches the end of the buffer and it doesn ' t find a delimiter and input isn ' t done it returns NULL . The parameter strlen will be set to the length of the found string or - 1 if the string is not found ( in which case the buffer position is not changed ) . The returned string is always null terminated . Allocation When target is specified it will use it as a destination for the token it will be filled with up to size - 1 characters from the buffer . When target is NULL it will allocate a string to fit up to size - 1 characters . If size is 0 any length is allowed to be allocated . Errors If the token doesn ' t fit in the maxlen - 1 space then NULL is returned and strlen is set to the unreturned token length ( so anything allocating memory would need to add one ) .
char* buf_get_token(BUF_T *buf, int (*is_delim)(void*, int), void* config, bool negate_is_delim, 
    bool done, char *target, int size, int* slen) {
  int index;
  int needed_size;
  index = buf_find_next(buf, is_delim, config, negate_is_delim);
  if (index == -1 && done) {
    index = buf->limit;
  }
  if (index > buf->position) {
    needed_size = index - buf->position;
    if (slen) *slen = needed_size;
    if (target == NULL) {
      if (size == 0 || size > needed_size) {
        target = (char*)mm_malloc(needed_size+1);
      } else {
        return NULL;
      }
    } else {
      if (size <= needed_size) {
        return NULL;
      }
    }
    strncpy(target, (buf->buffer)+(buf->position), needed_size);
    target[needed_size] = '\0';
    buf->position = index;
  } else { 
    if (slen) *slen = -1;
    target = NULL;
  }
  return target;
}
char * buf_get_token ( BUF_T * buf , int ( * is_delim ) ( void * , int ) , void * config , bool negate_is_delim , bool done , char * target , int size , int * slen ) { int index ; int needed_size ; index = buf_find_next ( buf , is_delim , config , negate_is_delim ) ; if ( index == -1 && done ) { index = buf -> limit ; } if ( index > buf -> position ) { needed_size = index - buf -> position ; if ( slen ) * slen = needed_size ; if ( target == NULL ) { if ( size == 0 || size > needed_size ) { target = ( char * ) mm_malloc ( needed_size + 1 ) ; } else { return NULL ; } } else { if ( size <= needed_size ) { return NULL ; } } strncpy ( target , ( buf -> buffer ) + ( buf -> position ) , needed_size ) ; target [ needed_size ] = ' \0 ' ; buf -> position = index ; } else { if ( slen ) * slen = -1 ; target = NULL ; } return target ; }
buf_get_token
 returns a string which ends at (ie.
buf_get_token returns a string which ends at ( ie .
//doesn't fit in allowed size //token not found