diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..f09c9d2 --- /dev/null +++ b/.clang-format @@ -0,0 +1,15 @@ +--- +# $Id$ +Language: Cpp +UseTab: Always +TabWidth: 8 +IndentWidth: 8 +PointerAlignment: Left +ColumnLimit: 1024 +AllowShortIfStatementsOnASingleLine: Always +AllowShortBlocksOnASingleLine: Never +AllowShortLoopsOnASingleLine: true +SpaceBeforeParens: Never +AlignEscapedNewlines: DontAlign +SortIncludes: false +AllowShortEnumsOnASingleLine: false diff --git a/Bot/Makefile b/Bot/Makefile new file mode 100644 index 0000000..f32e595 --- /dev/null +++ b/Bot/Makefile @@ -0,0 +1,19 @@ +# $Id$ + +include $(PWD)/Platform/$(PLATFORM).mk + +.PHONY: all clean +.SUFFIXES: .c .o + +OBJS = main.o util.o + +all: ircarc$(EXEC) + +ircarc$(EXEC): $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) + +.c.o: + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + rm -f *.o *.exe ircarc diff --git a/Bot/ia_util.h b/Bot/ia_util.h new file mode 100644 index 0000000..c881bac --- /dev/null +++ b/Bot/ia_util.h @@ -0,0 +1,9 @@ +/* $Id$ */ + +#ifndef __IA_UTIL_H__ +#define __IA_UTIL_H__ + +char* ia_strcat(const char* a, const char* b); +char* ia_strdup(const char* str); + +#endif diff --git a/Bot/main.c b/Bot/main.c index 9a89216..f30f393 100644 --- a/Bot/main.c +++ b/Bot/main.c @@ -5,10 +5,13 @@ #include #include -int main(int argc, char** argv){ +#include "ia_util.h" +#include "ia_util.h" + +int main(int argc, char** argv) { const char* fn = argv[1] == NULL ? "archiver.ini" : argv[1]; FILE* f = fopen(fn, "r"); - if(f == NULL){ + if(f == NULL) { fprintf(stderr, "Could not open the config: %s\n", fn); return 1; } @@ -23,20 +26,36 @@ int main(int argc, char** argv){ int i; int incr = 0; - for(i = 0;; i++){ - if(buf[i] == 0 || buf[i] == '\n'){ + char* host = NULL; + int port = 0; + char* username = NULL; + char* password = NULL; + + for(i = 0;; i++) { + if(buf[i] == 0 || buf[i] == '\n') { char oldc = buf[i]; buf[i] = 0; char* line = buf + incr; - if(strlen(line) > 0){ + if(strlen(line) > 0 && line[0] != '#') { int j; - for(j = 0; line[j] != 0; j++){ - if(line[j] == '='){ + for(j = 0; line[j] != 0; j++) { + if(line[j] == '=') { line[j] = 0; char* key = line; char* value = line + j + 1; - + if(strcmp(key, "host") == 0) { + if(host != NULL) free(host); + host = ia_strdup(value); + } else if(strcmp(key, "port") == 0) { + port = atoi(value); + } else if(strcmp(key, "username") == 0) { + if(username != NULL) free(username); + username = ia_strdup(value); + } else if(strcmp(key, "password") == 0) { + if(password != NULL) free(password); + password = ia_strdup(value); + } break; } @@ -44,10 +63,28 @@ int main(int argc, char** argv){ } incr = i + 1; if(oldc == 0) break; - }else{ } } free(buf); fclose(f); + + int st = 0; + if(host == NULL) { + fprintf(stderr, "Specify host\n"); + st = 1; + } + if(username == NULL) { + fprintf(stderr, "Specify username\n"); + st = 1; + } + if(password == NULL) { + fprintf(stderr, "Specify password\n"); + st = 1; + } + if(st == 1) return st; + + if(host != NULL) free(host); + if(username != NULL) free(username); + if(password != NULL) free(password); } diff --git a/Bot/util.c b/Bot/util.c new file mode 100644 index 0000000..5aa02e6 --- /dev/null +++ b/Bot/util.c @@ -0,0 +1,16 @@ +/* $Id$ */ + +#include "ia_util.h" + +#include +#include + +char* ia_strcat(const char* a, const char* b) { + char* str = malloc(strlen(a) + strlen(b) + 1); + memcpy(str, a, strlen(a)); + memcpy(str + strlen(a), b, strlen(b)); + str[strlen(a) + strlen(b)] = 0; + return str; +} + +char* ia_strdup(const char* str) { return ia_strcat(str, ""); } diff --git a/Makefile b/Makefile index 2ba12ff..5eb917f 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,17 @@ PWD = `pwd` PLATFORM = generic -FLAG = PWD=$(PWD) PLATFORM=$(PLATFORM) +FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) -.PHONY: all clean ./Bot +.PHONY: all format clean ./Bot all: ./Bot ./Bot:: $(MAKE) -C $@ $(FLAGS) +format: + clang-format --verbose -i `find . -name "*.c" -or -name "*.h"` + clean: $(MAKE) -C $@ $(FLAGS) clean