indent
This commit is contained in:
+10
-2
@@ -24,7 +24,9 @@ bstream_t* bstream_init(bstream_t* stream) {
|
|||||||
|
|
||||||
bstream_t* new_bstream() {
|
bstream_t* new_bstream() {
|
||||||
bstream_t* stream = malloc(sizeof(bstream_t));
|
bstream_t* stream = malloc(sizeof(bstream_t));
|
||||||
if (stream == NULL) return NULL;
|
|
||||||
|
if (stream == NULL)
|
||||||
|
return NULL;
|
||||||
stream->data = malloc(STREAM_INITCAPA);
|
stream->data = malloc(STREAM_INITCAPA);
|
||||||
stream->wpos = 0;
|
stream->wpos = 0;
|
||||||
stream->rpos = 0;
|
stream->rpos = 0;
|
||||||
@@ -43,6 +45,7 @@ size_t bstream_dump(bstream_t * stream) {
|
|||||||
ssize_t bstream_write(bstream_t * stream, void* buf, size_t size) {
|
ssize_t bstream_write(bstream_t * stream, void* buf, size_t size) {
|
||||||
if ((stream->wpos + size) > stream->capa) {
|
if ((stream->wpos + size) > stream->capa) {
|
||||||
size_t newcapa = stream->capa * 2;
|
size_t newcapa = stream->capa * 2;
|
||||||
|
|
||||||
stream->data = realloc(stream->data, (size_t)newcapa);
|
stream->data = realloc(stream->data, (size_t)newcapa);
|
||||||
stream->capa = newcapa;
|
stream->capa = newcapa;
|
||||||
}
|
}
|
||||||
@@ -55,6 +58,7 @@ ssize_t bstream_write(bstream_t * stream, void* buf, size_t size) {
|
|||||||
|
|
||||||
ssize_t bstream_read(bstream_t * stream, void* buf, size_t size) {
|
ssize_t bstream_read(bstream_t * stream, void* buf, size_t size) {
|
||||||
size_t unread = stream->wpos - stream->rpos;
|
size_t unread = stream->wpos - stream->rpos;
|
||||||
|
|
||||||
if (size > unread) {
|
if (size > unread) {
|
||||||
size = unread;
|
size = unread;
|
||||||
}
|
}
|
||||||
@@ -70,14 +74,18 @@ ssize_t bstream_read(bstream_t * stream, void* buf, size_t size) {
|
|||||||
ssize_t bstream_fread(bstream_t * stream, char* filename) {
|
ssize_t bstream_fread(bstream_t * stream, char* filename) {
|
||||||
|
|
||||||
int fd = open(filename, O_RDONLY);
|
int fd = open(filename, O_RDONLY);
|
||||||
if (fd < 0) return (ssize_t)-1;
|
|
||||||
|
if (fd < 0)
|
||||||
|
return (ssize_t) - 1;
|
||||||
|
|
||||||
char buf[RBUF_SIZE];
|
char buf[RBUF_SIZE];
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
size_t rsize = 0;
|
size_t rsize = 0;
|
||||||
|
|
||||||
while ((size = (size_t)read(fd, buf, RBUF_SIZE)) > 0) {
|
while ((size = (size_t)read(fd, buf, RBUF_SIZE)) > 0) {
|
||||||
if ((stream->wpos + size) > stream->capa) {
|
if ((stream->wpos + size) > stream->capa) {
|
||||||
size_t newcapa = stream->capa * 2;
|
size_t newcapa = stream->capa * 2;
|
||||||
|
|
||||||
stream->data = realloc(stream->data, (size_t)newcapa);
|
stream->data = realloc(stream->data, (size_t)newcapa);
|
||||||
stream->capa = newcapa;
|
stream->capa = newcapa;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,12 +11,14 @@
|
|||||||
|
|
||||||
void test_rwrite(void) {
|
void test_rwrite(void) {
|
||||||
bstream_t stream;
|
bstream_t stream;
|
||||||
|
|
||||||
bstream_init(&stream);
|
bstream_init(&stream);
|
||||||
char* data = "_123456789";
|
char* data = "_123456789";
|
||||||
size_t dsize = strlen(data);
|
size_t dsize = strlen(data);
|
||||||
ssize_t wsize = 0;
|
ssize_t wsize = 0;
|
||||||
|
|
||||||
size_t count = 3;
|
size_t count = 3;
|
||||||
|
|
||||||
for (size_t i = 0; i < count; i++) {
|
for (size_t i = 0; i < count; i++) {
|
||||||
wsize += bstream_write(&stream, data, dsize);
|
wsize += bstream_write(&stream, data, dsize);
|
||||||
}
|
}
|
||||||
@@ -27,9 +29,11 @@ void test_rwrite(void) {
|
|||||||
|
|
||||||
size_t bsize = bstream_wpos(&stream);
|
size_t bsize = bstream_wpos(&stream);
|
||||||
char* buf = malloc((size_t)bsize + (size_t)1);
|
char* buf = malloc((size_t)bsize + (size_t)1);
|
||||||
|
|
||||||
memset(buf, 0, (size_t)bsize + (size_t)1);
|
memset(buf, 0, (size_t)bsize + (size_t)1);
|
||||||
|
|
||||||
ssize_t rsize = bstream_read(&stream, buf, bsize);
|
ssize_t rsize = bstream_read(&stream, buf, bsize);
|
||||||
|
|
||||||
printf("rsize = %lu, buf = [%s]\n", rsize, buf);
|
printf("rsize = %lu, buf = [%s]\n", rsize, buf);
|
||||||
|
|
||||||
MASSERT(wsize == (ssize_t) (dsize * count));
|
MASSERT(wsize == (ssize_t) (dsize * count));
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
static char* strcopy(char* src) {
|
static char* strcopy(char* src) {
|
||||||
size_t srcsize = strlen(src) + 1;
|
size_t srcsize = strlen(src) + 1;
|
||||||
char* dst = malloc(srcsize);
|
char* dst = malloc(srcsize);
|
||||||
|
|
||||||
memset(dst, '\0', srcsize);
|
memset(dst, '\0', srcsize);
|
||||||
strcpy(dst, src);
|
strcpy(dst, src);
|
||||||
return dst;
|
return dst;
|
||||||
@@ -43,6 +44,7 @@ int clcomp_gettok(clcomp_t* comp, char* token) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int toktype = cllexer_gettok(&(comp->lexer), token);
|
int toktype = cllexer_gettok(&(comp->lexer), token);
|
||||||
|
|
||||||
if (toktype == TOKEN_ENDF && comp->argn != comp->argc) {
|
if (toktype == TOKEN_ENDF && comp->argn != comp->argc) {
|
||||||
comp->argn++;
|
comp->argn++;
|
||||||
cllexer_init(&(comp->lexer), comp->argv[comp->argn]);
|
cllexer_init(&(comp->lexer), comp->argv[comp->argn]);
|
||||||
@@ -59,6 +61,7 @@ int clcomp_parse(clcomp_t* comp) {
|
|||||||
|
|
||||||
char* key = NULL;
|
char* key = NULL;
|
||||||
char* val = NULL;
|
char* val = NULL;
|
||||||
|
|
||||||
while (toktype != TOKEN_ENDF) {
|
while (toktype != TOKEN_ENDF) {
|
||||||
toktype = clcomp_gettok(comp, token);
|
toktype = clcomp_gettok(comp, token);
|
||||||
i++;
|
i++;
|
||||||
|
|||||||
@@ -20,9 +20,11 @@ int main(int argc, char **argv) {
|
|||||||
int _argc = 2;
|
int _argc = 2;
|
||||||
|
|
||||||
vmapper_t vmapper;
|
vmapper_t vmapper;
|
||||||
|
|
||||||
vmapper_init(&vmapper);
|
vmapper_init(&vmapper);
|
||||||
|
|
||||||
clcomp_t clcomp;
|
clcomp_t clcomp;
|
||||||
|
|
||||||
clcomp_init(&clcomp, &vmapper, _argv, _argc);
|
clcomp_init(&clcomp, &vmapper, _argv, _argc);
|
||||||
|
|
||||||
clcomp_parse(&clcomp);
|
clcomp_parse(&clcomp);
|
||||||
|
|||||||
@@ -18,11 +18,13 @@ void clconfig_init(clconfig_t* clconfig, int argc, char **argv) {
|
|||||||
|
|
||||||
int clconfig_bind(clconfig_t * clconfig, int type, char* name, void* ptr) {
|
int clconfig_bind(clconfig_t * clconfig, int type, char* name, void* ptr) {
|
||||||
vmapper_t* vmapper = &(clconfig->vmapper);
|
vmapper_t* vmapper = &(clconfig->vmapper);
|
||||||
|
|
||||||
return vmapper_bind(vmapper, type, name, ptr);
|
return vmapper_bind(vmapper, type, name, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int clconfig_parse(clconfig_t * clconfig) {
|
int clconfig_parse(clconfig_t * clconfig) {
|
||||||
int res = clcomp_parse(&(clconfig->comp));
|
int res = clcomp_parse(&(clconfig->comp));
|
||||||
|
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
if ((clconfig->errstr = clcomp_geterr(&(clconfig->comp))) == NULL) {
|
if ((clconfig->errstr = clcomp_geterr(&(clconfig->comp))) == NULL) {
|
||||||
clconfig->errstr = "Undefined command line error";
|
clconfig->errstr = "Undefined command line error";
|
||||||
@@ -39,4 +41,3 @@ void clconfig_destroy(clconfig_t* clconfig) {
|
|||||||
clcomp_destroy(&(clconfig->comp));
|
clcomp_destroy(&(clconfig->comp));
|
||||||
vmapper_destroy(&(clconfig->vmapper));
|
vmapper_destroy(&(clconfig->vmapper));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ int main(int argc, char **argv) {
|
|||||||
int _argc = 2;
|
int _argc = 2;
|
||||||
|
|
||||||
clconfig_t clconfig;
|
clconfig_t clconfig;
|
||||||
|
|
||||||
clconfig_init(&clconfig, _argc, _argv);
|
clconfig_init(&clconfig, _argc, _argv);
|
||||||
|
|
||||||
int intkey = 0;
|
int intkey = 0;
|
||||||
@@ -26,6 +27,7 @@ int main(int argc, char **argv) {
|
|||||||
clconfig_bind(&clconfig, GCONF_STR, "strkey", &strkey);
|
clconfig_bind(&clconfig, GCONF_STR, "strkey", &strkey);
|
||||||
|
|
||||||
int res = clconfig_parse(&clconfig);
|
int res = clconfig_parse(&clconfig);
|
||||||
|
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
printf("error: %s\n", clconfig_geterr(&clconfig));
|
printf("error: %s\n", clconfig_geterr(&clconfig));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ int cllexer_gettok(cllexer_t* lexer, char* token) {
|
|||||||
lexer->newletter = arg[lexer->rpos++];
|
lexer->newletter = arg[lexer->rpos++];
|
||||||
int ltype = get_ltype(lexer->newletter);
|
int ltype = get_ltype(lexer->newletter);
|
||||||
int newcontext = LCONTEXT_ENDFL;
|
int newcontext = LCONTEXT_ENDFL;
|
||||||
|
|
||||||
switch (ltype) {
|
switch (ltype) {
|
||||||
case LTYPE_PREFIX:{
|
case LTYPE_PREFIX:{
|
||||||
newcontext = LCONTEXT_PREFIX;
|
newcontext = LCONTEXT_PREFIX;
|
||||||
@@ -90,6 +91,7 @@ int cllexer_gettok(cllexer_t* lexer, char* token) {
|
|||||||
}
|
}
|
||||||
case LCONTEXT_DELIM:{
|
case LCONTEXT_DELIM:{
|
||||||
int newcontext = lexer->currcontext;
|
int newcontext = lexer->currcontext;
|
||||||
|
|
||||||
switch (ltype) {
|
switch (ltype) {
|
||||||
case LTYPE_PREFIX:
|
case LTYPE_PREFIX:
|
||||||
case LTYPE_LETTER:{
|
case LTYPE_LETTER:{
|
||||||
@@ -111,6 +113,7 @@ int cllexer_gettok(cllexer_t* lexer, char* token) {
|
|||||||
}
|
}
|
||||||
case LCONTEXT_PREFIX:{
|
case LCONTEXT_PREFIX:{
|
||||||
int newcontext = lexer->currcontext;
|
int newcontext = lexer->currcontext;
|
||||||
|
|
||||||
switch (ltype) {
|
switch (ltype) {
|
||||||
case LTYPE_DELIM:{
|
case LTYPE_DELIM:{
|
||||||
newcontext = LCONTEXT_DELIM;
|
newcontext = LCONTEXT_DELIM;
|
||||||
@@ -135,6 +138,7 @@ int cllexer_gettok(cllexer_t* lexer, char* token) {
|
|||||||
}
|
}
|
||||||
case LCONTEXT_WORD:{
|
case LCONTEXT_WORD:{
|
||||||
int newcontext = lexer->currcontext;
|
int newcontext = lexer->currcontext;
|
||||||
|
|
||||||
switch (ltype) {
|
switch (ltype) {
|
||||||
case LTYPE_DELIM:{
|
case LTYPE_DELIM:{
|
||||||
newcontext = LCONTEXT_DELIM;
|
newcontext = LCONTEXT_DELIM;
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ int main(int argc, char **argv) {
|
|||||||
char* arg = "--qwerty=-num-12345";
|
char* arg = "--qwerty=-num-12345";
|
||||||
|
|
||||||
cllexer_t lex;
|
cllexer_t lex;
|
||||||
|
|
||||||
cllexer_init(&lex, arg);
|
cllexer_init(&lex, arg);
|
||||||
|
|
||||||
char token[1024];
|
char token[1024];
|
||||||
int toktype = TOKEN_NULL;
|
int toktype = TOKEN_NULL;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
while (toktype != TOKEN_ENDF) {
|
while (toktype != TOKEN_ENDF) {
|
||||||
toktype = cllexer_gettok(&lex, token);
|
toktype = cllexer_gettok(&lex, token);
|
||||||
printf("cllexer_gettok res: %d: %d [%s]\n", i, toktype, token);
|
printf("cllexer_gettok res: %d: %d [%s]\n", i, toktype, token);
|
||||||
|
|||||||
+6
-2
@@ -27,6 +27,7 @@
|
|||||||
static char* strcopy(char* src) {
|
static char* strcopy(char* src) {
|
||||||
size_t srcsize = strlen(src) + 1;
|
size_t srcsize = strlen(src) + 1;
|
||||||
char* dst = malloc(srcsize);
|
char* dst = malloc(srcsize);
|
||||||
|
|
||||||
memset(dst, '\0', srcsize);
|
memset(dst, '\0', srcsize);
|
||||||
strcpy(dst, src);
|
strcpy(dst, src);
|
||||||
return dst;
|
return dst;
|
||||||
@@ -34,7 +35,9 @@ static char* strcopy(char* src) {
|
|||||||
|
|
||||||
tccomp_t* new_tccomp(tclexer_t * lexer, vmapper_t * vmapper) {
|
tccomp_t* new_tccomp(tclexer_t * lexer, vmapper_t * vmapper) {
|
||||||
tccomp_t* comp = malloc(sizeof(tccomp_t));
|
tccomp_t* comp = malloc(sizeof(tccomp_t));
|
||||||
if (comp == NULL) return NULL;
|
|
||||||
|
if (comp == NULL)
|
||||||
|
return NULL;
|
||||||
comp->lexer = lexer;
|
comp->lexer = lexer;
|
||||||
comp->vmapper = vmapper;
|
comp->vmapper = vmapper;
|
||||||
comp->pos = 0;
|
comp->pos = 0;
|
||||||
@@ -111,7 +114,8 @@ int tccomp_parse(tccomp_t * comp) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (toktype == TOKEN_ENDFL) break;
|
if (toktype == TOKEN_ENDFL)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,12 +25,15 @@ int main(void) {
|
|||||||
bstream_init(&stream);
|
bstream_init(&stream);
|
||||||
|
|
||||||
tclexer_t lexer;
|
tclexer_t lexer;
|
||||||
|
|
||||||
tclexer_init(&lexer, &stream);
|
tclexer_init(&lexer, &stream);
|
||||||
|
|
||||||
vmapper_t vmapper;
|
vmapper_t vmapper;
|
||||||
|
|
||||||
vmapper_init(&vmapper);
|
vmapper_init(&vmapper);
|
||||||
|
|
||||||
tccomp_t comp;
|
tccomp_t comp;
|
||||||
|
|
||||||
tccomp_init(&comp, &lexer, &vmapper);
|
tccomp_init(&comp, &lexer, &vmapper);
|
||||||
|
|
||||||
bstream_write(&stream, src, strlen(src));
|
bstream_write(&stream, src, strlen(src));
|
||||||
|
|||||||
+4
-1
@@ -47,7 +47,9 @@ static int get_ltype(char letter) {
|
|||||||
|
|
||||||
tclexer_t* new_tclexer(bstream_t * stream) {
|
tclexer_t* new_tclexer(bstream_t * stream) {
|
||||||
tclexer_t* lexer = malloc(sizeof(tclexer_t));
|
tclexer_t* lexer = malloc(sizeof(tclexer_t));
|
||||||
if (lexer == NULL) return NULL;
|
|
||||||
|
if (lexer == NULL)
|
||||||
|
return NULL;
|
||||||
lexer->stream = stream;
|
lexer->stream = stream;
|
||||||
lexer->context = LEXCONT_UNDEF;
|
lexer->context = LEXCONT_UNDEF;
|
||||||
return lexer;
|
return lexer;
|
||||||
@@ -231,6 +233,7 @@ int tclexer_get_token(tclexer_t * lexer, char* token, int maxsize) {
|
|||||||
case LEXCONT_UNDEF:
|
case LEXCONT_UNDEF:
|
||||||
default:{
|
default:{
|
||||||
int newcontext = LEXCONT_UNDEF;
|
int newcontext = LEXCONT_UNDEF;
|
||||||
|
|
||||||
switch (ltype) {
|
switch (ltype) {
|
||||||
case LTYPE_SPACE:{
|
case LTYPE_SPACE:{
|
||||||
newcontext = LEXCONT_SPACE;
|
newcontext = LEXCONT_SPACE;
|
||||||
|
|||||||
@@ -17,15 +17,18 @@ int main(void) {
|
|||||||
char* src = " key1 = var1 # comment 1\nkey2 = var2 # comment 2 and 3\n# comment 4\nkey3 = var3";
|
char* src = " key1 = var1 # comment 1\nkey2 = var2 # comment 2 and 3\n# comment 4\nkey3 = var3";
|
||||||
|
|
||||||
bstream_t stream;
|
bstream_t stream;
|
||||||
|
|
||||||
bstream_init(&stream);
|
bstream_init(&stream);
|
||||||
bstream_write(&stream, src, strlen(src));
|
bstream_write(&stream, src, strlen(src));
|
||||||
|
|
||||||
|
|
||||||
tclexer_t lexer;
|
tclexer_t lexer;
|
||||||
|
|
||||||
tclexer_init(&lexer, &stream);
|
tclexer_init(&lexer, &stream);
|
||||||
|
|
||||||
int token_typ = TOKEN_NULL;
|
int token_typ = TOKEN_NULL;
|
||||||
char token[MAX_TOK_SIZE];
|
char token[MAX_TOK_SIZE];
|
||||||
|
|
||||||
while ((token_typ = tclexer_get_token(&lexer, token, MAX_TOK_SIZE)) != TOKEN_ENDFL) {
|
while ((token_typ = tclexer_get_token(&lexer, token, MAX_TOK_SIZE)) != TOKEN_ENDFL) {
|
||||||
printf("%d:[%s]\n", token_typ, token);
|
printf("%d:[%s]\n", token_typ, token);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,11 +20,13 @@ void tconfig_init(tconfig_t* tconfig) {
|
|||||||
|
|
||||||
int tconfig_bind(tconfig_t * tconfig, int type, char* name, void* ptr) {
|
int tconfig_bind(tconfig_t * tconfig, int type, char* name, void* ptr) {
|
||||||
vmapper_t* vmapper = &(tconfig->mapper);
|
vmapper_t* vmapper = &(tconfig->mapper);
|
||||||
|
|
||||||
return vmapper_bind(vmapper, type, name, ptr);
|
return vmapper_bind(vmapper, type, name, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t tconfig_read(tconfig_t * tconfig, char* filename) {
|
ssize_t tconfig_read(tconfig_t * tconfig, char* filename) {
|
||||||
bstream_t* stream = &(tconfig->stream);
|
bstream_t* stream = &(tconfig->stream);
|
||||||
|
|
||||||
return bstream_fread(stream, filename);
|
return bstream_fread(stream, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
|
|
||||||
tconfig_t tconfig;
|
tconfig_t tconfig;
|
||||||
|
|
||||||
tconfig_init(&tconfig);
|
tconfig_init(&tconfig);
|
||||||
|
|
||||||
int intkey = 0;
|
int intkey = 0;
|
||||||
@@ -25,10 +26,12 @@ int main(int argc, char **argv) {
|
|||||||
tconfig_bind(&tconfig, TCONF_STR, "strkey", &strkey);
|
tconfig_bind(&tconfig, TCONF_STR, "strkey", &strkey);
|
||||||
|
|
||||||
ssize_t rsize = tconfig_read(&tconfig, "test.conf");
|
ssize_t rsize = tconfig_read(&tconfig, "test.conf");
|
||||||
|
|
||||||
printf("%ld\n", rsize);
|
printf("%ld\n", rsize);
|
||||||
MASSERT(rsize > 0);
|
MASSERT(rsize > 0);
|
||||||
|
|
||||||
int res = tconfig_parse(&tconfig);
|
int res = tconfig_parse(&tconfig);
|
||||||
|
|
||||||
MASSERT(res == -1);
|
MASSERT(res == -1);
|
||||||
|
|
||||||
tconfig_destroy(&tconfig);
|
tconfig_destroy(&tconfig);
|
||||||
|
|||||||
+20
-2
@@ -12,6 +12,7 @@
|
|||||||
static char* copystr(char* src) {
|
static char* copystr(char* src) {
|
||||||
size_t srcsize = strlen(src) + 1;
|
size_t srcsize = strlen(src) + 1;
|
||||||
char* dest = malloc(srcsize);
|
char* dest = malloc(srcsize);
|
||||||
|
|
||||||
memset(dest, '\0', srcsize);
|
memset(dest, '\0', srcsize);
|
||||||
strcpy(dest, src);
|
strcpy(dest, src);
|
||||||
return dest;
|
return dest;
|
||||||
@@ -21,6 +22,7 @@ static char* copystr(char* src) {
|
|||||||
|
|
||||||
static mlink_t* new_mlink_string(char* name, char** val) {
|
static mlink_t* new_mlink_string(char* name, char** val) {
|
||||||
mlink_t* mlink = malloc(sizeof(mlink_t));
|
mlink_t* mlink = malloc(sizeof(mlink_t));
|
||||||
|
|
||||||
mlink->name = name;
|
mlink->name = name;
|
||||||
mlink->vptr = (void *)val;
|
mlink->vptr = (void *)val;
|
||||||
mlink->type = MAPPER_STR;
|
mlink->type = MAPPER_STR;
|
||||||
@@ -29,6 +31,7 @@ static mlink_t* new_mlink_string(char* name, char** val) {
|
|||||||
|
|
||||||
static mlink_t* new_mlink_integer(char* name, int* val) {
|
static mlink_t* new_mlink_integer(char* name, int* val) {
|
||||||
mlink_t* mlink = malloc(sizeof(mlink_t));
|
mlink_t* mlink = malloc(sizeof(mlink_t));
|
||||||
|
|
||||||
mlink->name = name;
|
mlink->name = name;
|
||||||
mlink->vptr = (void *)val;
|
mlink->vptr = (void *)val;
|
||||||
mlink->type = MAPPER_INT;
|
mlink->type = MAPPER_INT;
|
||||||
@@ -37,6 +40,7 @@ static mlink_t* new_mlink_integer(char* name, int* val) {
|
|||||||
|
|
||||||
static mlink_t* new_mlink_bool(char* name, bool* val) {
|
static mlink_t* new_mlink_bool(char* name, bool* val) {
|
||||||
mlink_t* mlink = malloc(sizeof(mlink_t));
|
mlink_t* mlink = malloc(sizeof(mlink_t));
|
||||||
|
|
||||||
mlink->name = name;
|
mlink->name = name;
|
||||||
mlink->vptr = (void *)val;
|
mlink->vptr = (void *)val;
|
||||||
mlink->type = MAPPER_BOOL;
|
mlink->type = MAPPER_BOOL;
|
||||||
@@ -49,7 +53,9 @@ static mlink_t* new_mlink_bool(char* name, bool* val) {
|
|||||||
|
|
||||||
vmapper_t* new_vmapper(void) {
|
vmapper_t* new_vmapper(void) {
|
||||||
vmapper_t* vmapper = malloc(sizeof(vmapper_t));
|
vmapper_t* vmapper = malloc(sizeof(vmapper_t));
|
||||||
if (vmapper == NULL) return NULL;
|
|
||||||
|
if (vmapper == NULL)
|
||||||
|
return NULL;
|
||||||
vmapper->mlinks = malloc(sizeof(mlink_t) * MAPPER_INITCAPA);
|
vmapper->mlinks = malloc(sizeof(mlink_t) * MAPPER_INITCAPA);
|
||||||
vmapper->capa = MAPPER_INITCAPA;
|
vmapper->capa = MAPPER_INITCAPA;
|
||||||
vmapper->size = 0;
|
vmapper->size = 0;
|
||||||
@@ -75,7 +81,9 @@ int vmapper_check_capa(vmapper_t* vmapper) {
|
|||||||
if (vmapper->size == vmapper->capa) {
|
if (vmapper->size == vmapper->capa) {
|
||||||
size_t newcapa = vmapper->capa * 2;
|
size_t newcapa = vmapper->capa * 2;
|
||||||
mlink_t** newmlinks = realloc(vmapper->mlinks, newcapa);
|
mlink_t** newmlinks = realloc(vmapper->mlinks, newcapa);
|
||||||
if (newmlinks == NULL) return RES_BIND_ERR;
|
|
||||||
|
if (newmlinks == NULL)
|
||||||
|
return RES_BIND_ERR;
|
||||||
vmapper->mlinks = newmlinks;
|
vmapper->mlinks = newmlinks;
|
||||||
vmapper->capa = newcapa;
|
vmapper->capa = newcapa;
|
||||||
}
|
}
|
||||||
@@ -84,10 +92,12 @@ int vmapper_check_capa(vmapper_t* vmapper) {
|
|||||||
|
|
||||||
int vmapper_bind_string(vmapper_t * vmapper, char* name, char** val) {
|
int vmapper_bind_string(vmapper_t * vmapper, char* name, char** val) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
|
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
mlink_t* mlink = new_mlink_string(name, val);
|
mlink_t* mlink = new_mlink_string(name, val);
|
||||||
|
|
||||||
vmapper->mlinks[vmapper->size] = mlink;
|
vmapper->mlinks[vmapper->size] = mlink;
|
||||||
vmapper->size++;
|
vmapper->size++;
|
||||||
return RES_BIND_OK;
|
return RES_BIND_OK;
|
||||||
@@ -95,10 +105,12 @@ int vmapper_bind_string(vmapper_t* vmapper, char* name, char** val) {
|
|||||||
|
|
||||||
int vmapper_bind_int(vmapper_t * vmapper, char* name, int* val) {
|
int vmapper_bind_int(vmapper_t * vmapper, char* name, int* val) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
|
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
mlink_t* mlink = new_mlink_integer(name, val);
|
mlink_t* mlink = new_mlink_integer(name, val);
|
||||||
|
|
||||||
vmapper->mlinks[vmapper->size] = mlink;
|
vmapper->mlinks[vmapper->size] = mlink;
|
||||||
vmapper->size++;
|
vmapper->size++;
|
||||||
return RES_BIND_OK;
|
return RES_BIND_OK;
|
||||||
@@ -107,10 +119,12 @@ int vmapper_bind_int(vmapper_t* vmapper, char* name, int* val) {
|
|||||||
|
|
||||||
int vmapper_bind_bool(vmapper_t * vmapper, char* name, bool* val) {
|
int vmapper_bind_bool(vmapper_t * vmapper, char* name, bool* val) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
|
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
mlink_t* mlink = new_mlink_bool(name, val);
|
mlink_t* mlink = new_mlink_bool(name, val);
|
||||||
|
|
||||||
vmapper->mlinks[vmapper->size] = mlink;
|
vmapper->mlinks[vmapper->size] = mlink;
|
||||||
vmapper->size++;
|
vmapper->size++;
|
||||||
return RES_BIND_OK;
|
return RES_BIND_OK;
|
||||||
@@ -119,8 +133,10 @@ int vmapper_bind_bool(vmapper_t* vmapper, char* name, bool* val) {
|
|||||||
int vmapper_set_int(vmapper_t * vmapper, char* key, char* val) {
|
int vmapper_set_int(vmapper_t * vmapper, char* key, char* val) {
|
||||||
for (size_t i = 0; i < vmapper->size; i++) {
|
for (size_t i = 0; i < vmapper->size; i++) {
|
||||||
mlink_t* mlink = vmapper->mlinks[i];
|
mlink_t* mlink = vmapper->mlinks[i];
|
||||||
|
|
||||||
if (mlink->type == MAPPER_INT && strcmp(mlink->name, key) == 0) {
|
if (mlink->type == MAPPER_INT && strcmp(mlink->name, key) == 0) {
|
||||||
char* eptr = NULL;
|
char* eptr = NULL;
|
||||||
|
|
||||||
*(int *)(mlink->vptr) = (int)strtol(val, &eptr, 10);
|
*(int *)(mlink->vptr) = (int)strtol(val, &eptr, 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,6 +146,7 @@ int vmapper_set_int(vmapper_t* vmapper, char* key, char* val) {
|
|||||||
int vmapper_set_string(vmapper_t * vmapper, char* key, char* val) {
|
int vmapper_set_string(vmapper_t * vmapper, char* key, char* val) {
|
||||||
for (size_t i = 0; i < vmapper->size; i++) {
|
for (size_t i = 0; i < vmapper->size; i++) {
|
||||||
mlink_t* mlink = vmapper->mlinks[i];
|
mlink_t* mlink = vmapper->mlinks[i];
|
||||||
|
|
||||||
if (mlink->type == MAPPER_STR && strcmp(mlink->name, key) == 0) {
|
if (mlink->type == MAPPER_STR && strcmp(mlink->name, key) == 0) {
|
||||||
*(char **)(mlink->vptr) = copystr(val);
|
*(char **)(mlink->vptr) = copystr(val);
|
||||||
}
|
}
|
||||||
@@ -140,6 +157,7 @@ int vmapper_set_string(vmapper_t* vmapper, char* key, char* val) {
|
|||||||
int vmapper_set_bool(vmapper_t * vmapper, char* key, char* val) {
|
int vmapper_set_bool(vmapper_t * vmapper, char* key, char* val) {
|
||||||
for (size_t i = 0; i < vmapper->size; i++) {
|
for (size_t i = 0; i < vmapper->size; i++) {
|
||||||
mlink_t* mlink = vmapper->mlinks[i];
|
mlink_t* mlink = vmapper->mlinks[i];
|
||||||
|
|
||||||
if (mlink->type == MAPPER_BOOL && strcmp(mlink->name, key) == 0) {
|
if (mlink->type == MAPPER_BOOL && strcmp(mlink->name, key) == 0) {
|
||||||
if (strcmp(val, "true") == 0) {
|
if (strcmp(val, "true") == 0) {
|
||||||
*(bool *)(mlink->vptr) = true;
|
*(bool *)(mlink->vptr) = true;
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ int main(int argc, char **argv) {
|
|||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
vmapper_t vmapper;
|
vmapper_t vmapper;
|
||||||
|
|
||||||
vmapper_init(&vmapper);
|
vmapper_init(&vmapper);
|
||||||
|
|
||||||
int port = 0;
|
int port = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user