changeset 80:df98a82b807e

ueda-mkbom refdes list output: refdes range detection implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 23 Feb 2017 18:54:47 +0000
parents ac13abc7dc0d
children 6e43956e740d
files ueda/mclutils/mkbom.c
diffstat 1 files changed, 54 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ueda/mclutils/mkbom.c	Mon Feb 01 06:11:57 2016 +0000
+++ b/ueda/mclutils/mkbom.c	Thu Feb 23 18:54:47 2017 +0000
@@ -2,6 +2,7 @@
  * This program generates a procurement-oriented BOM from the MCL.
  */
 
+#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -16,7 +17,8 @@
 extern struct component *find_partdef_by_name();
 
 struct refdeslist {
-	char	*refdes;
+	char	*first;
+	char	*last;
 	struct	refdeslist *next;
 };
 
@@ -124,6 +126,37 @@
 	add_refdes_to_bompart(bp, refdes);
 }
 
+is_string_num(str)
+	char *str;
+{
+	if (*str < '1' || *str > '9')
+		return(0);
+	while (isdigit(*str))
+		str++;
+	if (*str)
+		return(0);
+	else
+		return(1);
+}
+
+is_refdes_sequential(str1, str2)
+	char *str1, *str2;
+{
+	int num1, num2;
+
+	while (isupper(*str1))
+		if (*str1++ != *str2++)
+			return(0);
+	if (!is_string_num(str1) || !is_string_num(str2))
+		return(0);
+	num1 = atoi(str1);
+	num2 = atoi(str2);
+	if (num2 == num1 + 1)
+		return(1);
+	else
+		return(0);
+}
+
 add_refdes_to_bompart(bp, refdes)
 	struct bompart *bp;
 	char *refdes;
@@ -132,16 +165,28 @@
 
 	if (!refdes_lists)
 		return;
-	for (lep = &bp->refdeslist; le = *lep; lep = &le->next)
+	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->refdes = refdes;
+	le->first = refdes;
+	le->last = refdes;
 	le->next = NULL;
 	*lep = le;
+	return(0);
 }
 
 output()
@@ -238,7 +283,9 @@
 	register int acc, i;
 
 	for (acc = 0; le; le = le->next) {
-		i = strlen(le->refdes) + 1;
+		i = strlen(le->first) + 1;
+		if (le->last != le->first)
+			i += strlen(le->last) + 1;
 		if (le->next)
 			i++;
 		if (acc && (acc + i >= 80)) {
@@ -249,7 +296,9 @@
 			putchar(' ');
 			acc++;
 		}
-		printf(" %s", le->refdes);
+		printf(" %s", le->first);
+		if (le->last != le->first)
+			printf("-%s", le->last);
 		if (le->next)
 			putchar(',');
 		acc += i;