summaryrefslogtreecommitdiff
path: root/math/readmeta.c
diff options
context:
space:
mode:
Diffstat (limited to 'math/readmeta.c')
-rw-r--r--math/readmeta.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/math/readmeta.c b/math/readmeta.c
new file mode 100644
index 0000000..33fc5f7
--- /dev/null
+++ b/math/readmeta.c
@@ -0,0 +1,158 @@
+/* $Id$ */
+
+/*
+ * Copyright (c) 2006 Dimitri A. Sokolyuk <demon@vhost.dyndns.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "metadb.h"
+
+char *freadall(char *);
+
+#define STRIPWS(p) do { \
+ while (*(p) != '\0' && isascii(*(p)) && isblank(*(p))) \
+ ++(p); \
+} while (0)
+
+#define INITTAB(k, v) do { \
+ (k)->key = k; ((k)++)->val = (v); \
+} while (0)
+
+/* TODO: rewrite it all */
+
+char *
+freadall(char *fname)
+{
+ FILE *fd;
+ char *buf;
+ size_t len;
+
+ if ((fd = fopen(fname, "r")) == NULL)
+ return NULL;
+
+ fseek(fd, 0L, SEEK_END);
+ len = ftell(fd);
+ fseek(fd, 0L, SEEK_SET);
+
+ if ((buf = calloc(len + 1, sizeof(char))) == NULL)
+ return NULL;
+
+ fread(buf, sizeof(char), len, fd);
+ fclose(fd);
+
+ buf[len + 1] = '\0';
+
+ return buf;
+}
+
+struct meta *
+openmeta(char *fname)
+{
+ char *buf, *p, *key, *eol;
+ struct meta *mp;
+ struct kwords {
+ char *key;
+ char **val;
+ } *kwp, kw[16];
+
+ if ((buf = freadall(fname)) == NULL)
+ return NULL;
+
+ mp = malloc(sizeof(struct meta));
+ assert(mp);
+ memset(mp, 0, sizeof(struct meta));
+
+ kwp = kw;
+ kwp->key = "title"; (kwp++)->val = &mp->title;
+ kwp->key = "abstract"; (kwp++)->val = &mp->abstract;
+ kwp->key = "author"; (kwp++)->val = &mp->author;
+ kwp->key = "email"; (kwp++)->val = &mp->email;
+ kwp->key = "image"; (kwp++)->val = &mp->img;
+ kwp->key = "language"; (kwp++)->val = &mp->language;
+ kwp->key = "tokens"; (kwp++)->val = &mp->tokens;
+ kwp->key = NULL;
+
+#if DEBUG
+ printf("file: %s\n", fname);
+#endif
+ for (p = buf; p != NULL; p = eol) {
+ /* join lines */
+ for (eol = p; (eol = strchr(eol, '\n')) != NULL;) {
+ ++eol;
+ if (isascii(*eol) && !isblank(*eol)) {
+ *(eol - 1) = '\0';
+ break;
+ }
+ }
+
+ /* skip commentars and empty lines */
+ if (*p == '\0' || *p == '#' || (isascii(*p) && (isspace(*p))))
+ continue;
+
+ key = p;
+ while (*p != '\0' && isascii(*p) && !isblank(*p) && *p != ':' && *p != '=')
+ ++p;
+ *p++ = '\0';
+
+ /* strip whitespace */
+ while (*p != '\0' && isascii(*p) && isblank(*p))
+ ++p;
+
+#if DEBUG
+ printf("key: %s\tval: %s\n", key, p);
+#endif
+ for (kwp = kw; kwp->key != NULL; kwp++) {
+ if (strcmp(key, kwp->key) == 0)
+ *kwp->val = strdup(p);
+ }
+ }
+
+ free(buf);
+
+ if (mp->language == NULL)
+ mp->language = "en";
+
+ return mp;
+}
+
+void
+closemeta(struct meta *mp)
+{
+ if (mp->title != NULL)
+ free(mp->title);
+ if (mp->abstract != NULL)
+ free(mp->abstract);
+ if (mp->author != NULL)
+ free(mp->author);
+ if (mp->email != NULL)
+ free(mp->email);
+ if (mp->html != NULL)
+ free(mp->html);
+ if (mp->mw != NULL)
+ free(mp->mw);
+ if (mp->pdf != NULL)
+ free(mp->pdf);
+ if (mp->code != NULL)
+ free(mp->code);
+ if (mp->tokens != NULL)
+ free(mp->tokens);
+ free(mp);
+}