view lunakpd1/placetool/placetool.c @ 16:7e564c546dde

lunakpd1: beginning of sensible layout
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 09 May 2020 06:08:40 +0000
parents 36a6ba7f30ca
children
line wrap: on
line source

#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>

char *infname;
FILE *inf;
int xstart, xstep, ystart, ystep;
char linebuf[512], parsebuf[512], *fields[11];
int lineno;

parse_element_line()
{
	int count;
	char *cp;

	cp = parsebuf;
	for (count = 0; ; ) {
		if (!isdigit(*cp) && *cp != '-' && *cp != '\"') {
inv:			fprintf(stderr, "%s line %d: invalid Element line\n",
				infname, lineno);
			exit(1);
		}
		if (count >= 11)
			goto inv;
		fields[count++] = cp;
		while (*cp && *cp != ' ' && *cp != ']')
			cp++;
		if (!*cp)
			goto inv;
		if (*cp == ']') {
			*cp++ = '\0';
			break;
		}
		*cp++ = '\0';
	}
	if (count != 11 || *cp != '\n')
		goto inv;
}

emit_new_element_line()
{
	int row, col;

	row = fields[2][2] - '0';
	col = fields[2][3] - '0';
	printf("Element[%s %s %s %s %dmm %dmm %s %s %s %s %s]\n",
		fields[0], fields[1], fields[2], fields[3],
		xstart + col * xstep, ystart + row * ystep,
		fields[6], fields[7], fields[8], fields[9], fields[10]);
}

main(argc, argv)
	char **argv;
{
	if (argc != 6) {
		fprintf(stderr,
			"usage: %s input-file x-start x-step y-start y-step\n",
			argv[0]);
		exit(1);
	}
	infname = argv[1];
	xstart = atoi(argv[2]);
	xstep = atoi(argv[3]);
	ystart = atoi(argv[4]);
	ystep = atoi(argv[5]);
	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
		if (!index(linebuf, '\n')) {
			fprintf(stderr, "%s line %d: missing newline\n",
				infname, lineno);
			exit(1);
		}
		if (strncmp(linebuf, "Element[", 8)) {
			fputs(linebuf, stdout);
			continue;
		}
		strcpy(parsebuf, linebuf + 8);
		parse_element_line();
		if (fields[2][0] != '\"' || fields[2][1] != 'S' ||
		    !isdigit(fields[2][2]) || !isdigit(fields[2][3]) ||
		    fields[2][4] != '\"' || fields[2][5]) {
			fputs(linebuf, stdout);
			continue;
		}
		emit_new_element_line();
	}
	exit(0);
}