can trim line

git-svn-id: file:///raid/svn-personal/tewi/trunk@4 8739d7e6-ffea-ec47-b151-bdff447c6205
This commit is contained in:
Nishi 2024-09-13 09:39:33 +00:00
parent 2e35aca50c
commit 9e34a40944
7 changed files with 99 additions and 2 deletions

View 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

View 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;
}

View File

@ -2,7 +2,7 @@
PWD = `pwd`
PLATFORM = generic
PREFIX = /usr
PREFIX = /usr/local
FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) PREFIX=$(PREFIX)

View 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)

44
Server/config.c Normal file
View File

@ -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;
}
}

View 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");
}

8
Server/tw_config.h Normal file
View File

@ -0,0 +1,8 @@
/* $Id$ */
#ifndef __TW_CONFIG_H__
#define __TW_CONFIG_H__
int tw_config_read(const char* path);
#endif