tmp
[apps/madmutt.git] / lib-mime / parameter.d
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  */
17 /*
18  *  Copyright © 2006 Pierre Habouzit
19  */
20
21 import std.c.string;
22 import std.string;
23 import std.random;
24
25 extern(C) char *strdup(char*);
26 extern(C) char __m_b64chars[64];
27
28 alias char*[char[]]* parameters_t;
29
30 extern(C):
31
32 parameters_t *parameter_new() {
33     return new parameters_t;
34 }
35
36 parameters_t *parameter_dup(parameters_t *orig)
37 {
38     parameters_t *res = new parameters_t;
39     foreach (k, v; **orig) {
40         (**res)[k] = v;
41     }
42     return res;
43 }
44
45 void parameter_foreach(parameters_t *parms, void (*f)(char *, char *))
46 {
47     foreach (k, v; **parms) {
48         f(toStringz(k), v);
49     }
50 }
51
52 char *parameter_getval(parameters_t *parms, char *name)
53 {
54     return (**parms)[name[0..strlen(name)]];
55 }
56
57 void parameter_setval(parameters_t *parms, char *attribute, char *value)
58 {
59     ulong len = strlen(attribute);
60     if (len > 0)
61         (**parms)[attribute[0..len]] = strdup(value);
62 }
63
64 void parameter_delval(parameters_t *parms, char *attribute)
65 {
66     (**parms).remove(attribute[0..strlen(attribute)]);
67 }
68
69 bool parameter_equal(parameters_t *p1, parameters_t *p2)
70 {
71     if ((**p1).length != (**p2).length)
72         return false;
73
74     foreach (k, v; **p1) {
75         if (k in **p2) {
76             if (strcmp(v, (**p2)[k]))
77                 return false;
78         }
79     }
80     return true;
81 }
82
83 void parameter_purge_empty(parameters_t *parms)
84 {
85     foreach (k, v; **parms) {
86         if (!v || !strlen(v))
87             (**parms).remove(k);
88     }
89 }
90
91 void parameter_set_boundary(parameters_t *parms)
92 {
93     char rs[17];
94     for (int i = 0; i < rs.length - 1; i++) {
95         rs[i] = __m_b64chars[rand() % 64];
96     }
97     rs[16] = '\0';
98     parameter_setval(parms, cast(char*)"boundary", &rs[0]);
99 }