tmp
[apps/madmutt.git] / lib-mime / parameter.d
diff --git a/lib-mime/parameter.d b/lib-mime/parameter.d
new file mode 100644 (file)
index 0000000..bb992d4
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ *  This program 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 2 of the License, or (at
+ *  your option) any later version.
+ *
+ *  This program 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 this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ */
+/*
+ *  Copyright © 2006 Pierre Habouzit
+ */
+
+import std.c.string;
+import std.string;
+import std.random;
+
+extern(C) char *strdup(char*);
+extern(C) char __m_b64chars[64];
+
+alias char*[char[]]* parameters_t;
+
+extern(C):
+
+parameters_t *parameter_new() {
+    return new parameters_t;
+}
+
+parameters_t *parameter_dup(parameters_t *orig)
+{
+    parameters_t *res = new parameters_t;
+    foreach (k, v; **orig) {
+        (**res)[k] = v;
+    }
+    return res;
+}
+
+void parameter_foreach(parameters_t *parms, void (*f)(char *, char *))
+{
+    foreach (k, v; **parms) {
+        f(toStringz(k), v);
+    }
+}
+
+char *parameter_getval(parameters_t *parms, char *name)
+{
+    return (**parms)[name[0..strlen(name)]];
+}
+
+void parameter_setval(parameters_t *parms, char *attribute, char *value)
+{
+    ulong len = strlen(attribute);
+    if (len > 0)
+        (**parms)[attribute[0..len]] = strdup(value);
+}
+
+void parameter_delval(parameters_t *parms, char *attribute)
+{
+    (**parms).remove(attribute[0..strlen(attribute)]);
+}
+
+bool parameter_equal(parameters_t *p1, parameters_t *p2)
+{
+    if ((**p1).length != (**p2).length)
+        return false;
+
+    foreach (k, v; **p1) {
+        if (k in **p2) {
+            if (strcmp(v, (**p2)[k]))
+                return false;
+        }
+    }
+    return true;
+}
+
+void parameter_purge_empty(parameters_t *parms)
+{
+    foreach (k, v; **parms) {
+        if (!v || !strlen(v))
+            (**parms).remove(k);
+    }
+}
+
+void parameter_set_boundary(parameters_t *parms)
+{
+    char rs[17];
+    for (int i = 0; i < rs.length - 1; i++) {
+        rs[i] = __m_b64chars[rand() % 64];
+    }
+    rs[16] = '\0';
+    parameter_setval(parms, cast(char*)"boundary", &rs[0]);
+}