#include #include #define _GNU_SOURCE #include "contactList.h" contact* contactList; size_t nbContacts; int findContact(const char* number, contact** out) { if( !number || !out ) return 1; for(int i = 0 ; i < nbContacts ; i++) { if( !strcmp(number, contactList[i].number) ) { *out = contactList+i; return 0; } } return 2; } int cmpContact(const void* a, const void* b) { contact* A = (contact*)a; contact* B = (contact*)b; conversation* convA = ((conversation*)A->conversation); conversation* convB = ((conversation*)B->conversation); if( !convA && !convB ) { return strcmp(A->display_name, B->display_name); } else if( !convA ) { return 1; } else if (!convB ) { return -1; } else { sms* lastA = &convA->listSMS[convA->nbSMS-1]; sms* lastB = &convB->listSMS[convB->nbSMS-1]; return lastB->outDate - lastA->outDate; } } void sortContacts(void) { qsort(contactList, nbContacts, sizeof *contactList, cmpContact); } int findContactFromName(const char* name, contact** out, contact* from) { int I = 0; if( from ) I = from - contactList; if( !name || !out || !*name) return 1; for(int i = I+1 ; i < nbContacts ; i++) { if( strcasestr(contactList[i].display_name, name) ) { *out = contactList+i; return 0; } } for(int i = 0 ; i <= I ; i++) { if( strcasestr(contactList[i].display_name, name) ) { *out = contactList+i; return 0; } } return 2; } int addContact(const contact* cnct) { nbContacts++; contactList = realloc(contactList, nbContacts * sizeof *contactList); memcpy(&contactList[nbContacts-1],cnct, sizeof *cnct); } int deleteContact(contact* cnct) { if( cnct->conversation ) { *cnct->name = 0; strcpy(cnct->display_name, cnct->number); } else { int index = cnct - contactList; fprintf(stderr, "Index %d\n", index); memmove(contactList+index, contactList + index + 1, (nbContacts-index-1) * sizeof(contact)); nbContacts--; } } int fillContactList(const char* filename) { FILE* file = fopen(filename, "r"); nbContacts = 0; if( contactList ) { free(contactList); } if( !file ) { fprintf(stderr, "Unable to read contact file: "); return 1; } int c = 0; int v = 0; while( (c = fgetc(file)) != EOF ) { if( c == ';' ) { v++; if( v >= 3) { fprintf(stderr, "Invalid format (contact file) : too many field"); return 2; // Invalid } } if( c == '\n' ) { if( v == 2 ) { v = 0; nbContacts++; } else {// Not enougth fprintf(stderr, "Invalid format (contact file) : not enougth field"); return 2; // Invalid format } } } if( v == 1 ) nbContacts++; contactList = malloc( nbContacts * sizeof *contactList); rewind(file); int nameLength, numberLength, display_nameLength, i; nameLength = numberLength = display_nameLength = i = 0; contactList[0].hasNewMessage = 0; contactList[0].conversation = NULL; while( (c = fgetc(file)) != EOF ) { if( c == ';' ) { v++; switch(v) { case 1: contactList[i].name[nameLength] = 0; nameLength = 0; break; case 2: contactList[i].display_name[display_nameLength] = 0; display_nameLength = 0; break; default: fprintf(stderr, "Invalid format (contact file)"); return 2; // Invalid } } else if( c == '\n' ) { if( v == 2 ) { v = 0; contactList[i].number[numberLength] = 0; numberLength = 0; i++; contactList[i].hasNewMessage = 0; contactList[i].conversation = NULL; } else { fprintf(stderr, "Invalid format (contact file)"); return 2; // Invalid format } } else { 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; } } } } fclose(file); #ifdef DEBUG if( nbContacts == 0 ) { fprintf(stderr, "contact file empty"); return 10; } #endif return 0; } int writeContactList(const char* filename) { FILE* file = fopen(filename, "w"); if( !file ) { fprintf(stderr, "Unable to read contact file (%s)\n", filename); return 1; } for( int i = 0 ; i < nbContacts ; i++) { if( *contactList[i].name ) { fprintf(file, "%s;%s;%s\n", contactList[i].name, contactList[i].display_name, contactList[i].number); } } fclose(file); return 0; }