[gmime] Make use of GMimeFormatOptions:newline



commit ca400c4c7ec3ec6363081550173d026f2cc59246
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Sat Mar 18 07:38:56 2017 -0400

    Make use of GMimeFormatOptions:newline

 gmime/gmime-format-options.c |   42 ++++++++++++++++++++++++++++----------
 gmime/gmime-header.c         |   15 ++++++++++++-
 gmime/gmime-internal.h       |    2 +-
 gmime/gmime-message.c        |   10 ++++++--
 gmime/gmime-multipart.c      |   19 +++++++++--------
 gmime/gmime-part.c           |   45 ++++++++++++++++++++++++++---------------
 6 files changed, 90 insertions(+), 43 deletions(-)
---
diff --git a/gmime/gmime-format-options.c b/gmime/gmime-format-options.c
index 39bd8fd..1ad58ea 100644
--- a/gmime/gmime-format-options.c
+++ b/gmime/gmime-format-options.c
@@ -107,17 +107,21 @@ g_mime_format_options_new (void)
 /**
  * _g_mime_format_options_clone:
  * @options: a #GMimeFormatOptions
+ * @hidden: %TRUE if the hidden headers should also be cloned
  *
  * Clones a #GMimeFormatOptions.
  *
  * Returns: a newly allocated #GMimeFormatOptions.
  **/
 GMimeFormatOptions *
-_g_mime_format_options_clone (GMimeFormatOptions *options)
+_g_mime_format_options_clone (GMimeFormatOptions *options, gboolean hidden)
 {
        GMimeFormatOptions *clone;
        guint i;
        
+       if (options == NULL)
+               options = default_options;
+       
        clone = g_slice_new (GMimeFormatOptions);
        clone->method = options->method;
        clone->newline = options->newline;
@@ -126,8 +130,11 @@ _g_mime_format_options_clone (GMimeFormatOptions *options)
        clone->maxline = options->newline;
        
        clone->hidden = g_ptr_array_new ();
-       for (i = 0; i < options->hidden->len; i++)
-               g_ptr_array_add (clone->hidden, g_strdup (options->hidden->pdata[i]));
+       
+       if (hidden) {
+               for (i = 0; i < options->hidden->len; i++)
+                       g_ptr_array_add (clone->hidden, g_strdup (options->hidden->pdata[i]));
+       }
        
        return clone;
 }
@@ -168,7 +175,8 @@ g_mime_format_options_free (GMimeFormatOptions *options)
 GMimeParamEncodingMethod
 g_mime_format_options_get_param_encoding_method (GMimeFormatOptions *options)
 {
-       g_return_val_if_fail (options != NULL, GMIME_PARAM_ENCODING_METHOD_RFC2231);
+       if (options == NULL)
+               options = default_options;
        
        return options->method;
 }
@@ -205,7 +213,8 @@ g_mime_format_options_set_param_encoding_method (GMimeFormatOptions *options, GM
 GMimeNewLineFormat
 g_mime_format_options_get_newline_format (GMimeFormatOptions *options)
 {
-       g_return_val_if_fail (options != NULL, GMIME_NEWLINE_FORMAT_UNIX);
+       if (options == NULL)
+               options = default_options;
        
        return options->newline;
 }
@@ -239,7 +248,10 @@ g_mime_format_options_set_newline_format (GMimeFormatOptions *options, GMimeNewL
 const char *
 g_mime_format_options_get_newline (GMimeFormatOptions *options)
 {
-       if (options != NULL && options->newline == GMIME_NEWLINE_FORMAT_DOS)
+       if (options == NULL)
+               options = default_options;
+       
+       if (options->newline == GMIME_NEWLINE_FORMAT_DOS)
                return "\r\n";
        
        return "\n";
@@ -259,7 +271,10 @@ g_mime_format_options_get_newline (GMimeFormatOptions *options)
 GMimeFilter *
 g_mime_format_options_create_newline_filter (GMimeFormatOptions *options, gboolean ensure_newline)
 {
-       if (options != NULL && options->newline == GMIME_NEWLINE_FORMAT_DOS)
+       if (options == NULL)
+               options = default_options;
+       
+       if (options->newline == GMIME_NEWLINE_FORMAT_DOS)
                return g_mime_filter_crlf_new (TRUE, FALSE);
        
        return g_mime_filter_crlf_new (FALSE, FALSE);
@@ -278,7 +293,8 @@ g_mime_format_options_create_newline_filter (GMimeFormatOptions *options, gboole
 gboolean
 g_mime_format_options_get_allow_mixed_charsets (GMimeFormatOptions *options)
 {
-       g_return_val_if_fail (options != NULL, TRUE);
+       if (options == NULL)
+               options = default_options;
        
        return options->mixed_charsets;
 }
@@ -311,7 +327,8 @@ g_mime_format_options_set_allow_mixed_charsets (GMimeFormatOptions *options, gbo
 gboolean
 g_mime_format_options_get_allow_international (GMimeFormatOptions *options)
 {
-       g_return_val_if_fail (options != NULL, FALSE);
+       if (options == NULL)
+               options = default_options;
        
        return options->international;
 }
@@ -344,7 +361,8 @@ g_mime_format_options_set_allow_international (GMimeFormatOptions *options, gboo
 guint
 g_mime_format_options_get_max_line (GMimeFormatOptions *options)
 {
-       g_return_val_if_fail (options != NULL, 78);
+       if (options == NULL)
+               options = default_options;
        
        return options->maxline;
 }
@@ -380,9 +398,11 @@ g_mime_format_options_is_hidden_header (GMimeFormatOptions *options, const char
 {
        guint i;
        
-       g_return_val_if_fail (options != NULL, FALSE);
        g_return_val_if_fail (header != NULL, FALSE);
        
+       if (options == NULL)
+               options = default_options;
+       
        for (i = 0; i < options->hidden->len; i++) {
                if (!g_ascii_strcasecmp (options->hidden->pdata[i], header))
                        return TRUE;
diff --git a/gmime/gmime-header.c b/gmime/gmime-header.c
index 2e2364a..3e40917 100644
--- a/gmime/gmime-header.c
+++ b/gmime/gmime-header.c
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <ctype.h>
 
+#include "gmime-stream-filter.h"
 #include "gmime-stream-mem.h"
 #include "gmime-internal.h"
 #include "gmime-common.h"
@@ -849,16 +850,23 @@ ssize_t
 g_mime_header_list_write_to_stream (GMimeHeaderList *headers, GMimeFormatOptions *options, GMimeStream 
*stream)
 {
        ssize_t nwritten, total = 0;
+       GMimeStream *filtered;
        GMimeHeader *header;
+       GMimeFilter *filter;
        guint i;
        
        g_return_val_if_fail (GMIME_IS_HEADER_LIST (headers), -1);
        g_return_val_if_fail (GMIME_IS_STREAM (stream), -1);
        
+       filtered = g_mime_stream_filter_new (stream);
+       filter = g_mime_format_options_create_newline_filter (options, FALSE);
+       g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter);
+       g_object_unref (filter);
+       
        for (i = 0; i < headers->array->len; i++) {
                header = (GMimeHeader *) headers->array->pdata[i];
                
-               if (g_mime_format_options_is_hidden_header (options, header->name)) {
+               if (!g_mime_format_options_is_hidden_header (options, header->name)) {
                        if ((nwritten = g_mime_header_write_to_stream (headers, header, options, stream)) == 
-1)
                                return -1;
                        
@@ -866,6 +874,9 @@ g_mime_header_list_write_to_stream (GMimeHeaderList *headers, GMimeFormatOptions
                }
        }
        
+       g_mime_stream_flush (filtered);
+       g_object_unref (filtered);
+       
        return total;
 }
 
@@ -891,7 +902,7 @@ g_mime_header_list_to_string (GMimeHeaderList *headers, GMimeFormatOptions *opti
        
        array = g_byte_array_new ();
        stream = g_mime_stream_mem_new ();
-       g_mime_stream_mem_set_byte_array (GMIME_STREAM_MEM (stream), array);
+       g_mime_stream_mem_set_byte_array ((GMimeStreamMem *) stream, array);
        g_mime_header_list_write_to_stream (headers, options, stream);
        g_object_unref (stream);
        
diff --git a/gmime/gmime-internal.h b/gmime/gmime-internal.h
index 4cb7766..f5a5afe 100644
--- a/gmime/gmime-internal.h
+++ b/gmime/gmime-internal.h
@@ -45,7 +45,7 @@ typedef struct {
 /* GMimeParserOptions */
 G_GNUC_INTERNAL void g_mime_format_options_init (void);
 G_GNUC_INTERNAL void g_mime_format_options_shutdown (void);
-G_GNUC_INTERNAL GMimeFormatOptions *_g_mime_format_options_clone (GMimeFormatOptions *options);
+G_GNUC_INTERNAL GMimeFormatOptions *_g_mime_format_options_clone (GMimeFormatOptions *options, gboolean 
hidden);
 
 /* GMimeParserOptions */
 G_GNUC_INTERNAL void g_mime_parser_options_init (void);
diff --git a/gmime/gmime-message.c b/gmime/gmime-message.c
index 67b03f1..89e0b8d 100644
--- a/gmime/gmime-message.c
+++ b/gmime/gmime-message.c
@@ -905,7 +905,7 @@ write_headers_to_stream (GMimeObject *object, GMimeFormatOptions *options, GMime
                while (index < count) {
                        header = g_mime_header_list_get_header_at (object->headers, index);
                        
-                       if (g_mime_format_options_is_hidden_header (options, header->name)) {
+                       if (!g_mime_format_options_is_hidden_header (options, header->name)) {
                                if ((nwritten = g_mime_header_write_to_stream (object->headers, header, 
options, stream)) == -1)
                                        return -1;
                                
@@ -918,7 +918,7 @@ write_headers_to_stream (GMimeObject *object, GMimeFormatOptions *options, GMime
                while (body_index < body_count) {
                        header = g_mime_header_list_get_header_at (mime_part->headers, body_index);
                        
-                       if (g_mime_format_options_is_hidden_header (options, header->name)) {
+                       if (!g_mime_format_options_is_hidden_header (options, header->name)) {
                                if ((nwritten = g_mime_header_write_to_stream (mime_part->headers, header, 
options, stream)) == -1)
                                        return -1;
                                
@@ -960,6 +960,7 @@ message_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gbool
        GMimeMessage *message = (GMimeMessage *) object;
        GMimeObject *mime_part = message->mime_part;
        ssize_t nwritten, total = 0;
+       const char *newline;
        
        if (!content_only) {
                if ((nwritten = write_headers_to_stream (object, options, stream)) == -1)
@@ -967,7 +968,8 @@ message_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gbool
                
                total += nwritten;
                
-               if ((nwritten = g_mime_stream_write (stream, "\n", 1)) == -1)
+               newline = g_mime_format_options_get_newline (options);
+               if ((nwritten = g_mime_stream_write_string (stream, newline)) == -1)
                        return -1;
                
                total += nwritten;
@@ -976,6 +978,8 @@ message_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gbool
        if (mime_part) {
                GMimeObjectClass *klass = GMIME_OBJECT_GET_CLASS (mime_part);
                
+               options = _g_mime_format_options_clone (options, FALSE);
+               
                if ((nwritten = klass->write_to_stream (mime_part, options, TRUE, stream)) == -1)
                        return -1;
                
diff --git a/gmime/gmime-multipart.c b/gmime/gmime-multipart.c
index 4d533cc..b2c614e 100644
--- a/gmime/gmime-multipart.c
+++ b/gmime/gmime-multipart.c
@@ -151,12 +151,13 @@ static ssize_t
 multipart_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gboolean content_only, 
GMimeStream *stream)
 {
        GMimeMultipart *multipart = (GMimeMultipart *) object;
+       const char *boundary, *newline;
        ssize_t nwritten, total = 0;
-       const char *boundary;
        GMimeObject *part;
        guint i;
        
        boundary = g_mime_object_get_content_type_parameter (object, "boundary");
+       newline = g_mime_format_options_get_newline (options);
        
        if (!content_only) {
                /* write the content headers */
@@ -166,10 +167,10 @@ multipart_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gbo
                total += nwritten;
                
                /* terminate the headers */
-               if (g_mime_stream_write (stream, "\n", 1) == -1)
+               if ((nwritten = g_mime_stream_write_string (stream, newline)) == -1)
                        return -1;
                
-               total++;
+               total += nwritten;
        }
        
        /* write the preface */
@@ -179,17 +180,17 @@ multipart_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gbo
                
                total += nwritten;
                
-               if (g_mime_stream_write (stream, "\n", 1) == -1)
+               if ((nwritten = g_mime_stream_write_string (stream, newline)) == -1)
                        return -1;
                
-               total++;
+               total += nwritten;
        }
        
        for (i = 0; i < multipart->children->len; i++) {
                part = multipart->children->pdata[i];
                
                /* write the boundary */
-               if ((nwritten = g_mime_stream_printf (stream, "--%s\n", boundary)) == -1)
+               if ((nwritten = g_mime_stream_printf (stream, "--%s%s", boundary, newline)) == -1)
                        return -1;
                
                total += nwritten;
@@ -201,16 +202,16 @@ multipart_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gbo
                total += nwritten;
 
                if (!GMIME_IS_MULTIPART (part) || ((GMimeMultipart *) part)->write_end_boundary) {
-                       if (g_mime_stream_write (stream, "\n", 1) == -1)
+                       if ((nwritten = g_mime_stream_write_string (stream, newline)) == -1)
                                return -1;
                        
-                       total++;
+                       total += nwritten;
                }
        }
        
        /* write the end-boundary (but only if a boundary is set) */
        if (multipart->write_end_boundary && boundary) {
-               if ((nwritten = g_mime_stream_printf (stream, "--%s--\n", boundary)) == -1)
+               if ((nwritten = g_mime_stream_printf (stream, "--%s--%s", boundary, newline)) == -1)
                        return -1;
                
                total += nwritten;
diff --git a/gmime/gmime-part.c b/gmime/gmime-part.c
index 5c6bd33..96a6f3c 100644
--- a/gmime/gmime-part.c
+++ b/gmime/gmime-part.c
@@ -299,9 +299,11 @@ mime_part_headers_cleared (GMimeObject *object)
 
 
 static ssize_t
-write_content (GMimePart *part, GMimeStream *stream)
+write_content (GMimePart *part, GMimeFormatOptions *options, GMimeStream *stream)
 {
        ssize_t nwritten, total = 0;
+       GMimeStream *filtered;
+       GMimeFilter *filter;
        
        if (!part->content)
                return 0;
@@ -313,16 +315,17 @@ write_content (GMimePart *part, GMimeStream *stream)
         */
        
        if (part->encoding != g_mime_data_wrapper_get_encoding (part->content)) {
-               GMimeStream *filtered;
+               const char *newline = g_mime_format_options_get_newline (options);
                const char *filename;
-               GMimeFilter *filter;
+               
+               filtered = g_mime_stream_filter_new (stream);
                
                switch (part->encoding) {
                case GMIME_CONTENT_ENCODING_UUENCODE:
-                       filename = g_mime_part_get_filename (part);
-                       nwritten = g_mime_stream_printf (stream, "begin 0644 %s\n",
-                                                        filename ? filename : "unknown");
-                       if (nwritten == -1)
+                       if (!(filename = g_mime_part_get_filename (part)))
+                               filename = "unknown";
+                       
+                       if ((nwritten = g_mime_stream_printf (stream, "begin 0644 %s%s", filename, newline)) 
== -1)
                                return -1;
                        
                        total += nwritten;
@@ -330,17 +333,18 @@ write_content (GMimePart *part, GMimeStream *stream)
                        /* fall thru... */
                case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
                case GMIME_CONTENT_ENCODING_BASE64:
-                       filtered = g_mime_stream_filter_new (stream);
                        filter = g_mime_filter_basic_new (part->encoding, TRUE);
                        g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter);
                        g_object_unref (filter);
                        break;
                default:
-                       g_object_ref (stream);
-                       filtered = stream;
                        break;
                }
                
+               filter = g_mime_format_options_create_newline_filter (options, FALSE);
+               g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter);
+               g_object_unref (filter);
+               
                nwritten = g_mime_data_wrapper_write_to_stream (part->content, filtered);
                g_mime_stream_flush (filtered);
                g_object_unref (filtered);
@@ -351,9 +355,7 @@ write_content (GMimePart *part, GMimeStream *stream)
                total += nwritten;
                
                if (part->encoding == GMIME_CONTENT_ENCODING_UUENCODE) {
-                       /* FIXME: get rid of this special-case x-uuencode crap */
-                       nwritten = g_mime_stream_write (stream, "end\n", 4);
-                       if (nwritten == -1)
+                       if ((nwritten = g_mime_stream_printf (stream, "end%s", newline)) == -1)
                                return -1;
                        
                        total += nwritten;
@@ -363,8 +365,15 @@ write_content (GMimePart *part, GMimeStream *stream)
                
                content = g_mime_data_wrapper_get_stream (part->content);
                g_mime_stream_reset (content);
-               nwritten = g_mime_stream_write_to_stream (content, stream);
+               
+               filtered = g_mime_stream_filter_new (stream);
+               filter = g_mime_format_options_create_newline_filter (options, FALSE);
+               g_mime_stream_filter_add ((GMimeStreamFilter *) filtered, filter);
+               g_object_unref (filter);
+               
+               nwritten = g_mime_stream_write_to_stream (content, filtered);
                g_mime_stream_reset (content);
+               g_object_unref (filtered);
                
                if (nwritten == -1)
                        return -1;
@@ -380,6 +389,7 @@ mime_part_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gbo
 {
        GMimePart *mime_part = (GMimePart *) object;
        ssize_t nwritten, total = 0;
+       const char *newline;
        
        if (!content_only) {
                /* write the content headers */
@@ -389,13 +399,14 @@ mime_part_write_to_stream (GMimeObject *object, GMimeFormatOptions *options, gbo
                total += nwritten;
                
                /* terminate the headers */
-               if (g_mime_stream_write (stream, "\n", 1) == -1)
+               newline = g_mime_format_options_get_newline (options);
+               if ((nwritten = g_mime_stream_write_string (stream, newline)) == -1)
                        return -1;
                
-               total++;
+               total += nwritten;
        }
        
-       if ((nwritten = write_content (mime_part, stream)) == -1)
+       if ((nwritten = write_content (mime_part, options, stream)) == -1)
                return -1;
        
        total += nwritten;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]