This commit is contained in:
Олег Бородин
2025-10-01 12:35:52 +02:00
parent 9abd8a5e19
commit c04418d725
11 changed files with 112 additions and 165 deletions

View File

@@ -81,9 +81,7 @@ ssize_t bstream_fread(bstream_t * stream, char* filename) {
stream->data = realloc(stream->data, (size_t)newcapa);
stream->capa = newcapa;
}
if (buf != NULL) {
memcpy(&(stream->data[stream->wpos]), buf, (size_t)size);
}
memcpy(&(stream->data[stream->wpos]), buf, (size_t)size);
stream->wpos += size;
rsize += size;
}

53
libxtools/gconfig.c Normal file
View File

@@ -0,0 +1,53 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdio.h>
#include <galexer.h>
#include <gclexer.h>
#include <vmapper.h>
#include <gconfig.h>
void gconfig_init(gconfig_t* gconfig, int argc, char **argv) {
gclexer_init(&(gconfig->lex), argv, argc);
vmapper_init(&(gconfig->mapper));
}
int gconfig_bind(gconfig_t* gconfig, int type, char* name, void* ptr) {
vmapper_t* vmapper = &(gconfig->mapper);
return vmapper_bind(vmapper, type, name, ptr);
}
int gconfig_parse(gconfig_t* gconfig) {
// return tccomp_parse(&(gconfig->comp));
return 0;
}
void gconfig_destroy(gconfig_t* gconfig) {
//gclexer_destroy(&(gconfig->lexer));
vmapper_destroy(&(gconfig->mapper));
}
int __main(int argc, char **argv) {
(void)argc;
(void)argv;
char*_argv[] = { argv[0], "--qwerty=-num12345", "--foo=-bar" };
int _argc = 2;
gclexer_t lex;
gclexer_init(&lex, _argv, _argc);
char token[1024];
int toktype = TOKEN_NULL;
int i = 0;
while (toktype != TOKEN_ENDF) {
toktype = gclexer_gettok(&lex, token);
printf("%d: %d [%s]\n", i, toktype, token);
i++;
}
return 0;
}

26
libxtools/gconfig.h Normal file
View File

@@ -0,0 +1,26 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#ifndef GCONFIG_H_QWERTY
#define GCONFIG_H_QWERTY
#include <stdio.h>
#include <gclexer.h>
typedef struct {
gclexer_t lex;
vmapper_t mapper;
} gconfig_t;
#define GCONF_STR MAPPER_STR
#define GCONF_INT MAPPER_INT
#define GCONF_BOOL MAPPER_BOOL
void gconfig_init(gconfig_t* gconfig, int argc, char **argv);
int gconfig_bind(gconfig_t* gconfig, int type, char* name, void* ptr);
int gconfig_parse(gconfig_t* gconfig);
void gconfig_destroy(gconfig_t* gconfig);
#endif

View File

@@ -1,125 +0,0 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <tclexer.h>
#include <tccomp.h>
#include <vmapper.h>
#define RES_OK 0
#define RES_ERR -1
#define POS1TYPE TOKEN_WORD
#define POS2TYPE TOKEN_OPER
#define POS3TYPE TOKEN_WORD
#define POS4TYPE TOKEN_COMM
static char* strcopy(char* src) {
size_t srcsize = strlen(src) + 1;
char* dst = malloc(srcsize);
memset(dst, '\0', srcsize);
strcpy(dst, src);
return dst;
}
tccomp_t * new_tccomp(tclexer_t * lexer, vmapper_t* vmapper) {
tccomp_t *comp = malloc(sizeof(tccomp_t));
if (comp == NULL) return NULL;
comp->lexer = lexer;
comp->vmapper = vmapper;
comp->pos = 0;
comp->lnum = 0;
return comp;
}
void tccomp_init(tccomp_t * comp, tclexer_t * lexer, vmapper_t* vmapper) {
comp->lexer = lexer;
comp->vmapper = vmapper;
comp->pos = 0;
comp->lnum = 0;
}
int tccomp_parse(tccomp_t * comp) {
char token[MAX_TOK_SIZE];
int toktype = -1;
tclexer_t* lexer = comp->lexer;
char* key = NULL;
char* val = NULL;
while (true) {
toktype = tclexer_get_token(lexer, token, MAX_TOK_SIZE);
if (toktype == TOKEN_SPACE) {
continue;
}
if (toktype == TOKEN_COMM) {
continue;
}
//printf("tok=%d pos=%d line=%d [%s]\n", toktype, comp->pos, comp->lnum, token);
if (toktype == TOKEN_NEWLN) {
comp->lnum++;
}
switch (comp->pos) {
case 0: {
if (toktype == TOKEN_NEWLN) {
comp->pos = 0;
break;
}
if (toktype != TOKEN_WORD) {
return -1;
}
comp->pos++;
key = strcopy(token);
break;
}
case 1: {
if (toktype != TOKEN_OPER) {
return -1;
}
comp->pos++;
break;
}
case 2: {
if (toktype != TOKEN_WORD) {
return -1;
}
comp->pos++;
val = strcopy(token);
break;
}
case 3: {
if (toktype != TOKEN_NEWLN && toktype != TOKEN_ENDFL) {
return -1;
}
comp->pos = 0;
printf("keyval = [%s], [%s]\n", key, val);
vmapper_set(comp->vmapper, key, val);
free(key);
free(val);
break;
}
}
if (toktype == TOKEN_ENDFL) break;
}
return 0;
}
void tccomp_destroy(tccomp_t* comp) {
(void)comp;
}
void tccomp_free(tccomp_t* comp) {
free(comp);
}

View File

@@ -1,27 +0,0 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdio.h>
#include <tclexer.h>
#include <tccomp.h>
#include <bstream.h>
typedef struct {
bstream_t stream;
tclexer_t lexer;
tccomp_t comp;
vmapper_t mapper;
} tconfig_t;
#define TCONF_STR MAPPER_STR
#define TCONF_INT MAPPER_INT
#define TCONF_BOOL MAPPER_BOOL
void tconfig_init(tconfig_t* tconfig);
int tconfig_bind(tconfig_t* tconfig, int type, char* name, void* ptr);
ssize_t tconfig_read(tconfig_t* tconfig, char* filename);
int tconfig_parse(tconfig_t* tconfig);
void tconfig_destroy(tconfig_t* tconfig);