accessor.h
3.16 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 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 <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/util.h>
#include <mailutils/cstr.h>
#include <mailutils/sys/url.h>
/* General accessors: */
#define AC2(a,b) a ## b
#define METHOD(pfx,part) AC2(pfx,part)
#define AC4(a,b,c,d) a ## b ## c ## d
#define ACCESSOR(action,field) AC4(mu_url_,action,_,field)
/* Define a `static get' accessor */
int
ACCESSOR(sget,URL_PART) (mu_url_t url, char const **sptr)
{
if (url == NULL)
return EINVAL;
if (!url->URL_PART)
{
if (url->METHOD(_get_,URL_PART))
{
size_t n;
char *buf;
int status = url->METHOD(_get_,URL_PART) (url, NULL, 0, &n);
if (status)
return status;
buf = malloc (n + 1);
if (!buf)
return ENOMEM;
status = url->METHOD(_get_,URL_PART) (url, buf, n + 1, NULL);
if (status)
return status;
if (buf[0])
{
/* FIXME */
status = mu_str_url_decode (&url->URL_PART, buf);
if (status)
{
free (buf);
return status;
}
}
else
url->URL_PART = buf;
if (!url->URL_PART)
return ENOMEM;
}
else
return MU_ERR_NOENT;
}
*sptr = url->URL_PART;
return 0;
}
/* Define a `get' accessor */
int
ACCESSOR(get,URL_PART) (mu_url_t url, char *buf, size_t len, size_t *n)
{
size_t i;
const char *str;
int status = ACCESSOR(sget, URL_PART) (url, &str);
if (status)
return status;
i = mu_cpystr (buf, str, len);
if (n)
*n = i;
return 0;
}
/* Define an `allocated get' accessor */
int
ACCESSOR(aget, URL_PART) (mu_url_t url, char **buf)
{
const char *str;
int status = ACCESSOR(sget, URL_PART) (url, &str);
if (status)
return status;
if (str)
{
*buf = strdup (str);
if (!*buf)
status = ENOMEM;
}
else
*buf = NULL;
return status;
}
/* Define a comparator */
int
ACCESSOR(is_same,URL_PART) (mu_url_t url1, mu_url_t url2)
{
const char *s1, *s2;
int status1, status2;
status1 = ACCESSOR(sget, URL_PART) (url1, &s1);
if (status1 && status1 != MU_ERR_NOENT)
return 0;
status2 = ACCESSOR(sget, URL_PART) (url2, &s2);
if (status2 && status2 != MU_ERR_NOENT)
return 0;
if (status1 || status2)
return status1 == status2; /* Both fields are missing */
return mu_c_strcasecmp (s1, s2) == 0;
}