1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000 Ximian Inc.
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.
29 #include "camel-mime-filter-from.h"
33 static void camel_mime_filter_from_class_init (CamelMimeFilterFromClass *klass);
34 static void camel_mime_filter_from_init (CamelMimeFilterFrom *obj);
36 static CamelMimeFilterClass *camel_mime_filter_from_parent;
39 camel_mime_filter_from_get_type (void)
41 static CamelType type = CAMEL_INVALID_TYPE;
43 if (type == CAMEL_INVALID_TYPE) {
44 type = camel_type_register (camel_mime_filter_get_type (), "CamelMimeFilterFrom",
45 sizeof (CamelMimeFilterFrom),
46 sizeof (CamelMimeFilterFromClass),
47 (CamelObjectClassInitFunc) camel_mime_filter_from_class_init,
49 (CamelObjectInitFunc) camel_mime_filter_from_init,
57 struct fromnode *next;
62 complete(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
66 *outprespace = prespace;
69 /* Yes, it is complicated ... */
71 filter(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
73 CamelMimeFilterFrom *f = (CamelMimeFilterFrom *)mf;
74 register char *inptr, *inend;
76 int midline = f->midline;
78 struct fromnode *head = NULL, *tail = (struct fromnode *)&head, *node;
84 d(printf("Filtering '%.*s'\n", len, in));
86 /* first, see if we need to escape any from's */
91 while (inptr < inend && (c = *inptr++) != '\n')
94 if (c == '\n' || !midline) {
99 if (inptr[0] == 'F') {
100 camel_mime_filter_backup(mf, inptr, left);
106 if (!strncmp(inptr, "From ", 5)) {
108 /* yes, we do alloc them on the stack ... at most we're going to get
109 len / 7 of them anyway */
110 node = alloca(sizeof(*node));
111 node->pointer = inptr;
119 /* \n is at end of line, check next buffer */
125 f->midline = midline;
128 camel_mime_filter_set_size(mf, len + fromcount, FALSE);
133 memcpy(outptr, inptr, node->pointer - inptr);
134 outptr += node->pointer - inptr;
136 inptr = node->pointer;
139 memcpy(outptr, inptr, inend - inptr);
140 outptr += inend - inptr;
142 *outlen = outptr - mf->outbuf;
143 *outprespace = mf->outbuf - mf->outreal;
145 d(printf("Filtered '%.*s'\n", *outlen, *out));
148 *outlen = inend - in;
149 *outprespace = prespace;
151 d(printf("Filtered '%.*s'\n", *outlen, *out));
156 camel_mime_filter_from_class_init (CamelMimeFilterFromClass *klass)
158 CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;
160 camel_mime_filter_from_parent = CAMEL_MIME_FILTER_CLASS (camel_type_get_global_classfuncs (camel_mime_filter_get_type ()));
162 filter_class->filter = filter;
163 filter_class->complete = complete;
167 camel_mime_filter_from_init (CamelMimeFilterFrom *obj)
174 * camel_mime_filter_from_new:
176 * Create a new #CamelMimeFilterFrom object.
178 * Returns a new #CamelMimeFilterFrom object
180 CamelMimeFilterFrom *
181 camel_mime_filter_from_new (void)
183 CamelMimeFilterFrom *new = CAMEL_MIME_FILTER_FROM ( camel_object_new (camel_mime_filter_from_get_type ()));
191 int main(int argc, char **argv)
193 CamelMimeFilterFrom *f;
197 g_tk_init(&argc, &argv);
200 f = camel_mime_filter_from_new();
202 buffer = "This is a test\nFrom Someone\nTo someone. From Someone else, From\n From blah\nFromblah\nBye! \nFrom ";
203 len = strlen(buffer);
206 printf("input = '%.*s'\n", len, buffer);
207 camel_mime_filter_filter(f, buffer, len, prespace, &buffer, &len, &prespace);
208 printf("output = '%.*s'\n", len, buffer);
212 camel_mime_filter_complete(f, buffer, len, prespace, &buffer, &len, &prespace);
213 printf("complete = '%.*s'\n", len, buffer);