view ueda/mclutils/csvbom.c @ 139:bf188727e606

donl-rename-parts reader: no tEDAx-style escapes
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 07 Sep 2020 04:25:11 +0000
parents 422385f10084
children
line wrap: on
line source

/*
 * This program generates a BOM in CSV format whose content is a subset
 * of what is generated by ueda-mkbom; it is intended for passing BOM
 * information to assembly shops who would then import into M$ Excel.
 */

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

extern char *MCLfile;
extern char *get_comp_attr(), *get_comp_multiattr();

int check_completeness, refdes_lists = 1;
struct bompart *bomhead;

do_cmdline_opts(argc, argv)
	char **argv;
{
	register int c;

	while ((c = getopt(argc, argv, "cM:p:")) != EOF)
		switch (c) {
		case 'c':
			check_completeness++;
			break;
		case 'M':
			MCLfile = optarg;
			break;
		case 'p':
			set_popopt_list(optarg);
			break;
		default:
			/* getopt prints the error message */
			exit(1);
		}
}

main(argc, argv)
	char **argv;
{
	do_cmdline_opts(argc, argv);
	read_MCL();
	tally_parts();
	output();
	exit(0);
}

output()
{
	register struct component *part;
	register struct bompart *bp;
	register char *attr;
	static char unknownstr[] = "unknown";

	puts("Manufacturer,Part number,Description,Qty,Refdes list");

	for (bp = bomhead; bp; bp = bp->next) {
		part = bp->part;
		attr = get_comp_attr(part, "manufacturer");
		if (attr)
			emit_csv_string(attr);
		else
			emit_csv_string(unknownstr);
		putchar(',');
		if (attr = get_comp_attr(part, "manufacturer_part_number"))
			emit_csv_string(attr);
		else if (attr = get_comp_attr(part, "device"))
			emit_csv_string(attr);
		else
			emit_csv_string(unknownstr);
		putchar(',');
		if (attr = get_comp_attr(part, "description"))
			emit_csv_string(attr);
		printf(",%d,", bp->qty);
		emit_refdes_list(bp->refdeslist);
		putchar('\n');
	}
}

emit_csv_string(str)
	char *str;
{
	char *cp;
	int c;

	if (!index(str, ',') && !index(str, '"')) {
		fputs(str, stdout);
		return;
	}
	putchar('"');
	for (cp = str; c = *cp++; ) {
		if (c == '"')
			putchar(c);
		putchar(c);
	}
	putchar('"');
}

emit_refdes_list(le)
	register struct refdeslist *le;
{
	putchar('"');
	for (; le; le = le->next) {
		printf("%s", le->first);
		if (le->last != le->first)
			printf("-%s", le->last);
		if (le->next)
			putchar(',');
	}
	putchar('"');
}