view pads2gpcb/gpcbout.c @ 154:2328205328a9 default tip

m4-fp: WR-FPC 1mm pitch FFC/FPC connectors
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 25 Oct 2022 07:36:09 +0000
parents 3bdb1b5ff3d0
children
line wrap: on
line source

#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;
}