Root/
| 1 | /* |
| 2 | * kicad/lib.c - Parse Eeschema .lib file |
| 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 | #include <stdlib.h> |
| 15 | #include <stdio.h> |
| 16 | #include <assert.h> |
| 17 | |
| 18 | #include "misc/util.h" |
| 19 | #include "misc/diag.h" |
| 20 | #include "gfx/text.h" |
| 21 | #include "file/file.h" |
| 22 | #include "kicad/lib.h" |
| 23 | |
| 24 | |
| 25 | /* ----- Text -------------------------------------------------------------- */ |
| 26 | |
| 27 | |
| 28 | static enum text_style decode_style(const char *s) |
| 29 | { |
| 30 | if (!strcmp(s, "Normal")) |
| 31 | return text_normal; |
| 32 | if (!strcmp(s, "Italic")) |
| 33 | return text_italic; |
| 34 | assert(0); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /* ----- Polygons ---------------------------------------------------------- */ |
| 39 | |
| 40 | |
| 41 | static bool parse_poly(struct lib_poly *poly, const char *line, int points) |
| 42 | { |
| 43 | int i, n; |
| 44 | |
| 45 | poly->points = points; |
| 46 | poly->x = alloc_type_n(int, points); |
| 47 | poly->y = alloc_type_n(int, points); |
| 48 | for (i = 0; i != points; i++) { |
| 49 | if (sscanf(line, "%d %d %n", |
| 50 | poly->x + i, poly->y + i, &n) != 2) |
| 51 | return 0; |
| 52 | line += n; |
| 53 | } |
| 54 | if (sscanf(line, "%c", &poly->fill) != 1) |
| 55 | return 0; |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /* ----- Definitions ------------------------------------------------------- */ |
| 61 | |
| 62 | |
| 63 | static bool parse_def(struct lib *lib, const char *line) |
| 64 | { |
| 65 | char *s, *ref; |
| 66 | char draw_num, draw_name; |
| 67 | unsigned name_offset; |
| 68 | unsigned units; |
| 69 | bool power; |
| 70 | |
| 71 | if (sscanf(line, "DEF %ms %ms %*d %u %c %c %u", |
| 72 | &s, &ref, &name_offset, &draw_num, &draw_name, &units) != 6) |
| 73 | return 0; |
| 74 | |
| 75 | power = *ref == '#'; |
| 76 | free(ref); |
| 77 | |
| 78 | lib->curr_comp = alloc_type(struct comp); |
| 79 | if (*s == '~') { |
| 80 | char *tmp = alloc_size(strlen(s) + 1); |
| 81 | |
| 82 | /* we can't just s++, since that would break freeing */ |
| 83 | strcpy(tmp, s + 1); |
| 84 | free(s); |
| 85 | s = tmp; |
| 86 | } |
| 87 | lib->curr_comp->name = s; |
| 88 | lib->curr_comp->aliases = NULL; |
| 89 | lib->curr_comp->units = units; |
| 90 | |
| 91 | lib->curr_comp->visible = 0; |
| 92 | lib->curr_comp->show_pin_name = draw_name == 'Y' && !power; |
| 93 | lib->curr_comp->show_pin_num = draw_num == 'Y' && !power; |
| 94 | lib->curr_comp->name_offset = name_offset; |
| 95 | |
| 96 | lib->curr_comp->objs = NULL; |
| 97 | lib->next_obj = &lib->curr_comp->objs; |
| 98 | |
| 99 | lib->curr_comp->next = NULL; |
| 100 | *lib->next_comp = lib->curr_comp; |
| 101 | lib->next_comp = &lib->curr_comp->next; |
| 102 | |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* ----- Arcs -------------------------------------------------------------- */ |
| 108 | |
| 109 | |
| 110 | static bool parse_arc(struct lib_obj *obj, const char *line) |
| 111 | { |
| 112 | struct lib_arc *arc = &obj->u.arc; |
| 113 | int a1, a2; |
| 114 | |
| 115 | if (sscanf(line, "A %d %d %d %d %d %u %u %u %c", |
| 116 | &arc->x, &arc->y, &arc->r, &a1, &a2, &obj->unit, &obj->convert, |
| 117 | &arc->thick, &arc->fill) != 9) |
| 118 | return 0; |
| 119 | |
| 120 | /* |
| 121 | * KiCad arcs can be clockwise or counter-clockwise. They must always |
| 122 | * be smaller than 180 degrees. |
| 123 | */ |
| 124 | |
| 125 | while (a1 < 0) |
| 126 | a1 += 3600; |
| 127 | while (a2 < 0) |
| 128 | a2 += 3600; |
| 129 | a1 %= 3600; |
| 130 | a2 %= 3600; |
| 131 | if (a2 < a1) |
| 132 | a2 += 3600; |
| 133 | assert(a2 - a1 != 1800); |
| 134 | if (a2 - a1 > 1800) |
| 135 | swap(a1, a2); |
| 136 | |
| 137 | arc->start_a = (a1 % 3600) / 10; |
| 138 | arc->end_a = (a2 % 3600) / 10; |
| 139 | |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* ----- Aliases ----------------------------------------------------------- */ |
| 145 | |
| 146 | |
| 147 | static void add_alias(struct comp *comp, const char *alias) |
| 148 | { |
| 149 | struct comp_alias *new; |
| 150 | |
| 151 | new = alloc_type(struct comp_alias); |
| 152 | new->name = alias; |
| 153 | new->next = comp->aliases; |
| 154 | comp->aliases = new; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /* ----- Library parser ---------------------------------------------------- */ |
| 159 | |
| 160 | |
| 161 | static bool lib_parse_line(const struct file *file, |
| 162 | void *user, const char *line) |
| 163 | { |
| 164 | struct lib *lib = user; |
| 165 | int n = 0; |
| 166 | unsigned points; |
| 167 | struct lib_obj *obj; |
| 168 | char *s, *style; |
| 169 | unsigned zero1, zero2; |
| 170 | char vis; |
| 171 | |
| 172 | switch (lib->state) { |
| 173 | case lib_skip: |
| 174 | if (parse_def(lib, line)) { |
| 175 | lib->state = lib_def; |
| 176 | return 1; |
| 177 | } |
| 178 | return 1; |
| 179 | case lib_def: |
| 180 | if (sscanf(line, "DRAW%n", &n) == 0 && n) { |
| 181 | lib->state = lib_draw; |
| 182 | return 1; |
| 183 | } |
| 184 | if (sscanf(line, "F%d \"\" %*d %*d %*d %*c %c", &n, &vis) == 2 |
| 185 | || sscanf(line, "F%d \"%*[^\"]\" %*d %*d %*d %*c %c", |
| 186 | &n, &vis) == 2) { |
| 187 | if (vis == 'V') |
| 188 | lib->curr_comp->visible |= 1 << n; |
| 189 | return 1; |
| 190 | } |
| 191 | if (sscanf(line, "ALIAS %ms", &s) == 1) { |
| 192 | add_alias(lib->curr_comp, s); |
| 193 | return 1; |
| 194 | } |
| 195 | /* @@@ explicitly ignore FPLIST */ |
| 196 | return 1; |
| 197 | case lib_draw: |
| 198 | if (sscanf(line, "ENDDRAW%n", &n) == 0 && n) { |
| 199 | lib->state = lib_skip; |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | obj = alloc_type(struct lib_obj); |
| 204 | obj->next = NULL; |
| 205 | *lib->next_obj = obj; |
| 206 | lib->next_obj = &obj->next; |
| 207 | |
| 208 | if (sscanf(line, "P %u %u %u %u %n", |
| 209 | &points, &obj->unit, &obj->convert, &obj->u.poly.thick, |
| 210 | &n) == 4) { |
| 211 | obj->type = lib_obj_poly; |
| 212 | if (parse_poly(&obj->u.poly, line + n, points)) |
| 213 | return 1; |
| 214 | break; |
| 215 | } |
| 216 | if (sscanf(line, "S %d %d %d %d %u %u %d %c", |
| 217 | &obj->u.rect.sx, &obj->u.rect.sy, &obj->u.rect.ex, |
| 218 | &obj->u.rect.ey, &obj->unit, &obj->convert, |
| 219 | &obj->u.rect.thick, &obj->u.rect.fill) == 8) { |
| 220 | obj->type = lib_obj_rect; |
| 221 | return 1; |
| 222 | } |
| 223 | if (sscanf(line, "C %d %d %d %u %u %d %c", |
| 224 | &obj->u.circ.x, &obj->u.circ.y, &obj->u.circ.r, |
| 225 | &obj->unit, &obj->convert, &obj->u.circ.thick, |
| 226 | &obj->u.circ.fill) == 7) { |
| 227 | obj->type = lib_obj_circ; |
| 228 | return 1; |
| 229 | } |
| 230 | if (parse_arc(obj, line)) { |
| 231 | obj->type = lib_obj_arc; |
| 232 | return 1; |
| 233 | } |
| 234 | n = sscanf(line, |
| 235 | "T %d %d %d %d %u %u %u \"%m[^\"]\" %ms %u %c %c", |
| 236 | &obj->u.text.orient, &obj->u.text.x, &obj->u.text.y, |
| 237 | &obj->u.text.dim, &zero1, &obj->unit, &obj->convert, |
| 238 | &obj->u.text.s, &style, &zero2, |
| 239 | &obj->u.text.hor_align, &obj->u.text.vert_align); |
| 240 | if (n != 12) { |
| 241 | n = sscanf(line, |
| 242 | "T %d %d %d %d %u %u %u %ms %ms %u %c %c", |
| 243 | &obj->u.text.orient, &obj->u.text.x, &obj->u.text.y, |
| 244 | &obj->u.text.dim, &zero1, &obj->unit, &obj->convert, |
| 245 | &obj->u.text.s, &style, &zero2, |
| 246 | &obj->u.text.hor_align, &obj->u.text.vert_align); |
| 247 | while (n == 12) { |
| 248 | char *tilde; |
| 249 | |
| 250 | tilde = strchr(obj->u.text.s, '~'); |
| 251 | if (!tilde) |
| 252 | break; |
| 253 | *tilde = ' '; |
| 254 | } |
| 255 | } |
| 256 | /* |
| 257 | * zero2 seems to be the font style: 0 = normal, 1 = bold ? |
| 258 | */ |
| 259 | if (n == 12) { |
| 260 | if (zero1) |
| 261 | fatal("%u: only understand 0 x x\n" |
| 262 | "\"%s\"\n", file->lineno, line); |
| 263 | obj->u.text.style = decode_style(style); |
| 264 | obj->type = lib_obj_text; |
| 265 | return 1; |
| 266 | } |
| 267 | if (sscanf(line, "X %ms %ms %d %d %d %c %d %d %u %u %c", |
| 268 | &obj->u.pin.name, &obj->u.pin.number, |
| 269 | &obj->u.pin.x, &obj->u.pin.y, &obj->u.pin.length, |
| 270 | &obj->u.pin.orient, |
| 271 | &obj->u.pin.number_size, &obj->u.pin.name_size, |
| 272 | &obj->unit, &obj->convert, &obj->u.pin.etype) == 11) { |
| 273 | obj->type = lib_obj_pin; |
| 274 | return 1; |
| 275 | } |
| 276 | break; |
| 277 | default: |
| 278 | abort(); |
| 279 | } |
| 280 | fatal("%u: cannot parse\n\"%s\"\n", file->lineno, line); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | bool lib_parse_file(struct lib *lib, struct file *file) |
| 285 | { |
| 286 | lib->state = lib_skip; |
| 287 | return file_read(file, lib_parse_line, lib); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| 292 | bool lib_parse(struct lib *lib, const char *name, const struct file *related) |
| 293 | { |
| 294 | struct file file; |
| 295 | bool res; |
| 296 | |
| 297 | if (!file_open(&file, name, related)) |
| 298 | return 0; |
| 299 | res = lib_parse_file(lib, &file); |
| 300 | file_close(&file); |
| 301 | return res; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | void lib_init(struct lib *lib) |
| 306 | { |
| 307 | lib->comps = NULL; |
| 308 | lib->next_comp = &lib->comps; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | static void free_objs(struct lib_obj *objs) |
| 313 | { |
| 314 | struct lib_obj *next; |
| 315 | |
| 316 | while (objs) { |
| 317 | next = objs->next; |
| 318 | switch (objs->type) { |
| 319 | case lib_obj_text: |
| 320 | free((char *) objs->u.text.s); |
| 321 | break; |
| 322 | case lib_obj_pin: |
| 323 | free((char *) objs->u.pin.name); |
| 324 | free((char *) objs->u.pin.number); |
| 325 | break; |
| 326 | default: |
| 327 | break; |
| 328 | } |
| 329 | free(objs); |
| 330 | objs = next; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | static void free_comp(struct comp *comp) |
| 336 | { |
| 337 | struct comp_alias *next; |
| 338 | |
| 339 | free((char *) comp->name); |
| 340 | while (comp->aliases) { |
| 341 | next = comp->aliases->next; |
| 342 | free((char *) comp->aliases->name); |
| 343 | comp->aliases = next; |
| 344 | } |
| 345 | free_objs(comp->objs); |
| 346 | free(comp); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | void lib_free(struct lib *lib) |
| 351 | { |
| 352 | struct comp *comp, *next; |
| 353 | |
| 354 | for (comp = lib->comps; comp; comp = next) { |
| 355 | next = comp->next; |
| 356 | free_comp(comp); |
| 357 | } |
| 358 | } |
| 359 |
Branches:
master
