summaryrefslogtreecommitdiff
path: root/kicad
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-08-22 22:19:54 -0300
committerWerner Almesberger <werner@almesberger.net>2016-08-22 22:19:54 -0300
commit8f257d6b6a8fb0f07bab7bd99f4117a3cd9ada2b (patch)
treedd9a1dbfe3419708d1bd0667587f4e7602aa50f5 /kicad
parent57c459e51bf1625f33e7545b7b682c14bb982a08 (diff)
downloadeeshow-8f257d6b6a8fb0f07bab7bd99f4117a3cd9ada2b.tar.gz
eeshow-8f257d6b6a8fb0f07bab7bd99f4117a3cd9ada2b.tar.bz2
eeshow-8f257d6b6a8fb0f07bab7bd99f4117a3cd9ada2b.zip
eeshow/kicad/pro.c: KiCad profile processing (to find file names)
Diffstat (limited to 'kicad')
-rw-r--r--kicad/pro.c110
-rw-r--r--kicad/pro.h24
2 files changed, 134 insertions, 0 deletions
diff --git a/kicad/pro.c b/kicad/pro.c
new file mode 100644
index 0000000..41082cd
--- /dev/null
+++ b/kicad/pro.c
@@ -0,0 +1,110 @@
+/*
+ * kicad/pro.c - KiCad profile
+ *
+ * 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 <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "misc/util.h"
+#include "file/file.h"
+#include "kicad/ext.h"
+#include "kicad/pro.h"
+
+
+struct pro_ctx {
+ enum pro_state {
+ pro_idle,
+ pro_libs, // [eeschema/libraries]
+ pro_editor, // [schematic_editor]
+ } state;
+ struct file_names *fn;
+};
+
+
+static bool pro_parse_line(const struct file *file,
+ void *user, const char *line)
+{
+ struct pro_ctx *pro = user;
+ char *s;
+
+ if (strbegins(line, "[eeschema/libraries]")) {
+ pro->state = pro_libs;
+ return 1;
+ }
+ if (strbegins(line, "[schematic_editor]")) {
+ pro->state = pro_editor;
+ return 1;
+ }
+ if (*line == '[') {
+ pro->state = pro_idle;
+ return 1;
+ }
+
+ switch (pro->state) {
+ case pro_idle:
+ break;
+ case pro_libs:
+ if (sscanf(line, "LibName%*u=%ms", &s) == 1) {
+ char *dot;
+
+ pro->fn->n_libs++;
+ pro->fn->libs = realloc_type_n(pro->fn->libs,
+ const char *, pro->fn->n_libs);
+ dot = strrchr(s, '.');
+ if (!dot || strcmp(dot, ".lib")) {
+ s = realloc_size(s, strlen(s) + 5);
+ strcat(s, ".lib");
+ }
+ pro->fn->libs[pro->fn->n_libs - 1] = s;
+ return 1;
+ }
+ break;
+ case pro_editor:
+ if (sscanf(line, "PageLayoutDescrFile=%ms", &s) == 1) {
+ free((void *) pro->fn->pl);
+ pro->fn->pl = s;
+ return 1;
+ }
+ break;
+ default:
+ abort();
+ }
+ return 1;
+}
+
+
+struct file_names *pro_parse_file(struct file *file,
+ const struct file_names *fn_default)
+{
+ struct pro_ctx pro = {
+ .state = pro_idle,
+ .fn = clone_file_names(fn_default),
+ };
+
+ if (!file_read(file, pro_parse_line, &pro)) {
+ free_file_names(pro.fn);
+ free(pro.fn);
+ return NULL;
+ }
+
+ if (!pro.fn->sch) {
+ char *s, *dot;
+
+ assert(pro.fn->pro);
+ s = stralloc(pro.fn->pro);
+ dot = strchr(s, '.');
+ strcpy(dot, ".sch");
+ pro.fn->sch = s;
+ }
+ return pro.fn;
+}
diff --git a/kicad/pro.h b/kicad/pro.h
new file mode 100644
index 0000000..2d777bd
--- /dev/null
+++ b/kicad/pro.h
@@ -0,0 +1,24 @@
+/*
+ * kicad/pro.h - KiCad profile
+ *
+ * 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 KICAD_PRO_H
+#define KICAD_PRO_H
+
+#include "kicad/ext.h"
+#include "file/file.h"
+
+
+struct file_names *pro_parse_file(struct file *file,
+ const struct file_names *fn_default);
+
+#endif /* !KICAD_PRO_H */