aboutsummaryrefslogtreecommitdiff
path: root/contactList.c
blob: 7dadf410a527251254aa0d6cbdf9b99da93566e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdlib.h>
#include <stdio.h>
#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;
}