view pads2gpcb/rdunits.c @ 38:aa0539cc3d41

pads2gpcb project started, skeleton compiles
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Mon, 11 Jan 2016 04:56:51 +0000
parents
children 43ba91b137e2
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "globals.h"

long
convert_input_dim(srcstr)
	char *srcstr;
{
	long accum;
	int sign = 1;
	char *cp;
	int maxdec, ndec;

	cp = srcstr;
	if (*cp == '-') {
		cp++;
		sign = -1;
	}
	if (!isdigit(*cp)) {
inv:		fprintf(stderr, "%s line %d: invalid dimension \"%s\"\n",
			input_filename, input_lineno, srcstr);
		exit(1);
	}
	for (accum = 0; isdigit(*cp); ) {
		accum *= 10;
		accum += *cp++ - '0';
	}
	switch (input_units_current) {
	case 'B':
		maxdec = 0;
		break;
	case 'I':
		maxdec = 2;
		break;
	case 'M':
		maxdec = 6;
		break;
	}
	if (*cp == '.') {
		cp++;
		for (ndec = 0; isdigit(*cp); ndec++) {
			if (ndec >= maxdec) {
				fprintf(stderr,
		"%s line %d: dimension \"%s\": too many digits after '.'\n",
					input_filename, input_lineno, srcstr);
				exit(1);
			}
			accum *= 10;
			accum += *cp++ - '0';
		}
	} else
		ndec = 0;
	if (*cp)
		goto inv;
	while (ndec < maxdec) {
		accum *= 10;
		ndec++;
	}
	switch (input_units_current) {
	case 'B':
		if (accum % 3) {
			fprintf(stderr,
		"%s line %d: basic units dimension \"%s\" not divisible by 3\n",
				input_filename, input_lineno, srcstr);
			exit(1);
		}
		accum /= 3;
		accum *= 2;
		break;
	case 'I':
		accum *= 254;
		break;
	}
	return(accum * sign);
}

parse_input_angle_90s(srcstr)
	char *srcstr;
{
	unsigned long num;
	char *cp;

	num = strtoul(srcstr, &cp, 10);
	switch (num) {
	case 0:
	case 90:
	case 180:
	case 270:
		break;
	default:
		return(-1);
	}
	if (*cp == '.') {
		cp++;
		while (*cp)
			if (*cp++ != '0')
				return(-1);
	} else if (*cp)
		return(-1);
	return(num);
}