From dd85a7374675c3f91e215c2e318b2c5045a01f53 Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <lfleischer@calcurse.org>
Date: Mon, 11 Jan 2016 20:24:06 +0100
Subject: Add long format specifiers "raw" and "hash"

Add new format specifiers to print the raw item representation or an
object's hash value.

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
---
 src/apoint.c   | 11 +++++++++++
 src/calcurse.h |  5 +++++
 src/event.c    | 11 +++++++++++
 src/recur.c    | 21 ++++++++++++++++++++
 src/todo.c     | 11 +++++++++++
 src/utils.c    | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 6 files changed, 114 insertions(+), 5 deletions(-)

(limited to 'src')

diff --git a/src/apoint.c b/src/apoint.c
index 263db9d..23c3163 100644
--- a/src/apoint.c
+++ b/src/apoint.c
@@ -40,6 +40,7 @@
 #include <time.h>
 
 #include "calcurse.h"
+#include "sha1.h"
 
 llist_ts_t alist_p;
 
@@ -167,6 +168,16 @@ char *apoint_tostr(struct apoint *o)
 	return string_buf(&s);
 }
 
+char *apoint_hash(struct apoint *apt)
+{
+	char *raw = apoint_tostr(apt);
+	char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1);
+	sha1_digest(raw, sha1);
+	mem_free(raw);
+
+	return sha1;
+}
+
 void apoint_write(struct apoint *o, FILE * f)
 {
 	char *str = apoint_tostr(o);
diff --git a/src/calcurse.h b/src/calcurse.h
index 0292cd6..5724265 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -684,6 +684,7 @@ struct apoint *apoint_new(char *, char *, long, long, char);
 unsigned apoint_inday(struct apoint *, long *);
 void apoint_sec2str(struct apoint *, long, char *, char *);
 char *apoint_tostr(struct apoint *);
+char *apoint_hash(struct apoint *);
 void apoint_write(struct apoint *, FILE *);
 struct apoint *apoint_scan(FILE *, struct tm, struct tm, char, char *,
 			   struct item_filter *);
@@ -778,6 +779,7 @@ void event_llist_free(void);
 struct event *event_new(char *, char *, long, int);
 unsigned event_inday(struct event *, long *);
 char *event_tostr(struct event *);
+char *event_hash(struct event *);
 void event_write(struct event *, FILE *);
 struct event *event_scan(FILE *, struct tm, int, char *, struct item_filter *);
 void event_delete(struct event *);
@@ -965,8 +967,10 @@ struct recur_event *recur_event_scan(FILE *, struct tm, int, char,
 				     int, struct tm, char *, llist_t *,
 				     struct item_filter *);
 char *recur_apoint_tostr(struct recur_apoint *);
+char *recur_apoint_hash(struct recur_apoint *);
 void recur_apoint_write(struct recur_apoint *, FILE *);
 char *recur_event_tostr(struct recur_event *);
+char *recur_event_hash(struct recur_event *);
 void recur_event_write(struct recur_event *, FILE *);
 void recur_save_data(FILE *);
 unsigned recur_item_find_occurrence(long, long, llist_t *, int,
@@ -1007,6 +1011,7 @@ extern llist_t todolist;
 struct todo *todo_get_item(int);
 struct todo *todo_add(char *, int, char *);
 char *todo_tostr(struct todo *);
+char *todo_hash(struct todo *);
 void todo_write(struct todo *, FILE *);
 void todo_delete_note(struct todo *);
 void todo_delete(struct todo *);
diff --git a/src/event.c b/src/event.c
index 39224a3..1e9d940 100644
--- a/src/event.c
+++ b/src/event.c
@@ -40,6 +40,7 @@
 #include <time.h>
 
 #include "calcurse.h"
+#include "sha1.h"
 
 llist_t eventlist;
 
@@ -123,6 +124,16 @@ char *event_tostr(struct event *o)
 	return string_buf(&s);
 }
 
+char *event_hash(struct event *ev)
+{
+	char *raw = event_tostr(ev);
+	char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1);
+	sha1_digest(raw, sha1);
+	mem_free(raw);
+
+	return sha1;
+}
+
 void event_write(struct event *o, FILE * f)
 {
 	char *str = event_tostr(o);
diff --git a/src/recur.c b/src/recur.c
index 5946217..ddfddb4 100644
--- a/src/recur.c
+++ b/src/recur.c
@@ -41,6 +41,7 @@
 #include <time.h>
 
 #include "calcurse.h"
+#include "sha1.h"
 
 llist_ts_t recur_alist_p;
 llist_t recur_elist;
@@ -498,6 +499,16 @@ char *recur_apoint_tostr(struct recur_apoint *o)
 	return string_buf(&s);
 }
 
+char *recur_apoint_hash(struct recur_apoint *rapt)
+{
+	char *raw = recur_apoint_tostr(rapt);
+	char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1);
+	sha1_digest(raw, sha1);
+	mem_free(raw);
+
+	return sha1;
+}
+
 void recur_apoint_write(struct recur_apoint *o, FILE * f)
 {
 	char *str = recur_apoint_tostr(o);
@@ -545,6 +556,16 @@ char *recur_event_tostr(struct recur_event *o)
 	return string_buf(&s);
 }
 
+char *recur_event_hash(struct recur_event *rev)
+{
+	char *raw = recur_event_tostr(rev);
+	char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1);
+	sha1_digest(raw, sha1);
+	mem_free(raw);
+
+	return sha1;
+}
+
 void recur_event_write(struct recur_event *o, FILE * f)
 {
 	char *str = recur_event_tostr(o);
diff --git a/src/todo.c b/src/todo.c
index 152fbaa..9869a1e 100644
--- a/src/todo.c
+++ b/src/todo.c
@@ -39,6 +39,7 @@
 #include <unistd.h>
 
 #include "calcurse.h"
+#include "sha1.h"
 
 llist_t todolist;
 
@@ -91,6 +92,16 @@ char *todo_tostr(struct todo *todo)
 	return res;
 }
 
+char *todo_hash(struct todo *todo)
+{
+	char *raw = todo_tostr(todo);
+	char *sha1 = mem_malloc(SHA1_DIGESTLEN * 2 + 1);
+	sha1_digest(raw, sha1);
+	mem_free(raw);
+
+	return sha1;
+}
+
 void todo_write(struct todo *todo, FILE * f)
 {
 	char *str = todo_tostr(todo);
diff --git a/src/utils.c b/src/utils.c
index 14446fc..58b1d44 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -48,6 +48,7 @@
 #include <termios.h>
 
 #include "calcurse.h"
+#include "sha1.h"
 
 #define ISLEAP(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
 
@@ -62,6 +63,8 @@ enum format_specifier {
 	FS_NOTE,
 	FS_NOTEFILE,
 	FS_PRIORITY,
+	FS_RAW,
+	FS_HASH,
 	FS_PSIGN,
 	FS_EOF,
 	FS_UNKNOWN
@@ -1293,6 +1296,10 @@ static enum format_specifier parse_fs(const char **s, char *extformat)
 			return FS_NOTEFILE;
 		else if (!strcmp(buf, "priority"))
 			return FS_PRIORITY;
+		else if (!strcmp(buf, "raw"))
+			return FS_RAW;
+		else if (!strcmp(buf, "hash"))
+			return FS_HASH;
 		else
 			return FS_UNKNOWN;
 	case '%':
@@ -1405,12 +1412,12 @@ static void print_datediff(long difference, const char *extformat)
 }
 
 /* Print a formatted appointment to stdout. */
-void print_apoint(const char *format, long day, struct apoint *apt)
+static void print_apoint_helper(const char *format, long day,
+				struct apoint *apt, struct recur_apoint *rapt)
 {
 	const char *p;
 	char extformat[FS_EXT_MAXLEN];
 
-
 	for (p = format; *p; p++) {
 		if (*p == '%') {
 			p++;
@@ -1442,6 +1449,18 @@ void print_apoint(const char *format, long day, struct apoint *apt)
 			case FS_NOTEFILE:
 				print_notefile(stdout, apt->note, 1);
 				break;
+			case FS_RAW:
+				if (rapt)
+					recur_apoint_write(rapt, stdout);
+				else
+					apoint_write(apt, stdout);
+				break;
+			case FS_HASH:
+				if (rapt)
+					printf("%s", recur_apoint_hash(rapt));
+				else
+					printf("%s", apoint_hash(apt));
+				break;
 			case FS_PSIGN:
 				putchar('%');
 				break;
@@ -1461,7 +1480,8 @@ void print_apoint(const char *format, long day, struct apoint *apt)
 }
 
 /* Print a formatted event to stdout. */
-void print_event(const char *format, long day, struct event *ev)
+static void print_event_helper(const char *format, long day, struct event *ev,
+			       struct recur_event *rev)
 {
 	const char *p;
 	char extformat[FS_EXT_MAXLEN];
@@ -1482,6 +1502,18 @@ void print_event(const char *format, long day, struct event *ev)
 			case FS_PSIGN:
 				putchar('%');
 				break;
+			case FS_RAW:
+				if (rev)
+					recur_event_write(rev, stdout);
+				else
+					event_write(ev, stdout);
+				break;
+			case FS_HASH:
+				if (rev)
+					printf("%s", recur_event_tostr(rev));
+				else
+					printf("%s", event_tostr(ev));
+				break;
 			case FS_EOF:
 				return;
 				break;
@@ -1497,6 +1529,18 @@ void print_event(const char *format, long day, struct event *ev)
 	}
 }
 
+/* Print a formatted appointment to stdout. */
+void print_apoint(const char *format, long day, struct apoint *apt)
+{
+	print_apoint_helper(format, day, apt, NULL);
+}
+
+/* Print a formatted event to stdout. */
+void print_event(const char *format, long day, struct event *ev)
+{
+	print_event_helper(format, day, ev, NULL);
+}
+
 /* Print a formatted recurrent appointment to stdout. */
 void
 print_recur_apoint(const char *format, long day, time_t occurrence,
@@ -1509,7 +1553,7 @@ print_recur_apoint(const char *format, long day, time_t occurrence,
 	apt.mesg = rapt->mesg;
 	apt.note = rapt->note;
 
-	print_apoint(format, day, &apt);
+	print_apoint_helper(format, day, &apt, rapt);
 }
 
 /* Print a formatted recurrent event to stdout. */
@@ -1521,7 +1565,7 @@ void print_recur_event(const char *format, long day,
 	ev.mesg = rev->mesg;
 	ev.note = rev->note;
 
-	print_event(format, day, &ev);
+	print_event_helper(format, day, &ev, rev);
 }
 
 /* Print a formatted todo item to stdout. */
@@ -1546,6 +1590,12 @@ void print_todo(const char *format, struct todo *todo)
 			case FS_NOTEFILE:
 				print_notefile(stdout, todo->note, 1);
 				break;
+			case FS_RAW:
+				todo_write(todo, stdout);
+				break;
+			case FS_HASH:
+				printf("%s", todo_hash(todo));
+				break;
 			case FS_PSIGN:
 				putchar('%');
 				break;
-- 
cgit v1.2.3-70-g09d2