eeshow/kicad/ext.c |
| 1 | /* |
| 2 | * kicad/ext.c - Identify file by their extension |
| 3 | * |
| 4 | * Written 2016 by Werner Almesberger |
| 5 | * Copyright 2016 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include "misc/diag.h" |
| 17 | #include "kicad/ext.h" |
| 18 | |
| 19 | |
| 20 | enum ext identify(const char *path) |
| 21 | { |
| 22 | const char *dot, *slash; |
| 23 | |
| 24 | dot = strrchr(path, '.'); |
| 25 | if (!dot) |
| 26 | return ext_unknown; |
| 27 | slash = strchr(dot + 1, '/'); |
| 28 | if (slash) |
| 29 | return ext_unknown; |
| 30 | |
| 31 | if (!strcmp(dot, ".pro")) |
| 32 | return ext_project; |
| 33 | if (!strcmp(dot, ".sch")) |
| 34 | return ext_sch; |
| 35 | if (!strcmp(dot, ".lib")) |
| 36 | return ext_lib; |
| 37 | if (!strcmp(dot, ".kicad_wks")) |
| 38 | return ext_pl; |
| 39 | |
| 40 | return ext_unknown; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void classify_files(struct file_names *fn, char *const *args, |
| 45 | unsigned n_args) |
| 46 | { |
| 47 | unsigned i; |
| 48 | |
| 49 | fn->pro = fn->sch = fn->pl = NULL; |
| 50 | fn->libs = NULL; |
| 51 | fn->n_libs = 0; |
| 52 | |
| 53 | for (i = 0; i != n_args; i++) { |
| 54 | switch (identify(args[i])) { |
| 55 | case ext_unknown: |
| 56 | fatal("%s: unknown file type", args[i]); |
| 57 | case ext_project: |
| 58 | if (fn->pro) |
| 59 | fatal("%s: there can only be one project", |
| 60 | args[i]); |
| 61 | fn->pro = args[i]; |
| 62 | break; |
| 63 | case ext_sch: |
| 64 | if (fn->sch) |
| 65 | fatal("%s: there can only be one top sheet", |
| 66 | args[i]); |
| 67 | fn->sch = args[i]; |
| 68 | break; |
| 69 | case ext_lib: |
| 70 | fn->n_libs++; |
| 71 | fn->libs = realloc(fn->libs, |
| 72 | fn->n_libs * sizeof(const char *)); |
| 73 | if (!fn->libs) |
| 74 | diag_pfatal("realloc"); |
| 75 | fn->libs[fn->n_libs - 1] = args[i]; |
| 76 | break; |
| 77 | case ext_pl: |
| 78 | if (fn->pl) |
| 79 | fatal("%s: there can only be one page layout", |
| 80 | args[i]); |
| 81 | fn->pl = args[i]; |
| 82 | break; |
| 83 | default: |
| 84 | abort(); |
| 85 | } |
| 86 | } |
| 87 | } |
eeshow/kicad/ext.h |
| 1 | /* |
| 2 | * kicad/ext.h - Identify files by their extension |
| 3 | * |
| 4 | * Written 2016 by Werner Almesberger |
| 5 | * Copyright 2016 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #ifndef KICAD_EXT_H |
| 15 | #define KICAD_EXT_H |
| 16 | |
| 17 | enum ext { |
| 18 | ext_unknown, |
| 19 | ext_project, |
| 20 | ext_sch, |
| 21 | ext_lib, |
| 22 | ext_pl, /* .kicad_wks, work sheet */ |
| 23 | }; |
| 24 | |
| 25 | |
| 26 | struct file_names { |
| 27 | const char *pro; /* just one allowed, may be NULL */ |
| 28 | const char *sch; /* just one allowed, may be NULL */ |
| 29 | const char *pl; /* just one allowed, may be NULL */ |
| 30 | const char **libs; |
| 31 | unsigned n_libs; |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | enum ext identify(const char *path); |
| 36 | void classify_files(struct file_names *fn, char *const *args, |
| 37 | unsigned n_args); |
| 38 | |
| 39 | #endif /* !KICAD_EXT_H */ |