unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: david@tethera.net
To: notmuch@notmuchmail.org
Cc: David Bremner <bremner@unb.ca>
Subject: [PATCH 2/3] notmuch-output.[ch]: initial implementation of output selection argument handling.
Date: Sat, 24 Apr 2010 08:44:37 -0300	[thread overview]
Message-ID: <1272109478-20686-3-git-send-email-david@tethera.net> (raw)
In-Reply-To: <1272109478-20686-1-git-send-email-david@tethera.net>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 5139 bytes --]

From: David Bremner <bremner@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@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@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

  parent reply	other threads:[~2010-04-24 11:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-24 11:44 Initial support for selecting what is output david
2010-04-24 11:44 ` [PATCH 1/3] remove stale "unused" from around argv and argc in notmuch_show_command david
2010-04-24 11:44 ` david [this message]
2010-04-24 11:44 ` [PATCH 3/3] notmuch-show.c: control which headers are show for json output david

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1272109478-20686-3-git-send-email-david@tethera.net \
    --to=david@tethera.net \
    --cc=bremner@unb.ca \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).