#include #include #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) { if( !name || !out || !*name) return 1; for(int i = 0 ; i < nbContacts ; i++) { if( strstr(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 fillContactList(char* filename) { FILE* file = fopen(filename, "r"); nbContacts = 0; if( contactList ) { free(contactList); } if( !file ) { perror("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 {// Pas assez 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].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) ) { fprintf(stderr, "too long name ?"); return 3; } }else if( v == 1) { contactList[i].display_name[display_nameLength++] = c; if( display_nameLength > (sizeof contactList[i].display_name -1) ) { fprintf(stderr, "too display name ?"); return 9; } } else { contactList[i].number[numberLength++] = c; if( numberLength > (sizeof contactList[i].number -1) ) { puts(contactList[i].number); fprintf(stderr, "too long number ?"); return 27+i; } } } } return 0; }