This commit is contained in:
Олег Бородин
2025-10-01 11:21:05 +02:00
commit 9abd8a5e19
50 changed files with 12685 additions and 0 deletions

0
libxtools/.dirstamp Normal file
View File

121
libxtools/bstream.c Normal file
View File

@@ -0,0 +1,121 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <bstream.h>
#define STREAM_INITCAPA 128
bstream_t* bstream_init(bstream_t* stream) {
stream->data = malloc(STREAM_INITCAPA);
stream->wpos = 0;
stream->rpos = 0;
stream->capa = STREAM_INITCAPA;
return stream;
}
bstream_t* new_bstream() {
bstream_t* stream = malloc(sizeof(bstream_t));
if (stream == NULL) return NULL;
stream->data = malloc(STREAM_INITCAPA);
stream->wpos = 0;
stream->rpos = 0;
stream->capa = STREAM_INITCAPA;
return stream;
}
size_t bstream_dump(bstream_t * stream) {
for (size_t i = 0; i < stream->wpos; i++) {
printf("%c", stream->data[i]);
}
return stream->wpos;
}
ssize_t bstream_write(bstream_t * stream, void* buf, size_t size) {
if ((stream->wpos + size) > stream->capa) {
size_t newcapa = stream->capa * 2;
stream->data = realloc(stream->data, (size_t)newcapa);
stream->capa = newcapa;
}
if (buf != NULL) {
memcpy(&(stream->data[stream->wpos]), buf, (size_t)size);
}
stream->wpos += size;
return (ssize_t)size;
}
ssize_t bstream_read(bstream_t * stream, void* buf, size_t size) {
size_t unread = stream->wpos - stream->rpos;
if (size > unread) {
size = unread;
}
if (buf != NULL) {
memcpy(buf, &(stream->data[stream->rpos]), size);
}
stream->rpos += size;
return (ssize_t)size;
}
#define RBUF_SIZE 64
ssize_t bstream_fread(bstream_t * stream, char* filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) return (ssize_t)-1;
char buf[RBUF_SIZE];
size_t size = 0;
size_t rsize = 0;
while ((size = (size_t)read(fd, buf, RBUF_SIZE)) > 0) {
if ((stream->wpos + size) > stream->capa) {
size_t newcapa = stream->capa * 2;
stream->data = realloc(stream->data, (size_t)newcapa);
stream->capa = newcapa;
}
if (buf != NULL) {
memcpy(&(stream->data[stream->wpos]), buf, (size_t)size);
}
stream->wpos += size;
rsize += size;
}
close(fd);
return (ssize_t)rsize;
}
char bstream_getc(bstream_t * stream) {
size_t unread = stream->wpos - stream->rpos;
if (unread == 0)
return EOF;
return (char)stream->data[stream->rpos++];
}
size_t bstream_wpos(bstream_t * stream) {
return stream->wpos;
}
size_t bstream_rpos(bstream_t * stream) {
return stream->rpos;
}
void bstream_destroy(bstream_t * stream) {
if (stream != NULL)
free(stream->data);
}
void bstream_free(bstream_t * stream) {
if (stream != NULL)
free(stream->data);
free(stream);
}

36
libxtools/bstream.h Normal file
View File

@@ -0,0 +1,36 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#ifndef BSTREAM_H_QWERTY
#define BSTREAM_H_QWERTY
#include <stdint.h>
#include <unistd.h>
typedef struct {
size_t rpos;
size_t wpos;
size_t capa;
uint8_t* data;
} bstream_t;
bstream_t* new_bstream();
bstream_t* bstream_init(bstream_t * stream);
size_t bstream_dump(bstream_t * stream);
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_fread(bstream_t * stream, char* filename);
char bstream_getc(bstream_t * stream);
size_t bstream_wpos(bstream_t * stream);
size_t bstream_rpos(bstream_t * stream);
void bstream_destroy(bstream_t * stream);
void bstream_free(bstream_t * stream);
#endif

47
libxtools/bstream_test.c Normal file
View File

@@ -0,0 +1,47 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <bstream.h>
#include <massert.h>
void test_rwrite(void) {
bstream_t stream;
bstream_init(&stream);
char* data = "_123456789";
size_t dsize = strlen(data);
ssize_t wsize = 0;
size_t count = 3;
for (size_t i = 0; i < count; i++) {
wsize += bstream_write(&stream, data, dsize);
}
printf("wsize = %ld, data = [%s]\n", wsize, stream.data);
MASSERT(stream.wpos == dsize * count);
MASSERT(wsize == (ssize_t)(dsize * count));
size_t bsize = bstream_wpos(&stream);
char* buf = malloc((size_t)bsize + (size_t)1);
memset(buf, 0, (size_t)bsize + (size_t)1);
ssize_t rsize = bstream_read(&stream, buf, bsize);
printf("rsize = %lu, buf = [%s]\n", rsize, buf);
MASSERT(wsize == (ssize_t)(dsize * count));
MASSERT(rsize == wsize);
bstream_destroy(&stream);
}
int main(int argc, char **argv) {
(void)argc;
(void)argv;
test_rwrite();
return 0;
}

166
libxtools/galexer.c Normal file
View File

@@ -0,0 +1,166 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#include <string.h>
#include <stdio.h>
#include <galexer.h>
#define LTYPE_LETTER 1
#define LTYPE_PREFIX 2
#define LTYPE_DELIM 3
#define LTYPE_EOF 4
#define LCONTEXT_START 1
#define LCONTEXT_WORD 2
#define LCONTEXT_DELIM 3
#define LCONTEXT_PREFIX 4
#define LCONTEXT_ENDFL 5
#define LCONTEXT_UNDEF 7
static int get_ltype(char newletter) {
switch (newletter) {
case '-':
case '+':
return LTYPE_PREFIX;
case '=':
return LTYPE_DELIM;
case EOF:
return LTYPE_EOF;
}
return LTYPE_LETTER;
}
void galexer_init(galexer_t* lexer, char* arg) {
lexer->arg = arg;
lexer->rpos = 0;
lexer->tpos = 0;
lexer->currcontext = LCONTEXT_START;
lexer->newletter = '\0';
}
static void galexer_getletter(galexer_t* lexer, char* arg, int size) {
lexer->newletter = EOF;
if (lexer->rpos < size) {
lexer->newletter = arg[lexer->rpos++];
}
}
int galexer_gettok(galexer_t* lexer, char* token) {
char* arg = lexer->arg;
size_t size = strlen(arg);
if (lexer->currcontext == LCONTEXT_START) {
lexer->newletter = arg[lexer->rpos++];
int ltype = get_ltype(lexer->newletter);
int newcontext = LCONTEXT_ENDFL;
switch (ltype) {
case LTYPE_PREFIX: {
newcontext = LCONTEXT_PREFIX;
break;
}
case LTYPE_DELIM: {
newcontext = LCONTEXT_DELIM;
break;
}
case LTYPE_LETTER: {
newcontext = LCONTEXT_WORD;
break;
}
case LTYPE_EOF: {
newcontext = LCONTEXT_ENDFL;
break;
}
}
lexer->currcontext = newcontext;
}
while (lexer->currcontext != LCONTEXT_ENDFL) {
int ltype = get_ltype(lexer->newletter);
switch (lexer->currcontext) {
case LCONTEXT_ENDFL: {
lexer->currcontext = LCONTEXT_ENDFL;
break;
}
case LCONTEXT_DELIM: {
int newcontext = lexer->currcontext;
switch (ltype) {
case LTYPE_PREFIX:
case LTYPE_LETTER: {
newcontext = LCONTEXT_WORD;
break;
}
case LTYPE_EOF: {
newcontext = LCONTEXT_ENDFL;
break;
}
}
if (newcontext != lexer->currcontext) {
lexer->currcontext = newcontext;
token[lexer->tpos] = '\0';
lexer->tpos = 0;
return TOKEN_DELIM;
}
break;
}
case LCONTEXT_PREFIX: {
int newcontext = lexer->currcontext;
switch (ltype) {
case LTYPE_DELIM: {
newcontext = LCONTEXT_DELIM;
break;
}
case LTYPE_LETTER: {
newcontext = LCONTEXT_WORD;
break;
}
case LTYPE_EOF: {
newcontext = LCONTEXT_ENDFL;
break;
}
}
if (newcontext != lexer->currcontext) {
lexer->currcontext = newcontext;
token[lexer->tpos++] = '\0';
lexer->tpos = 0;
return TOKEN_PREF;
}
break;
}
case LCONTEXT_WORD: {
int newcontext = lexer->currcontext;
switch (ltype) {
case LTYPE_DELIM: {
newcontext = LCONTEXT_DELIM;
break;
}
case LTYPE_EOF: {
newcontext = LCONTEXT_ENDFL;
break;
}
}
if (newcontext != lexer->currcontext) {
lexer->currcontext = newcontext;
token[lexer->tpos] = '\0';
lexer->tpos = 0;
return TOKEN_WORD;
}
break;
}
}
token[lexer->tpos++] = lexer->newletter;
galexer_getletter(lexer, arg, size);
}
lexer->currcontext = LCONTEXT_ENDFL;
lexer->tpos = 0;
token[lexer->tpos] = '\0';
strcpy(token, "EOF");
return TOKEN_ENDF;
}

30
libxtools/galexer.h Normal file
View File

@@ -0,0 +1,30 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#ifndef GALEXER_H_QWERTY
#define GALEXER_H_QWERTY
#define TOKEN_NULL 1
#define TOKEN_WORD 2
#define TOKEN_DELIM 3
#define TOKEN_PREF 4
#define TOKEN_ENDF 5
#define TOKEN_SPACE 6
typedef struct {
char* arg;
int currcontext;
int argn;
int rpos;
int tpos;
char newletter;
} galexer_t;
void galexer_init(galexer_t* lexer, char* arg);
int galexer_gettok(galexer_t* lexer, char* token);
#endif

32
libxtools/galexer_test.c Normal file
View File

@@ -0,0 +1,32 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <galexer.h>
int main(int argc, char **argv) {
(void)argc;
(void)argv;
char* arg = "--qwerty=-num-12345";
galexer_t lex;
galexer_init(&lex, arg);
char token[1024];
int toktype = TOKEN_NULL;
int i = 0;
while (toktype != TOKEN_ENDF) {
toktype = galexer_gettok(&lex, token);
printf("%d: %d [%s]\n", i, toktype, token);
i++;
}
return 0;
}

38
libxtools/gclexer.c Normal file
View File

@@ -0,0 +1,38 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <galexer.h>
#include <gclexer.h>
void gclexer_init(gclexer_t* lexer, char** argv, int argc) {
lexer->argv = argv;
lexer->argc = argc;
lexer->argn = 1;
if (lexer->argc > lexer->argn) {
galexer_init(&(lexer->alex), lexer->argv[lexer->argn]);
}
}
int gclexer_gettok(gclexer_t* lexer, char* token) {
if (lexer->argn >= lexer->argc) {
strcpy(token, "EOF");
return TOKEN_ENDF;
}
int toktype = galexer_gettok(&(lexer->alex), token);
if (toktype == TOKEN_ENDF && lexer->argn != lexer->argc) {
lexer->argn++;
galexer_init(&(lexer->alex), lexer->argv[lexer->argn]);
strcpy(token, "space");
return TOKEN_SPACE;
}
return toktype;
}

26
libxtools/gclexer.h Normal file
View File

@@ -0,0 +1,26 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#ifndef GCLEXER_H_QWERTY
#define GCLEXER_H_QWERTY
//#include <string.h>
//#include <stdbool.h>
//#include <stdio.h>
#include <galexer.h>
typedef struct {
char** argv;
int argc;
int argn;
galexer_t alex;
} gclexer_t;
void gclexer_init(gclexer_t* lexer, char** argv, int argc);
int gclexer_gettok(gclexer_t* lexer, char* token);
#endif

34
libxtools/gclexer_test.c Normal file
View File

@@ -0,0 +1,34 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <galexer.h>
#include <gclexer.h>
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;
}

14
libxtools/massert.c Normal file
View File

@@ -0,0 +1,14 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include <stdio.h>
#include <stdlib.h>
#include <massert.h>
void x__assert (char* path, int line, const char* func) {
printf("%s:%d: assert error in %s\n", path, line, func);
exit(1);
}

16
libxtools/massert.h Normal file
View File

@@ -0,0 +1,16 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#ifndef MASSERT_H_QWERTY
#define MASSERT_H_QWERTY
#define massert(expr) if (!(expr)) \
{ x__assert(__FILE__, __LINE__, (const char*)__func__); }
#define MASSERT(expr) if (!(expr)) \
{ x__assert(__FILE__, __LINE__, (const char*)__func__); }
void x__assert (char* path, int line, const char* func);
#endif

125
libxtools/tccomp.c Normal file
View File

@@ -0,0 +1,125 @@
/*
*
* 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);
}

125
libxtools/tccomp.c~ Normal file
View File

@@ -0,0 +1,125 @@
/*
*
* 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);
}

24
libxtools/tccomp.h Normal file
View File

@@ -0,0 +1,24 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#ifndef TCCOMP_H_QWERTY
#define TCCOMP_H_QWERTY
#include <tclexer.h>
#include <vmapper.h>
typedef struct {
tclexer_t* lexer;
vmapper_t* vmapper;
int pos;
int lnum;
} tccomp_t;
tccomp_t * new_tccomp(tclexer_t * lexer, vmapper_t* vmapper);
void tccomp_init(tccomp_t* comp, tclexer_t* lexer, vmapper_t* vmapper);
int tccomp_parse(tccomp_t* comp);
void tccomp_destroy(tccomp_t* comp);
void tccomp_free(tccomp_t* comp);
#endif

72
libxtools/tccomp_test.c Normal file
View File

@@ -0,0 +1,72 @@
/*
* 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 <bstream.h>
#include <tclexer.h>
#include <tccomp.h>
#include <vmapper.h>
#include <massert.h>
int main(void) {
char* src = "key1 = var1 # comment 1\nkey2 = var2 # comment 2 and 3\n# comment 4\nkey3 = var3";
bstream_t stream;
bstream_init(&stream);
tclexer_t lexer;
tclexer_init(&lexer, &stream);
vmapper_t vmapper;
vmapper_init(&vmapper);
tccomp_t comp;
tccomp_init(&comp, &lexer, &vmapper);
bstream_write(&stream, src, strlen(src));
int port = 0;
bool flag = false;
char* ident = NULL;
vmapper_bind_int(&vmapper, "port", &port);
vmapper_bind_string(&vmapper, "ident", &ident);
vmapper_bind_bool(&vmapper, "flag", &flag);
vmapper_set(&vmapper, "port", "12345");
vmapper_set(&vmapper, "ident", "qwerty");
vmapper_set(&vmapper, "flag", "true");
int res = tccomp_parse(&comp);
if (res < 0) {
printf("parsing error pos %d line %d\n", comp.pos, comp.lnum);
}
printf("port = %d\n", port);
MASSERT(port == 12345);
printf("ident = %s\n", ident);
MASSERT(strcmp(ident, "qwerty") == 0);
free(ident);
printf("flag = %d\n", flag);
MASSERT(flag == true);
tccomp_destroy(&comp);
tclexer_destroy(&lexer);
vmapper_destroy(&vmapper);
bstream_destroy(&stream);
return 0;
}

276
libxtools/tclexer.c Normal file
View File

@@ -0,0 +1,276 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <tclexer.h>
#define RES_OK 0
#define RES_ERR -1
#define LEXCONT_UNDEF 0
#define LEXCONT_WORD 1
#define LEXCONT_SPACE 2
#define LEXCONT_COMM 3
#define LEXCONT_OPER 4
#define LEXCONT_ENDFL 5
#define LEXCONT_NEWLN 7
#define LTYPE_SPACE 1
#define LTYPE_LETTER 2
#define LTYPE_OPER 3
#define LTYPE_COMMB 4
#define LTYPE_NEWLN 5
#define LTYPE_ENDFL 7
static int get_ltype(char letter) {
switch (letter) {
case '\n':
return LTYPE_NEWLN;
case ' ':
return LTYPE_SPACE;
case '#':
case ';':
return LTYPE_COMMB;
case '=':
return LTYPE_OPER;
case EOF:
return LTYPE_ENDFL;
}
return LTYPE_LETTER;
}
tclexer_t* new_tclexer(bstream_t * stream) {
tclexer_t* lexer = malloc(sizeof(tclexer_t));
if (lexer == NULL) return NULL;
lexer->stream = stream;
lexer->context = LEXCONT_UNDEF;
return lexer;
}
void tclexer_init(tclexer_t * lexer, bstream_t * stream) {
lexer->stream = stream;
lexer->context = LEXCONT_UNDEF;
lexer->pos = 0;
}
int tclexer_get_token(tclexer_t * lexer, char* token, int maxsize) {
lexer->pos = 0;
if (lexer->pos > (maxsize - 1)) {
return -2;
}
if (lexer->context == LEXCONT_UNDEF) {
lexer->letter = bstream_getc(lexer->stream);
}
while (true) {
int ltype = get_ltype(lexer->letter);
switch (lexer->context) {
case LEXCONT_ENDFL:{
return TOKEN_ENDFL;
}
case LEXCONT_WORD:{
int newcontext = LEXCONT_WORD;
switch (ltype) {
case LTYPE_SPACE:{
newcontext = LEXCONT_SPACE;
break;
}
case LTYPE_NEWLN:{
newcontext = LEXCONT_NEWLN;
break;
}
case LTYPE_COMMB:{
newcontext = LEXCONT_COMM;
break;
}
case LTYPE_OPER:{
newcontext = LEXCONT_OPER;
break;
}
case LTYPE_ENDFL:{
newcontext = LEXCONT_ENDFL;
break;
}
}
if (newcontext != lexer->context) {
lexer->context = newcontext;
token[lexer->pos++] = '\0';
return TOKEN_WORD;
}
token[lexer->pos++] = lexer->letter;
break;
}
case LEXCONT_COMM:{
int newcontext = LEXCONT_COMM;
switch (ltype) {
case LTYPE_NEWLN:{
newcontext = LEXCONT_NEWLN;
break;
}
case LTYPE_ENDFL:{
newcontext = LEXCONT_ENDFL;
break;
}
}
if (newcontext != lexer->context) {
token[lexer->pos++] = '\0';
lexer->context = newcontext;
return TOKEN_COMM;
}
token[lexer->pos++] = lexer->letter;
break;
}
case LEXCONT_SPACE:{
int newcontext = LEXCONT_SPACE;
switch (ltype) {
case LTYPE_OPER:{
newcontext = LEXCONT_OPER;
break;
}
case LTYPE_COMMB:{
newcontext = LEXCONT_COMM;
break;
}
case LTYPE_LETTER:{
newcontext = LEXCONT_WORD;
break;
}
case LTYPE_NEWLN:{
newcontext = LEXCONT_NEWLN;
break;
}
case LTYPE_ENDFL:{
newcontext = LEXCONT_ENDFL;
break;
}
}
if (newcontext != lexer->context) {
lexer->context = newcontext;
strcpy(token, "SPACE");
return TOKEN_SPACE;
}
token[lexer->pos++] = lexer->letter;
break;
}
case LEXCONT_OPER:{
int newcontext = LEXCONT_OPER;
switch (ltype) {
case LTYPE_SPACE:{
newcontext = LEXCONT_SPACE;
break;
}
case LTYPE_NEWLN:{
newcontext = LEXCONT_NEWLN;
break;
}
case LTYPE_COMMB:{
newcontext = LEXCONT_COMM;
break;
}
case LTYPE_LETTER:{
newcontext = LEXCONT_WORD;
break;
}
case LTYPE_ENDFL:{
newcontext = LEXCONT_ENDFL;
break;
}
}
if (newcontext != lexer->context) {
lexer->context = newcontext;
strcpy(token, "=");
return TOKEN_OPER;
}
token[lexer->pos++] = lexer->letter;
break;
}
case LEXCONT_NEWLN:{
int newcontext = LEXCONT_NEWLN;
switch (ltype) {
case LTYPE_SPACE:{
newcontext = LEXCONT_SPACE;
break;
}
case LTYPE_COMMB:{
newcontext = LEXCONT_COMM;
break;
}
case LTYPE_LETTER:{
newcontext = LEXCONT_WORD;
break;
}
case LTYPE_ENDFL:{
newcontext = LEXCONT_ENDFL;
break;
}
}
if (newcontext != lexer->context) {
lexer->context = newcontext;
strcpy(token, "NL");
return TOKEN_NEWLN;
}
token[lexer->pos++] = lexer->letter;
break;
}
case LEXCONT_UNDEF:
default:{
int newcontext = LEXCONT_UNDEF;
switch (ltype) {
case LTYPE_SPACE:{
newcontext = LEXCONT_SPACE;
break;
}
case LTYPE_NEWLN:{
newcontext = LEXCONT_NEWLN;
break;
}
case LTYPE_COMMB:{
newcontext = LEXCONT_COMM;
break;
}
case LTYPE_LETTER:{
newcontext = LEXCONT_WORD;
break;
}
case LTYPE_OPER:{
newcontext = LEXCONT_OPER;
break;
}
case LTYPE_ENDFL:{
newcontext = LEXCONT_ENDFL;
break;
}
}
lexer->context = newcontext;
token[lexer->pos++] = lexer->letter;
break;
}
}
lexer->letter = bstream_getc(lexer->stream);
}
return TOKEN_ENDFL;
}
void tclexer_destroy(tclexer_t* lexer) {
(void)lexer;
}
void tclexer_free(tclexer_t* lexer) {
free(lexer);
}

32
libxtools/tclexer.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#ifndef TCLEXER_H_QWERTY
#define TCLEXER_H_QWERTY
#include <bstream.h>
typedef struct {
bstream_t* stream;
int context;
char letter;
int pos;
} tclexer_t;
#define MAX_TOK_SIZE 1024
#define TOKEN_NULL 0
#define TOKEN_WORD 1
#define TOKEN_SPACE 2
#define TOKEN_COMM 3
#define TOKEN_OPER 4
#define TOKEN_ENDFL 5
#define TOKEN_NEWLN 7
tclexer_t* new_tclexer(bstream_t * stream);
void tclexer_init(tclexer_t * tclexer, bstream_t * stream);
int tclexer_get_token(tclexer_t * tclexer, char* token, int maxsize);
void tclexer_destroy(tclexer_t* lexer);
void tclexer_free(tclexer_t* lexer);
#endif

37
libxtools/tclexer_test.c Normal file
View File

@@ -0,0 +1,37 @@
/*
* 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 <bstream.h>
#include <tclexer.h>
int main(void) {
char* src = " key1 = var1 # comment 1\nkey2 = var2 # comment 2 and 3\n# comment 4\nkey3 = var3";
bstream_t stream;
bstream_init(&stream);
bstream_write(&stream, src, strlen(src));
tclexer_t lexer;
tclexer_init(&lexer, &stream);
int token_typ = TOKEN_NULL;
char token[MAX_TOK_SIZE];
while ((token_typ = tclexer_get_token(&lexer, token, MAX_TOK_SIZE)) != TOKEN_ENDFL) {
printf("%d:[%s]\n", token_typ, token);
}
tclexer_destroy(&lexer);
bstream_destroy(&stream);
return 0;
}

41
libxtools/tconfig.c Normal file
View File

@@ -0,0 +1,41 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdio.h>
#include <tclexer.h>
#include <tccomp.h>
#include <bstream.h>
#include <vmapper.h>
#include <tconfig.h>
void tconfig_init(tconfig_t* tconfig) {
bstream_init(&(tconfig->stream));
vmapper_init(&(tconfig->mapper));
tclexer_init(&(tconfig->lexer), &(tconfig->stream));
tccomp_init(&(tconfig->comp), &(tconfig->lexer), &(tconfig->mapper));
}
int tconfig_bind(tconfig_t* tconfig, int type, char* name, void* ptr) {
vmapper_t* vmapper = &(tconfig->mapper);
return vmapper_bind(vmapper, type, name, ptr);
}
ssize_t tconfig_read(tconfig_t* tconfig, char* filename) {
bstream_t* stream = &(tconfig->stream);
return bstream_fread(stream, filename);
}
int tconfig_parse(tconfig_t* tconfig) {
return tccomp_parse(&(tconfig->comp));
}
void tconfig_destroy(tconfig_t* tconfig) {
tccomp_destroy(&(tconfig->comp));
tclexer_destroy(&(tconfig->lexer));
bstream_destroy(&(tconfig->stream));
vmapper_destroy(&(tconfig->mapper));
}

31
libxtools/tconfig.h Normal file
View File

@@ -0,0 +1,31 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#ifndef TCONFIG_H_QWERTY
#define TCONFIG_H_QWERTY
#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);
#endif

27
libxtools/tconfig.h~ Normal file
View File

@@ -0,0 +1,27 @@
/*
* 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);

39
libxtools/tconfig_test.c Normal file
View File

@@ -0,0 +1,39 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#include <stdio.h>
#include <fcntl.h>
#include <tconfig.h>
#include <massert.h>
int main(int argc, char **argv) {
(void)argc;
(void)argv;
tconfig_t tconfig;
tconfig_init(&tconfig);
int intkey = 0;
char* strkey = NULL;
tconfig_bind(&tconfig, TCONF_INT, "intkey", &intkey);
tconfig_bind(&tconfig, TCONF_STR, "strkey", &strkey);
ssize_t rsize = tconfig_read(&tconfig, "test.conf");
printf("%ld\n", rsize);
MASSERT(rsize > 0);
int res = tconfig_parse(&tconfig);
MASSERT(res == -1);
tconfig_destroy(&tconfig);
printf("int key = %d\n", intkey);
printf("str key = %s\n", strkey);
return 0;
}

6
libxtools/test.conf Normal file
View File

@@ -0,0 +1,6 @@
# comment
intkey = 123456
strkey =qwerty
#EOF

197
libxtools/vmapper.c Normal file
View File

@@ -0,0 +1,197 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <vmapper.h>
static char* copystr(char* src) {
size_t srcsize = strlen(src) + 1;
char* dest = malloc(srcsize);
memset(dest, '\0', srcsize);
strcpy(dest, src);
return dest;
}
static mlink_t* new_mlink_string(char* name, char** val) {
mlink_t* mlink = malloc(sizeof(mlink_t));
mlink->name = name;
mlink->vptr = (void*)val;
mlink->type = MAPPER_STR;
return mlink;
}
static mlink_t* new_mlink_integer(char* name, int* val) {
mlink_t* mlink = malloc(sizeof(mlink_t));
mlink->name = name;
mlink->vptr = (void*)val;
mlink->type = MAPPER_INT;
return mlink;
}
static mlink_t* new_mlink_bool(char* name, bool* val) {
mlink_t* mlink = malloc(sizeof(mlink_t));
mlink->name = name;
mlink->vptr = (void*)val;
mlink->type = MAPPER_BOOL;
return mlink;
}
#define MAPPER_INITCAPA 64
vmapper_t* new_vmapper(void) {
vmapper_t* vmapper = malloc(sizeof(vmapper_t));
if (vmapper == NULL) return NULL;
vmapper->mlinks = malloc(sizeof(mlink_t) * MAPPER_INITCAPA);
vmapper->capa = MAPPER_INITCAPA;
vmapper->size = 0;
vmapper->err = false;
vmapper->errstr = NULL;
return vmapper;
}
void vmapper_init(vmapper_t* vmapper) {
vmapper->mlinks = malloc(sizeof(mlink_t) * MAPPER_INITCAPA);
vmapper->capa = MAPPER_INITCAPA;
vmapper->size = 0;
vmapper->err = false;
vmapper->errstr = NULL;
return;
}
#define RES_BIND_OK ((int)0)
#define RES_BIND_ERR ((int)-1)
int vmapper_check_capa(vmapper_t* vmapper) {
if (vmapper->size == vmapper->capa) {
size_t newcapa = vmapper->capa * 2;
mlink_t** newmlinks = realloc(vmapper->mlinks, newcapa);
if (newmlinks == NULL) return RES_BIND_ERR;
vmapper->mlinks = newmlinks;
vmapper->capa = newcapa;
}
return RES_BIND_OK;
}
int vmapper_bind_string(vmapper_t* vmapper, char* name, char** val) {
int res = 0;
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
return res;
}
mlink_t* mlink = new_mlink_string(name, val);
vmapper->mlinks[vmapper->size] = mlink;
vmapper->size++;
return RES_BIND_OK;
}
int vmapper_bind_int(vmapper_t* vmapper, char* name, int* val) {
int res = 0;
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
return res;
}
mlink_t* mlink = new_mlink_integer(name, val);
vmapper->mlinks[vmapper->size] = mlink;
vmapper->size++;
return RES_BIND_OK;
}
int vmapper_bind_bool(vmapper_t* vmapper, char* name, bool* val) {
int res = 0;
if ((res = vmapper_check_capa(vmapper)) != RES_BIND_OK) {
return res;
}
mlink_t* mlink = new_mlink_bool(name, val);
vmapper->mlinks[vmapper->size] = mlink;
vmapper->size++;
return RES_BIND_OK;
}
int vmapper_set_int(vmapper_t* vmapper, char* key, char* val) {
for (size_t i = 0; i < vmapper->size; i++) {
mlink_t* mlink = vmapper->mlinks[i];
if (mlink->type == MAPPER_INT && strcmp(mlink->name, key) == 0) {
char* eptr = NULL;
*(int*)(mlink->vptr) = (int)strtol(val, &eptr, 10);
}
}
return 0;
}
int vmapper_set_string(vmapper_t* vmapper, char* key, char* val) {
for (size_t i = 0; i < vmapper->size; i++) {
mlink_t* mlink = vmapper->mlinks[i];
if (mlink->type == MAPPER_STR && strcmp(mlink->name, key) == 0) {
*(char**)(mlink->vptr) = copystr(val);
}
}
return 0;
}
int vmapper_set_bool(vmapper_t* vmapper, char* key, char* val) {
for (size_t i = 0; i < vmapper->size; i++) {
mlink_t* mlink = vmapper->mlinks[i];
if (mlink->type == MAPPER_BOOL && strcmp(mlink->name, key) == 0) {
if (strcmp(val, "true") == 0) {
*(bool*)(mlink->vptr) = true;
}
if (strcmp(val, "false") == 0) {
*(bool*)(mlink->vptr) = false;
}
}
}
return 0;
}
int vmapper_bind(vmapper_t* vmapper, int type, char* name, void* val) {
switch (type) {
case MAPPER_STR: {
return vmapper_bind_string(vmapper, name, (char**)val);
}
case MAPPER_INT: {
return vmapper_bind_int(vmapper, name, (int*)val);
}
case MAPPER_BOOL: {
return vmapper_bind_bool(vmapper, name, (bool*)val);
}
}
return 0;
}
int vmapper_set(vmapper_t* vmapper, char* key, char* val) {
vmapper_set_int(vmapper, key, val);
vmapper_set_string(vmapper, key, val);
vmapper_set_bool(vmapper, key, val);
return 0;
}
void vmapper_destroy(vmapper_t* vmapper) {
if (vmapper != NULL) {
for (size_t i = 0; i < vmapper->size; i++) {
free(vmapper->mlinks[i]);
}
free(vmapper->mlinks);
}
return;
}
void vmapper_free(vmapper_t* vmapper) {
if (vmapper != NULL) {
for (size_t i = 0; i < vmapper->size; i++) {
free(vmapper->mlinks[i]);
}
free(vmapper->mlinks);
}
free(vmapper);
}

49
libxtools/vmapper.h Normal file
View File

@@ -0,0 +1,49 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#ifndef MAPPER_H_QWERTY
#define MAPPER_H_QWERTY
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char* name;
int type;
void* vptr;
} mlink_t;
#define MAPPER_STR 1
#define MAPPER_INT 2
#define MAPPER_BOOL 3
typedef struct {
mlink_t** mlinks;
size_t size;
size_t capa;
bool err;
char* errstr;
} vmapper_t;
vmapper_t* new_vmapper(void);
void vmapper_init(vmapper_t* vmapper);
int vmapper_check_capa(vmapper_t* vmapper);
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_bool(vmapper_t* vmapper, char* name, bool* val);
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_bool(vmapper_t* vmapper, char* key, char* val);
int vmapper_set(vmapper_t* vmapper, char* key, char* val);
int vmapper_bind(vmapper_t* vmapper, int type, char* name, void* val);
void vmapper_destroy(vmapper_t* vmapper);
void vmapper_free(vmapper_t* vmapper);
#endif

45
libxtools/vmapper_test.c Normal file
View File

@@ -0,0 +1,45 @@
/*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <vmapper.h>
#include <massert.h>
int main(int argc, char **argv) {
(void)argc;
(void)argv;
vmapper_t vmapper;
vmapper_init(&vmapper);
int port = 0;
bool flag = false;
char* ident = NULL;
vmapper_bind_int(&vmapper, "port", &port);
vmapper_bind_string(&vmapper, "ident", &ident);
vmapper_bind_bool(&vmapper, "flag", &flag);
vmapper_set(&vmapper, "port", "12345");
vmapper_set(&vmapper, "ident", "qwerty");
vmapper_set(&vmapper, "flag", "true");
printf("port = %d\n", port);
MASSERT(port = 12345);
printf("ident = %s\n", ident);
MASSERT(strcmp(ident, "qwerty") == 0);
free(ident);
printf("flag = %d\n", flag);
MASSERT(flag = true);
vmapper_destroy(&vmapper);
return 0;
}