diff pads2gpcb/gpcbout.c @ 45:3bdb1b5ff3d0

pads2gpcb: gpcb dimension output implemented
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Sat, 30 Jan 2016 07:15:31 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pads2gpcb/gpcbout.c	Sat Jan 30 07:15:31 2016 +0000
@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+static char *
+generate_decimal(dim, nfract, cp)
+	long dim;
+	int nfract;
+	char *cp;
+{
+	int i;
+
+	*cp = '\0';
+	for (i = 0; i < nfract; i++) {
+		*--cp = '0' + dim % 10;
+		dim /= 10;
+	}
+	*--cp = '.';
+	do {
+		*--cp = '0' + dim % 10;
+		dim /= 10;
+	} while (dim);
+	return cp;
+}
+
+static void
+trim_decimal(str)
+	char *str;
+{
+	char *point, *cp;
+
+	point = index(str, '.');
+	if (!point) {
+		fprintf(stderr,
+			"BUG: gpcbout.c:trim_decimal(): no '.' found\n");
+		exit(1);
+	}
+	cp = index(str, '\0');
+	while (cp > point + 1) {
+		if (cp[-1] != '0')
+			break;
+		cp--;
+	}
+	if (cp == point + 1)
+		cp--;
+	*cp = '\0';
+}
+
+char *
+output_gpcb_dimension(dim, buf)
+	long dim;
+	char *buf;
+{
+	int negative = 0;
+	int nfract;
+	char *unit_suffix, *outptr;
+
+	if (dim == 0)
+		return "0";
+	if (dim < 0) {
+		negative = 1;
+		dim = -dim;
+	}
+	if (dim % 254) {
+		nfract = 6;
+		unit_suffix = "mm";
+	} else {
+		dim /= 254;
+		nfract = 2;
+		unit_suffix = "mil";
+	}
+	outptr = generate_decimal(dim, nfract, buf + 12);
+	trim_decimal(outptr);
+	strcat(outptr, unit_suffix);
+	if (negative)
+		*--outptr = '-';
+	return outptr;
+}