3 * $Id: unihash-mkstatic.c,v 1.2 2004/04/08 01:36:13 mdw Exp $
5 * Build static universal hash tables
7 * (c) 2003 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
46 /*----- Main code ---------------------------------------------------------*/
50 static unsigned long getint(const char *p
, unsigned long max
,
54 unsigned long x
= strtoul(p
, &pp
, 0);
55 if (*pp
|| (max
&& x
> max
))
56 die(EXIT_FAILURE
, "bad %s `%s'", what
, p
);
60 static void version(FILE *fp
)
62 pquis(fp
, "$, mLib version " VERSION
"\n");
65 static void usage(FILE *fp
)
67 pquis(fp
, "Usage: $ [-c] [-o FILE] [-g GUARD] [-i HEADER] [-s SYM]\n\
71 static void help(FILE *fp
)
77 Emits a precomputed unihash_info structure for a given key.\n\
79 -h, --help Show this help text.\n\
80 -v, --version Show the program's version number.\n\
81 -u, --usage Show a terse usage message.\n\
83 -c, --c-source Emit a C source file rather than a header.\n\
84 -k, --key=KEY Use KEY as the universal hashing key.\n\
85 -g, --guard=GUARD Use GUARD as a multiple-inclusion guard constant.\n\
86 -i, --include=HEADER Include HEADER at top of C source file.\n\
87 -s, --symbol=SYM Name the generated table SYM.\n\
88 -o, --output=FILE Write the output to FILE.\n\
92 int main(int argc
, char *argv
[])
94 uint32 key
= 0xe07e5bd1;
98 const char *guard
= 0;
110 static struct option opts
[] = {
111 { "help", 0, 0, 'h' },
112 { "version", 0, 0, 'v' },
113 { "usage", 0, 0, 'u' },
115 { "output", OPTF_ARGREQ
, 0, 'o' },
116 { "c-source", 0, 0, 'c' },
117 { "key", OPTF_ARGREQ
, 0, 'k' },
118 { "symbol", OPTF_ARGREQ
, 0, 's' },
119 { "include", OPTF_ARGREQ
, 0, 'i' },
120 { "guard", OPTF_ARGREQ
, 0, 'g' },
124 int i
= mdwopt(argc
, argv
, "hvu o:ck:s:i:g:", opts
, 0, 0, 0);
155 key
= getint(optarg
, 0xffffffff, "key");
163 if ((flags
& f_bogus
) || optind
!= argc
) {
168 /* --- Sort stuff out --- */
170 unihash_setkey(&u
, key
);
172 sym
= (flags
& f_ctab
) ?
"uhi" : "UHI_INIT";
174 /* --- Start output --- */
179 if (!(flags
& f_ctab
) && !guard
) {
182 if ((p
= malloc(strlen(file
) + 1)) == 0)
183 die(EXIT_FAILURE
, "not enough memory");
185 for (q
= file
; *q
; p
++, q
++) {
186 if (isalnum((unsigned char)*q
))
187 *p
= toupper((unsigned char)*q
);
193 if ((fp
= fopen(file
, "w")) == 0)
194 die(EXIT_FAILURE
, "couldn't write `%s': %s", file
, strerror(errno
));
197 /* --- Dump out the first chunk of the file --- */
202 * Unihash table (key = %08lx) [generated]\n\
207 if (flags
& f_ctab
) {
209 fprintf(fp
, "#include \"%s\"\n\n", inc
);
211 fputs("#include <mLib/unihash.h>\n\n", fp
);
212 fprintf(fp
, "unihash_info %s = { {\n", sym
);
216 fprintf(fp
, "#ifndef %s\n#define %s\n\n", guard
, guard
);
217 n
= fprintf(fp
, "#define %s { {", sym
);
225 /* --- Main output --- */
227 for (i
= 0; i
< N(u
.s
); i
++) {
229 for (j
= 0; j
< N(u
.s
[i
]); j
++) {
231 for (k
= 0; k
< N(u
.s
[i
][j
]); k
++) {
232 fprintf(fp
, " 0x%08lx", (unsigned long)u
.s
[i
][j
][k
]);
233 if (k
< N(u
.s
[i
][j
]) - 1) {
236 fputs(flags
& f_ctab ?
"\n " : "\t\t\t\\\n ", fp
);
239 if (j
< N(u
.s
[i
]) - 1) {
240 fputs(flags
& f_ctab ?
" },\n\n " :
241 " },\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n ", fp
);
244 if (i
< N(u
.s
) - 1) {
245 fputs(flags
& f_ctab ?
" } },\n\n" :
246 " } },\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n", fp
);
252 fputs(flags
& f_ctab ?
" } }\n} };\n" :
253 " } }\t\t\\\n} }\n", fp
);
254 if (!(flags
& f_ctab
) && guard
)
255 fputs("\n#endif\n", fp
);
260 /*----- That's all, folks -------------------------------------------------*/