Annotation of win32/gnome/gmime-x.x.x/test-parser.c, revision 1.1

1.1     ! paf         1: #include "config.h"
        !             2: #include <stdio.h>
        !             3: #include <stdlib.h>
        !             4: #ifdef HAVE_UNISTD_H
        !             5: #include <unistd.h>
        !             6: #endif
        !             7: #include <sys/types.h>
        !             8: #include <sys/stat.h>
        !             9: #include <fcntl.h>
        !            10: #include <glib.h>
        !            11: #include <time.h>
        !            12: 
        !            13: #include "gmime.h"
        !            14: 
        !            15: //#define ENABLE_ZENTIMER
        !            16: #ifdef ENABLE_ZENTIMER
        !            17: #include "zentimer.h"
        !            18: #else
        !            19: #define ZenTimerStart()
        !            20: #define ZenTimerStop()
        !            21: #define ZenTimerReport(x)
        !            22: #endif
        !            23: 
        !            24: #define TEST_PRESERVE_HEADERS
        !            25: #define TEST_GET_BODY
        !            26: #define PRINT_MIME_STRUCT
        !            27: 
        !            28: void
        !            29: print_depth (int depth)
        !            30: {
        !            31:        int i;
        !            32:        
        !            33:        for (i = 0; i < depth; i++)
        !            34:                fprintf (stdout, "   ");
        !            35: }
        !            36: 
        !            37: void
        !            38: print_mime_struct (GMimePart *part, int depth)
        !            39: {
        !            40:        const GMimeContentType *type;
        !            41:        
        !            42:        print_depth (depth);
        !            43:        type = g_mime_part_get_content_type (part);
        !            44:        fprintf (stdout, "Content-Type: %s/%s\n", type->type, type->subtype);
        !            45:        
        !            46:        if (g_mime_content_type_is_type (type, "multipart", "*")) {
        !            47:                GList *child;
        !            48:                
        !            49:                child = part->children;
        !            50:                while (child) {
        !            51:                        print_mime_struct (child->data, depth + 1);
        !            52:                        child = child->next;
        !            53:                }
        !            54:        }
        !            55: }
        !            56: 
        !            57: void
        !            58: test_parser (GMimeStream *stream)
        !            59: {
        !            60:        GMimeMessage *message;
        !            61:        gboolean is_html;
        !            62:        char *text;
        !            63:        
        !            64:        fprintf (stdout, "\nTesting MIME parser...\n\n");
        !            65:        
        !            66:        ZenTimerStart();
        !            67:        message = g_mime_parser_construct_message (stream);
        !            68:        ZenTimerStop();
        !            69:        ZenTimerReport ("gmime::parser_construct_message");
        !            70:        
        !            71:        ZenTimerStart();
        !            72:        text = g_mime_message_to_string (message);
        !            73:        ZenTimerStop();
        !            74:        ZenTimerReport ("gmime::message_to_string");
        !            75:        /*fprintf (stdout, "Result should match previous MIME message dump\n\n%s\n", text);*/
        !            76:        g_free (text);
        !            77:        
        !            78: #ifdef TEST_PRESERVE_HEADERS
        !            79:        {
        !            80:                GMimeStream *stream;
        !            81:                
        !            82:                fprintf (stdout, "\nTesting preservation of headers...\n\n");
        !            83:                stream = g_mime_stream_file_new (stdout);
        !            84:                g_mime_header_write_to_stream (message->header->headers, stream);
        !            85:                g_mime_stream_flush (stream);
        !            86:                GMIME_STREAM_FILE (stream)->fp = NULL;
        !            87:                g_mime_stream_unref (stream);
        !            88:                fprintf (stdout, "\n");
        !            89:        }
        !            90: #endif
        !            91:        
        !            92: #ifdef TEST_GET_BODY
        !            93:        {
        !            94:                /* test of get_body */
        !            95:                char *body;
        !            96:                
        !            97:                body = g_mime_message_get_body (message, FALSE, &is_html);
        !            98:                fprintf (stdout, "Testing get_body (looking for html...%s)\n\n%s\n\n",
        !            99:                         body && is_html ? "found" : "not found",
        !           100:                         body ? body : "No message body found");
        !           101:                
        !           102:                g_free (body);
        !           103:        }
        !           104: #endif
        !           105:        
        !           106: #ifdef PRINT_MIME_STRUCT
        !           107:        /* print mime structure */
        !           108:        print_mime_struct (message->mime_part, 0);
        !           109: #endif
        !           110:        
        !           111:        g_mime_object_unref (GMIME_OBJECT (message));
        !           112: }
        !           113: 
        !           114: 
        !           115: 
        !           116: /* you can only enable one of these at a time... */
        !           117: /*#define STREAM_BUFFER*/
        !           118: /*#define STREAM_MEM*/
        !           119: /*#define STREAM_MMAP*/
        !           120: #define CRLF_FILTER
        !           121: 
        !           122: int main (int argc, char **argv)
        !           123: {
        !           124:        char *filename = NULL;
        !           125:        GMimeStream *stream, *istream;
        !           126:        GMimeFilter *filter;
        !           127:        int fd;
        !           128:        
        !           129:        if (argc > 1)
        !           130:                filename = argv[1];
        !           131:        else
        !           132:                return 0;
        !           133:        
        !           134:        fd = open (filename, O_RDONLY);
        !           135:        if (fd == -1)
        !           136:                return 0;
        !           137:        
        !           138:        g_mime_init (GMIME_INIT_FLAG_UTF8);
        !           139:        
        !           140: #ifdef STREAM_MMAP
        !           141:        stream = g_mime_stream_mmap_new (fd, PROT_READ, MAP_PRIVATE);
        !           142:        g_assert (stream != NULL);
        !           143: #else
        !           144:        stream = g_mime_stream_fs_new (fd);
        !           145: #endif /* STREAM_MMAP */
        !           146:        
        !           147: #ifdef STREAM_MEM
        !           148:        istream = g_mime_stream_mem_new ();
        !           149:        g_mime_stream_write_to_stream (stream, istream);
        !           150:        g_mime_stream_reset (istream);
        !           151:        g_mime_stream_unref (stream);
        !           152:        stream = istream;
        !           153: #endif
        !           154:        
        !           155: #ifdef STREAM_BUFFER
        !           156:        istream = g_mime_stream_buffer_new (stream,
        !           157:                                            GMIME_STREAM_BUFFER_BLOCK_READ);
        !           158:        g_mime_stream_unref (stream);
        !           159:        stream = istream;
        !           160: #endif
        !           161:        
        !           162: #ifdef CRLF_FILTER
        !           163:        istream = g_mime_stream_filter_new_with_stream (stream);
        !           164:        filter = g_mime_filter_crlf_new (GMIME_FILTER_CRLF_DECODE, GMIME_FILTER_CRLF_MODE_CRLF_ONLY);
        !           165:        g_mime_stream_filter_add (GMIME_STREAM_FILTER (istream), filter);
        !           166:        g_mime_stream_unref (stream);
        !           167:        stream = istream;
        !           168: #endif
        !           169:        
        !           170:        test_parser (stream);
        !           171:        
        !           172:        g_mime_stream_unref (stream);
        !           173:        
        !           174:        return 0;
        !           175: }

E-mail: