Exemplo n.º 1
0
int
str_getline (str_t d, FILE *f)
{
  char *res, *ptr;
  int szres, pending = 0;

  str_size_check (d, 0);
  str_trunc (d, 0);

  for (;;)
    {
      int j;

      ptr = d->heap + d->length;
      szres = d->size - d->length;

      res = fgets (ptr, szres, f);

      if (res == NULL)
	{
	  if (feof (f) && pending)
	    return 0;
	  return (-1);
	}

      for (j = 0; j < szres; j++)
	{
	  if (ptr[j] == '\n' || ptr[j] == 0)
	    {
	      ptr[j] = 0;
	      d->length += j;
	      break;
	    }
	}

      if (j < szres - 1)
	break;

      pending = 1;

      str_size_check (d, 2 * d->length);
    }

  if (d->length > 0)
    {
      if (d->heap[d->length - 1] == '\015')
	{
	  d->heap[d->length - 1] = 0;
	  d->length --;
	}
    }

  return 0;
}
Exemplo n.º 2
0
void
str_copy_c (str_t d, const char *s)
{
  const size_t len = strlen (s);
  str_size_check (d, len);
  memcpy (d->heap, s, len+1);
  d->length = len;
}
Exemplo n.º 3
0
void
str_copy (str_t d, const str_t s)
{
  const size_t len = s->length;
  str_size_check (d, len);
  memcpy (d->heap, s->heap, len+1);
  d->length = len;
}
Exemplo n.º 4
0
void
str_copy_c_substr (str_t d, const char *s, int len)
{
  len = (len >= 0 ? len : 0);
  str_size_check (d, (size_t)len);
  memcpy (d->heap, s, (size_t)len);
  d->heap[len] = 0;
  d->length = len;
}
Exemplo n.º 5
0
void
str_pad (str_t s, int len, char sep)
{
  int diff = len - s->length;

  if (diff <= 0)
    return;

  str_size_check (s, (size_t)len-1);

  memmove (s->heap + diff, s->heap, (s->length + 1) * sizeof(char));
  memset (s->heap, sep, diff * sizeof(char));

  s->length += diff;
}
Exemplo n.º 6
0
void
str_append_c (str_t to, const char *from, int sep)
{
  size_t flen = strlen (from);
  int use_sep = (sep != 0 && to->length > 0);
  size_t newlen = STR_LENGTH(to) + flen + (use_sep ? 1 : 0);
  int idx = STR_LENGTH(to);

  str_size_check (to, newlen);

  if (use_sep)
    to->heap[idx++] = sep;

  memcpy (to->heap + idx, from, flen+1);
  to->length = newlen;
}
Exemplo n.º 7
0
int
str_getcwd (str_t dir)
{
    size_t bsize;

    for (bsize = 64; /* */; bsize *= 2)
    {
        str_size_check  (dir, bsize);
        if (getcwd (dir->heap, bsize) == dir->heap)
        {
            dir->length = strlen (dir->heap);
            return 0;
        }
        if (errno != ERANGE)
            break;
    }

    return 1;
}
Exemplo n.º 8
0
int
str_loadfile (const char *filename, str_t text)
{
    struct stat info[1];
    FILE *fp;
    int j, n, tsize;

    if(stat (filename, info) != 0)
        return 1;

    tsize = info->st_size;

    fp = fopen (filename, "r");
    if (fp == NULL)
        return 1;

    str_size_check (text, tsize + 1);

    n = fread (text->heap, 1, tsize, fp);
#ifndef WIN32
    if(n != tsize)
    {
        str_trunc (text, 0);
        fclose (fp);
        return 1;
    }
#endif

    text->length = n;
    text->heap[n] = '\0';

    /* This is a dirty hack to get rid of the stupid \015 on Windows */
    for (j = 0; j < tsize; j++)
        if (text->heap[j] == '\015')
            text->heap[j] = ' ';

    fclose (fp);

    return 0;
}