1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000 Ximian, Inc.
5 * Authors: Jeffrey Stedfast <fejj@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-linewrap.h"
32 static void filter (CamelMimeFilter *f, char *in, size_t len, size_t prespace,
33 char **out, size_t *outlen, size_t *outprespace);
34 static void complete (CamelMimeFilter *f, char *in, size_t len,
35 size_t prespace, char **out, size_t *outlen,
37 static void reset (CamelMimeFilter *f);
41 camel_mime_filter_linewrap_class_init (CamelMimeFilterLinewrapClass *klass)
43 CamelMimeFilterClass *mime_filter_class =
44 (CamelMimeFilterClass *) klass;
46 mime_filter_class->filter = filter;
47 mime_filter_class->complete = complete;
48 mime_filter_class->reset = reset;
52 camel_mime_filter_linewrap_get_type (void)
54 static CamelType type = CAMEL_INVALID_TYPE;
56 if (type == CAMEL_INVALID_TYPE) {
57 type = camel_type_register (camel_mime_filter_get_type(), "CamelMimeFilterLinewrap",
58 sizeof (CamelMimeFilterLinewrap),
59 sizeof (CamelMimeFilterLinewrapClass),
60 (CamelObjectClassInitFunc) camel_mime_filter_linewrap_class_init,
70 filter (CamelMimeFilter *f, char *in, size_t len, size_t prespace,
71 char **out, size_t *outlen, size_t *outprespace)
73 CamelMimeFilterLinewrap *linewrap = (CamelMimeFilterLinewrap *)f;
75 int nchars = linewrap->nchars;
77 /* we'll be adding chars here so we need a bigger buffer */
78 camel_mime_filter_set_size (f, 3 * len, FALSE);
88 } else if (isspace (*p)) {
89 if (nchars >= linewrap->wrap_len) {
101 /* line is getting way too long, we must force a wrap here */
102 if (nchars >= (linewrap->max_len - 1) && *p != '\n') {
104 *q++ = linewrap->indent;
109 linewrap->nchars = nchars;
112 *outlen = q - f->outbuf;
113 *outprespace = f->outpre;
117 complete (CamelMimeFilter *f, char *in, size_t len, size_t prespace,
118 char **out, size_t *outlen, size_t *outprespace)
121 filter (f, in, len, prespace, out, outlen, outprespace);
125 reset (CamelMimeFilter *f)
127 CamelMimeFilterLinewrap *linewrap = (CamelMimeFilterLinewrap *)f;
129 linewrap->nchars = 0;
133 camel_mime_filter_linewrap_new (guint preferred_len, guint max_len, char indent_char)
135 CamelMimeFilterLinewrap *linewrap =
136 CAMEL_MIME_FILTER_LINEWRAP (camel_object_new (CAMEL_MIME_FILTER_LINEWRAP_TYPE));
138 linewrap->indent = indent_char;
139 linewrap->wrap_len = preferred_len;
140 linewrap->max_len = max_len;
141 linewrap->nchars = 0;
143 return (CamelMimeFilter *) linewrap;