about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-07-17 21:51:41 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-07-17 21:51:41 +0200
commit796531b22db24b48192ab4f00a22e1606726fcb6 (patch)
tree16540f3a75683fd3e0b9a2ca24213d09a3196f45
parent66544292ff8d1073872efd9a1ca6fc0325556a96 (diff)
downloadmblaze-796531b22db24b48192ab4f00a22e1606726fcb6.tar.gz
mblaze-796531b22db24b48192ab4f00a22e1606726fcb6.tar.xz
mblaze-796531b22db24b48192ab4f00a22e1606726fcb6.zip
mseq: extract code into seq.c
-rw-r--r--Makefile2
-rw-r--r--blaze822.h6
-rw-r--r--mseq.c25
-rw-r--r--seq.c101
4 files changed, 110 insertions, 24 deletions
diff --git a/Makefile b/Makefile
index e5c5d7c..6760978 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ hdr: blaze822.o hdr.o rfc2047.o
 show: blaze822.o show.o rfc2045.o rfc2047.c
 list: list.o
 unmime: blaze822.o unmime.o rfc2045.o rfc2047.o
-mseq: mseq.o
+mseq: mseq.o seq.o
 
 clean: FRC
 	-rm -f $(ALL) *.o
diff --git a/blaze822.h b/blaze822.h
index 0d3d60b..96588a7 100644
--- a/blaze822.h
+++ b/blaze822.h
@@ -38,3 +38,9 @@ int blaze822_check_mime(struct message *msg);
 int blaze822_mime_body(struct message *msg, char **cto, char **bodyo, size_t *bodyleno, char **bodychunko);
 int blaze822_multipart(struct message *msg, struct message **imsg);
 int blaze822_mime_parameter(char *s, char *name, char **starto, char **stopo);
+
+// seq.c
+
+char *blaze822_seq_open(char *file);
+int blaze822_seq_load(char *map);
+long blaze822_seq_find(char *ref);
diff --git a/mseq.c b/mseq.c
index 7c65c8e..776966d 100644
--- a/mseq.c
+++ b/mseq.c
@@ -10,28 +10,7 @@
 #include <limits.h>
 #include <errno.h>
 
-char *
-seq_open(char *file)
-{
-	int fd;
-	struct stat st;
-
-	// env $SEQ or something
-	if (!file)
-		file = "map";
-	fd = open(file, O_RDONLY);
-	if (!fd)
-		return 0;
-
-	fstat(fd, &st);
-	char *map = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-	close(fd);
-
-	if (map == MAP_FAILED)
-		return 0;
-
-	return map;
-}
+#include "blaze822.h"
 
 static const char *
 readlin(const char *p)
@@ -84,7 +63,7 @@ parse_relnum(char *a, long cur, long last, long *out)
 int
 main(int argc, char *argv[])
 {
-	char *map = seq_open(0);
+	char *map = blaze822_seq_open(0);
 	if (!map)
 		return 1;
 
diff --git a/seq.c b/seq.c
new file mode 100644
index 0000000..26b0328
--- /dev/null
+++ b/seq.c
@@ -0,0 +1,101 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <limits.h>
+#include <errno.h>
+#include <search.h>
+
+#include "blaze822.h"
+#include "blaze822_priv.h"
+
+char *
+blaze822_seq_open(char *file)
+{
+	int fd;
+	struct stat st;
+
+	// env $SEQ or something
+	if (!file)
+		file = "map";
+	fd = open(file, O_RDONLY);
+	if (!fd)
+		return 0;
+
+	fstat(fd, &st);
+	char *map = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+	close(fd);
+
+	if (map == MAP_FAILED)
+		return 0;
+
+	return map;
+}
+
+static void *msgnums;
+
+struct msgnum {
+	char *file;
+	long pos;
+};
+
+int
+msgnumorder(const void *a, const void *b)
+{
+        struct msgnum *ia = (struct msgnum *)a;
+        struct msgnum *ib = (struct msgnum *)b;
+
+        return strcmp(ia->file, ib->file);
+}
+
+long
+blaze822_seq_find(char *file)
+{
+	struct msgnum key, **result;
+        key.file = file;
+
+        if (!(result = tfind(&key, &msgnums, msgnumorder)))
+		return 0;
+
+	return (*result)->pos;
+}
+
+int
+blaze822_seq_load(char *map)
+{
+	char *s, *t;
+	long line;
+
+	for (s = map, line = 0; s; s = t+1) {
+		t = strchr(s, '\n');
+		if (!t)
+			break;
+		line++;
+		while (*s && iswsp(*s))
+			s++;
+		char *e = t;
+		while (s < e && isfws(*(e-1)))
+			e--;
+//		printf("{%.*s}\n", e-s, s);
+		char *f = strndup(s, e-s);
+		if (!f)
+			return -1;
+
+		struct msgnum key, **result;
+		key.file = f;
+
+		if (!(result = tfind(&key, &msgnums, msgnumorder))) {
+			struct msgnum *c = malloc(sizeof (struct msgnum));
+			c->file = f;
+			c->pos = line;
+			tsearch(c, &msgnums, msgnumorder);
+		}
+	}
+
+	return 0;
+}