claws-mail/src/enriched.c
Colin Leroy 49a81f6a9c 2009-02-23 [cleroy] 3.7.0cvs73
* src/account.c
	* src/action.c
	* src/addr_compl.c
	* src/addrbook.c
	* src/addrcache.c
	* src/addrcindex.c
	* src/addrclip.c
	* src/addrduplicates.c
	* src/addressadd.c
	* src/addressbook.c
	* src/addressbook_foldersel.c
	* src/addrharvest.c
	* src/addrindex.c
	* src/addritem.c
	* src/addrquery.c
	* src/addrselect.c
	* src/codeconv.c
	* src/compose.c
	* src/customheader.c
	* src/editaddress.c
	* src/editaddress_other_attributes_ldap.c
	* src/editgroup.c
	* src/editldap.c
	* src/enriched.c
	* src/exporthtml.c
	* src/exportldif.c
	* src/filtering.c
	* src/folder.c
	* src/foldersel.c
	* src/folderutils.c
	* src/folderview.c
	* src/gedit-print.c
	* src/html.c
	* src/imap.c
	* src/imap_gtk.c
	* src/inc.c
	* src/jpilot.c
	* src/ldapctrl.c
	* src/ldapquery.c
	* src/ldapserver.c
	* src/ldapupdate.c
	* src/ldif.c
	* src/localfolder.c
	* src/main.c
	* src/mainwindow.c
	* src/matcher.c
	* src/mbox.c
	* src/messageview.c
	* src/mh.c
	* src/mh_gtk.c
	* src/mimeview.c
	* src/msgcache.c
	* src/mutt.c
	* src/news.c
	* src/news_gtk.c
	* src/noticeview.c
	* src/pine.c
	* src/pop.c
	* src/prefs_account.c
	* src/prefs_actions.c
	* src/prefs_common.c
	* src/prefs_filtering.c
	* src/prefs_filtering_action.c
	* src/prefs_folder_column.c
	* src/prefs_folder_item.c
	* src/prefs_gtk.c
	* src/prefs_matcher.c
	* src/prefs_quote.c
	* src/prefs_send.c
	* src/prefs_summaries.c
	* src/prefs_summary_column.c
	* src/prefs_template.c
	* src/prefs_themes.c
	* src/prefs_toolbar.c
	* src/printing.c
	* src/privacy.c
	* src/procheader.c
	* src/procmime.c
	* src/procmsg.c
	* src/quote_fmt.c
	* src/remotefolder.c
	* src/send_message.c
	* src/sourcewindow.c
	* src/statusbar.c
	* src/stock_pixmap.c
	* src/summary_search.c
	* src/summaryview.c
	* src/textview.c
	* src/toolbar.c
	* src/undo.c
	* src/uri_opener.c
	* src/vcard.c
	* src/common/base64.c
	* src/common/claws.c
	* src/common/hooks.c
	* src/common/plugin.c
	* src/common/prefs.c
	* src/common/session.c
	* src/common/smtp.c
	* src/common/socket.c
	* src/common/string_match.c
	* src/common/stringtable.c
	* src/common/utils.c
	* src/common/utils.h
	* src/common/xml.c
	* src/common/xmlprops.c
	* src/gtk/colorlabel.c
	* src/gtk/combobox.c
	* src/gtk/foldersort.c
	* src/gtk/gtkaspell.c
	* src/gtk/gtkcmclist.c
	* src/gtk/gtkcmctree.c
	* src/gtk/gtkcmoptionmenu.c
	* src/gtk/gtksctree.c
	* src/gtk/gtkshruler.c
	* src/gtk/gtksourceprintjob.c
	* src/gtk/gtkutils.c
	* src/gtk/gtkvscrollbutton.c
	* src/gtk/logwindow.c
	* src/gtk/menu.c
	* src/gtk/pluginwindow.c
	* src/gtk/prefswindow.c
	* src/gtk/quicksearch.c
	* src/gtk/spell_entry.c
	* src/plugins/pgpcore/select-keys.c
	* src/plugins/pgpinline/pgpinline.c
	* src/plugins/pgpmime/pgpmime.c
	* src/plugins/smime/smime.c
	* src/plugins/spamassassin/spamassassin_gtk.c
		Mass commit changing g_return_* to cm_return_*
		and making these more interesting
2009-02-23 16:55:51 +00:00

219 lines
4.9 KiB
C

/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
* Copyright (C) 1999,2000 Hiroyuki Yamamoto
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "enriched.h"
#include "utils.h"
#define ERTFBUFSIZE 8192
static ERTFState ertf_read_line (ERTFParser *parser);
static void ertf_append_char (ERTFParser *parser,
gchar ch);
static ERTFState ertf_parse_tag (ERTFParser *parser);
static void ertf_get_parenthesis (ERTFParser *parser,
gchar *buf,
gint len);
ERTFParser *ertf_parser_new(FILE *fp, CodeConverter *conv)
{
ERTFParser *parser;
cm_return_val_if_fail(fp != NULL, NULL);
cm_return_val_if_fail(conv != NULL, NULL);
parser = g_new0(ERTFParser, 1);
parser->fp = fp;
parser->conv = conv;
parser->str = g_string_new(NULL);
parser->buf = g_string_new(NULL);
parser->bufp = parser->buf->str;
parser->newline = TRUE;
parser->empty_line = TRUE;
parser->space = FALSE;
parser->pre = FALSE;
return parser;
}
void ertf_parser_destroy(ERTFParser *parser)
{
g_string_free(parser->str, TRUE);
g_string_free(parser->buf, TRUE);
g_free(parser);
}
gchar *ertf_parse(ERTFParser *parser)
{
parser->state = ERTF_NORMAL;
g_string_truncate(parser->str, 0);
if (*parser->bufp == '\0') {
g_string_truncate(parser->buf, 0);
parser->bufp = parser->buf->str;
if (ertf_read_line(parser) == ERTF_EOF)
return NULL;
}
while (*parser->bufp != '\0') {
switch (*parser->bufp) {
case '<':
if (parser->str->len == 0)
ertf_parse_tag(parser);
else
return parser->str->str;
break;
case '\n':
case '\r':
if (parser->bufp[0] == '\r' && parser->bufp[1] == '\n')
parser->bufp++;
if (!parser->pre) {
/* When not pre (not <nofill>), 1 CRLF = SPACE, N>1 CRLF = N-1 CRLF*/
if (!parser->newline) {
parser->newline = TRUE;
parser->space = TRUE;
parser->bufp++;
break;
}
}
default:
ertf_append_char(parser, *parser->bufp++);
}
}
return parser->str->str;
}
static ERTFState ertf_read_line(ERTFParser *parser)
{
gchar buf[ERTFBUFSIZE];
gchar buf2[ERTFBUFSIZE];
gint index;
if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
parser->state = ERTF_EOF;
return ERTF_EOF;
}
if (conv_convert(parser->conv, buf2, sizeof(buf2), buf) < 0) {
g_warning("ertf_read_line(): code conversion failed\n");
index = parser->bufp - parser->buf->str;
g_string_append(parser->buf, buf);
parser->bufp = parser->buf->str + index;
return ERTF_ERR;
}
index = parser->bufp - parser->buf->str;
g_string_append(parser->buf, buf2);
parser->bufp = parser->buf->str + index;
return ERTF_NORMAL;
}
static void ertf_append_char(ERTFParser *parser, gchar ch)
{
GString *str = parser->str;
if (!parser->pre && parser->space) {
if (ch != '\n')
g_string_append_c(str, ' ');
parser->space = FALSE;
}
g_string_append_c(str, ch);
parser->empty_line = FALSE;
if (ch == '\n') {
if (parser->newline)
parser->empty_line = TRUE;
else
parser->newline = TRUE;
} else
parser->newline = FALSE;
}
static ERTFState ertf_parse_tag(ERTFParser *parser)
{
gchar buf[ERTFBUFSIZE];
gchar *p;
gchar *down;
ertf_get_parenthesis (parser, buf, sizeof(buf));
for (p = buf; *p != '\0'; p++) {
if (isspace (*(guchar *)p)) {
*p = '\0';
break;
}
}
parser->state = ERTF_UNKNOWN;
if (buf[0] == '\0') return parser->state;
down = g_utf8_strdown (buf, -1);
if (!strcmp(down, "nofill")) {
parser->pre = TRUE;
parser->state = ERTF_NOFILL;
}
else if (!strcmp(down, "/nofill")) {
parser->pre = FALSE;
parser->state = ERTF_NORMAL;
}
g_free(down);
return parser->state;
}
static void ertf_get_parenthesis(ERTFParser *parser, gchar *buf, gint len)
{
gchar *p;
buf[0] = '\0';
cm_return_if_fail(*parser->bufp == '<');
/* ignore params */
if (!g_ascii_strncasecmp(parser->bufp, "<param>", 4)) {
parser->bufp += 7;
while ((p = strstr(parser->bufp, "</param>")) == NULL)
if (ertf_read_line(parser) == ERTF_EOF) return;
parser->bufp = p + 8;
return;
}
parser->bufp++;
while ((p = strchr(parser->bufp, '>')) == NULL)
if (ertf_read_line(parser) == ERTF_EOF) return;
strncpy2(buf, parser->bufp, MIN(p - parser->bufp + 1, len));
parser->bufp = p + 1;
}