summaryrefslogtreecommitdiff
path: root/kicad
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-08-22 20:45:31 -0300
committerWerner Almesberger <werner@almesberger.net>2016-08-22 20:45:31 -0300
commitc9a05d88435e5cc47492b4b62f8312ad8011fd2c (patch)
tree467579b12ff6ceb908f1d8e36e139a338bbc6320 /kicad
parente40b2d362266908aefbcbc02503eb742bb8007da (diff)
downloadeeshow-c9a05d88435e5cc47492b4b62f8312ad8011fd2c.tar.gz
eeshow-c9a05d88435e5cc47492b4b62f8312ad8011fd2c.tar.bz2
eeshow-c9a05d88435e5cc47492b4b62f8312ad8011fd2c.zip
eeshow/kicad/ext.c: identify and classify files by their extension
Diffstat (limited to 'kicad')
-rw-r--r--kicad/ext.c87
-rw-r--r--kicad/ext.h39
2 files changed, 126 insertions, 0 deletions
diff --git a/kicad/ext.c b/kicad/ext.c
new file mode 100644
index 0000000..961f231
--- /dev/null
+++ b/kicad/ext.c
@@ -0,0 +1,87 @@
+/*
+ * kicad/ext.c - Identify file by their extension
+ *
+ * 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 <string.h>
+
+#include "misc/diag.h"
+#include "kicad/ext.h"
+
+
+enum ext identify(const char *path)
+{
+ const char *dot, *slash;
+
+ dot = strrchr(path, '.');
+ if (!dot)
+ return ext_unknown;
+ slash = strchr(dot + 1, '/');
+ if (slash)
+ return ext_unknown;
+
+ if (!strcmp(dot, ".pro"))
+ return ext_project;
+ if (!strcmp(dot, ".sch"))
+ return ext_sch;
+ if (!strcmp(dot, ".lib"))
+ return ext_lib;
+ if (!strcmp(dot, ".kicad_wks"))
+ return ext_pl;
+
+ return ext_unknown;
+}
+
+
+void classify_files(struct file_names *fn, char *const *args,
+ unsigned n_args)
+{
+ unsigned i;
+
+ fn->pro = fn->sch = fn->pl = NULL;
+ fn->libs = NULL;
+ fn->n_libs = 0;
+
+ for (i = 0; i != n_args; i++) {
+ switch (identify(args[i])) {
+ case ext_unknown:
+ fatal("%s: unknown file type", args[i]);
+ case ext_project:
+ if (fn->pro)
+ fatal("%s: there can only be one project",
+ args[i]);
+ fn->pro = args[i];
+ break;
+ case ext_sch:
+ if (fn->sch)
+ fatal("%s: there can only be one top sheet",
+ args[i]);
+ fn->sch = args[i];
+ break;
+ case ext_lib:
+ fn->n_libs++;
+ fn->libs = realloc(fn->libs,
+ fn->n_libs * sizeof(const char *));
+ if (!fn->libs)
+ diag_pfatal("realloc");
+ fn->libs[fn->n_libs - 1] = args[i];
+ break;
+ case ext_pl:
+ if (fn->pl)
+ fatal("%s: there can only be one page layout",
+ args[i]);
+ fn->pl = args[i];
+ break;
+ default:
+ abort();
+ }
+ }
+}
diff --git a/kicad/ext.h b/kicad/ext.h
new file mode 100644
index 0000000..0986edd
--- /dev/null
+++ b/kicad/ext.h
@@ -0,0 +1,39 @@
+/*
+ * kicad/ext.h - Identify files by their extension
+ *
+ * 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_EXT_H
+#define KICAD_EXT_H
+
+enum ext {
+ ext_unknown,
+ ext_project,
+ ext_sch,
+ ext_lib,
+ ext_pl, /* .kicad_wks, work sheet */
+};
+
+
+struct file_names {
+ const char *pro; /* just one allowed, may be NULL */
+ const char *sch; /* just one allowed, may be NULL */
+ const char *pl; /* just one allowed, may be NULL */
+ const char **libs;
+ unsigned n_libs;
+};
+
+
+enum ext identify(const char *path);
+void classify_files(struct file_names *fn, char *const *args,
+ unsigned n_args);
+
+#endif /* !KICAD_EXT_H */