diff options
author | ache <ache@ache.one> | 2019-02-05 19:16:42 +0100 |
---|---|---|
committer | ache <ache@ache.one> | 2019-02-05 19:16:42 +0100 |
commit | 446e47937737980f6757134b77267bd83a69f066 (patch) | |
tree | 5324351635695458782522fb03f31a6ed7a7ea45 | |
parent | Search function (diff) |
Add config file management
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | config.h | 15 | ||||
-rw-r--r-- | contactList.c | 17 | ||||
-rw-r--r-- | contactList.h | 1 | ||||
-rw-r--r-- | main.c | 38 | ||||
-rw-r--r-- | main.h | 1 | ||||
-rw-r--r-- | readConfig.c | 59 | ||||
-rw-r--r-- | sms.c | 8 | ||||
-rw-r--r-- | sms.h | 7 |
9 files changed, 112 insertions, 37 deletions
@@ -4,13 +4,14 @@ CC=gcc PROG=mesms LIBS=-lcurses -lreadline HEADERS=$(wildcard *.h) -FILES=readline.c wind.c contactList.c basic_curses.c contactWind.c sms.c messageWind.c +FILES=readline.c wind.c contactList.c basic_curses.c contactWind.c sms.c messageWind.c readConfig.c OBJ_FILES := $(FILES:.c=.o) CFLAGS+=-std=gnu99 all: $(PROG) +debug: clean debug: CFLAGS += -DDEBUG -g debug: $(PROG) diff --git a/config.h b/config.h new file mode 100644 index 0000000..d053bc7 --- /dev/null +++ b/config.h @@ -0,0 +1,15 @@ +#ifndef CONFIG_H +#define CONFIG_H + +typedef struct config_t { + char SMS_INBOX[256]; + char SMS_OUTBOX[256]; + char SMS_SENTBOX[256]; + char MMS_DIR[256]; + char CONTACT_FILENAME[256]; + +} config_t; + +int readConfigFile(void); + +#endif diff --git a/contactList.c b/contactList.c index d817120..a5c7107 100644 --- a/contactList.c +++ b/contactList.c @@ -1,6 +1,7 @@ #include <stdlib.h> #include <stdio.h> -#include"contactList.h" +#define _GNU_SOURCE +#include "contactList.h" contact* contactList; size_t nbContacts; @@ -70,7 +71,7 @@ int fillContactList(char* filename) { if( contactList ) { free(contactList); } - + if( !file ) { perror("Unable to read contact file"); return 1; @@ -82,7 +83,7 @@ int fillContactList(char* filename) { if( c == ';' ) { v++; if( v >= 3) { - fprintf(stderr, "Invalid format (contact file) : too many field"); + perror("Invalid format (contact file) : too many field"); return 2; // Invalid } } @@ -91,8 +92,8 @@ int fillContactList(char* filename) { v = 0; nbContacts++; } - else {// Pas assez - fprintf(stderr, "Invalid format (contact file) : not enougth field"); + else {// Not enougth + perror("Invalid format (contact file) : not enougth field"); return 2; // Invalid format } } @@ -141,20 +142,26 @@ int fillContactList(char* filename) { if( v == 0) { contactList[i].name[nameLength++] = c; if( nameLength > (sizeof contactList[i].name -1) ) { +#ifdef DEBUG fprintf(stderr, "too long name ?"); +#endif return 3; } }else if( v == 1) { contactList[i].display_name[display_nameLength++] = c; if( display_nameLength > (sizeof contactList[i].display_name -1) ) { +#ifdef DEBUG fprintf(stderr, "too display name ?"); +#endif return 9; } } else { contactList[i].number[numberLength++] = c; if( numberLength > (sizeof contactList[i].number -1) ) { puts(contactList[i].number); +#ifdef DEBUG fprintf(stderr, "too long number ?"); +#endif return 27+i; } } diff --git a/contactList.h b/contactList.h index 43199a1..c5cf10c 100644 --- a/contactList.h +++ b/contactList.h @@ -3,6 +3,7 @@ #include <ncurses.h> #include <stdio.h> +#include <string.h> #include "basic_curses.h" typedef struct conversation conversation; @@ -31,13 +31,8 @@ const int color[] = { COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE}; - - - #define MAX_PATH 1024 - - extern char* msg_win_str; extern conversation* currentConv; @@ -49,14 +44,14 @@ char status[10]; extern int firstSMSIndex; extern int canAugment; extern int showName; - -#define DEBUG - +extern config_t config; void fail_exit(const char *msg) { if (visual_mode) endwin(); +#ifdef DEBUG fprintf(stderr, "%s\n", msg); +#endif exit(EXIT_FAILURE); } @@ -65,6 +60,7 @@ int main(int argc, char* argv[]){ #ifdef DEBUG freopen("/tmp/mesms.debug", "w", stderr); #endif + readConfigFile(); /* Gestion des paramètres */ while (1) { @@ -97,12 +93,16 @@ int main(int argc, char* argv[]){ case 'c': if( optarg ) { int r = 0; - if( r = fillContactList(DEFAULT_CONTACT_FILENAME) ) { - fprintf(stderr, "Erreur à la lecture de la liste des contacts"); + if( r = fillContactList(config.CONTACT_FILENAME) ) { +#ifdef DEBUG + fprintf(stderr, "Error reading contact list\n"); +#endif return r; } if( r = loadConv() ) { - fprintf(stderr, "Erreur à la lecture de la liste des contacts"); +#ifdef DEBUG + fprintf(stderr, "Error reading contact list\n"); +#endif return r; } contact* contact = NULL; @@ -113,7 +113,9 @@ int main(int argc, char* argv[]){ else r = findContactFromName(optarg, &contact); if( r ) { - fprintf(stderr, "Contact introuvable"); +#ifdef DEBUG + fprintf(stderr, "Missing contact"); +#endif printf("%s",optarg); return r; } @@ -123,7 +125,9 @@ int main(int argc, char* argv[]){ printf("%s", contact->number); return 0; } else { - fprintf(stderr, "Numéro de téléphone manquant"); +#ifdef DEBUG + fprintf(stderr, "Missing phone number"); +#endif } break; @@ -133,10 +137,6 @@ int main(int argc, char* argv[]){ } } - - fprintf(stderr, "test"); - - /* Initialisation */ int size = 0; @@ -172,18 +172,14 @@ int main(int argc, char* argv[]){ int r = 0; - fprintf(stderr, "test1"); if( r = fillContactList(DEFAULT_CONTACT_FILENAME) ) { return r; } if( r = loadConv() ) { return r; } - fprintf(stderr, "test2"); sortContacts(); - fprintf(stderr, "test3"); - for(int i = 0 ; i < 9 ; i++) { init_pair( i+1, i, COLOR_BLACK); } @@ -20,6 +20,7 @@ #include "contactList.h" #include "wind.h" #include "sms.h" +#include "config.h" #define HIDDEN 1 diff --git a/readConfig.c b/readConfig.c new file mode 100644 index 0000000..94aecc7 --- /dev/null +++ b/readConfig.c @@ -0,0 +1,59 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include "config.h" + +config_t config = { .SMS_INBOX = "/var/spool/sms/inbox/", + .SMS_SENTBOX = "/var/spool/sms/sent/", + .SMS_OUTBOX = "/var/spool/sms/inbox/", + .MMS_DIR = "", + .CONTACT_FILENAME = "/var/spool/sms/contacts.csv"}; + +const char* search_key[]= {"SMS_INBOX", "SMS_SENTBOX", "SMS_OUTBOX", "CONTACT_FILENAME", "MMS_DIR"}; + +int searchConfigFile(char* configFile) { + // Search a config file. + // First then in the home, then in /etc and finaly in /var. + // In debug mode, this function search in ./fixtures + + strcpy(configFile, "/home/ache/.mesmsrc"); + + return 0; +} +int readConfigFile(void) { + char fileName[256] = ""; + +#ifdef DEBUG + fprintf(stderr, "ReadConfig\n"); +#endif + + if( searchConfigFile(fileName) > 0 ) { + return 1; + } + + FILE* file = fopen(fileName, "r"); + char line[300] = ""; + + if( file ) { + while(fgets(line, 300, file) != NULL) { + int elem = -1; + for( int i = 0 ; i < sizeof search_key / sizeof *search_key ; i++) { + if( strncmp(line, search_key[i], strlen(search_key[i])) == 0 ) { + elem = i; + break; + } + } + if(elem >= 0) { + strtok(line, "\""); +#ifdef DEBUG + fprintf(stderr, "%s is \"%s\"\n", search_key[elem], strtok(NULL, "\"") ); +#endif + } + } + } +#ifdef DEBUG + else { + fprintf(stderr, "Error reading config file [%s]\n", fileName); + } +#endif +} @@ -2,11 +2,9 @@ #include <stdio.h> #include <stdlib.h> - - - conversation* listConv; size_t nbConv; +extern config_t config; int insertSMS(sms* S) { int index = -1; @@ -200,12 +198,12 @@ int listSMS(char* smsInboxDir, int inorout) { int loadConv(void) { - int r = listSMS("/var/spool/sms/inbox/", SMS_IN); + int r = listSMS(config.SMS_INBOX, SMS_IN); if( r ) { fprintf(stderr, "Impossible de lire les SMS reçu"); return 1; } - r = listSMS("/var/spool/sms/sent/" , SMS_OUT); + r = listSMS(config.SMS_SENTBOX , SMS_OUT); if( r ) { fprintf(stderr, "Impossible de lire les SMS envoyés"); return 2; @@ -6,14 +6,11 @@ #include <dirent.h> #include <sys/types.h> -#include <string.h> #include <time.h> - - +#include <string.h> #include "contactList.h" - - +#include "config.h" #define SMS_IN 0 #define SMS_OUT 1 |