From 9e34a40944ee1804786b37e3e7d0eceb4f95689f Mon Sep 17 00:00:00 2001 From: Nishi Date: Fri, 13 Sep 2024 09:39:33 +0000 Subject: [PATCH] can trim line git-svn-id: file:///raid/svn-personal/tewi/trunk@4 8739d7e6-ffea-ec47-b151-bdff447c6205 --- Common/cm_string.h | 3 +++ Common/string.c | 29 +++++++++++++++++++++++++++++ Makefile | 2 +- Server/Makefile | 2 +- Server/config.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ Server/main.c | 13 +++++++++++++ Server/tw_config.h | 8 ++++++++ 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 Server/config.c create mode 100644 Server/tw_config.h diff --git a/Common/cm_string.h b/Common/cm_string.h index 66e646a..c055ec3 100644 --- a/Common/cm_string.h +++ b/Common/cm_string.h @@ -5,5 +5,8 @@ char* cm_strcat(const char* a, const char* b); char* cm_strdup(const char* str); +char* cm_trimstart(const char* str); +char* cm_trimend(const char* str); +char* cm_trim(const char* str); #endif diff --git a/Common/string.c b/Common/string.c index dfd94d3..f35c330 100644 --- a/Common/string.c +++ b/Common/string.c @@ -12,3 +12,32 @@ char* cm_strcat(const char* a, const char* b) { } char* cm_strdup(const char* str) { return cm_strcat(str, ""); } + +char* cm_trimstart(const char* str){ + int i; + for(i = 0; str[i] != 0; i++){ + if(str[i] != ' ' && str[i] != '\t'){ + return cm_strdup(str + i); + } + } + return cm_strdup(""); +} + +char* cm_trimend(const char* str){ + char* s = cm_strdup(str); + int i; + for(i = strlen(s) - 1; i >= 0; i--){ + if(s[i] != '\t' && s[i] != ' '){ + s[i + 1] = 0; + break; + } + } + return s; +} + +char* cm_trim(const char* str){ + char* tmp = cm_trimstart(str); + char* s = cm_trimend(tmp); + free(tmp); + return s; +} diff --git a/Makefile b/Makefile index 496f459..f5e456d 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ PWD = `pwd` PLATFORM = generic -PREFIX = /usr +PREFIX = /usr/local FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) PREFIX=$(PREFIX) diff --git a/Server/Makefile b/Server/Makefile index f427984..bc17049 100644 --- a/Server/Makefile +++ b/Server/Makefile @@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk .PHONY: all clean .SUFFIXES: .c .o -OBJS = version.o main.o +OBJS = version.o main.o config.o all: tewi$(EXEC) diff --git a/Server/config.c b/Server/config.c new file mode 100644 index 0000000..eba0c2c --- /dev/null +++ b/Server/config.c @@ -0,0 +1,44 @@ +/* $Id$ */ + +#include "tw_config.h" + +#include +#include +#include + +#include +#include + +int tw_config_read(const char* path){ + cm_log("Config", "Reading %s", path); + char cbuf[2]; + cbuf[1] = 0; + FILE* f = fopen(path, "r"); + if(f != NULL){ + char* line = malloc(1); + line[0] = 0; + while(1){ + int c = fread(cbuf, 1, 1, f); + if(cbuf[0] == '\n' || c <= 0){ + char* l = cm_trim(line); + if(strlen(l) > 0 && l[0] != '#'){ + printf("[%s]\n", l); + } + free(l); + free(line); + line = malloc(1); + line[0] = 0; + if(c <= 0) break; + }else if(cbuf[0] != '\r'){ + char* tmp = line; + line = cm_strcat(tmp, cbuf); + free(tmp); + } + } + free(line); + fclose(f); + return 0; + }else{ + return 1; + } +} diff --git a/Server/main.c b/Server/main.c index ec48162..3c30f32 100644 --- a/Server/main.c +++ b/Server/main.c @@ -6,12 +6,14 @@ #include +#include "tw_config.h" #include "tw_version.h" extern bool cm_do_log; int main(int argc, char** argv) { int i; + const char* config = PREFIX "/etc/tewi.conf"; for(i = 1; i < argc; i++) { if(argv[i][0] == '-') { if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) { @@ -21,11 +23,22 @@ int main(int argc, char** argv) { } else { cm_do_log = true; } + } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0){ + i++; + if(argv[i] == NULL){ + fprintf(stderr, "Missing argument\n"); + return 1; + } + config = argv[i]; } else { fprintf(stderr, "Unknown option: %s\n", argv[i]); return 1; } } } + if(tw_config_read(config) != 0){ + fprintf(stderr, "Could not read the config\n"); + return 1; + } cm_log("Daemon", "Ready"); } diff --git a/Server/tw_config.h b/Server/tw_config.h new file mode 100644 index 0000000..b568ab8 --- /dev/null +++ b/Server/tw_config.h @@ -0,0 +1,8 @@ +/* $Id$ */ + +#ifndef __TW_CONFIG_H__ +#define __TW_CONFIG_H__ + +int tw_config_read(const char* path); + +#endif -- 2.43.0