comparison pads2gpcb/partinst.c @ 65:2b71943a311b

pads2gpcb: PART section parsing implemented, compiles
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Sun, 31 Jan 2016 21:32:08 +0000
parents
children 68900e8dcda1
comparison
equal deleted inserted replaced
64:6a1197106b6f 65:2b71943a311b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include <strings.h>
6 #include "globals.h"
7 #include "struct.h"
8
9 extern struct part_type *find_parttype_by_name();
10 extern struct part_decal *find_decal_by_name();
11 extern long convert_input_dim();
12
13 static struct part_instance *our_part;
14
15 static void
16 enter_part_inst()
17 {
18 struct part_instance *p, **pp;
19 char *name = input_line_fields[0];
20
21 for (pp = &part_inst_list; p = *pp; pp = &p->next)
22 if (!strcmp(p->name, name)) {
23 fprintf(stderr,
24 "%s line %d: part instance \"%s\" already defined\n",
25 input_filename, input_lineno, name);
26 exit(1);
27 }
28 p = malloc(sizeof(struct part_instance) + strlen(name) + 1);
29 if (!p) {
30 perror("malloc of struct part_instance");
31 exit(1);
32 }
33 bzero(p, sizeof(struct part_instance));
34 p->name = (char *)(p + 1);
35 strcpy(p->name, name);
36 *pp = p;
37 our_part = p;
38 }
39
40 static void
41 select_decal_by_name(decalname)
42 char *decalname;
43 {
44 our_part->decal = find_decal_by_name(decalname);
45 if (!our_part->decal) {
46 fprintf(stderr,
47 "%s line %d: part instantiation refers to unknown decal name \"%s\"\n",
48 input_filename, input_lineno, decalname);
49 exit(1);
50 }
51 }
52
53 static void
54 select_decal_by_number()
55 {
56 char *strnum = input_line_fields[7];
57 int num = atoi(strnum);
58
59 if (num < 0 || num >= our_part->type->ndecals) {
60 fprintf(stderr,
61 "%s line %d: invalid decal number \"%s\" in PART line\n",
62 input_filename, input_lineno, strnum);
63 exit(1);
64 }
65 our_part->decal = our_part->type->decals[num];
66 }
67
68 static void
69 find_type_and_decal()
70 {
71 char *typename = input_line_fields[1];
72 char *decalname;
73
74 decalname = index(typename, '@');
75 if (decalname)
76 *decalname++;
77 our_part->type = find_parttype_by_name(typename);
78 if (!our_part->type) {
79 fprintf(stderr,
80 "%s line %d: part instantiation refers to unknown part type \"%s\"\n",
81 input_filename, input_lineno, typename);
82 exit(1);
83 }
84 if (decalname)
85 select_decal_by_name(decalname);
86 else
87 select_decal_by_number();
88 }
89
90 static void
91 one_attr_label()
92 {
93 int i;
94
95 /* just skip it for now */
96 for (i = 0; i < 3; i++) {
97 if (!get_input_line()) {
98 fprintf(stderr,
99 "error: EOF when expecting a part label line\n");
100 exit(1);
101 }
102 }
103 }
104
105 static void
106 read_one_part()
107 {
108 int num_attr_labels, i;
109
110 if (input_line_nfields < 8 || input_line_nfields > 12) {
111 fprintf(stderr,
112 "%s line %d: expected beginning of part instance, wrong # of fields\n",
113 input_filename, input_lineno);
114 exit(1);
115 }
116 enter_part_inst();
117 printf("Processing part instance %s\n", our_part->name);
118 find_type_and_decal();
119 our_part->mark_x = convert_input_dim(input_line_fields[2]);
120 our_part->mark_y = -convert_input_dim(input_line_fields[3]);
121 our_part->ori = parse_input_angle_90s(input_line_fields[4]);
122 if (!strcmp(input_line_fields[6], "N"))
123 our_part->onbottom = 0;
124 else if (!strcmp(input_line_fields[6], "M"))
125 our_part->onbottom = 1;
126 else {
127 fprintf(stderr, "%s line %d: expected 'N' or 'M' in field 6\n",
128 input_filename, input_lineno);
129 exit(1);
130 }
131 if (input_line_nfields > 11)
132 num_attr_labels = atoi(input_line_fields[11]);
133 else
134 num_attr_labels = 0;
135 for (i = 0; i < num_attr_labels; i++)
136 one_attr_label();
137 }
138
139 static void
140 process_one_part()
141 {
142 read_one_part();
143 }
144
145 process_part_section()
146 {
147 input_units_current = input_units_global;
148 for (;;) {
149 if (!get_input_line()) {
150 fprintf(stderr, "error: EOF in PART section\n");
151 exit(1);
152 }
153 if (input_line_buf[0] == '*') {
154 parse_starline();
155 if (strcmp(input_line_starkw, "REMARK"))
156 break;
157 else
158 continue;
159 }
160 parse_input_line_fields();
161 if (input_line_nfields)
162 process_one_part();
163 }
164 }