From fe3aa7772afccc7bc4f791c79099050fc72acaa9 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 31 Jan 2010 23:55:22 +0200 Subject: [PATCH] * mu_msg_gmime: refactor part_to_string; set function line number check to < 40 --- Makefile.am | 6 +++--- src/mu-msg-gmime.c | 54 ++++++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Makefile.am b/Makefile.am index e2968260..6087e246 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,11 +34,11 @@ tags: cc10: @pmccabe `find -name '*.c' -o -name '*.cc'` | sort -nr | awk '($$1 > 10)' -# this warns about functions that are over 30 non-comment lines long, which is a +# this warns about functions that are over 40 non-comment lines long, which is a # sign that they need some refactoring. requires the pmccabe tool. if # all is fine, it outputs nothing -line30: - @pmccabe -c `find -name '*.c' -o -name '*.cc'` | sort -nr | awk '($$5 > 30)' +line40: + @pmccabe -c `find -name '*.c' -o -name '*.cc'` | awk '($$5 > 40)' diff --git a/src/mu-msg-gmime.c b/src/mu-msg-gmime.c index b7a888cc..bd2bc5f8 100644 --- a/src/mu-msg-gmime.c +++ b/src/mu-msg-gmime.c @@ -577,13 +577,40 @@ convert_to_utf8 (GMimePart *part, char *buffer) } -static char* +static gchar* +stream_to_string (GMimeStream *stream, size_t buflen, gboolean convert_utf8) +{ + char *buffer; + ssize_t bytes; + + buffer = (char*)malloc(buflen + 1); + if (!buffer) { + g_warning ("%s: failed to allocate %u bytes", __FUNCTION__, + buflen); + return NULL; + } + g_mime_stream_reset (stream); + + /* we read everything in one go */ + bytes = g_mime_stream_read (stream, buffer, buflen); + if (bytes < 0) { + g_warning ("%s: failed to read from stream", __FUNCTION__); + free (buffer); + return NULL; + } + + buffer[bytes]='\0'; + + return buffer; +} + + +static gchar* part_to_string (GMimePart *part, gboolean convert_utf8) { GMimeDataWrapper *wrapper; GMimeStream *stream = NULL; - - ssize_t buflen, bytes; + ssize_t buflen; char *buffer = NULL; g_return_val_if_fail (GMIME_IS_OBJECT(part), NULL); @@ -601,33 +628,18 @@ part_to_string (GMimePart *part, gboolean convert_utf8) } buflen = g_mime_data_wrapper_write_to_stream (wrapper, stream); - if (buflen == 0) /* empty buffer */ + if (buflen <= 0) /* empty buffer */ goto cleanup; - buffer = (char*)malloc(buflen + 1); - if (!buffer) { - g_warning ("failed to allocate %d bytes", (int)buflen); - goto cleanup; - } - g_mime_stream_reset (stream); + buffer = stream_to_string (stream, (size_t)buflen, convert_utf8); - /* we read everything in one go */ - bytes = g_mime_stream_read (stream, buffer, buflen); - if (bytes < 0) { - free (buffer); - buffer = NULL; - } else - buffer[bytes]='\0'; - /* convert_to_utf8 will free the old 'buffer' if needed */ - if (buffer && convert_utf8) + if (convert_utf8) buffer = convert_to_utf8 (part, buffer); cleanup: if (stream) g_object_unref (G_OBJECT(stream)); - /* if (wrapper) */ - /* g_object_unref (G_OBJECT(wrapper)); */ return buffer; }