Date:2016-08-23 01:45:31 (7 years 3 months ago)
Author:Werner Almesberger
Commit:e6a4224d812349661b91940b1569fdad497d778d
Message:eeshow/kicad/ext.c: identify and classify files by their extension

Files: eeshow/Makefile (1 diff)
eeshow/kicad/ext.c (1 diff)
eeshow/kicad/ext.h (1 diff)

Change Details

eeshow/Makefile
1616OBJS = main.o version.o \
1717       kicad/sch-parse.o kicad/sch-render.o kicad/lib-parse.o \
1818       kicad/lib-render.o kicad/dwg.o kicad/delta.o kicad/sexpr.o \
19       kicad/pl-parse.o kicad/pl-render.o \
19       kicad/pl-parse.o kicad/pl-render.o kicad/ext.o \
2020       gui/gui.o gui/over.o gui/style.o gui/aoi.o gui/fmt-pango.o gui/input.o \
2121       gui/progress.o gui/glabel.o gui/sheet.o gui/history.o gui/render.o \
2222       gui/help.o gui/icons.o \
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
20enum 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
44void 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
17enum ext {
18    ext_unknown,
19    ext_project,
20    ext_sch,
21    ext_lib,
22    ext_pl, /* .kicad_wks, work sheet */
23};
24
25
26struct 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
35enum ext identify(const char *path);
36void classify_files(struct file_names *fn, char *const *args,
37    unsigned n_args);
38
39#endif /* !KICAD_EXT_H */

Archive Download the corresponding diff file

Branches:
master



interactive