1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5 * Authors: Michael Zucchi <notzed@ximian.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
28 #include "camel-mime-filter-from.h"
30 #define CAMEL_MIME_FILTER_FROM_GET_PRIVATE(obj) \
31 (G_TYPE_INSTANCE_GET_PRIVATE \
32 ((obj), CAMEL_TYPE_MIME_FILTER_FROM, CamelMimeFilterFromPrivate))
36 struct _CamelMimeFilterFromPrivate {
37 gboolean midline; /* are we between lines? */
41 struct fromnode *next;
45 G_DEFINE_TYPE (CamelMimeFilterFrom, camel_mime_filter_from, CAMEL_TYPE_MIME_FILTER)
47 /* Yes, it is complicated ... */
49 mime_filter_from_filter (CamelMimeFilter *mime_filter,
57 CamelMimeFilterFromPrivate *priv;
58 const gchar *inptr, *inend;
61 struct fromnode *head = NULL, *tail = (struct fromnode *) &head, *node;
64 priv = CAMEL_MIME_FILTER_FROM_GET_PRIVATE (mime_filter);
69 d (printf ("Filtering '%.*s'\n", len, in));
71 /* first, see if we need to escape any from's */
72 while (inptr < inend) {
76 while (inptr < inend && (c = *inptr++) != '\n')
79 if (c == '\n' || !priv->midline) {
84 if (inptr[0] == 'F') {
85 camel_mime_filter_backup (mime_filter, inptr, left);
86 priv->midline = FALSE;
91 if (!strncmp (inptr, "From ", 5)) {
93 /* yes, we do alloc them on the stack ... at most we're going to get
94 * len / 7 of them anyway */
95 node = alloca (sizeof (*node));
96 node->pointer = inptr;
104 /* \n is at end of line, check next buffer */
105 priv->midline = FALSE;
111 camel_mime_filter_set_size (mime_filter, len + fromcount, FALSE);
114 outptr = mime_filter->outbuf;
116 memcpy (outptr, inptr, node->pointer - inptr);
117 outptr += node->pointer - inptr;
119 inptr = node->pointer;
122 memcpy (outptr, inptr, inend - inptr);
123 outptr += inend - inptr;
124 *out = mime_filter->outbuf;
125 *outlen = outptr - mime_filter->outbuf;
126 *outprespace = mime_filter->outbuf - mime_filter->outreal;
128 d (printf ("Filtered '%.*s'\n", *outlen, *out));
131 *outlen = inend - in;
132 *outprespace = prespace;
134 d (printf ("Filtered '%.*s'\n", *outlen, *out));
139 mime_filter_from_complete (CamelMimeFilter *mime_filter,
149 *outprespace = prespace;
153 camel_mime_filter_from_class_init (CamelMimeFilterFromClass *class)
155 CamelMimeFilterClass *mime_filter_class;
157 g_type_class_add_private (class, sizeof (CamelMimeFilterFromPrivate));
159 mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
160 mime_filter_class->filter = mime_filter_from_filter;
161 mime_filter_class->complete = mime_filter_from_complete;
165 camel_mime_filter_from_init (CamelMimeFilterFrom *filter)
167 filter->priv = CAMEL_MIME_FILTER_FROM_GET_PRIVATE (filter);
171 * camel_mime_filter_from_new:
173 * Create a new #CamelMimeFilterFrom object.
175 * Returns: a new #CamelMimeFilterFrom object
178 camel_mime_filter_from_new (void)
180 return g_object_new (CAMEL_TYPE_MIME_FILTER_FROM, NULL);
187 gint main (gint argc, gchar **argv)
189 CamelMimeFilterFrom *f;
193 g_tk_init (&argc, &argv);
195 f = camel_mime_filter_from_new ();
197 buffer = "This is a test\nFrom Someone\nTo someone. From Someone else, From\n From blah\nFromblah\nBye! \nFrom ";
198 len = strlen (buffer);
201 printf ("input = '%.*s'\n", len, buffer);
202 camel_mime_filter_filter (f, buffer, len, prespace, &buffer, &len, &prespace);
203 printf ("output = '%.*s'\n", len, buffer);
207 camel_mime_filter_complete (f, buffer, len, prespace, &buffer, &len, &prespace);
208 printf ("complete = '%.*s'\n", len, buffer);