summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-10-15 21:35:51 (GMT)
committerWerner Almesberger <werner@almesberger.net>2016-10-15 21:35:51 (GMT)
commitdbbf05c1d3adbc5a26bdaf5ca1237f7c3cea342a (patch)
treeb606c3fa37f1abc910e27c501f4e4f74ce8546b7
parent691128f611669cad769f8a1b7a15406704741019 (diff)
downloadeeshow-dbbf05c1d3adbc5a26bdaf5ca1237f7c3cea342a.zip
eeshow-dbbf05c1d3adbc5a26bdaf5ca1237f7c3cea342a.tar.gz
eeshow-dbbf05c1d3adbc5a26bdaf5ca1237f7c3cea342a.tar.bz2
db/doc.c: documentation reference database
The format is intended mainly for machine-generation.
-rw-r--r--Makefile1
-rw-r--r--db/doc.c148
-rw-r--r--db/doc.h26
3 files changed, 175 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 41df1ac..cb47d1f 100644
--- a/Makefile
+++ b/Makefile
@@ -34,6 +34,7 @@ EESHOW_OBJS = main/eeshow.o main/common.o \
$(OBJS_FILE) \
gfx/style.o gfx/fig.o gfx/record.o gfx/cro.o gfx/diff.o gfx/gfx.o \
gfx/text.o gfx/misc.o gfx/pdftoc.o \
+ db/doc.o \
$(OBJS_MISC)
EEPLOT_OBJS = main/eeplot.o main/common.o \
$(OBJS_KICAD) \
diff --git a/db/doc.c b/db/doc.c
new file mode 100644
index 0000000..e06983d
--- /dev/null
+++ b/db/doc.c
@@ -0,0 +1,148 @@
+/*
+ * db/doc.c - Documentation database
+ *
+ * Written 2016 by Werner Almesberger
+ * Copyright 2016 by Werner Almesberger
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+
+#include "misc/util.h"
+#include "misc/diag.h"
+#include "file/file.h"
+#include "db/doc.h"
+
+
+static struct doc {
+ const char *ref; /* component reference */
+ const char *tag; /* document type (shown in pop-up) */
+ const char *s; /* string to pass to viewer */
+ struct doc *next;
+} *docs = NULL, *curr_doc = NULL;
+
+
+/* ----- Lookup ------------------------------------------------------------ */
+
+
+bool doc_find(const char *ref,
+ void (*fn)(void *user, const char *tag, const char *s), void *user)
+{
+ const struct doc *d;
+ bool found = 0;
+ for (d = docs; d; d = d->next)
+ if (!strcmp(d->ref, ref)) {
+ fn(user, d->tag, d->s);
+ found = 1;
+ }
+ return found;
+}
+
+
+/* ----- Parsing ----------------------------------------------------------- */
+
+
+static const char *untail(const char *s)
+{
+ const char *start = s;
+ const char *last = s;
+ char *new;
+
+ while (*s) {
+ if (!isspace(*s))
+ last = s;
+ s++;
+ }
+ new = alloc_size(last - start + 2);
+ memcpy(new, start, last - start + 1);
+ new[last - start + 1] = 0;
+ return new;
+}
+
+
+static bool parse_line(const struct file *file,
+ void *user, const char *line)
+{
+ bool *start = user;
+ const char *s = line;
+ struct doc *new;
+
+ while (1) {
+ switch (*s) {
+ case 0: /* empty line(s) separate(s) records */
+ *start = 1;
+ return 1;
+ case '\r': /* ignore leading whitespace */
+ case ' ':
+ case '\t':
+ s++;
+ continue;
+ case '#': /* ignore comment-only line */
+ return 1;
+ default:
+ break;
+
+ }
+ break;
+ }
+
+ if (*start) {
+ new = alloc_type(struct doc);
+ new->ref = new->tag = new->s = NULL;
+ new->next = NULL;
+ if (curr_doc)
+ curr_doc->next = new;
+ else
+ docs = new;
+ curr_doc = new;
+ *start = 0;
+ }
+ if (curr_doc->s)
+ warning("ignoring extra line for \"%s\"", curr_doc->ref);
+ else if (curr_doc->tag)
+ curr_doc->s = untail(line);
+ else if (curr_doc->ref)
+ curr_doc->tag = untail(line);
+ else
+ curr_doc->ref = untail(line);
+ return 1;
+}
+
+
+bool doc_load(const char *name, const struct file *related)
+{
+ struct file file;
+ bool start = 1;
+
+ if (!file_open(&file, name, related))
+ return 0;
+ file_read(&file, parse_line, &start);
+ file_close(&file);
+ return 1;
+}
+
+
+/* ----- Cleanup ----------------------------------------------------------- */
+
+
+void doc_free(void)
+{
+ struct doc *next;
+
+ while (docs) {
+ next = docs->next;
+ free((void *) docs->ref);
+ free((void *) docs->tag);
+ free((void *) docs->s);
+ free(docs);
+ docs = next;
+ }
+ curr_doc = NULL;
+}
diff --git a/db/doc.h b/db/doc.h
new file mode 100644
index 0000000..bd48e9a
--- /dev/null
+++ b/db/doc.h
@@ -0,0 +1,26 @@
+/*
+ * db/doc.h - Documentation database
+ *
+ * Written 2016 by Werner Almesberger
+ * Copyright 2016 by Werner Almesberger
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef DB_DOC_H
+#define DB_DOC_H
+
+#include <stdbool.h>
+
+#include "file/file.h"
+
+
+bool doc_find(const char *ref,
+ void (*fn)(void *user, const char *tag, const char *s), void *user);
+bool doc_load(const char *name, const struct file *related);
+void doc_free(void);
+
+#endif /* !DB_DOC_H */