From d5a5ce02552c4d58c34226eaa0b9c71743630a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 21 Jan 2019 11:32:33 +0100 Subject: Bunch of renames + macros. --- hash.h | 4 +++- macro.h | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- main.c | 7 +++++-- parse.c | 4 ++-- strbuf.c | 12 ++++++++++-- strbuf.h | 8 +++++++- vcal.c | 26 +++++++++++++++++--------- vcal.h | 10 ++++++---- 8 files changed, 101 insertions(+), 22 deletions(-) diff --git a/hash.h b/hash.h index cf955a95..7706f089 100644 --- a/hash.h +++ b/hash.h @@ -10,7 +10,9 @@ unsigned long hash(char*); #define TABLE(T) TP(table_, T) #define HASH_PUT(T) TP(hash_put_, T) #define HASH_GET(T) TP(hash_get_, T) -#define HASH_INIT(T) TP(hash_init_, T) +// #define HASH_INIT(T) TP(hash_init_, T) +// #define HASH_INIT(T) CONSTRUCTOR_T(hash_ ## T, 1) +#define HASH_INIT(T) CONSTRUCTOR_GEN(hash, T, 1) #endif /* HASH_H */ #ifdef TYPE diff --git a/macro.h b/macro.h index c4b3fc9b..ca3fce24 100644 --- a/macro.h +++ b/macro.h @@ -1,10 +1,60 @@ #ifndef MACRO_H #define MACRO_H +/* + * NOTE This file uses __VA_OPT__. This is not standard compliant. + */ + +/* + * Token paste + */ #define TP(a, b) a ## b +#define TP3(a, b, c) a ## b ## c +#define TP4(a, b, c, d) a ## b ## c ## d + +/* + * Get length of __VA_ARGS__ + * Borrowed fram: + * https://stackoverflow.com/a/35986932 + */ +#define VA_ARGS_NUM_PRIV(P1, P2, P3, P4, P5, P6, Pn, ...) Pn +#define VA_ARGS_NUM(...) VA_ARGS_NUM_PRIV(-1, ## __VA_ARGS__, 5, 4, 3, 2, 1, 0) + +#define NEW_HELPER(T, ARG_COUNT) \ + TP3(T, _init_, ARG_COUNT) + +/* + * Constructor type name + */ +#define CONSTRUCTOR_T(T, C) TP3(T, _init_, C) +#define CONSTRUCTOR_GEN(parent, child, C) \ + CONSTRUCTOR_T(parent ## _ ## child, C) + +/* + * Returns full type of constructor + */ +#define CONSTRUCTOR_DECL(T, ...) \ + CONSTRUCTOR_T(T, VA_ARGS_NUM(__VA_ARGS__)) (T* this __VA_OPT__(,) __VA_ARGS__) + +/* + * Call the constructor of an object + */ +#define CONSTRUCT(T, N, ...) \ + CONSTRUCTOR_T(T, VA_ARGS_NUM(__VA_ARGS__)) (N __VA_OPT__(,) __VA_ARGS__) + +/* + * Allocate a new object on the HEAP + */ #define NEW(T, N, ...) \ T* N = malloc(sizeof(*N)); \ - TP(T, _init) (N, __VA_ARGS__); + CONSTRUCT(T, N, __VA_ARGS__); + +/* + * Allocate a new object on the STACK + */ +#define SNEW(T, N, ...) \ + T N; \ + CONSTRUCT(T, & N, __VA_ARGS__); #endif /* MACRO_H */ diff --git a/main.c b/main.c index dc50f3cb..0cf0439a 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include #include "parse.h" +#include "macro.h" int main (int argc, char* argv[argc]) { if (argc < 2) { @@ -16,8 +17,10 @@ int main (int argc, char* argv[argc]) { puts("Please give vdir as first argument"); exit (1); } - vcalendar cal; - init_vcalendar(&cal); + // vcalendar cal; + // init_vcalendar(&cal); + // CONSTRUCT(vcalendar, &cal); + SNEW(vcalendar, cal); char* dname = argv[1]; DIR* dir = opendir(dname); diff --git a/parse.c b/parse.c index cd85ec53..f71e264d 100644 --- a/parse.c +++ b/parse.c @@ -17,7 +17,7 @@ int parse_file(FILE* f, vcalendar* cal) { int segments = 1; string str; - init_string (&str, segments * SEGSIZE); + strbuf_init_1 (&str, segments * SEGSIZE); part_context p_ctx = p_key; scope_context s_ctx = s_none; @@ -30,7 +30,7 @@ int parse_file(FILE* f, vcalendar* cal) { NEW(vevent, ev, /**/ 100); content_line cline; - init_content_line (&cline, keylen, vallen); + content_line_init_2 (&cline, keylen, vallen); char c; while ( (c = fgetc(f)) != EOF) { diff --git a/strbuf.c b/strbuf.c index 48702cf8..f6aad92a 100644 --- a/strbuf.c +++ b/strbuf.c @@ -5,7 +5,15 @@ #include #define ERR(s) fprintf(stderr, "\x1B[0;31mERR\x1b[m (strbuf %3i): %s\n", __LINE__, s) -int init_string(string* str, size_t len) { +int strbuf_init_0(string* str) { + str->mem = NULL; + str->alloc = 0; + str->len = 0; + str->ptr = 0; + return 0; +} + +int strbuf_init_1(string* str, size_t len) { str->mem = malloc(len); str->alloc = len; str->ptr = 0; @@ -96,7 +104,7 @@ int strbuf_init_copy(string* dest, string* src) { } #endif - init_string(dest, src->len + 1); + strbuf_init_1(dest, src->len + 1); copy_strbuf(dest, src); return 0; diff --git a/strbuf.h b/strbuf.h index d21fa117..1a49ca5d 100644 --- a/strbuf.h +++ b/strbuf.h @@ -17,10 +17,16 @@ typedef struct { * TODO Check memmory allocation for last +1 byte for null. */ +/* + * Init string to size of 0 + * Doesnt't call malloc. + */ +int strbuf_init_0(string* str); + /* * Constructor */ -int init_string(string* str, size_t len); +int strbuf_init_1(string* str, size_t len); /* * Like realloc, but for strbuf diff --git a/vcal.c b/vcal.c index d40ef7cc..6c318a19 100644 --- a/vcal.c +++ b/vcal.c @@ -6,8 +6,8 @@ #include "hash_help.inc" #undef TYPE -int vevent_init(vevent* ev, int init_size) { - HASH_INIT(content_line)(&ev->clines, init_size); +int CONSTRUCTOR_DECL(vevent, int init_size) { + HASH_INIT(content_line)(&this->clines, init_size); return 0; } @@ -19,13 +19,21 @@ int add_content_line (vevent* ev, content_line* c) { return HASH_PUT(content_line)(&ev->clines, c); } -int init_content_line (content_line* c, int keylen, int vallen) { - init_string(&c->key, keylen); - init_string(&c->val, vallen); +int CONSTRUCTOR_DECL(content_line) { + CONSTRUCT(strbuf, &this->key); + CONSTRUCT(strbuf, &this->val); // TODO remaining fields return 0; } +int CONSTRUCTOR_DECL(content_line, int keylen, int vallen) { + CONSTRUCT(strbuf, &this->key, keylen); + CONSTRUCT(strbuf, &this->val, vallen); + // TODO remaining fields + return 0; +} + + int content_line_copy (content_line* dest, content_line* src) { strbuf_init_copy(&dest->key, &src->key); strbuf_init_copy(&dest->val, &src->val); @@ -86,10 +94,10 @@ int push_event(vcalendar* cal, vevent* ev) { return 0; } -int init_vcalendar(vcalendar* cal) { - cal->events = malloc(sizeof(*cal->events)); - cal->alloc = 1; - cal->n_events = 0; +int CONSTRUCTOR_DECL(vcalendar) { + this->events = malloc(sizeof(*this->events)); + this->alloc = 1; + this->n_events = 0; return 0; } diff --git a/vcal.h b/vcal.h index 8335cbc4..58d347c1 100644 --- a/vcal.h +++ b/vcal.h @@ -40,9 +40,11 @@ struct s_vevent { struct s_vevent; typedef struct s_vevent vevent; -int vevent_init (vevent* ev, int init_size); +int CONSTRUCTOR_DECL(vevent, int init_size); + +int CONSTRUCTOR_DECL(content_line); +int CONSTRUCTOR_DECL(content_line, int keylen, int vallen); -int init_content_line (content_line* c, int keylen, int vallen); int content_line_free (content_line* c); int content_line_copy (content_line* dest, content_line* src); @@ -70,8 +72,8 @@ typedef struct { vevent* events; } vcalendar; -int init_vcalendar(vcalendar* cal); -int free_vcalendar(vcalendar* cal); +int CONSTRUCTOR_DECL(vcalendar); +int free_vcalendar (vcalendar* cal); int push_event(vcalendar* cal, vevent* ev); -- cgit v1.2.3