Browse Source

* f2b_buf_append() : return size of actually appended data instead true/false

master
Alex 'AdUser' Z 4 years ago
parent
commit
555c781237
  1. 11
      src/buf.c
  2. 2
      src/buf.h

11
src/buf.c

@ -22,21 +22,22 @@ f2b_buf_free(f2b_buf_t *buf) {
memset(buf, 0x0, sizeof(f2b_buf_t)); memset(buf, 0x0, sizeof(f2b_buf_t));
} }
bool size_t
f2b_buf_append(f2b_buf_t *buf, const char *str, size_t len) { f2b_buf_append(f2b_buf_t *buf, const char *str, size_t len) {
assert(buf != NULL); assert(buf != NULL);
assert(str != NULL); assert(str != NULL);
if (len == 0) if (len == 0)
len = strlen(str); len = strlen(str);
if (buf->size < (buf->used + len)) if ((buf->used + len) > buf->size) {
return false; /* not enough space */ /* not enough space, append as much as possible */
len = buf->size - buf->used;
}
memcpy(&buf->data[buf->used], str, len); memcpy(&buf->data[buf->used], str, len);
buf->used += len; buf->used += len;
buf->data[buf->used] = '\0'; buf->data[buf->used] = '\0';
return true; return len;
} }
/** /**

2
src/buf.h

@ -9,7 +9,7 @@ typedef struct f2b_buf_t {
bool f2b_buf_alloc(f2b_buf_t *buf, size_t max); bool f2b_buf_alloc(f2b_buf_t *buf, size_t max);
void f2b_buf_free(f2b_buf_t *buf); void f2b_buf_free(f2b_buf_t *buf);
bool f2b_buf_append(f2b_buf_t *buf, const char *str, size_t size); size_t f2b_buf_append(f2b_buf_t *buf, const char *str, size_t size);
char * f2b_buf_extract(f2b_buf_t *buf, const char *end); char * f2b_buf_extract(f2b_buf_t *buf, const char *end);
size_t f2b_buf_splice(f2b_buf_t *buf, size_t len); size_t f2b_buf_splice(f2b_buf_t *buf, size_t len);

Loading…
Cancel
Save