[PATCH 2/3] notmuch-output.[ch]: initial implementation of output selection argument handling.

david at tethera.net david at tethera.net
Sat Apr 24 04:44:37 PDT 2010


From: David Bremner <bremner at unb.ca>

These two files provide a more or less "object oriented" approach to
parsing output selection arguments.  The use of a struct to track
which output pieces are selected is intended to hide the
implementation, which currently uses bitmasks.
---
 Makefile.local   |    1 +
 notmuch-output.c |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 notmuch-output.h |   49 ++++++++++++++++++++++++++++
 3 files changed, 145 insertions(+), 0 deletions(-)
 create mode 100644 notmuch-output.c
 create mode 100644 notmuch-output.h

diff --git a/Makefile.local b/Makefile.local
index 5bb570b..eb78679 100644
--- a/Makefile.local
+++ b/Makefile.local
@@ -240,6 +240,7 @@ notmuch_client_srcs =		\
 	notmuch-count.c		\
 	notmuch-dump.c		\
 	notmuch-new.c		\
+	notmuch-output.c	\
 	notmuch-reply.c		\
 	notmuch-restore.c	\
 	notmuch-search.c	\
diff --git a/notmuch-output.c b/notmuch-output.c
new file mode 100644
index 0000000..a84bbe1
--- /dev/null
+++ b/notmuch-output.c
@@ -0,0 +1,95 @@
+/* notmuch-output.h --- encapsulate handling of output selection.
+ *
+ * Copyright 2010 © David Bremner
+ *
+ * This file is part of Notmuch.
+ *
+ * Notmuch is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * Notmuch is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Notmuch.  If not, see <http:www.gnu.org/licenses/>.
+ *
+ * Authors: David Bremner <david at tethera.net>
+ */
+
+#include "notmuch-client.h"
+#include "notmuch-output.h"
+
+notmuch_output_options_t *
+output_init(void *ctx){
+    notmuch_output_options_t *output_opts;
+
+    output_opts = talloc (ctx, notmuch_output_options_t);
+    output_opts->output_all=TRUE;
+    output_opts->mask=0;
+    return output_opts;
+}
+
+int
+output_parse_arg(notmuch_output_options_t *output_opts, const char *arg){
+
+    char *opt;
+
+    struct { const char *name; notmuch_output_t key; } parse_try[]={
+      {"bcc", NOTMUCH_OUTPUT_BCC},
+      {"body", NOTMUCH_OUTPUT_BODY},
+      {"cc", NOTMUCH_OUTPUT_CC},
+      {"date",NOTMUCH_OUTPUT_DATE},
+      {"from",NOTMUCH_OUTPUT_FROM},
+      {"message-id",NOTMUCH_OUTPUT_MESSAGE_ID},
+      {"subject",NOTMUCH_OUTPUT_SUBJECT},
+      {"to", NOTMUCH_OUTPUT_TO},
+    };
+
+    opt = strchr(arg, '=');
+    if (!opt)
+	return 1;
+
+    opt++;
+
+    output_opts->output_all=FALSE;
+
+    while(opt) {
+	unsigned int i;
+	notmuch_bool_t found = FALSE;
+	notmuch_output_t key = 0;
+
+	for (i = 0; i<ARRAY_SIZE(parse_try) ; i++){
+	    if (strncmp(opt, parse_try[i].name,
+			strlen(parse_try[i].name)) == 0){
+		key = parse_try[i].key;
+		found = TRUE;
+	    }
+	}
+
+	if (!found) {
+	    fprintf (stderr, "Unrecognized output selection: %s\n", opt);
+	    return 1;
+	}
+
+	output_opts->mask |= (1<<key);
+
+	opt = strchr(opt, ',');
+	if (opt) opt++;
+    }
+
+    return 0;
+}
+
+notmuch_bool_t
+output_get_flag(notmuch_output_options_t *opts,
+		notmuch_output_t flag){
+
+    if (opts->output_all)
+	return 1;
+
+    return (opts->mask & (1<<flag));
+}
diff --git a/notmuch-output.h b/notmuch-output.h
new file mode 100644
index 0000000..1216499
--- /dev/null
+++ b/notmuch-output.h
@@ -0,0 +1,49 @@
+/* notmuch-output.h --- encapsulate handling of output selection.
+ *
+ * Copyright 2010 © David Bremner
+ *
+ * This file is part of Notmuch.
+ *
+ * Notmuch is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * Notmuch is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Notmuch.  If not, see <http:www.gnu.org/licenses/>.
+ *
+ * Authors: David Bremner <david at tethera.net>
+ */
+
+#ifndef NOTMUCH_OUTPUT_H
+#define NOTMUCH_OUTPUT_H
+
+typedef struct notmuch_output_struct {
+    notmuch_bool_t output_all;
+    int mask;
+} notmuch_output_options_t;
+
+typedef enum notmuch_output_enum {
+    NOTMUCH_OUTPUT_BCC,
+    NOTMUCH_OUTPUT_BODY,
+    NOTMUCH_OUTPUT_CC,
+    NOTMUCH_OUTPUT_DATE,
+    NOTMUCH_OUTPUT_FROM,
+    NOTMUCH_OUTPUT_MESSAGE_ID,
+    NOTMUCH_OUTPUT_SUBJECT,
+    NOTMUCH_OUTPUT_TO
+} notmuch_output_t;
+
+notmuch_output_options_t *output_init(void *ctx);
+
+int output_parse_arg(notmuch_output_options_t *opts,
+		     const char* arg);
+
+notmuch_bool_t output_get_flag(notmuch_output_options_t *opts,
+			       notmuch_output_t flag);
+#endif
-- 
1.7.0



More information about the notmuch mailing list