File: read_high_misc.c

package info (click to toggle)
pcb-rnd 3.1.7b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,108 kB
  • sloc: ansic: 213,400; yacc: 6,241; sh: 4,698; awk: 3,016; makefile: 2,254; lex: 1,166; python: 519; xml: 261; lisp: 154; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (330 lines) | stat: -rw-r--r-- 9,913 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *
 *  PADS IO plugin - read: decode, parse, interpret
 *  pcb-rnd Copyright (C) 2020,2021 Tibor 'Igor2' Palinkas
 *  (Supported by NLnet NGI0 PET Fund in 2020 and 2021)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 */

/*** high level: *MISC* has a different format than other sections ***/

/* read a {} block, call line_cb() at the start of each line (if not NULL),
   then eat up the rest of the line */
static int pads_parse_misc_lines(pads_read_ctx_t *rctx, int level, int (*line_cb)(pads_read_ctx_t *rctx, int level))
{
	pads_eatup_till_nl(rctx);

	for(;;) {
		int c, res;
		pads_eatup_ws(rctx);
		c = fgetc(rctx->f);
		if (c == EOF)
			return 0;
		ungetc(c, rctx->f);
		if (c == '{') {
			if ((res = pads_parse_misc_lines(rctx, level+1, line_cb)) <= 0) return res;
			continue;
		}
		if ((c != '}') && (line_cb != NULL)) {
			if ((res = line_cb(rctx, level)) <= 0) return res;
		}
		pads_eatup_till_nl(rctx);
		if (c == '}')
			return 1;
	}
	return 1;
}

/* read everything till the closing of this block; recurse if more blocks are
   open */
static int pads_parse_misc_ignore_block(pads_read_ctx_t *rctx)
{
	return pads_parse_misc_lines(rctx, 0, NULL);
}


/* read everything till the closing of this block; recurse if more blocks are
   open */
static int pads_parse_misc_generic(pads_read_ctx_t *rctx, int (*parse_child)(pads_read_ctx_t *rctx))
{
	char open[64];

	fgets(open, sizeof(open), rctx->f);
	if (*open != '{') {
		PADS_ERROR((RND_MSG_ERROR, "Expected block open brace\n"));
		return -1;
	}

	for(;;) {
		int c, res;

		pads_eatup_ws(rctx);
		c = fgetc(rctx->f);
		if (c == EOF)
			return 0;
		ungetc(c, rctx->f);
		if (c == '{') {
			PADS_ERROR((RND_MSG_ERROR, "Unexpected block open brace\n"));
			return -1;
		}
		if (c == '}') {
			pads_eatup_till_nl(rctx);
			return 1;
		}
		res = parse_child(rctx);
		if (res <= 0)
			return res;
	}
	return 1;
}

static int pads_parse_misc_open(pads_read_ctx_t *rctx)
{
	char open[4];
	int res;

	if ((res = pads_read_word(rctx, open, sizeof(open), 0)) <= 0) return res;
	rnd_trace("open='%s'\n", open);
	if (strcmp(open, "{") != 0) {
		PADS_ERROR((RND_MSG_ERROR, "Expected block open brace\n"));
		return -1;
	}
	return 1;
}

static int pads_parse_misc_layer(pads_read_ctx_t *rctx)
{
	char key[32], val[1024];
	int res;
	pads_layer_t *pl = rctx->layer->user_data;

	if ((res = pads_read_word(rctx, key, sizeof(key), 0)) <= 0) return res;
	if ((res = pads_read_word_all(rctx, val, sizeof(val), 0)) <= 0) return res;
	pads_eatup_till_nl(rctx);

	if (strcmp(key, "LAYER_NAME") == 0) {
		rnd_trace("  name='%s'\n", val);
		rctx->layer->name = rnd_strdup(val);
	}
	else if (strcmp(key, "LAYER_TYPE") == 0) {
		rnd_trace("  type='%s'\n", val);
		if (strcmp(val, "UNASSIGNED") == 0)         { rctx->layer->lyt = 0; }
		else if (strcmp(val, "ROUTING") == 0)       { rctx->layer->lyt |= PCB_LYT_COPPER; }
		else if (strcmp(val, "COMPONENT") == 0)     { rctx->layer->lyt |= PCB_LYT_COPPER; }
		else if (strcmp(val, "SOLDER_MASK") == 0)   { rctx->layer->lyt |= PCB_LYT_MASK; rctx->layer->comb =  PCB_LYC_SUB | PCB_LYC_AUTO; }
		else if (strcmp(val, "PASTE_MASK") == 0)    { rctx->layer->lyt |= PCB_LYT_PASTE; rctx->layer->comb =  PCB_LYC_AUTO;}
		else if (strcmp(val, "SILK_SCREEN") == 0)   { rctx->layer->lyt |= PCB_LYT_SILK; rctx->layer->comb =  PCB_LYC_AUTO; }
		else if (strcmp(val, "DRILL") == 0)         { rctx->layer->lyt |= PCB_LYT_MECH; rctx->layer->purpose = "proute"; }
		else if (strcmp(val, "ASSEMBLY") == 0)      { rctx->layer->lyt |= PCB_LYT_DOC; rctx->layer->purpose = "assy"; }
		else {
			PADS_ERROR((RND_MSG_ERROR, "Ignoring unknown layer type: %s\n", val));
		}
	}
	else if (strcmp(key, "COMPONENT") == 0) {
		rnd_trace("  component='%s'\n", val);
		if (*val == 'Y')
			rctx->layer->can_have_components = 1;
	}
	else if (strcmp(key, "ASSOCIATED_SILK_SCREEN") == 0) {
		rnd_trace("  ASSOCIATED_SILK_SCREEN='%s'\n", key, val);
		pl->assoc_silk = rnd_strdup(val);
	}
	else if (strcmp(key, "ASSOCIATED_PASTE_MASK") == 0) {
		rnd_trace("  ASSOCIATED_PASTE_MASK='%s'\n", key, val);
		pl->assoc_paste = rnd_strdup(val);
	}
	else if (strcmp(key, "ASSOCIATED_SOLDER_MASK") == 0) {
		rnd_trace("  ASSOCIATED_SOLDER_MASK='%s'\n", key, val);
		pl->assoc_mask = rnd_strdup(val);
	}
	else if (strcmp(key, "ASSOCIATED_ASSEMBLY") == 0) {
		rnd_trace("  ASSOCIATED_ASSEMBLY='%s'\n", key, val);
		pl->assoc_assy = rnd_strdup(val);
	}
	else if (strcmp(key, "COLORS") == 0) {
		if ((res = pads_parse_misc_open(rctx)) <= 0) return res;
		return pads_parse_misc_ignore_block(rctx);
	}

	return 1;
}

static int pads_parse_misc_layer_hdr(pads_read_ctx_t *rctx)
{
	char key[32];
	long id;
	int res;

	if ((res = pads_read_word(rctx, key, sizeof(key), 0)) <= 0) return res;
	if ((res = pads_read_long(rctx, &id)) <= 0) return res;
	pads_eatup_till_nl(rctx);

	if (strcmp(key, "LAYER") != 0) {
		PADS_ERROR((RND_MSG_ERROR, "Expected LAYER block, got '%s' instead\n", key));
		return -1;
	}

	rnd_trace(" layer %ld key='%s'\n", id, key);
	rctx->layer = pads_layer_alloc(id);

	res = pads_parse_misc_generic(rctx, pads_parse_misc_layer);
	if (rctx->layer->lyt != 0)
		pcb_dlcr_layer_reg(&rctx->dlcr, rctx->layer);
	else
		pcb_dlcr_layer_free(rctx->layer);
	rctx->layer = NULL;
	return res;
}


static int pads_parse_misc_layers(pads_read_ctx_t *rctx)
{
	char unit[64];
	int res;

	if ((res = pads_read_word(rctx, unit, sizeof(unit), 0)) <= 0) return res;
	pads_eatup_till_nl(rctx);

	rnd_trace("layers: '%s'\n", unit);
	return pads_parse_misc_generic(rctx, pads_parse_misc_layer_hdr);
}

static int pads_parse_misc_design_rule_line(pads_read_ctx_t *rctx, int level)
{
	if (level == 1) {
		char key[32];
		int res = pads_read_word(rctx, key, sizeof(key), 0);
		if (res > 0) {
			if ((strcmp(key, "COPPER_TO_TRACK") == 0) && ((res = pads_read_coord(rctx, &rctx->clr[PCB_DLCL_TRACE])) <= 0)) return res;
			if ((strcmp(key, "COPPER_TO_VIA") == 0) && ((res = pads_read_coord(rctx, &rctx->clr[PCB_DLCL_VIA])) <= 0)) return res;
			if ((strcmp(key, "COPPER_TO_PAD") == 0) && ((res = pads_read_coord(rctx, &rctx->clr[PCB_DLCL_TRH_TERM])) <= 0)) return res;
			if ((strcmp(key, "COPPER_TO_SMD") == 0) && ((res = pads_read_coord(rctx, &rctx->clr[PCB_DLCL_SMD_TERM])) <= 0)) return res;
		}
	}
	return 1;
}


static int pads_parse_misc_design_rules_hdr(pads_read_ctx_t *rctx)
{
	char rname[32];
	int res;

	if ((res = pads_read_word(rctx, rname, sizeof(rname), 0)) <= 0) return res;
	pads_eatup_till_nl(rctx);

	rnd_trace(" design rule name='%s' got:%d\n", rname, rctx->got_design_rules);
	if (!rctx->got_design_rules) {
		res = pads_parse_misc_lines(rctx, 0, pads_parse_misc_design_rule_line);
		rctx->got_design_rules = 1;
	}
	else
		res = pads_parse_misc_ignore_block(rctx);
	return res;
}

static int pads_parse_misc_rules_hdr(pads_read_ctx_t *rctx)
{
	char key1[32], key2[32];
	int res;

	retry:;

	key2[0] = '\0';
	if ((res = pads_read_word(rctx, key1, sizeof(key1), 0)) <= 0) return res;
	if (*key1 == '\0')
		return 1;
	if (pads_has_field(rctx)) pads_read_word(rctx, key2, sizeof(key2), 0);
	pads_eatup_till_nl(rctx);

	if ((strcmp(key1, "NET_CLASS") == 0) && (strcmp(key2, "DATA") == 0)) /* ignore */
		goto retry;

	rnd_trace(" rules? key='%s' '%s' line=%ld\n", key1, key2, rctx->line);
	if ((strcmp(key1, "GROUP") == 0) && (strcmp(key2, "DATA") == 0)) {
		key2[0] = '\0';
		if ((res = pads_read_word(rctx, key1, sizeof(key1), 0)) <= 0) return res;
		if (pads_has_field(rctx)) pads_read_word(rctx, key2, sizeof(key2), 0);
		pads_eatup_till_nl(rctx);

		if ((strcmp(key1, "DESIGN") == 0) && (strcmp(key2, "RULES") == 0))
			res = pads_parse_misc_generic(rctx, pads_parse_misc_design_rules_hdr);
		else
			res = pads_parse_misc_ignore_block(rctx);
	}
	else
		res = pads_parse_misc_ignore_block(rctx);
	return res;
}

static int pads_parse_misc_rules(pads_read_ctx_t *rctx)
{
	pads_eatup_till_nl(rctx);
	rnd_trace("---------rules: %ld\n", rctx->line);
	return pads_parse_misc_generic(rctx, pads_parse_misc_rules_hdr);
}


static int pads_parse_misc(pads_read_ctx_t *rctx)
{
	pads_eatup_till_nl(rctx);

	rnd_trace("*MISC*\n");
	for(;;) {
		char key[256];
		int c, res;

		retry:;
		pads_eatup_ws(rctx);
		c = fgetc(rctx->f);
		if (c == EOF) {
			rnd_trace("unexpected eof in *MISC*\n");
			return 0;
		}
		if (c == '\n') {
			pads_update_loc(rctx, c);
			goto retry;
		}

		ungetc(c, rctx->f);
		if (c == '{') {
			rnd_trace("misc ignore block from %ld (%ld)!\n", rctx->line, ftell(rctx->f));
			if ((res = pads_parse_misc_ignore_block(rctx)) <= 0) return res;
			continue;
		}

		/*read key; if it is a new *SECTION* then res will be -4 */
		if ((res = pads_read_word(rctx, key, sizeof(key), 0)) <= 0)
			return res;
		if (strcmp(key, "LAYER") == 0) res = pads_parse_misc_layers(rctx);
		else if (strcmp(key, "RULES_SECTION") == 0) res = pads_parse_misc_rules(rctx);
		else if (*key != '\0') {
			pads_eatup_till_nl(rctx);
			res = 1;
		}
		if (res <= 0)
			return res;
	}
	return 1;
}