view ueda/sverp-bind/starpoints.c @ 16:65a515c20db8

unet-bind: starpoint entry implemented
author Space Falcon <falcon@ivan.Harhan.ORG>
date Sun, 02 Aug 2015 01:12:18 +0000
parents
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "struct.h"

extern struct instance *enter_instance();

extern char *starpoints_file;

struct outcomp *starpoint_list_head;
static struct outcomp **global_tailp = &starpoint_list_head;

process_starpoints_file()
{
	FILE *stream;
	char linebuf[256];
	int lineno;
	register char *cp;
	char *instname, *numstr;
	int npins;
	register struct instance *inst;
	register struct outcomp *oc;

	stream = fopen(starpoints_file, "r");
	if (!stream) {
		perror(starpoints_file);
		exit(1);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, stream); lineno++) {
		cp = index(linebuf, '\n');
		if (!cp) {
			fprintf(stderr,
			"error: %s line %d is too long or unterminated\n",
				starpoints_file, lineno);
			exit(1);
		}
		*cp = '\0';
		for (cp = linebuf; isspace(*cp); cp++)
			;
		if (*cp == '\0' || *cp == '#')
			continue;
		instname = cp;
		while (*cp && !isspace(*cp))
			cp++;
		if (*cp)
			*cp++ = '\0';
		while (isspace(*cp))
			cp++;
		if (*cp == '\0' || *cp == '#') {
npins_error:		fprintf(stderr,
"error in %s line %d: expected number of pins after the instance name\n",
				starpoints_file, lineno);
			exit(1);
		}
		numstr = cp;
		while (*cp && !isspace(*cp))
			cp++;
		if (*cp)
			*cp++ = '\0';
		if (!string_is_valid_decnum(numstr))
			goto npins_error;
		npins = atoi(numstr);
		if (npins < 2) {
			fprintf(stderr,
		"error in %s line %d: a starpoint must have 2 or more pins\n",
				starpoints_file, lineno);
			exit(1);
		}
		inst = enter_instance(instname);
		oc = (struct outcomp *)
			malloc(sizeof(struct outcomp) +
			       sizeof(struct pinconn *) * npins);
		if (!oc) {
			perror("malloc");
			exit(1);
		}
		bzero(oc, sizeof(struct outcomp) +
			  sizeof(struct pinconn *) * npins);
		oc->name = inst->name;
		oc->npins = npins;
		oc->conn_array = (struct pinconn **)(oc + 1);
		inst->outcomp = oc;
		*global_tailp = oc;
		global_tailp = &oc->next;
	}
	fclose(stream);
}