]> Git repositories of Nishi - tewi.git/commitdiff
can trim line
authorNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 09:39:33 +0000 (09:39 +0000)
committerNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 09:39:33 +0000 (09:39 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@4 8739d7e6-ffea-ec47-b151-bdff447c6205

Common/cm_string.h
Common/string.c
Makefile
Server/Makefile
Server/config.c [new file with mode: 0644]
Server/main.c
Server/tw_config.h [new file with mode: 0644]

index 66e646a862b4d5a2a9ff0ae2f654c8961b59d840..c055ec352a33daa4e6899860163972d27ddb5d38 100644 (file)
@@ -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
index dfd94d39b716e9c268bf8fde9b2c1f8e51f3152f..f35c3307d9cacca2c5578f4f0fd1348a758454e1 100644 (file)
@@ -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;
+}
index 496f459d30e1fd83bd0ee7c187bcb810af10d1fe..f5e456df6282720fe1f4fb89701d5ddac3186967 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
 
 PWD = `pwd`
 PLATFORM = generic
-PREFIX = /usr
+PREFIX = /usr/local
 
 FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) PREFIX=$(PREFIX)
 
index f427984f22f0127bf3ae878cf50d0e3a6f0175ca..bc170491b31ab8d88ce99bd4c212b5c3a34a0bd8 100644 (file)
@@ -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 (file)
index 0000000..eba0c2c
--- /dev/null
@@ -0,0 +1,44 @@
+/* $Id$ */
+
+#include "tw_config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <cm_string.h>
+#include <cm_log.h>
+
+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;
+       }
+}
index ec48162ab92022157ddc5e62582b9a6337873caf..3c30f32bdeaf1d740e4ca5e51784ccf3f69b84b4 100644 (file)
@@ -6,12 +6,14 @@
 
 #include <cm_log.h>
 
+#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 (file)
index 0000000..b568ab8
--- /dev/null
@@ -0,0 +1,8 @@
+/* $Id$ */
+
+#ifndef __TW_CONFIG_H__
+#define __TW_CONFIG_H__
+
+int tw_config_read(const char* path);
+
+#endif