1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* mime-utils.c : misc utilities for mime */
6 * Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@inria.fr> .
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include "gmime-utils.h"
27 #include "gstring-util.h"
28 #include "camel-log.h"
29 #include "camel-stream.h"
32 gmime_write_header_pair_to_stream (CamelStream *stream, gchar* name, GString *value)
40 if (!value || !(value->str)) return;
41 len = strlen (name) + strlen (value->str) +3;
42 /* 3 is for ": " and "\n" */
43 strtmp = g_string_sized_new (len);
45 sprintf(strtmp->str, "%s: %s\n", name, value->str);
46 camel_stream_write (stream, strtmp->str, len);
47 CAMEL_LOG (FULL_DEBUG, "gmime_write_header_pair_to_stream:\n writing %s\n", strtmp->str);
48 g_string_free (strtmp, FALSE);
53 _write_one_header_to_stream (gpointer key, gpointer value, gpointer user_data)
55 GString *header_name = (GString *)key;
56 GString *header_value = (GString *)value;
57 CamelStream *stream = (CamelStream *)user_data;
59 if ( (header_name) && (header_name->str) &&
60 (header_value) && (header_value->str) )
61 gmime_write_header_pair_to_stream (stream, header_name->str, header_value);
65 write_header_table_to_stream (CamelStream *stream, GHashTable *header_table)
67 g_hash_table_foreach (header_table,
68 _write_one_header_to_stream,
74 write_header_with_glist_to_stream (CamelStream *stream, gchar *header_name, GList *header_values, gchar *separator)
79 if ( (header_name) && (header_values) )
83 camel_stream_write (stream, header_name, strlen (header_name) );
84 camel_stream_write (stream, ": ", 2);
86 while (header_values) {
87 current = (GString *)header_values->data;
88 if ( (current) && (current->str) ) {
89 if (!first) camel_stream_write_string (stream, separator);
91 camel_stream_write (stream, current->str, strlen (current->str));
93 header_values = g_list_next(header_values);
95 camel_stream_write (stream, "\n", 1);
104 /* * * * * * * * * * * */
105 /* scanning functions */
108 _store_header_pair_from_gstring (GHashTable *header_table, GString *header_line)
111 GString *header_name, *header_value;
113 g_assert (header_table);
114 if ( (header_line) && (header_line->str) ) {
115 dich_result = g_string_dichotomy(header_line, ':', &header_name, &header_value, DICHOTOMY_NONE);
116 if (dich_result != 'o')
118 "store_header_pair_from_gstring : dichotomy result is %c"
119 "header line is :\n--\n%s\n--\n");
122 g_string_trim (header_value, " \t", TRIM_STRIP_LEADING | TRIM_STRIP_TRAILING);
123 g_hash_table_insert (header_table, header_name, header_value);
133 get_header_table_from_stream (CamelStream *stream)
137 gboolean crlf = FALSE;
138 gboolean end_of_header_line = FALSE;
139 gboolean end_of_headers = FALSE;
140 gboolean end_of_file = FALSE;
141 GString *header_line=NULL;
142 GHashTable *header_table;
144 header_table = g_hash_table_new (g_string_hash, g_string_equal_for_hash);
145 camel_stream_read (stream, &next_char, 1);
147 header_line = g_string_new("");
148 end_of_header_line = FALSE;
151 /* read a whole header line */
156 end_of_header_line = TRUE;
158 case '\n': /* a blank line means end of headers */
161 end_of_header_line = TRUE;
173 if (!crlf) header_line = g_string_append_c (header_line, next_char);
174 else end_of_header_line = TRUE;
176 /* if we have read a whole header line, we have also read
177 the first character of the next line to be sure the
178 crlf was not followed by a space or a tab char */
179 if (!end_of_header_line) camel_stream_read (stream, &next_char, 1);
181 } while ( !end_of_header_line );
182 if ( strlen(header_line->str) )
183 _store_header_pair_from_gstring (header_table, header_line);
184 g_string_free (header_line, FALSE);
186 } while ( (!end_of_headers) && (!end_of_file) );