-
Notifications
You must be signed in to change notification settings - Fork 21
/
osdialog.c
95 lines (82 loc) · 2.31 KB
/
osdialog.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
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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "osdialog.h"
char* osdialog_strdup(const char* s) {
return osdialog_strndup(s, strlen(s));
}
char* osdialog_strndup(const char* s, size_t n) {
char* d = OSDIALOG_MALLOC(n + 1);
memcpy(d, s, n);
d[n] = '\0';
return d;
}
osdialog_filters* osdialog_filters_parse(const char* str) {
osdialog_filters* filters_head = OSDIALOG_MALLOC(sizeof(osdialog_filters));
filters_head->next = NULL;
osdialog_filters* filters = filters_head;
osdialog_filter_patterns* patterns = NULL;
const char* text = str;
while (1) {
switch (*str) {
case ':': {
filters->name = osdialog_strndup(text, str - text);
filters->patterns = OSDIALOG_MALLOC(sizeof(osdialog_filter_patterns));
patterns = filters->patterns;
patterns->next = NULL;
text = str + 1;
} break;
case ',': {
assert(patterns);
patterns->pattern = osdialog_strndup(text, str - text);
patterns->next = OSDIALOG_MALLOC(sizeof(osdialog_filter_patterns));
patterns = patterns->next;
patterns->next = NULL;
text = str + 1;
} break;
case ';': {
assert(patterns);
patterns->pattern = osdialog_strndup(text, str - text);
filters->next = OSDIALOG_MALLOC(sizeof(osdialog_filters));
filters = filters->next;
filters->next = NULL;
patterns = NULL;
text = str + 1;
} break;
case '\0': {
assert(patterns);
patterns->pattern = osdialog_strndup(text, str - text);
} break;
default: break;
}
if (!*str)
break;
str++;
}
return filters_head;
}
void osdialog_filter_patterns_free(osdialog_filter_patterns* patterns) {
if (!patterns)
return;
OSDIALOG_FREE(patterns->pattern);
osdialog_filter_patterns* next = patterns->next;
OSDIALOG_FREE(patterns);
osdialog_filter_patterns_free(next);
}
void osdialog_filters_free(osdialog_filters* filters) {
if (!filters)
return;
OSDIALOG_FREE(filters->name);
osdialog_filter_patterns_free(filters->patterns);
osdialog_filters* next = filters->next;
OSDIALOG_FREE(filters);
osdialog_filters_free(next);
}
osdialog_save_callback osdialog_save_cb = NULL;
osdialog_restore_callback osdialog_restore_cb = NULL;
void osdialog_set_save_callback(osdialog_save_callback cb) {
osdialog_save_cb = cb;
}
void osdialog_set_restore_callback(osdialog_restore_callback cb) {
osdialog_restore_cb = cb;
}