mh_ctx.c
5.02 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
229
230
231
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009,
2010 Free Software Foundation, Inc.
GNU Mailutils 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, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
/* MH context functions. */
#include <mh.h>
#include <sys/types.h>
#include <sys/stat.h>
mh_context_t *
mh_context_create (const char *name, int copy)
{
mh_context_t *ctx;
ctx = malloc (sizeof (*ctx));
if (!ctx)
mh_err_memory (1);
if (copy)
ctx->name = name;
else
{
ctx->name = strdup (name);
if (!ctx->name)
mh_err_memory (1);
}
ctx->header = NULL;
return ctx;
}
void
mh_context_destroy (mh_context_t **pctx)
{
mh_context_t *ctx = *pctx;
free ((char*) ctx->name);
if (ctx->header)
mu_header_destroy (&ctx->header);
free (ctx);
*pctx = NULL;
}
void
mh_context_merge (mh_context_t *dst, mh_context_t *src)
{
if (!dst->header)
{
dst->header = src->header;
src->header = NULL;
}
else
{
size_t i, count;
mu_header_get_field_count (src->header, &count);
for (i = 1; i <= count; i++)
{
const char *name = NULL;
const char *value = NULL;
mu_header_sget_field_name (src->header, i, &name);
mu_header_sget_field_value (src->header, i, &value);
mu_header_set_value (dst->header, name, value, 1);
}
}
}
int
mh_context_read (mh_context_t *ctx)
{
int rc;
char *blurb, *p;
mu_stream_t stream;
mu_off_t stream_size;
char *buf = NULL;
size_t size = 0, n;
if (!ctx)
return MU_ERR_OUT_NULL;
rc = mu_file_stream_create (&stream, ctx->name, MU_STREAM_READ);
if (rc)
return rc;
rc = mu_stream_size (stream, &stream_size);
if (rc)
{
mu_stream_destroy (&stream);
return rc;
}
blurb = malloc (stream_size + 1);
if (!blurb)
{
mu_stream_destroy (&stream);
return ENOMEM;
}
p = blurb;
while (mu_stream_getline (stream, &buf, &size, &n) == 0 && n > 0)
{
char *q = mu_str_skip_class (buf, MU_CTYPE_SPACE);
if (!*q || *q == '#')
continue;
for (q = buf; *q;)
*p++ = *q++;
}
mu_stream_destroy (&stream);
rc = mu_header_create (&ctx->header, blurb, p - blurb);
free (blurb);
return rc;
}
int
mh_context_write (mh_context_t *ctx)
{
int rc;
mu_stream_t instream, outstream;
mu_off_t size;
if (!ctx)
return MU_ERR_OUT_NULL;
rc = mu_file_stream_create (&outstream, ctx->name,
MU_STREAM_WRITE|MU_STREAM_CREAT);
if (rc)
{
mu_error (_("cannot open context file %s for writing: %s"),
ctx->name, mu_strerror (rc));
return MU_ERR_FAILURE;
}
mu_header_get_streamref (ctx->header, &instream);
rc = mu_stream_copy (outstream, instream, 0, &size);
if (rc)
{
mu_error (_("error writing to context file %s: %s"),
ctx->name, mu_strerror (rc));
return MU_ERR_FAILURE;
}
else
rc = mu_stream_truncate (outstream, size);
mu_stream_destroy (&instream);
mu_stream_destroy (&outstream);
return 0;
}
const char *
mh_context_get_value (mh_context_t *ctx, const char *name, const char *defval)
{
const char *p;
if (!ctx || mu_header_sget_value (ctx->header, name, &p))
p = defval;
return p;
}
int
mh_context_set_value (mh_context_t *ctx, const char *name, const char *value)
{
if (!ctx)
return EINVAL;
if (!ctx->header)
{
int rc;
if ((rc = mu_header_create (&ctx->header, NULL, 0)) != 0)
{
mu_error (_("cannot create context %s: %s"),
ctx->name,
mu_strerror (rc));
return 1;
}
}
return mu_header_set_value (ctx->header, name, value, 1);
}
int
mh_context_iterate (mh_context_t *ctx, mh_context_iterator fp, void *data)
{
size_t i, nfields;
int rc = 0;
if (!ctx)
return EINVAL;
if (!ctx->header)
return 0;
rc = mu_header_get_field_count (ctx->header, &nfields);
if (rc)
{
mu_error (_("cannot obtain field count for context %s"), ctx->name);
return rc;
}
for (i = 1; i <= nfields && rc == 0; i++)
{
const char *name, *value;
rc = mu_header_sget_field_name (ctx->header, i, &name);
if (rc)
{
mu_error (_("cannot obtain field name for context %s:%d: %s"),
ctx->name,i,mu_strerror (rc));
break;
}
rc = mu_header_sget_field_value (ctx->header, i, &value);
if (rc)
{
mu_error (_("cannot obtain field value for context %s:%d: %s"),
ctx->name,i,mu_strerror (rc));
break;
}
rc = fp (name, value, data);
}
return rc;
}