view ueda/mclutils/bomtally.c @ 83:88cdef7e6b1b

BOM tallying code factored out of ueda-mkbom
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 23 Feb 2017 19:27:14 +0000
parents
children
line wrap: on
line source

/*
 * This code generates a tallied form of the BOM from the MCL,
 * to be used for generation of various BOM output formats.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../libueda/mcl.h"
#include "bomstruct.h"

extern struct component components[];
extern int ncomponents;
extern char *get_comp_attr();
extern struct component *find_partdef_by_name();

extern int check_completeness, refdes_lists;
extern struct bompart *bomhead;

tally_parts()
{
	int c;
	register struct component *comp;
	register char *attr;
	struct component *socket;

	for (comp = components, c = 0; c < ncomponents; comp++, c++) {
		if (!check_component_popopt(comp))
			continue;
		if (comp->partdef == NULL) {
			attr = get_comp_attr(comp, "part");
			if (attr && !strcmp(attr, "none"))
				continue;
			if (check_completeness)
				fprintf(stderr, "%s has no part defined\n",
					comp->name);
			continue;
		}
		add_part_to_bom(comp->partdef, comp->name);
		attr = get_comp_attr(comp, "socket");
		if (attr) {
			socket = find_partdef_by_name(attr);
			if (socket)
				add_part_to_bom(socket, comp->name);
			else
				fprintf(stderr,
					"%s: socket part %s not found\n",
					comp->name, attr);
		}
	}
}

add_part_to_bom(part, refdes)
	struct component *part;
	char *refdes;
{
	register struct bompart *bp, **bpp;

	for (bpp = &bomhead; bp = *bpp; bpp = &bp->next)
		if (bp->part == part) {
			bp->qty++;
			if (refdes_lists)
				add_refdes_to_bompart(bp, refdes);
			return;
		}
	/* new part */
	bp = (struct bompart *) malloc(sizeof(struct bompart));
	if (bp == NULL) {
		perror("malloc");
		exit(1);
	}
	bp->part = part;
	bp->qty = 1;
	bp->next = NULL;
	bp->refdeslist = NULL;
	*bpp = bp;
	if (refdes_lists)
		add_refdes_to_bompart(bp, refdes);
}

add_refdes_to_bompart(bp, refdes)
	struct bompart *bp;
	char *refdes;
{
	register struct refdeslist *le, **lep;

	if (!bp->refdeslist) {
		lep = &bp->refdeslist;
		goto add_new;
	}
	for (le = bp->refdeslist; le->next; le = le->next)
		;
	if (is_refdes_sequential(le->last, refdes)) {
		le->last = refdes;
		return(1);
	}
	lep = &le->next;
add_new:
	le = (struct refdeslist *) malloc(sizeof(struct refdeslist));
	if (!le) {
		perror("malloc");
		exit(1);
	}
	le->first = refdes;
	le->last = refdes;
	le->next = NULL;
	*lep = le;
	return(0);
}