-
Notifications
You must be signed in to change notification settings - Fork 0
/
picker.c
56 lines (48 loc) · 1.2 KB
/
picker.c
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
#include <stdlib.h>
#include <string.h>
#include "picker.h"
struct picker *
create_picker(void)
{
struct picker *new = malloc(sizeof(struct picker));
bzero(new, sizeof(*new));
new->entry_h = DEFAULT_ENTRY_H;
new->entry_h_pad = DEFAULT_ENTRY_H_PAD;
return new;
}
int
add_item_to_picker(struct picker *picker, char *item)
{
int i;
if (!item)
return 0;
/* Look for the item in the picker, don't allow duplicates */
for (i=0; i<picker->entry_count; i++)
if (!strcmp(picker->entries[i], item))
return 1;
picker->entry_count++;
picker->entries=realloc(picker->entries, picker->entry_count*sizeof(char*));
picker->entries[picker->entry_count-1] = malloc(strlen(item)+1);
strcpy(picker->entries[picker->entry_count-1], item);
return 0;
}
int
clear_picker(struct picker *picker)
{
int i;
for (i=0; i<picker->entry_count; i++)
free(picker->entries[i]);
free(picker->entries);
picker->entries = NULL;
picker->entry_count = 0;
return 0;
}
void
destroy_picker(struct picker *picker)
{
int i;
for (i=0; i<picker->entry_count; i++)
free(picker->entries[i]);
free(picker->entries);
free(picker);
}