From 55cb74435b02136ce4e386cfc133d9d2c766da3d Mon Sep 17 00:00:00 2001 From: Nikita Ivanov Date: Mon, 6 Jun 2022 04:43:46 +0500 Subject: [PATCH] Remove char_v, proper custom function naming --- utils.c | 74 -------------------------------------------------------- utils.h | 14 +---------- vector.h | 21 ++++++++-------- 3 files changed, 12 insertions(+), 97 deletions(-) diff --git a/utils.c b/utils.c index ef5b01b..7065a3c 100644 --- a/utils.c +++ b/utils.c @@ -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; -} diff --git a/utils.h b/utils.h index 78eb245..63468cf 100644 --- a/utils.h +++ b/utils.h @@ -32,19 +32,7 @@ int strcmpnull(const char *s1, const char *s2); int strlennull(const char *s); int get_cache_dir(char *buf, size_t len, char *name); + int mkpath(char* file_path, mode_t mode); -#define CHAR_V_LEN(vec) ((vec).len - 1) -#define CHAR_V_STR(vec) ((vec).buf) - -typedef struct { - size_t len, cap; - char *buf; -} CharVec; - -CharVec char_v_new(size_t cap); -void char_v_free(CharVec *v); -void char_v_append(CharVec *v, char c); -void char_v_append_str(CharVec *v, char *s); - #endif diff --git a/vector.h b/vector.h index 531de92..a9c2ea0 100644 --- a/vector.h +++ b/vector.h @@ -10,37 +10,38 @@ } Vector##name #define VECTOR_SIGN(name, type, func, ret, ...) \ - ret vector_##func##_##type(__VA_ARGS__) + ret vector##name##_##func(__VA_ARGS__) #define VECTOR_SIGN_V(name, type, func, ret, ...) \ - ret vector_##func##_##type(Vector##name *v __VA_OPT__(,) __VA_ARGS__) + VECTOR_SIGN(name, type, func, ret, \ + Vector##name *v __VA_OPT__(, ) __VA_ARGS__) #define VECTOR_SIGN_NEW(name, type) VECTOR_SIGN(name, type, new, Vector##name *, size_t cap) #define VECTOR_SIGN_FREE(name, type) VECTOR_SIGN_V(name, type, free, void) #define VECTOR_SIGN_APPEND_ARR(name, type) VECTOR_SIGN_V(name, type, append_arr, void, type *arr, size_t len) #define VECTOR_SIGN_APPEND(name, type) VECTOR_SIGN_V(name, type, append, void, type val) -#define VECTOR_SIGN_GET(name, type) VECTOR_SIGN_V(name, type, get, type, size_t i) +#define VECTOR_SIGN_GET(name, type) VECTOR_SIGN_V(name, type, get, type *, size_t i) #define VECTOR_GEN_SOURCE_(name, type, spec) \ - spec VECTOR_SIGN_NEW(name, type) \ + inline spec VECTOR_SIGN_NEW(name, type) \ { \ return (Vector##name *)vector_new(sizeof(type), cap); \ } \ - spec VECTOR_SIGN_FREE(name, type) \ + inline spec VECTOR_SIGN_FREE(name, type) \ { \ vector_free((Vector *)v); \ } \ - spec VECTOR_SIGN_APPEND_ARR(name, type) \ + inline spec VECTOR_SIGN_APPEND_ARR(name, type) \ { \ vector_append_arr((Vector *)v, arr, len); \ } \ - spec VECTOR_SIGN_APPEND(name, type) \ + inline spec VECTOR_SIGN_APPEND(name, type) \ { \ vector_append((Vector *)v, &val); \ } \ - spec VECTOR_SIGN_GET(name, type) \ + inline spec VECTOR_SIGN_GET(name, type) \ { \ - return *(type *)vector_get((Vector *)v, i); \ + return (type *)vector_get((Vector *)v, i); \ } #define VECTOR_GEN_SOURCE(name, type) VECTOR_GEN_SOURCE_(name, type, ) @@ -51,9 +52,9 @@ #define VECTOR_GEN_HEADER(name, type) \ VECTOR_TYPE(name, type); \ VECTOR_SIGN_NEW(name, type); \ + VECTOR_SIGN_FREE(name, type); \ VECTOR_SIGN_APPEND_ARR(name, type); \ VECTOR_SIGN_APPEND(name, type); \ - VECTOR_SIGN_FREE(name, type); \ VECTOR_SIGN_GET(name, type); VECTOR_TYPE(, void);