assocprop.c
5 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2001, 2004-2005, 2007-2008, 2010-2012, 2014-2015
Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <mailutils/sys/property.h>
#include <mailutils/errno.h>
#include <mailutils/assoc.h>
#include <mailutils/stream.h>
#include <mailutils/iterator.h>
#include <stdlib.h>
struct property_item
{
char *value;
};
static void
_assoc_prop_done (struct _mu_property *prop)
{
mu_assoc_t assoc = prop->_prop_data;
mu_stream_t str = prop->_prop_init_data;
mu_assoc_destroy (&assoc);
mu_stream_destroy (&str);
}
static int
_assoc_prop_getval (struct _mu_property *prop,
const char *key, const char **pval)
{
mu_assoc_t assoc = prop->_prop_data;
struct property_item *item;
item = mu_assoc_ref (assoc, key);
if (item == NULL)
return MU_ERR_NOENT;
if (pval)
*pval = item->value;
return 0;
}
static int
_assoc_prop_setval (struct _mu_property *prop, const char *key,
const char *val, int overwrite)
{
mu_assoc_t assoc = prop->_prop_data;
struct property_item *item;
int rc;
rc = mu_assoc_ref_install (assoc, key, (void **)&item);
if (rc == 0)
{
item->value = strdup (val);
if (!item->value)
{
mu_assoc_remove (assoc, key);
return ENOMEM;
}
}
else if (rc == MU_ERR_EXISTS && overwrite)
{
char *newval = strdup (val);
if (!newval)
return ENOMEM;
free (item->value);
item->value = newval;
}
else
return rc;
return 0;
}
static int
_assoc_prop_unset (struct _mu_property *prop, const char *key)
{
mu_assoc_t assoc = prop->_prop_data;
return mu_assoc_remove (assoc, key);
}
static int
_assoc_prop_clear (struct _mu_property *prop)
{
mu_assoc_t assoc = prop->_prop_data;
mu_assoc_clear (assoc);
return 0;
}
static void *
_assoc_prop_dataptr (void *in)
{
struct property_item *item = in;
return item->value;
}
static int
_assoc_prop_getitr (struct _mu_property *prop, mu_iterator_t *pitr)
{
int rc;
mu_iterator_t itr;
rc = mu_assoc_get_iterator ((mu_assoc_t)prop->_prop_data, &itr);
if (rc)
return rc;
mu_iterator_set_dataptr (itr, _assoc_prop_dataptr);
*pitr = itr;
return 0;
}
static void
prop_free_value (void *data)
{
struct property_item *item = data;
free (item->value);
}
static int
_assoc_prop_fill (struct _mu_property *prop)
{
int rc;
mu_stream_t str = prop->_prop_init_data;
int state = 0;
char *buf[2] = { NULL, NULL };
size_t size[2] = { 0, 0 }, n;
if (!str)
return 0;
mu_stream_seek (str, 0, MU_SEEK_SET, NULL);
while ((rc = mu_stream_getdelim (str, &buf[state], &size[state],
0, &n)) == 0 &&
n > 0)
{
if (state == 1)
_assoc_prop_setval (prop, buf[0], buf[1], 1);
state = !state;
}
free (buf[0]);
free (buf[1]);
return rc;
}
static int
_assoc_prop_save (struct _mu_property *prop)
{
mu_stream_t str = prop->_prop_init_data;
mu_iterator_t itr;
int rc;
mu_off_t off;
if (!str)
return 0;
rc = mu_property_get_iterator (prop, &itr);
if (rc)
return rc;
mu_stream_seek (str, 0, MU_SEEK_SET, NULL);
for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
const char *name, *val;
mu_iterator_current_kv (itr, (const void **)&name, (void**)&val);
rc = mu_stream_write (str, name, strlen (name) + 1, NULL);
if (rc)
break;
rc = mu_stream_write (str, val, strlen (val) + 1, NULL);
if (rc)
break;
}
mu_iterator_destroy (&itr);
rc = mu_stream_seek (str, 0, MU_SEEK_CUR, &off);
if (rc == 0)
rc = mu_stream_truncate (str, off);
return rc;
}
int
mu_assoc_property_init (struct _mu_property *prop)
{
mu_assoc_t assoc;
int rc;
rc = mu_assoc_create (&assoc, sizeof (struct property_item), 0);
if (rc)
return rc;
mu_assoc_set_free (assoc, prop_free_value);
prop->_prop_data = assoc;
prop->_prop_done = _assoc_prop_done;
if (prop->_prop_init_data)
{
prop->_prop_fill = _assoc_prop_fill;
prop->_prop_save = _assoc_prop_save;
}
else
{
prop->_prop_fill = NULL;
prop->_prop_save = NULL;
}
prop->_prop_getval = _assoc_prop_getval;
prop->_prop_setval = _assoc_prop_setval;
prop->_prop_unset = _assoc_prop_unset;
prop->_prop_getitr = _assoc_prop_getitr;
prop->_prop_clear = _assoc_prop_clear;
return 0;
}