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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* mime-utils.c : misc utilities for mime */
/*
*
* Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@inria.fr> .
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include "gmime-utils.h"
#include "gstring-util.h"
#include "camel-log.h"
void
gmime_write_header_pair_to_file (FILE* file, gchar* name, GString *value)
{
g_assert(name);
if ((value) && (value->str))
fprintf(file, "%s: %s\n", name, value->str);
}
static void
_write_one_header_to_file (gpointer key, gpointer value, gpointer user_data)
{
GString *header_name = (GString *)key;
GString *header_value = (GString *)value;
FILE *file = (FILE *)user_data;
if ( (header_name) && (header_name->str) &&
(header_value) && (header_value->str) )
fprintf(file, "%s: %s\n", header_name->str, header_value->str);
}
void
write_header_table_to_file (FILE *file, GHashTable *header_table)
{
g_hash_table_foreach (header_table,
_write_one_header_to_file,
(gpointer)file);
}
void
write_header_with_glist_to_file (FILE *file, gchar *header_name, GList *header_values)
{
GString *current;
if ( (header_name) && (header_values) )
{
gboolean first;
fprintf(file, "%s: ", header_name);
first = TRUE;
while (header_values) {
current = (GString *)header_values->data;
if ( (current) && (current->str) ) {
if (!first) fprintf(file, ", ");
else first = FALSE;
fprintf(file, "%s", current->str);
}
header_values = g_list_next(header_values);
}
fprintf(file, "\n");
}
}
/* * * * * * * * * * * */
/* scanning functions */
static void
_store_header_pair_from_gstring (GHashTable *header_table, GString *header_line)
{
gchar dich_result;
GString *header_name, *header_value;
g_assert (header_table);
if ( (header_line) && (header_line->str) ) {
dich_result = g_string_dichotomy(header_line, ':', &header_name, &header_value, DICHOTOMY_NONE);
if (dich_result != 'o')
camel_log(WARNING,
"store_header_pair_from_gstring : dichotomy result is %c"
"header line is :\n--\n%s\n--\n");
else {
g_string_trim (header_value, " \t", TRIM_STRIP_LEADING | TRIM_STRIP_TRAILING);
g_hash_table_insert (header_table, header_name, header_value);
}
}
}
GHashTable *
get_header_table_from_file (FILE *file)
{
int next_char;
gboolean crlf = FALSE;
gboolean end_of_header_line = FALSE;
gboolean end_of_headers = FALSE;
gboolean end_of_file = FALSE;
GString *header_line=NULL;
GHashTable *header_table;
header_table = g_hash_table_new (g_string_hash, g_string_equal_for_hash);
next_char = fgetc (file);
do {
header_line = g_string_new("");
end_of_header_line = FALSE;
crlf = FALSE;
/* read a whole header line */
do {
switch (next_char) {
case EOF:
end_of_file=TRUE;
end_of_header_line = TRUE;
break;
case '\n': /* a blank line means end of headers */
if (crlf) {
end_of_headers=TRUE;
end_of_header_line = TRUE;
}
else crlf = TRUE;
break;
case ' ':
case 't':
if (crlf) crlf = FALSE;
default:
if (!crlf) header_line = g_string_append_c (header_line, next_char);
else end_of_header_line = TRUE;
}
/* if we have read a whole header line, we have also read
the first character of the next line to be sure the
crlf was not followed by a space or a tab char */
if (!end_of_header_line) next_char = fgetc (file);
} while ( !end_of_header_line );
if ( strlen(header_line->str) )
_store_header_pair_from_gstring (header_table, header_line);
g_string_free (header_line, FALSE);
} while ( (!end_of_headers) && (!end_of_file) );
return header_table;
}
GHashTable *
get_header_table_from_stream (CamelStream *stream)
{
gchar next_char;
gboolean crlf = FALSE;
gboolean end_of_header_line = FALSE;
gboolean end_of_headers = FALSE;
gboolean end_of_file = FALSE;
GString *header_line=NULL;
GHashTable *header_table;
header_table = g_hash_table_new (g_string_hash, g_string_equal_for_hash);
camel_stream_read (stream, &next_char, 1);
do {
header_line = g_string_new("");
end_of_header_line = FALSE;
crlf = FALSE;
/* read a whole header line */
do {
switch (next_char) {
case -1:
end_of_file=TRUE;
end_of_header_line = TRUE;
break;
case '\n': /* a blank line means end of headers */
if (crlf) {
end_of_headers=TRUE;
end_of_header_line = TRUE;
}
else crlf = TRUE;
break;
case ' ':
case 't':
if (crlf) crlf = FALSE;
default:
if (!crlf) header_line = g_string_append_c (header_line, next_char);
else end_of_header_line = TRUE;
}
/* if we have read a whole header line, we have also read
the first character of the next line to be sure the
crlf was not followed by a space or a tab char */
if (!end_of_header_line) camel_stream_read (stream, &next_char, 1);
} while ( !end_of_header_line );
if ( strlen(header_line->str) )
_store_header_pair_from_gstring (header_table, header_line);
g_string_free (header_line, FALSE);
} while ( (!end_of_headers) && (!end_of_file) );
return header_table;
}
|