Root/eeshow/kicad/pro.c

Source at commit 49d603aca384177fcc4dad19964a15fafe200460 created 7 years 4 months ago.
By Werner Almesberger, eeshow/kicad/pro.c: KiCad profile processing (to find file names)
1/*
2 * kicad/pro.c - KiCad profile
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 <stdio.h>
15#include <string.h>
16#include <assert.h>
17
18#include "misc/util.h"
19#include "file/file.h"
20#include "kicad/ext.h"
21#include "kicad/pro.h"
22
23
24struct pro_ctx {
25    enum pro_state {
26        pro_idle,
27        pro_libs, // [eeschema/libraries]
28        pro_editor, // [schematic_editor]
29    } state;
30    struct file_names *fn;
31};
32
33
34static bool pro_parse_line(const struct file *file,
35    void *user, const char *line)
36{
37    struct pro_ctx *pro = user;
38    char *s;
39
40    if (strbegins(line, "[eeschema/libraries]")) {
41        pro->state = pro_libs;
42        return 1;
43    }
44    if (strbegins(line, "[schematic_editor]")) {
45        pro->state = pro_editor;
46        return 1;
47    }
48    if (*line == '[') {
49        pro->state = pro_idle;
50        return 1;
51    }
52
53    switch (pro->state) {
54    case pro_idle:
55        break;
56    case pro_libs:
57        if (sscanf(line, "LibName%*u=%ms", &s) == 1) {
58            char *dot;
59
60            pro->fn->n_libs++;
61            pro->fn->libs = realloc_type_n(pro->fn->libs,
62                const char *, pro->fn->n_libs);
63            dot = strrchr(s, '.');
64            if (!dot || strcmp(dot, ".lib")) {
65                s = realloc_size(s, strlen(s) + 5);
66                strcat(s, ".lib");
67            }
68            pro->fn->libs[pro->fn->n_libs - 1] = s;
69            return 1;
70        }
71        break;
72    case pro_editor:
73        if (sscanf(line, "PageLayoutDescrFile=%ms", &s) == 1) {
74            free((void *) pro->fn->pl);
75            pro->fn->pl = s;
76            return 1;
77        }
78        break;
79    default:
80        abort();
81    }
82    return 1;
83}
84
85
86struct file_names *pro_parse_file(struct file *file,
87    const struct file_names *fn_default)
88{
89    struct pro_ctx pro = {
90        .state = pro_idle,
91        .fn = clone_file_names(fn_default),
92    };
93
94    if (!file_read(file, pro_parse_line, &pro)) {
95        free_file_names(pro.fn);
96        free(pro.fn);
97        return NULL;
98    }
99
100    if (!pro.fn->sch) {
101        char *s, *dot;
102
103        assert(pro.fn->pro);
104        s = stralloc(pro.fn->pro);
105        dot = strchr(s, '.');
106        strcpy(dot, ".sch");
107        pro.fn->sch = s;
108    }
109    return pro.fn;
110}
111

Archive Download this file

Branches:
master



interactive