update ui_seperation.txt
[claws.git] / src / customheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include "customheader.h"
29 #include "utils.h"
30
31
32 gchar *custom_header_get_str(CustomHeader *ch)
33 {
34         return g_strdup_printf("%i:%s: %s",
35                                ch->account_id, ch->name,
36                                ch->value ? ch->value : "");
37 }
38
39 CustomHeader *custom_header_read_str(const gchar *buf)
40 {
41         CustomHeader *ch;
42         gchar *account_id_str;
43         gint id;
44         gchar *name;
45         gchar *value;
46         gchar *tmp;
47
48         Xstrdup_a(tmp, buf, return NULL);
49         g_strstrip(tmp);
50
51         account_id_str = tmp;
52
53         name = strchr(account_id_str, ':');
54         if (!name)
55                 return NULL;
56         else {
57                 gchar *endp;
58
59                 *name++ = '\0';
60                 id = strtol(account_id_str, &endp, 10);
61                 if (*endp != '\0') return NULL;
62         }
63
64         while (*name == ' ') name++;
65
66         value = strchr(name, ':');
67         if (!value) return NULL;
68
69         *value++ = '\0';
70
71         ch = g_new0(CustomHeader, 1);
72         ch->account_id = id;
73         ch->name = *name ? g_strdup(name) : NULL;
74         while (*value == ' ') value++;
75         ch->value = *value ? g_strdup(value) : NULL;
76
77         return ch;
78 }
79
80 CustomHeader *custom_header_find(GSList *header_list, const gchar *header)
81 {
82         GSList *cur;
83         CustomHeader *chdr;
84
85         for (cur = header_list; cur != NULL; cur = cur->next) {
86                 chdr = (CustomHeader *)cur->data;
87                 if (!strcasecmp(chdr->name, header))
88                         return chdr;
89         }
90
91         return NULL;
92 }
93
94 void custom_header_free(CustomHeader *ch)
95 {
96         if (!ch) return;
97
98         g_free(ch->name);
99         g_free(ch->value);
100         g_free(ch);
101 }