Example #1
0
void gt_splitter_split(GtSplitter *s, char *string, GtUword length,
                    char delimiter)
{

  char *end_of_token, *string_index = string;

  gt_assert(s && string);

  /* splitting */
  while (string_index < string + length &&
         (end_of_token = strchr(string_index, delimiter))) {
    gt_assert(end_of_token);
    *end_of_token = '\0';
    if ((s->num_of_tokens + 1) * sizeof (char*) > s->allocated)
      s->tokens = gt_dynalloc(s->tokens, &s->allocated,
                              (s->num_of_tokens + 1) * sizeof (char*));
    s->tokens[s->num_of_tokens++] = string_index;
    string_index = end_of_token + 1;
  }

  /* save last token */
  if ((s->num_of_tokens + 2) * sizeof (char*) > s->allocated)
    s->tokens = gt_dynalloc(s->tokens, &s->allocated,
                            (s->num_of_tokens + 2) * sizeof (char*));
  s->tokens[s->num_of_tokens++] = string_index;
  s->tokens[s->num_of_tokens]   = NULL;

  gt_assert(s->num_of_tokens);
}
Example #2
0
static void check_space(GtQueue *q)
{
  if (!q->allocated) { /* empty queue without allocated memory */
    q->contents = gt_dynalloc(q->contents, &q->allocated, sizeof (void*));
    q->size = q->allocated / sizeof (void*);
  }
  else if (q->front < q->back) { /* no wraparound */
    if (q->back == q->size) {
      if (q->front)
        q->back = 0; /* perform wraparound */
      else { /* extend contents buffer */
        q->contents = gt_dynalloc(q->contents, &q->allocated,
                                  q->allocated + sizeof (void*));
        q->size = q->allocated / sizeof (void*);
      }
    }
  }
  else if (q->back && (q->back == q->front)) { /* wraparound */
    q->contents = gt_dynalloc(q->contents, &q->allocated,
                              q->allocated + q->front * sizeof (void*));
    memcpy(q->contents + q->size, q->contents, q->front * sizeof (void*));
    /* dynalloc() always doubles the already allocated memory region, which
       means we always have some additional space after the copied memory region
       left to set the back pointer to (otherwise we would have to reset the
       back pointer to 0).
     */
    gt_assert((size_t) q->front + q->size < q->allocated / sizeof (void*));
    q->back = q->front + q->size;
    q->size = q->allocated / sizeof (void*);
  }
}
void gt_desc_buffer_append_char(GtDescBuffer *db, char c)
{
  gt_assert(db);
  if (db->shorten) {
    if (db->seen_whitespace)
      return;
    if (isspace(c)) {
      db->seen_whitespace = true;
      return;
    }
  }
  if (db->finished) {
    gt_queue_add(db->startqueue, (void*) (db->length));
    db->finished = false;
  }
  if (db->length + 2 > db->allocated) {
    db->buf = gt_dynalloc(db->buf, &db->allocated,
                          (db->length + 2) * sizeof (char));
  }
  db->curlength++;
  db->buf[db->length++] = c;
}