summary refs log tree commit diff stats
Commit message (Expand)AuthorAgeFilesLines
* echo: only lock when threading is enabled (#20890)Tanguy2022-11-211-1/+2
* fixes strict effects (#20885)ringabout2022-11-211-1/+1
* channel send() condition variable lock ordering (#20879)Ico Doornekamp2022-11-211-1/+1
* fix #20835 (#20872)rockcavera2022-11-181-2/+21
* remove useless defines (#20867)ringabout2022-11-171-4/+0
* fixes #20856; store defaults directly (#20859)ringabout2022-11-173-10/+16
* fixes a severe bug of testament (#20832)ringabout2022-11-1713-1/+13
* rename `std/threads` to `std/typedthreads` (#20850)ringabout2022-11-1611-12/+12
* fixes ptr to cstring warnings[backport] (#20848)ringabout2022-11-1625-50/+50
* fixes a long-standing ARC bug (#20849)ringabout2022-11-162-2/+18
* add documentation and changelog for default object fields (#20845)ringabout2022-11-152-0/+53
* createDir is now callable at compile time (#20824)Don-Duong Quach2022-11-141-3/+18
* add more test for #20836 (#20842)Derek 呆2022-11-141-0/+15
* fixes object default fields bugs and add tests (#20839)ringabout2022-11-154-20/+66
* Allow users to set log flushing rules (#20817)Matt Haggard2022-11-142-17/+38
* fixes #20836; fixes #20833; fixes `unsafeNew` (#20841)ringabout2022-11-142-1/+14
* fix a few "broken link" warnings (#20837)Andrey Makarov2022-11-146-6/+6
* fixes a CI error (#20834)ringabout2022-11-141-1/+1
* fixes flaky CI with channels and ORC (#20831)ringabout2022-11-131-0/+1
* fixes tests and add notes (#20830)ringabout2022-11-131-1/+4
* fix #3505 wrong var {.global.} initialization, asign variable to it (#20812)Bung2022-11-122-1/+54
* Revert "fixes #20699; generate an empty struct for void type" (#20806)Andreas Rumpf2022-11-123-29/+32
* [minor] documentation improvement (#20825)ringabout2022-11-121-4/+4
* chore: wider nim cache name detection (#20826)scarf2022-11-121-3/+3
* fixes a strange issue most likely caused by a git merge (#20822)Andreas Rumpf2022-11-121-4/+2
* [testament] `parsetoml` switches to official URL (#20819)ringabout2022-11-121-1/+1
* fixes #20807, refs #20450, regression with seq inference (#20818)metagn2022-11-122-1/+16
* issue a warning for ptr to cstring conversion[backport] (#20814)ringabout2022-11-113-0/+11
* support `UncheckedArray[T]` in repr_v2.nim (#20816)Derek 呆2022-11-112-1/+12
* switch to official docopt URL (#20815)ringabout2022-11-111-1/+1
* fixes tests for quit (#20813)ringabout2022-11-111-1/+1
* revert #19891; `nimRawSetjmp` causes problems for mingw 32 bits too [backport...ringabout2022-11-111-5/+0
* enable yaml tests (#20810)ringabout2022-11-111-1/+1
* [sugar] handle HiddenDeref in capture, error at CT if unsupported nnk (#20680)Vindaar2022-11-102-3/+73
* refactor strongSpaceB with an enum (#20809)ringabout2022-11-105-23/+26
* add `taskpools` to important packages (#20803)ringabout2022-11-101-0/+1
* fixes #20798; add stew to important_packages (#20799)ringabout2022-11-101-0/+1
* fixes #13781; fixes #14901; add acyclic pragmas to FlowVar (#20804)ringabout2022-11-102-3/+12
* use `tlsEmulation:off` for freebsd (#20802)ringabout2022-11-101-1/+3
* fix topt_no_cursor.nim (#20791)Bung2022-11-091-11/+22
* fixes #20699; generate an empty struct for void type (#20790)Aditya Siram2022-11-092-11/+28
* reduce openArray-related C undefined behavior (#20795)tersec2022-11-093-10/+25
* fixes documentation regression and a bit clean up (#20793)ringabout2022-11-091-9/+7
* remove name field for ARC (#20797)ringabout2022-11-093-14/+19
* add more `{.cursor.}` to vm (#20796)ringabout2022-11-091-13/+13
* Use same defaults as JS for fetch options (#20783)Jake Leahy2022-11-092-4/+4
* clean up `system/arc` (#20792)ringabout2022-11-091-37/+0
* arc/orc no longer needs type names by default; with `nimTypeNames` it gets ni...ringabout2022-11-081-20/+16
* implements display based subtype checking (6.4x faster without threads; 2.8x ...ringabout2022-11-085-53/+84
* fixes nightlies regression; patches for freebsd (#20786)ringabout2022-11-081-0/+12
e>
e6b076ce ^
cfc23078 ^



e6b076ce ^
f7584469 ^




e6b076ce ^
cfc23078 ^










7a9d127a ^
78bd151c ^
2b743ea9 ^

7a9d127a ^
e6b076ce ^
7a9d127a ^
2b743ea9 ^
7a9d127a ^

78bd151c ^
dab37236 ^

7a9d127a ^

dab37236 ^
e568c588 ^


30d30133 ^
e568c588 ^


2b743ea9 ^

7a9d127a ^
91c157b1 ^
78bd151c ^
e568c588 ^
7a9d127a ^

78bd151c ^

2b743ea9 ^
7a9d127a ^


78bd151c ^
7a9d127a ^
78bd151c ^
78bd151c ^

dab37236 ^
78bd151c ^
dab37236 ^

7a9d127a ^
dab37236 ^
78bd151c ^
dab37236 ^

7a9d127a ^
dab37236 ^






30d30133 ^


7a9d127a ^
dab37236 ^
7a9d127a ^
dab37236 ^

78bd151c ^
7a9d127a ^
dab37236 ^
f23f768d ^
b1bf0540 ^
f23f768d ^
b1bf0540 ^

f23f768d ^
f7584469 ^
b1bf0540 ^



f7584469 ^
b1bf0540 ^

f7584469 ^
b1bf0540 ^





f7584469 ^
b1bf0540 ^



f7584469 ^
b1bf0540 ^
f7584469 ^

b1bf0540 ^

f7584469 ^
dab37236 ^












b1bf0540 ^

dab37236 ^
b1bf0540 ^




f23f768d ^
b1bf0540 ^





f23f768d ^

b1bf0540 ^
f23f768d ^
b1bf0540 ^

f23f768d ^

dab37236 ^
2b743ea9 ^
7a9d127a ^
dab37236 ^

2b743ea9 ^
7a9d127a ^




30d30133 ^

e6b076ce ^
30d30133 ^

e6b076ce ^


2b743ea9 ^



e6b076ce ^

2b743ea9 ^




e6b076ce ^
2b743ea9 ^
e6b076ce ^




dab37236 ^
e6b076ce ^


dab37236 ^
e6b076ce ^
dab37236 ^


2b743ea9 ^
dab37236 ^

e6b076ce ^

dab37236 ^
e6b076ce ^

30d30133 ^














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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327





















                                                                       
                  




                         

                                                    
 


                                                  
                                 
 

                                                                           
                                                        

                                                                

                                                                               


                                                          
 

                             
                                                


                     
                                                      
                                      

                              
 
                            
                             

                                
     

 











                                                  

                                




                              

 
                                                
 


                         

                                                    

                     
                                                      
                                                   



                                               
 




                                                                               
                                          










                              
 
 

                                                                       
 
 
                         
                                                               

                 
            

                                                    

                     
                                                           


                                                       
                                                                


                                                               

                                                                 
                         
             
 
                      

                              

         
                                                          


                              
 
                 
     

 
                                              
 

                                                
    
                                    
 

                
 






                                                                       


                                                     
 
                                                    
 

                    
 
 
                                     
 
                       
 

                            
                    
 



                                                                               
 

                                                         
 





                                                             
 



                                                         
 
                                         

                                

     
 












                                                                     

                 
                                                       




                                                                                 
 





                                                           

         
                          
     

                

 
                                                                           
                                                       
 

                                                                        
                                                    




                     

                                                                
 

                                                              


                                                                   



                                                                           

                                                                       




                                                                           
            
                           




               
                                                       


                        
                                
                            


                                  
                              

                               

                  
                   

 














                                                                            
/* 
 * contact_list.c
 *
 * Copyright (C) 2012 James Booth <boothj5@gmail.com>
 * 
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "contact_list.h"

// internal contact list
static struct contact_node_t * _contact_list = NULL;

// state of current tab completion, currrent node 
static struct contact_node_t * _last_found = NULL;
// and current search pattern
static char * _search_str = NULL;

static char * _search_contact_list_from(struct contact_node_t * curr);
static struct contact_node_t * _make_contact_node(const char * const name, 
    const char * const show, const char * const status);
static struct contact_t * _new_contact(const char * const name, 
    const char * const show, const char * const status);
static void _destroy_contact(struct contact_t *contact);
static struct contact_node_t * _copy_contact_list(struct contact_node_t *node);
static void _insert_contact(struct contact_node_t *curr, 
    struct contact_node_t *prev, const char * const name, 
    const char * const show, const char * const status);

void contact_list_clear(void)
{
    struct contact_node_t *curr = _contact_list;
    
    if (curr) {
        while(curr) {
            struct contact_t *contact = curr->contact;
            _destroy_contact(contact);
            curr = curr->next;
        }

        free(_contact_list);
        _contact_list = NULL;
        
        reset_search_attempts();
    }
}

void destroy_list(struct contact_node_t *list)
{
    while(list) {
        struct contact_t *contact = list->contact;
        _destroy_contact(contact);
        list = list->next;
    }

    free(list);
    list = NULL;
}

void reset_search_attempts(void)
{
    _last_found = NULL;
    if (_search_str != NULL) {
        free(_search_str);
        _search_str = NULL;
    }
}

int contact_list_remove(const char * const name)
{
    if (!_contact_list) {
        return 0;
    } else {
        struct contact_node_t *curr = _contact_list;
        struct contact_node_t *prev = NULL;
        
        while(curr) {
            struct contact_t *contact = curr->contact;
            if (strcmp(contact->name, name) == 0) {
                if (prev)
                    prev->next = curr->next;
                else
                    _contact_list = curr->next;

                // reset last found if it points at the node to be removed
                if (_last_found != NULL)
                    if (strcmp(_last_found->contact->name, contact->name) == 0)
                        _last_found = NULL;

                _destroy_contact(contact);
                free(curr);

                return 1;
            }

            prev = curr;
            curr = curr->next;
        }

        return 0;
    }
}

int contact_list_add(const char * const name, const char * const show, 
    const char * const status)
{

    if (!_contact_list) {
        _contact_list = _make_contact_node(name, show, status);
        
        return 1;
    } else {
        struct contact_node_t *curr = _contact_list;
        struct contact_node_t *prev = NULL;

        while(curr) {
            struct contact_t *curr_contact = curr->contact;

            // insert    
            if (strcmp(curr_contact->name, name) > 0) {
                _insert_contact(curr, prev, name, show, status);
                return 0;
            // update
            } else if (strcmp(curr_contact->name, name) == 0) {
                _destroy_contact(curr->contact);
                curr->contact = _new_contact(name, show, status);
                return 0;
            }

            // move on
            prev = curr;
            curr = curr->next;
        }

        curr = _make_contact_node(name, show, status);    
        
        if (prev)
            prev->next = curr;

        return 1;
    }
}

struct contact_node_t * get_contact_list(void)
{
    struct contact_node_t *copy = NULL;
    struct contact_node_t *curr = _contact_list;
    
    copy = _copy_contact_list(curr);

    return copy;
}

struct contact_node_t * _copy_contact_list(struct contact_node_t *node)
{
    if (node == NULL) {
        return NULL;
    } else {
        struct contact_t *curr_contact = node->contact;
        struct contact_node_t *copy = 
            _make_contact_node(curr_contact->name, 
                               curr_contact->show, 
                               curr_contact->status);

        copy->next = _copy_contact_list(node->next);

        return copy;
    }
}

char * find_contact(char *search_str)
{
    char *found = NULL;

    // no contacts to search
    if (!_contact_list)
        return NULL;

    // first search attempt
    if (_last_found == NULL) {
        _search_str = (char *) malloc((strlen(search_str) + 1) * sizeof(char));
        strcpy(_search_str, search_str);

        found = _search_contact_list_from(_contact_list);
        return found;

    // subsequent search attempt
    } else {
        // search from here+1 to end 
        found = _search_contact_list_from(_last_found->next);
        if (found != NULL)
            return found;

        // search from beginning
        found = _search_contact_list_from(_contact_list);
        if (found != NULL)
            return found;

        // we found nothing, reset search
        reset_search_attempts();
        return NULL;
    }
}

int get_size(struct contact_node_t *list)
{
    int size = 0;

    while(list) {
        size++;
        list = list->next;
    }

    return size;
}

static char * _search_contact_list_from(struct contact_node_t * curr)
{
    while(curr) {
        struct contact_t *curr_contact = curr->contact;
        
        // match found
        if (strncmp(curr_contact->name, _search_str, strlen(_search_str)) == 0) {
            char *result = 
                (char *) malloc((strlen(curr_contact->name) + 1) * sizeof(char));

            // set pointer to last found
            _last_found = curr;
            
            // return the contact, must be free'd by caller
            strcpy(result, curr_contact->name);
            return result;
        }

        curr = curr->next;
    }

    return NULL;
}

static struct contact_node_t * _make_contact_node(const char * const name, 
    const char * const show, const char * const status)
{
    struct contact_node_t *new = 
        (struct contact_node_t *) malloc(sizeof(struct contact_node_t));
    new->contact = _new_contact(name, show, status);
    new->next = NULL;

    return new;
}

static struct contact_t * _new_contact(const char * const name, 
    const char * const show, const char * const status)
{
    struct contact_t *new = 
        (struct contact_t *) malloc(sizeof(struct contact_t));
    new->name = (char *) malloc((strlen(name) + 1) * sizeof(char));
    strcpy(new->name, name);
    
    if (show == NULL || (strcmp(show, "") == 0)) {
        new->show = (char *) malloc((strlen("online") + 1) * sizeof(char));
        strcpy(new->show, "online");
    } else {
        new->show = (char *) malloc((strlen(show) + 1) * sizeof(char));
        strcpy(new->show, show);
    }

    if (status != NULL) {
        new->status = (char *) malloc((strlen(status) + 1) * sizeof(char));
        strcpy(new->status, status);
    } else {
        new->status = NULL;
    }

    return new;
}

static void _destroy_contact(struct contact_t *contact)
{
    free(contact->name);

    if (contact->show != NULL) {
        free(contact->show);
        contact->show = NULL;
    }
    if (contact->status != NULL) {
        free(contact->status);
        contact->status = NULL;
    }

    free(contact);
    contact = NULL;
}

static void _insert_contact(struct contact_node_t *curr, 
    struct contact_node_t *prev, const char * const name,
    const char * const show, const char * const status)
{
    if (prev) {
        struct contact_node_t *new = _make_contact_node(name, show, status);
        new->next = curr;
        prev->next = new;
    } else {
        struct contact_node_t *new = _make_contact_node(name, show, status);
        new->next = _contact_list;
        _contact_list = new;
    }
}