aboutsummaryrefslogtreecommitdiffstats
path: root/mktable.c
blob: b21d6e328d450ce0b7d8011fd9ebe837c5d09b82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* $Id: mktable.c,v 1.8.2.1 2002/02/04 16:26:29 ukai Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "config.h"
#include "hash.h"
#include "Str.h"
#include <gc.h>

#include "gcmain.c"

/* *INDENT-OFF* */
defhash(HashItem_ss *, int, hss_i)
/* *INDENT-ON* */

#define keycomp(x,y) ((x)==(y))

/* XXX: we assume sizeof(unsigned long) >= sizeof(void *) */
static unsigned long
hashfunc(HashItem_ss * x)
{
    return (unsigned long)x;
}

/* *INDENT-OFF* */
defhashfunc(HashItem_ss *, int, hss_i)
/* *INDENT-ON* */

int
MAIN(int argc, char *argv[], char **envp)
{
    FILE *f;
    Hash_ss *hash;
    HashItem_ss **hashitems, *hi;
    int size, n, i, j;
    Str s, name, fbase;
    char *p;
    Hash_hss_i *rhash;

    if (argc != 3) {
	fprintf(stderr, "usage: %s hashsize file.tab > file.c\n", argv[0]);
	exit(1);
    }
    size = atoi(argv[1]);
    if (size <= 0) {
	fprintf(stderr, "hash size should be positive\n");
	exit(1);
    }
    if ((f = fopen(argv[2], "r")) == NULL) {
	fprintf(stderr, "Can't open %s\n", argv[2]);
	exit(1);
    }
    fbase = Strnew_charp(argv[2]);
    if (strchr(fbase->ptr, '.'))
	while (Strlastchar(fbase) != '.')
	    Strshrink(fbase, 1);
    Strshrink(fbase, 1);

    hash = newHash_ss(size);
    printf("#include \"hash.h\"\n");
    for (;;) {
	s = Strfgets(f);
	if (s->length == 0)
	    exit(0);
	Strremovetrailingspaces(s);
	if (Strcmp_charp(s, "%%") == 0)
	    break;
	puts(s->ptr);
    }
    n = 0;
    for (;;) {
	s = Strfgets(f);
	if (s->length == 0)
	    break;
	Strremovefirstspaces(s);
	Strremovetrailingspaces(s);
	name = Strnew();
	for (p = s->ptr; *p; p++) {
	    if (isspace(*p))
		break;
	    Strcat_char(name, *p);
	}
	while (*p && isspace(*p))
	    p++;
	putHash_ss(hash, name->ptr, p);
	n++;
    }
    fclose(f);

    hashitems = (HashItem_ss **) GC_malloc(sizeof(HashItem_ss *) * n);
    rhash = newHash_hss_i(n * 2);
    j = 0;
    for (i = 0; i < hash->size; i++) {
	for (hi = hash->tab[i]; hi != NULL; hi = hi->next) {
	    hashitems[j] = hi;
	    putHash_hss_i(rhash, hi, j);
	    j++;
	}
    }
    printf("static HashItem_si MyHashItem[] = {\n");
    for (i = 0; i < j; i++) {
	printf("  /* %d */ {\"%s\",%s,", i,
	       hashitems[i]->key, hashitems[i]->value);
	if (hashitems[i]->next == NULL) {
	    printf("NULL},\n");
	}
	else {
	    printf("&MyHashItem[%d]},\n",
		   getHash_hss_i(rhash, hashitems[i]->next, -1));
	}
    }
    printf("};\n\nstatic HashItem_si *MyHashItemTbl[] = {\n");

    for (i = 0; i < hash->size; i++) {
	if (hash->tab[i])
	    printf("  &MyHashItem[%d],\n",
		   getHash_hss_i(rhash, hash->tab[i], -1));
	else
	    printf("  NULL,\n");
    }
    printf("};\n\n");
    printf("Hash_si %s = {%d, MyHashItemTbl};\n", fbase->ptr, hash->size);

    exit(0);
}