2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #define ERTFBUFSIZE 8192
32 static ERTFState ertf_read_line (ERTFParser *parser);
33 static void ertf_append_char (ERTFParser *parser,
35 static void ertf_append_str (ERTFParser *parser,
39 static ERTFState ertf_parse_tag (ERTFParser *parser);
40 static void ertf_get_parenthesis (ERTFParser *parser,
44 ERTFParser *ertf_parser_new(FILE *fp, CodeConverter *conv)
48 g_return_val_if_fail(fp != NULL, NULL);
49 g_return_val_if_fail(conv != NULL, NULL);
51 parser = g_new0(ERTFParser, 1);
54 parser->str = g_string_new(NULL);
55 parser->buf = g_string_new(NULL);
56 parser->bufp = parser->buf->str;
57 parser->newline = TRUE;
58 parser->empty_line = TRUE;
59 parser->space = FALSE;
65 void ertf_parser_destroy(ERTFParser *parser)
67 g_string_free(parser->str, TRUE);
68 g_string_free(parser->buf, TRUE);
72 gchar *ertf_parse(ERTFParser *parser)
74 parser->state = ERTF_NORMAL;
75 g_string_truncate(parser->str, 0);
77 if (*parser->bufp == '\0') {
78 g_string_truncate(parser->buf, 0);
79 parser->bufp = parser->buf->str;
80 if (ertf_read_line(parser) == ERTF_EOF)
84 while (*parser->bufp != '\0') {
85 switch (*parser->bufp) {
87 if (parser->str->len == 0)
88 ertf_parse_tag(parser);
90 return parser->str->str;
94 if (parser->bufp[0] == '\r' && parser->bufp[1] == '\n')
98 /* When not pre (not <nofill>), 1 CRLF = SPACE, N>1 CRLF = N-1 CRLF*/
99 if (!parser->newline) {
100 parser->newline = TRUE;
101 parser->space = TRUE;
108 ertf_append_char(parser, *parser->bufp++);
112 return parser->str->str;
115 static ERTFState ertf_read_line(ERTFParser *parser)
117 gchar buf[ERTFBUFSIZE];
118 gchar buf2[ERTFBUFSIZE];
121 if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
122 parser->state = ERTF_EOF;
126 if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
127 g_warning("ertf_read_line(): code conversion failed\n");
129 index = parser->bufp - parser->buf->str;
131 g_string_append(parser->buf, buf);
133 parser->bufp = parser->buf->str + index;
138 index = parser->bufp - parser->buf->str;
140 g_string_append(parser->buf, buf2);
142 parser->bufp = parser->buf->str + index;
147 static void ertf_append_char(ERTFParser *parser, gchar ch)
149 GString *str = parser->str;
151 if (!parser->pre && parser->space) {
153 g_string_append_c(str, ' ');
154 parser->space = FALSE;
157 g_string_append_c(str, ch);
159 parser->empty_line = FALSE;
162 parser->empty_line = TRUE;
164 parser->newline = TRUE;
166 parser->newline = FALSE;
169 static void ertf_append_str(ERTFParser *parser, const gchar *str, gint len)
171 GString *string = parser->str;
173 if (!parser->pre && parser->space) {
174 g_string_append_c(string, ' ');
175 parser->space = FALSE;
178 if (len == 0) return;
180 g_string_append(string, str);
183 Xstrndup_a(s, str, len, return);
184 g_string_append(string, s);
187 parser->empty_line = FALSE;
188 if (string->len > 0 && string->str[string->len - 1] == '\n') {
189 parser->newline = TRUE;
190 if (string->len > 1 && string->str[string->len - 2] == '\n')
191 parser->empty_line = TRUE;
193 parser->newline = FALSE;
196 static ERTFState ertf_parse_tag(ERTFParser *parser)
198 gchar buf[ERTFBUFSIZE];
201 ertf_get_parenthesis (parser, buf, sizeof(buf));
203 for (p = buf; *p != '\0'; p++) {
210 parser->state = ERTF_UNKNOWN;
211 if (buf[0] == '\0') return parser->state;
215 if (!strcmp(buf, "nofill")) {
217 parser->state = ERTF_NOFILL;
219 else if (!strcmp(buf, "/nofill")) {
221 parser->state = ERTF_NORMAL;
224 return parser->state;
227 static void ertf_get_parenthesis(ERTFParser *parser, gchar *buf, gint len)
232 g_return_if_fail(*parser->bufp == '<');
235 if (!g_strncasecmp(parser->bufp, "<param>", 4)) {
237 while ((p = strstr(parser->bufp, "</param>")) == NULL)
238 if (ertf_read_line(parser) == ERTF_EOF) return;
239 parser->bufp = p + 8;
243 while ((p = strchr(parser->bufp, '>')) == NULL)
244 if (ertf_read_line(parser) == ERTF_EOF) return;
246 strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len));
247 parser->bufp = p + 1;