aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..b5e998d
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,89 @@
+#include <iostream>
+#include <err.h>
+#include <sysexits.h>
+#include <getopt.h>
+#include "trygvis/kicad.h"
+
+using namespace std;
+using namespace trygvis::kicad;
+
+char *program;
+
+__attribute__((noreturn))
+void usage(const char *reason = nullptr) {
+ if (reason) {
+ fprintf(stderr, "%s\n", reason);
+ }
+ fprintf(stderr, "usage: %s -f file -r ref\n", program);
+ exit(EX_USAGE);
+}
+
+void parse_args(int argc, char **argv, bool *debug, char **filename, char **ref) {
+ *debug = false;
+ *filename = *ref = nullptr;
+
+ int c;
+ while ((c = getopt(argc, argv, "Df:r:")) != -1) {
+ switch (c) {
+ case 'D':
+ *debug = true;
+ break;
+ case 'f':
+ *filename = optarg;
+ break;
+ case 'r':
+ *ref = optarg;
+ break;
+ default:
+ abort();
+ }
+ }
+
+ if (!*filename) {
+ usage();
+ }
+}
+
+bool generate(const char *ref, const trygvis::kicad::netlist &netlist) {
+ opt<const component *> componentO = netlist.find_component(ref);
+
+ if (!componentO) {
+ cerr << "Could not find component '" << ref << "'" << endl;
+ return false;
+ }
+
+ const component *c = *componentO;
+
+ cerr << "Generating connections for " << ref << endl;
+
+ cerr << c->value << endl;
+
+ return true;
+}
+
+int main(int argc, char **argv) {
+ program = argv[0];
+
+ bool debug;
+ char *filename;
+ char *ref;
+ parse_args(argc, argv, &debug, &filename, &ref);
+
+ kicad_net_loader loader;
+
+ try {
+ auto netlist = loader.load(filename);
+
+ auto ok = generate(ref, netlist);
+
+ if (ok) {
+ return EXIT_SUCCESS;
+ }
+ return EXIT_FAILURE;
+ } catch (kicad_parse_exception &e) {
+ for (auto &m: e.messages) {
+ fprintf(stderr, "%s\n", m.c_str());
+ }
+ errx(EX_DATAERR, "Could not parse netlist");
+ }
+}