[gmime: 26/27] Ported uuencode/uudecode examples to GLib's GOptionContext API



commit 0e29bb597fc109d3dc4b1a03d9af13ab229159e7
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Mon Nov 20 17:42:36 2017 -0500

    Ported uuencode/uudecode examples to GLib's GOptionContext API

 examples/Makefile.am |   12 +-
 examples/getopt.c    | 1050 --------------------------------------------------
 examples/getopt.h    |  133 -------
 examples/getopt1.c   |  190 ---------
 examples/uudecode.c  |   87 ++---
 examples/uuencode.c  |   91 ++---
 6 files changed, 70 insertions(+), 1493 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index f74c3cb..0076782 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -17,12 +17,6 @@ LDADDS =                                     \
        $(top_builddir)/gmime/libgmime-$(GMIME_API_VERSION).la  \
        $(GLIB_LIBS)
 
-if SYSTEM_GETOPT
-GETOPT_SOURCES =
-else
-GETOPT_SOURCES = getopt.c getopt.h getopt1.c
-endif
-
 basic_example_SOURCES = basic-example.c
 basic_example_LDFLAGS = 
 basic_example_DEPENDENCIES = $(DEPS)
@@ -33,17 +27,17 @@ imap_example_LDFLAGS =
 imap_example_DEPENDENCIES = $(DEPS)
 imap_example_LDADD = $(LDADDS)
 
-uuencode_SOURCES = $(GETOPT_SOURCES) uuencode.c
+uuencode_SOURCES = uuencode.c
 uuencode_LDFLAGS = 
 uuencode_DEPENDENCIES = $(DEPS)
 uuencode_LDADD = $(LDADDS)
 
-uudecode_SOURCES = $(GETOPT_SOURCES) uudecode.c
+uudecode_SOURCES = uudecode.c
 uudecode_LDFLAGS = 
 uudecode_DEPENDENCIES = $(DEPS)
 uudecode_LDADD = $(LDADDS)
 
-msgcheck_SOURCES = $(GETOPT_SOURCES) msgcheck.c
+msgcheck_SOURCES = msgcheck.c
 msgcheck_LDFLAGS = 
 msgcheck_DEPENDENCIES = $(DEPS)
 msgcheck_LDADD = $(LDADDS)
diff --git a/examples/uudecode.c b/examples/uudecode.c
index bfc1cb6..2082c98 100644
--- a/examples/uudecode.c
+++ b/examples/uudecode.c
@@ -33,40 +33,17 @@
 #include <fcntl.h>
 #include <errno.h>
 
-#ifdef HAVE_GETOPT_H
-#define _GNU_SOURCE
-#include <getopt.h>
-#else
-#include "getopt.h"
-#endif
-
-
 #define DEFAULT_FILENAME "-"
 
+static gboolean version;
+static const char *outfile;
 
-static struct option longopts[] = {
-       { "help",        no_argument,       NULL, 'h' },
-       { "version",     no_argument,       NULL, 'v' },
-       { "output-file", required_argument, NULL, 'o' },
-       { NULL,          no_argument,       NULL,  0  }
+static GOptionEntry options[] = {
+       { "version",     'v', 0, G_OPTION_ARG_NONE,     &version, "display version and exit", NULL   },
+       { "output-file", 'o', 0, G_OPTION_ARG_FILENAME, &outfile, "output to FILE",           "FILE" },
+       { NULL, }
 };
 
-static void
-usage (const char *progname)
-{
-       printf ("Usage: %s [options] [ file ]...\n\n", progname);
-       printf ("Options:\n");
-       printf ("  -h, --help               display help and exit\n");
-       printf ("  -v, --version            display version and exit\n");
-       printf ("  -o, --output-file=FILE   output to FILE\n");
-}
-
-static void
-version (const char *progname)
-{
-       printf ("%s - GMime %s\n", progname, GMIME_VERSION);
-}
-
 static FILE *
 uufopen (const char *filename, const char *rw, int flags, mode_t mode)
 {
@@ -90,44 +67,44 @@ static int
 uudecode (const char *progname, int argc, char **argv)
 {
        gboolean midline = FALSE, base64 = FALSE;
-       int opt, longindex = 0, state = 0;
+       GOptionContext *context;
        register const char *p;
        char inbuf[4096], *q;
        GString *str = NULL;
+       GError *err = NULL;
        const char *infile;
        char outbuf[4100];
        FILE *fin, *fout;
        guint32 save = 0;
        Decoder decode;
-       char *outfile;
+       int state = 0;
        mode_t mode;
        size_t n;
+       int i;
        
-       outfile = NULL;
-       while ((opt = getopt_long (argc, argv, "hvo:", longopts, &longindex)) != -1) {
-               switch (opt) {
-               case 'h':
-                       usage (progname);
-                       return 0;
-                       break;
-               case 'v':
-                       version (progname);
-                       return 0;
-                       break;
-               case 'o':
-                       if (!(outfile = optarg)) {
-                               usage (progname);
-                               return -1;
-                       }
-                       break;
-               default:
-                       printf ("Try `%s --help' for more information.\n", progname);
-                       return -1;
-               }
+       context = g_option_context_new ("[FILE]...");
+       g_option_context_add_main_entries (context, options, progname);
+       g_option_context_set_help_enabled (context, TRUE);
+       
+       if (!g_option_context_parse (context, &argc, &argv, &err)) {
+               printf ("Try `%s --help' for more information.\n", progname);
+               g_option_context_free (context);
+               return -1;
+       }
+       
+       g_option_context_free (context);
+       
+       if (version) {
+               printf ("%s - GMime %s\n", progname, GMIME_VERSION);
+               return 0;
+       }
+       
+       if (argc == 1) {
+               printf ("Try `%s --help' for more information.\n", progname);
+               return -1;
        }
        
-       optarg = outfile;
-       infile = optind < argc ? argv[optind] : DEFAULT_FILENAME;
+       infile = argc > 1 ? argv[1] : DEFAULT_FILENAME;
        
        do {
                if ((fin = uufopen (infile, "rt", O_RDONLY, 0)) == NULL) {
@@ -192,7 +169,7 @@ uudecode (const char *progname, int argc, char **argv)
                                goto nexti;
                }
                
-               if (!outfile || outfile != optarg)
+               if (!outfile)
                        outfile = g_strchomp (str->str);
                
                if (!(fout = fopen (outfile, "wb"))) {
diff --git a/examples/uuencode.c b/examples/uuencode.c
index 9839908..a2510d0 100644
--- a/examples/uuencode.c
+++ b/examples/uuencode.c
@@ -63,80 +63,59 @@
 #include <fcntl.h>
 #include <errno.h>
 
-#ifdef HAVE_GETOPT_H
-#define _GNU_SOURCE
-#include <getopt.h>
-#else
-#include "getopt.h"
-#endif
-
+static gboolean version;
+static gboolean base64;
 
-static struct option longopts[] = {
-       { "help",        no_argument,       NULL, 'h' },
-       { "version",     no_argument,       NULL, 'v' },
-       { "base64",      no_argument,       NULL, 'm' },
-       { NULL,          no_argument,       NULL,  0  }
+static GOptionEntry options[] = {
+       { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "display version and exit",    NULL },
+       { "base64",  'm', 0, G_OPTION_ARG_NONE, &base64,  "use RFC1521 base64 encoding", NULL },
+       { NULL, }
 };
 
-
-static void
-usage (const char *progname)
-{
-       printf ("Usage: %s [options] [ file ] name\n\n", progname);
-       printf ("Options:\n");
-       printf ("  -h, --help               display help and exit\n");
-       printf ("  -v, --version            display version and exit\n");
-       printf ("  -m, --base64             use RFC1521 base64 encoding\n");
-}
-
-static void
-version (const char *progname)
-{
-       printf ("%s - GMime %s\n", progname, GMIME_VERSION);
-}
-
 static int
 uuencode (const char *progname, int argc, char **argv)
 {
+       GMimeContentEncoding encoding = GMIME_CONTENT_ENCODING_UUENCODE;
        GMimeStream *istream, *ostream, *fstream;
-       GMimeContentEncoding encoding;
        const char *filename, *name;
+       GOptionContext *context;
        GMimeFilter *filter;
-       gboolean base64;
+       GError *err = NULL;
        struct stat st;
-       int fd, opt;
-       
-       base64 = FALSE;
-       encoding = GMIME_CONTENT_ENCODING_UUENCODE;
-       while ((opt = getopt_long (argc, argv, "hvm", longopts, NULL)) != -1) {
-               switch (opt) {
-               case 'h':
-                       usage (progname);
-                       return 0;
-               case 'v':
-                       version (progname);
-                       return 0;
-               case 'm':
-                       base64 = TRUE;
-                       encoding = GMIME_CONTENT_ENCODING_BASE64;
-                       break;
-               default:
-                       printf ("Try `%s --help' for more information.\n", progname);
-                       return -1;
-               }
+       int index = 1;
+       int fd;
+       
+       context = g_option_context_new ("[FILE] name");
+       g_option_context_add_main_entries (context, options, progname);
+       g_option_context_set_help_enabled (context, TRUE);
+       
+       if (!g_option_context_parse (context, &argc, &argv, &err)) {
+               printf ("Try `%s --help' for more information.\n", progname);
+               g_option_context_free (context);
+               return -1;
        }
        
-       if (optind >= argc) {
+       g_option_context_free (context);
+       
+       if (version) {
+               printf ("%s - GMime %s\n", progname, GMIME_VERSION);
+               return 0;
+       }
+       
+       if (base64)
+               encoding = GMIME_CONTENT_ENCODING_BASE64;
+       
+       if (argc == 0) {
                printf ("Try `%s --help' for more information.\n", progname);
                return -1;
        }
        
-       if (optind + 1 < argc)
-               filename = argv[optind++];
+       if (argc > 2)
+               filename = argv[index++];
        else
                filename = NULL;
        
-       name = argv[optind];
+       name = argv[index];
        
        /* open our input file... */
        if ((fd = filename ? open (filename, O_RDONLY, 0) : dup (0)) == -1) {
@@ -161,8 +140,8 @@ uuencode (const char *progname, int argc, char **argv)
        istream = g_mime_stream_fs_new (fd);
        
        /* open our output stream */
-       ostream = g_mime_stream_fs_new (1);
-       g_mime_stream_fs_set_owner ((GMimeStreamFs *) ostream, FALSE);
+       ostream = g_mime_stream_pipe_new (1);
+       g_mime_stream_pipe_set_owner ((GMimeStreamPipe *) ostream, FALSE);
        
        fstream = g_mime_stream_filter_new (ostream);
        


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