Root/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/util.h"
17#include "misc/diag.h"
18#include "kicad/ext.h"
19
20
21enum ext identify(const char *path)
22{
23    const char *dot, *slash;
24
25    dot = strrchr(path, '.');
26    if (!dot)
27        return ext_unknown;
28    slash = strchr(dot + 1, '/');
29    if (slash)
30        return ext_unknown;
31
32    if (!strcmp(dot, ".pro"))
33        return ext_project;
34    if (!strcmp(dot, ".sch"))
35        return ext_sch;
36    if (!strcmp(dot, ".lib"))
37        return ext_lib;
38    if (!strcmp(dot, ".kicad_wks"))
39        return ext_pl;
40
41    return ext_unknown;
42}
43
44
45void classify_files(struct file_names *fn, char *const *args,
46    unsigned n_args)
47{
48    unsigned i;
49
50    fn->pro = fn->sch = fn->pl = NULL;
51    fn->libs = NULL;
52    fn->n_libs = 0;
53
54    for (i = 0; i != n_args; i++) {
55        switch (identify(args[i])) {
56        case ext_unknown:
57            fatal("%s: unknown file type", args[i]);
58        case ext_project:
59            if (fn->pro)
60                fatal("%s: there can only be one project",
61                    args[i]);
62            fn->pro = args[i];
63            break;
64        case ext_sch:
65            if (fn->sch)
66                fatal("%s: there can only be one top sheet",
67                    args[i]);
68            fn->sch = args[i];
69            break;
70        case ext_lib:
71            fn->n_libs++;
72            fn->libs = realloc_type_n(fn->libs, const char *,
73                fn->n_libs);
74            fn->libs[fn->n_libs - 1] = args[i];
75            break;
76        case ext_pl:
77            if (fn->pl)
78                fatal("%s: there can only be one page layout",
79                    args[i]);
80            fn->pl = args[i];
81            break;
82        default:
83            abort();
84        }
85    }
86}
87
88
89struct file_names *clone_file_names(const struct file_names *fn)
90{
91    struct file_names *new;
92    unsigned i;
93
94    new = alloc_type(struct file_names);
95    new->pro = fn && fn->pro ? stralloc(fn->pro) : NULL;
96    new->sch = fn && fn->sch ? stralloc(fn->sch) : NULL;
97    new->pl = fn && fn->pl ? stralloc(fn->pl) : NULL;
98    new->n_libs = fn ? fn->n_libs : 0;
99    if (!fn) {
100        new->libs = NULL;
101        return new;
102    }
103    new->libs = alloc_type_n(const char *, fn->n_libs);
104    for (i = 0; i != fn->n_libs; i++)
105        new->libs[i] = stralloc(fn->libs[i]);
106    return new;
107}
108
109
110void free_file_names(struct file_names *fn)
111{
112    unsigned i;
113
114    free((void *) fn->pro);
115    free((void *) fn->sch);
116    free((void *) fn->pl);
117    for (i = 0; i != fn->n_libs; i++)
118        free((void *) fn->libs[i]);
119    free(fn->libs);
120}
121

Archive Download this file

Branches:
master



interactive