diff --git a/eeshow/Makefile b/eeshow/Makefile index 5fd498f..0e28242 100644 --- a/eeshow/Makefile +++ b/eeshow/Makefile @@ -16,7 +16,7 @@ NAME = eeshow OBJS = main.o version.o \ kicad/sch-parse.o kicad/sch-render.o kicad/lib-parse.o \ kicad/lib-render.o kicad/dwg.o kicad/delta.o kicad/sexpr.o \ - kicad/pl-parse.o kicad/pl-render.o kicad/ext.o \ + kicad/pl-parse.o kicad/pl-render.o kicad/ext.o kicad/pro.o \ gui/gui.o gui/over.o gui/style.o gui/aoi.o gui/fmt-pango.o gui/input.o \ gui/progress.o gui/glabel.o gui/sheet.o gui/history.o gui/render.o \ gui/help.o gui/icons.o \ diff --git a/eeshow/kicad/pro.c b/eeshow/kicad/pro.c new file mode 100644 index 0000000..41082cd --- /dev/null +++ b/eeshow/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 +#include +#include +#include + +#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/eeshow/kicad/pro.h b/eeshow/kicad/pro.h new file mode 100644 index 0000000..2d777bd --- /dev/null +++ b/eeshow/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 */