changeset 13:1f3283f8e482

unet-bind: instance hash implemented
author Space Falcon <falcon@ivan.Harhan.ORG>
date Sun, 02 Aug 2015 00:00:15 +0000
parents 51893347bc42
children 068ea2458c5d
files ueda/sverp-bind/Makefile ueda/sverp-bind/insthash.c
diffstat 2 files changed, 61 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ueda/sverp-bind/Makefile	Sat Aug 01 23:33:05 2015 +0000
+++ b/ueda/sverp-bind/Makefile	Sun Aug 02 00:00:15 2015 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	main.o outcomp.o
+OBJS=	insthash.o main.o outcomp.o
 LIBS=	../libueda/libueda.a
 PROG=	unet-bind
 BINDIR=	/usr/local/bin
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ueda/sverp-bind/insthash.c	Sun Aug 02 00:00:15 2015 +0000
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "struct.h"
+
+#define	HASH_SIZE	1103
+static struct instance *hashtab[HASH_SIZE];
+
+static int
+hash_instname(str)
+	char *str;
+{
+	register u_long accum = 0;
+	register char *cp;
+	register int c, i;
+
+	for (cp = str, i = 1; c = *cp; cp++, i++)
+		accum += c * i;
+	return(accum % HASH_SIZE);
+}
+
+struct instance *
+enter_instance(newname)
+	char *newname;
+{
+	register struct instance *n, **np;
+	int namelen;
+
+	for (np = hashtab + hash_instname(newname); n = *np;
+	     np = &n->nextinhash)
+		if (!strcmp(n->name, newname)) {
+			fprintf(stderr, "error: duplicate instance name %s\n",
+				newname);
+			exit(1);
+		}
+	namelen = strlen(newname);
+	n = (struct instance *) malloc(sizeof(struct instance) + namelen + 1);
+	if (!n) {
+		perror("malloc");
+		exit(1);
+	}
+	bzero(n, sizeof(struct instance));
+	n->name = (char *)(n + 1);
+	strcpy(n->name, newname);
+	*np = n;
+	return n;
+}
+
+struct instance *
+find_instance(soughtname)
+	register char *soughtname;
+{
+	register struct instance *n;
+
+	for (n = hashtab[hash_instname(soughtname)]; n; n = n->nextinhash)
+		if (!strcmp(n->name, soughtname))
+			return(n);
+	return(0);
+}