Remove char_v, proper custom function naming

This commit is contained in:
Nikita Ivanov
2022-06-06 04:43:46 +05:00
parent 38c0b56286
commit 55cb74435b
3 changed files with 12 additions and 97 deletions

74
utils.c
View File

@ -120,77 +120,3 @@ int mkpath(char* file_path, mode_t mode)
return 0;
}
CharVec char_v_new(size_t cap)
{
CharVec v;
v.buf = NULL;
v.len = 0;
v.cap = cap;
return v;
}
void char_v_free(CharVec *v)
{
if (v->buf) {
free(v->buf);
v->buf = NULL;
v->len = 0;
}
}
static void char_v_create(CharVec *v)
{
if (v->buf)
return;
if (!(v->buf = malloc(v->cap * sizeof(*v->buf)))) {
PRINTINTERR(FUNCFAILED("malloc"), ERRNOS);
abort();
}
v->buf[0] = '\0';
v->len++;
}
static void char_v_check_cap(CharVec *v, size_t len_inc)
{
void *new_buf;
size_t new_len = v->len + len_inc;
if (new_len < v->cap)
return;
while (new_len >= v->cap)
v->cap *= 2;
if (!(new_buf = realloc(v->buf, v->cap * sizeof(*v->buf)))) {
free(v->buf);
PRINTINTERR(FUNCFAILED("realloc"), ERRNOS);
abort();
}
v->buf = new_buf;
}
void char_v_append(CharVec *v, char c)
{
char_v_create(v);
char_v_check_cap(v, 1);
v->buf[v->len - 1] = c;
v->buf[v->len] = '\0';
v->len++;
}
void char_v_append_str(CharVec *v, char *s)
{
size_t len = strlen(s);
char_v_create(v);
char_v_check_cap(v, len);
memcpy(v->buf + v->len * sizeof(*v->buf), s, len + 1);
v->len += len;
}