new func. Parses message header zone and returns a Glist of all header
1999-05-26 bertrand <Bertrand.Guiheneuf@inria.fr> * camel/gmime-utils.c (get_header_lines_from_file): new func. Parses message header zone and returns a Glist of all header lines. * tests/test2.c: tests message parsing * camel/gmime-utils.c (write_header_table_to_file): new func to write a table of headers. svn path=/trunk/; revision=948
This commit is contained in:
committed by
Bertrand Guiheneuf
parent
fa1d506238
commit
72c4a8deda
11
ChangeLog
11
ChangeLog
@ -1,3 +1,14 @@
|
||||
1999-05-26 bertrand <Bertrand.Guiheneuf@inria.fr>
|
||||
|
||||
* camel/gmime-utils.c (get_header_lines_from_file):
|
||||
new func. Parses message header zone and returns
|
||||
a Glist of all header lines.
|
||||
|
||||
* tests/test2.c: tests message parsing
|
||||
|
||||
* camel/gmime-utils.c (write_header_table_to_file):
|
||||
new func to write a table of headers.
|
||||
|
||||
1999-05-20 bertrand <Bertrand.Guiheneuf@inria.fr>
|
||||
|
||||
* camel/camel-mime-message.c (_write_to_file):
|
||||
|
||||
@ -495,28 +495,10 @@ _write_one_recipient_to_file (gpointer key, gpointer value, gpointer user_data)
|
||||
{
|
||||
GString *recipient_type = (GString *)key;
|
||||
GList *recipients = (GList *)value;
|
||||
GString *current;
|
||||
// GString *current;
|
||||
FILE *file = (FILE *)user_data;
|
||||
|
||||
if ( (recipient_type) && (recipient_type->str) &&
|
||||
(recipients) )
|
||||
{
|
||||
gboolean first;
|
||||
|
||||
fprintf(file, "%s: ", recipient_type->str);
|
||||
first = TRUE;
|
||||
while (recipients) {
|
||||
current = (GString *)recipients->data;
|
||||
if ( (current) && (current->str) ) {
|
||||
if (!first) fprintf(file, ", ");
|
||||
else first = FALSE;
|
||||
fprintf(file, "%s", current->str);
|
||||
}
|
||||
recipients = g_list_next(recipients);
|
||||
|
||||
}
|
||||
fprintf(file, "\n");
|
||||
}
|
||||
if ( (recipient_type) && (recipient_type->str) )
|
||||
write_header_with_glist_to_file (file, recipient_type->str, recipients);
|
||||
}
|
||||
|
||||
static void
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
|
||||
/* camelMimePart.c : Abstract class for a mime_part */
|
||||
|
||||
/** THIS IS MOSTLY AN ABSTRACT CLASS THAT SHOULD HAVE BEEN AN
|
||||
INTERFACE. **/
|
||||
|
||||
/*
|
||||
*
|
||||
@ -485,6 +483,9 @@ _write_content_to_file (CamelMimePart *mime_part, FILE *file)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static void
|
||||
_write_to_file(CamelDataWrapper *data_wrapper, FILE *file)
|
||||
{
|
||||
@ -495,6 +496,8 @@ _write_to_file(CamelDataWrapper *data_wrapper, FILE *file)
|
||||
WHPTF (file, "Content-Description", mp->description);
|
||||
WHPTF (file, "Content-MD5", mp->content_MD5);
|
||||
WHPTF (file, "Content-id", mp->content_id);
|
||||
write_header_with_glist_to_file (file, "Content-Language", mp->content_languages);
|
||||
write_header_table_to_file (file, mp->headers);
|
||||
fprintf(file,"\n");
|
||||
if (mp->content) camel_data_wrapper_write_to_file (mp->content, file);
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@
|
||||
|
||||
#include "gmime-utils.h"
|
||||
void
|
||||
gmime_write_header_pair_to_file(FILE* file, gchar* name, GString *value)
|
||||
gmime_write_header_pair_to_file (FILE* file, gchar* name, GString *value)
|
||||
{
|
||||
g_assert(name);
|
||||
|
||||
@ -34,3 +34,116 @@ gmime_write_header_pair_to_file(FILE* file, gchar* name, GString *value)
|
||||
}
|
||||
|
||||
|
||||
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 */
|
||||
|
||||
|
||||
GList *
|
||||
get_header_lines_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;
|
||||
GList *header_lines=NULL;
|
||||
|
||||
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) )
|
||||
header_lines = g_list_append (header_lines, header_line);
|
||||
else
|
||||
g_string_free (header_line, FALSE);
|
||||
|
||||
} while ( (!end_of_headers) && (!end_of_file) );
|
||||
|
||||
return header_lines;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -34,8 +34,11 @@ extern "C" {
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void gmime_write_header_pair_to_file(FILE* file, gchar* name, GString *value);
|
||||
void gmime_write_header_pair_to_file (FILE* file, gchar* name, GString *value);
|
||||
void write_header_table_to_file (FILE *file, GHashTable *header_table);
|
||||
void write_header_with_glist_to_file (FILE *file, gchar *header_name, GList *header_values);
|
||||
|
||||
GList *get_header_lines_from_file (FILE *file);
|
||||
|
||||
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ LDADD = \
|
||||
$(GNOME_LIBDIR) \
|
||||
$(GNOMEUI_LIBS) $(INTLLIBS)
|
||||
|
||||
noinst_PROGRAMS = \
|
||||
test1
|
||||
noinst_PROGRAMS = \
|
||||
test1 \
|
||||
test2
|
||||
|
||||
|
||||
@ -11,6 +11,10 @@ main (int argc, char**argv)
|
||||
gtk_init (&argc, &argv);
|
||||
message = camel_mime_message_new_with_session( (CamelSession *)NULL);
|
||||
camel_mime_part_set_description (CAMEL_MIME_PART (message), g_string_new ("a test"));
|
||||
camel_mime_part_add_header (CAMEL_MIME_PART (message), g_string_new ("X-test1"), g_string_new ("the value of a test"));
|
||||
camel_mime_part_add_header (CAMEL_MIME_PART (message), g_string_new ("X-test2"), g_string_new ("the value of another test"));
|
||||
/*camel_mime_part_add_content_language (CAMEL_MIME_PART (message), g_string_new ("es-ca"));*/
|
||||
|
||||
camel_mime_message_set_received_date (message, g_string_new ("Thu, 20 May 1999, 10:39:14 +0200"));
|
||||
camel_mime_message_set_subject (message, g_string_new ("A test message"));
|
||||
camel_mime_message_set_reply_to (message, g_string_new ("toto@toto.com"));
|
||||
@ -19,12 +23,9 @@ main (int argc, char**argv)
|
||||
camel_mime_message_add_recipient (message, g_string_new (RECIPIENT_TYPE_TO), g_string_new ("franck.dechamps@alseve.fr"));
|
||||
camel_mime_message_add_recipient (message, g_string_new (RECIPIENT_TYPE_TO), g_string_new ("mc@alseve.fr"));
|
||||
camel_mime_message_add_recipient (message, g_string_new (RECIPIENT_TYPE_TO), g_string_new ("richard.lengagne@inria.fr"));
|
||||
|
||||
camel_mime_message_add_recipient (message, g_string_new (RECIPIENT_TYPE_CC), g_string_new ("Francois.fleuret@inria.fr"));
|
||||
camel_mime_message_add_recipient (message, g_string_new (RECIPIENT_TYPE_CC), g_string_new ("maury@justmagic.com"));
|
||||
|
||||
camel_mime_message_add_recipient (message, g_string_new (RECIPIENT_TYPE_BCC), g_string_new ("guiheneu@aful.org"));
|
||||
|
||||
|
||||
output_file = fopen ("mail.test", "w");
|
||||
if (!output_file) {
|
||||
|
||||
36
tests/test2.c
Normal file
36
tests/test2.c
Normal file
@ -0,0 +1,36 @@
|
||||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
|
||||
/* tests mime message file parsing */
|
||||
#include "gmime-utils.h"
|
||||
#include "stdio.h"
|
||||
|
||||
void print_header_line (gpointer data, gpointer user_data)
|
||||
{
|
||||
GString *header_line = (GString *)data;
|
||||
printf("\n--------- New Header ----------\n");
|
||||
if ((header_line) && (header_line->str))
|
||||
printf("%s\n", header_line->str);
|
||||
printf("--------- End -----------------\n");
|
||||
}
|
||||
|
||||
void
|
||||
main (int argc, char**argv)
|
||||
{
|
||||
FILE *input_file;
|
||||
GList *header_lines;
|
||||
|
||||
|
||||
gtk_init (&argc, &argv);
|
||||
|
||||
input_file = fopen ("mail.test", "r");
|
||||
if (!input_file) {
|
||||
perror("could not open input file");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
header_lines = get_header_lines_from_file (input_file);
|
||||
if (header_lines) g_list_foreach (header_lines, print_header_line, NULL);
|
||||
else printf("header is empty, no header line present\n");
|
||||
fclose (input_file);
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user