From: Pierre Habouzit Date: Sat, 4 Nov 2006 00:14:59 +0000 (+0100) Subject: make code a bit more readable. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;ds=inline;h=a743b55a20cbf3a33699fdb24dcb8638e878cc46;hp=738fbd2175e916fb1d5b60af57968dbdc5bf56bd;p=apps%2Fmadmutt.git make code a bit more readable. Signed-off-by: Pierre Habouzit --- diff --git a/lib-mime/rfc2047.c b/lib-mime/rfc2047.c index 1d4fa46..06db456 100644 --- a/lib-mime/rfc2047.c +++ b/lib-mime/rfc2047.c @@ -37,6 +37,7 @@ #include "charset.h" #include "thread.h" +#include #include #include #include @@ -49,7 +50,6 @@ #ifdef NDEBUG #define assert(x) #else -#include #endif #define ENCWORD_LEN_MAX 75 @@ -75,7 +75,6 @@ convert_string(const char *from, const char *f, ssize_t flen, iconv_t cd; char *buf, *ob; ssize_t obl, n; - int e; cd = mutt_iconv_open(to, from, 0); @@ -87,38 +86,47 @@ convert_string(const char *from, const char *f, ssize_t flen, n = my_iconv(cd, &f, &flen, &ob, &obl); if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) { - e = errno; - p_delete(&buf); - iconv_close (cd); + int e = errno; + iconv_close(cd); errno = e; + p_delete(&buf); return -1; } + iconv_close(cd); *ob = '\0'; *tlen = ob - buf; - - p_realloc(&buf, ob - buf + 1); - *t = buf; - iconv_close (cd); + *t = buf; return n; } +/* choose the shortest encoding for u */ char *mutt_choose_charset(const char *fromcode, const char *charsets, - char *u, ssize_t ulen, char **d, ssize_t *dlen) + char *u, ssize_t ulen, char **dst, ssize_t *dlen) { - char canonical_buff[LONG_STRING]; - char *e = 0, *tocode = 0; - ssize_t elen = 0, bestn = 0; - const char *p, *q; + char *res = NULL; + ssize_t reslen = 0; - for (p = charsets; p; p = q ? q + 1 : 0) { - char *s, *t; - ssize_t slen, n; + char *tocode = NULL; + ssize_t bestn = 0; - q = strchr (p, ':'); + const char *p = charsets; - n = q ? q - p : m_strlen(p); + while (*p) { + char cset[SHORT_STRING]; + ssize_t slen, n; + const char *q; + char *s; + + q = strchr(p, ':'); + if (q) { + n = m_strncpy(cset, sizeof(cset), p, q - p); + p = ++q; + } else { + n = m_strcpy(cset, sizeof(cset), p); + p += n; + } if (!n || /* Assume that we never need more than 12 characters of @@ -126,39 +134,36 @@ char *mutt_choose_charset(const char *fromcode, const char *charsets, n > (ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 2 - 12)) continue; - t = p_dupstr(p, n); - - n = convert_string(fromcode, u, ulen, t, &s, &slen); + n = convert_string(fromcode, u, ulen, cset, &s, &slen); if (n < 0) continue; if (!tocode || n < bestn) { + m_strreplace(&tocode, cset); bestn = n; - p_delete(&tocode); - tocode = t; - if (d) { - p_delete(&e); - e = s; - } else { - p_delete(&s); - } - elen = slen; + + p_delete(&res); + res = s; + reslen = slen; if (!bestn) break; } else { - p_delete(&t); p_delete(&s); } } if (tocode) { - if (d) - *d = e; - if (dlen) - *dlen = elen; + char buf[LONG_STRING]; + + if (dst && dlen) { + *dst = res; + *dlen = reslen; + } else { + p_delete(&res); + } - mutt_canonical_charset(canonical_buff, sizeof(canonical_buff), tocode); - m_strreplace(&tocode, canonical_buff); + mutt_canonical_charset(buf, sizeof(buf), tocode); + m_strreplace(&tocode, buf); } return tocode; @@ -419,7 +424,8 @@ static int rfc2047_encode (const char *d, ssize_t dlen, int col, /* Choose target charset. */ tocode = fromcode; if (icode) { - if ((tocode1 = mutt_choose_charset (icode, charsets, u, ulen, 0, 0))) + if ((tocode1 = mutt_choose_charset(icode, charsets, u, ulen, + NULL, NULL))) tocode = tocode1; else ret = 2, icode = 0; @@ -747,7 +753,7 @@ void rfc2047_decode (char **pd) } if (ascii_strcasecmp (AssumedCharset, "us-ascii")) { char *t; - size_t tlen; + ssize_t tlen; t = p_dupstr(s, n); if (mutt_convert_nonmime_string (&t) == 0) { @@ -769,7 +775,7 @@ void rfc2047_decode (char **pd) } if (p != s) { - n = (size_t) (p - s); + n = (p - s); /* ignore spaces between encoded words * and linear white spaces between encoded word and *text */ if (!option (OPTSTRICTMIME)) { @@ -812,32 +818,31 @@ void rfc2047_decode (char **pd) str_adjust (pd); } -void rfc2047_decode_adrlist (address_t * a) +void rfc2047_decode_adrlist(address_t *a) { while (a) { if (a->personal) - rfc2047_decode (&a->personal); + rfc2047_decode(&a->personal); a = a->next; } } -void rfc2047_decode_envelope (ENVELOPE* e) { - - if (!e) - return; +void rfc2047_decode_envelope(ENVELOPE* e) +{ + assert (e); /* do RFC2047 decoding */ - rfc2047_decode_adrlist (e->from); - rfc2047_decode_adrlist (e->to); - rfc2047_decode_adrlist (e->cc); - rfc2047_decode_adrlist (e->bcc); - rfc2047_decode_adrlist (e->reply_to); - rfc2047_decode_adrlist (e->mail_followup_to); - rfc2047_decode_adrlist (e->return_path); - rfc2047_decode_adrlist (e->sender); + rfc2047_decode_adrlist(e->from); + rfc2047_decode_adrlist(e->to); + rfc2047_decode_adrlist(e->cc); + rfc2047_decode_adrlist(e->bcc); + rfc2047_decode_adrlist(e->reply_to); + rfc2047_decode_adrlist(e->mail_followup_to); + rfc2047_decode_adrlist(e->return_path); + rfc2047_decode_adrlist(e->sender); if (e->subject) { - rfc2047_decode (&e->subject); - mutt_adjust_subject (e); + rfc2047_decode(&e->subject); + mutt_adjust_subject(e); } } diff --git a/lib-mime/rfc2231.c b/lib-mime/rfc2231.c index 865877d..1d53c8e 100644 --- a/lib-mime/rfc2231.c +++ b/lib-mime/rfc2231.c @@ -306,7 +306,7 @@ int rfc2231_encode_string(char **s) char *charset = NULL; char *e, *p, *t, *d = NULL; int escapes = 0; - size_t dlen = 0; + ssize_t dlen = 0; /* * A shortcut to detect pure 7bit data. diff --git a/nntp/nntp.c b/nntp/nntp.c index 6869b21..2559221 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -620,7 +620,7 @@ static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr) hdr->env->xref = m_strdup(b); nntp_parse_xref (ctx, nntp_data->group, b, hdr); } - rfc2047_decode_envelope (hdr->env); + rfc2047_decode_envelope(hdr->env); if (!*p) return -1; b = p; diff --git a/parse.c b/parse.c index 9d1fcba..eae5511 100644 --- a/parse.c +++ b/parse.c @@ -1385,7 +1385,7 @@ ENVELOPE *mutt_read_rfc822_header (FILE * f, HEADER * hdr, short user_hdrs, if (hdr) { hdr->content->hdr_offset = hdr->offset; hdr->content->offset = ftello (f); - rfc2047_decode_envelope (e); + rfc2047_decode_envelope(e); /* check for missing or invalid date */ if (hdr->date_sent <= 0) { debug_print (1, ("no date found, using received "