Esempio n. 1
0
/*
 * string_append_c()
 *
 * dodaje do danego ci±gu jeden znak, alokuj±c przy tym odpowiedni± ilo¶æ
 * pamiêci.
 *
 *	- s - ci±g znaków.
 *	- c - znaczek do dopisania.
 */
int string_append_c(string_t s, char c)
{
	if (!s) {
		errno = EFAULT;
		return -1;
	}

	string_realloc(s, s->len + 1);

	s->str[s->len + 1] = 0;
	s->str[s->len++] = c;

	return 0;
}
static void
string_append(struct string *s, char *b, size_t n)
{
  size_t alloc;
  char *sbuf;

  /*
   * avoid allocating memory for a static buffer if we're
   * not appending data.  Avoid having realloc free our
   * memory if the total allocation size is zero.
   *
  if (!n) return;

  /*
   * compute total size of string, being careful not to overflow.
   */
  if (s->size > (SIZE_MAX - n)) _die_overflow_add(s->size, n);
  alloc = s->size + n;

  /* add room for nul pointer */
  if (alloc > (SIZE_MAX - 1)) _die_overflow_add(alloc, 1);
  alloc += 1;

  /* copy the static buffer into newly allocated memory. */
  if (!s->alloc) {
    sbuf = s->buf;
    s->buf = (char*)0;
    string_realloc(s, alloc);
    memcpy(s->buf, sbuf, s->size);
  } else if (alloc > s->alloc) {
    string_realloc(s, alloc);
  }

  memcpy(s->buf + s->size, b, n);
  s->size += n;
  s->buf[s->size] = '\0';
}
Esempio n. 3
0
/*
 * string_insert_n()
 *
 * wstawia tekst w podane miejsce bufora.
 *
 *	- s - ci±g znaków,
 *	- index - miejsce, gdzie mamy wpisaæ (liczone od 0),
 *	- str - tekst do dopisania,
 *	- count - ilo¶æ znaków do dopisania (-1 znaczy, ¿e wszystkie).
 */
void string_insert_n(string_t s, int index, const char *str, int count)
{
	if (!s || !str)
		return;

	if (count == -1)
		count = (int)strlen(str);

	if (index > s->len)
		index = s->len;

	string_realloc(s, s->len + count);

	memmove(s->str + index + count, s->str + index, s->len + 1 - index);
	memmove(s->str + index, str, count);

	s->len += count;
}
Esempio n. 4
0
BUILTIN("socket-recv") socket_recv(Context &c, Value sock, Value max_len_) {
    VERIFY_ARG_SOCKET(sock, 1);
    VERIFY_ARG_NUM(max_len_, 2);

    String *str = string_alloc(num_val(max_len_));

    int res = recv(fd_of(sock), str->data, str->len, 0);
    if (res < 0)
        return c.error("recv() error");

    if (res == 0) {
        free(str);
        return c.str_empty();
    }

    string_realloc(&str, res);

    return c.str(str);
}
Esempio n. 5
0
/*
 * string_append_n()
 *
 * dodaje tekst do bufora alokuj±c odpowiedni± ilo¶æ pamiêci.
 *
 *	- s - ci±g znaków,
 *	- str - tekst do dopisania,
 *	- count - ile znaków tego tekstu dopisaæ? (-1 znaczy, ¿e ca³y).
 */
int string_append_n(string_t s, const char *str, int count)
{
	if (!s || !str) {
		errno = EFAULT;
		return -1;
	}

	if (count == -1)
		count = (int)strlen(str);

	string_realloc(s, s->len + count);

	s->str[s->len + count] = 0;
	strncpy(s->str + s->len, str, count);

	s->len += count;

	return 0;
}