1 /* ========================================================================
2  * Copyright 2008 Mark Crispin
3  * ========================================================================
4  */
5 
6 /*
7  * Program:	Miscellaneous utility routines
8  *
9  * Author:	Mark Crispin
10  *
11  * Date:	5 July 1988
12  * Last Edited:	19 November 2008
13  *
14  * Previous versions of this file were
15  *
16  * Copyright 1988-2006 University of Washington
17  *
18  * Licensed under the Apache License, Version 2.0 (the "License");
19  * you may not use this file except in compliance with the License.
20  * You may obtain a copy of the License at
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * This original version of this file is
25  * Copyright 1988 Stanford University
26  * and was developed in the Symbolic Systems Resources Group of the Knowledge
27  * Systems Laboratory at Stanford University in 1987-88, and was funded by the
28  * Biomedical Research Technology Program of the NationalInstitutes of Health
29  * under grant number RR-00785.
30  */
31 
32 /* Hash table operations */
33 
34 #define HASHMULT 29		/* hash polynomial multiplier */
35 
36 #define HASHENT struct hash_entry
37 
38 HASHENT {
39   HASHENT *next;		/* next entry with same hash code */
40   char *name;			/* name of this hash entry */
41   void *data[1];		/* data of this hash entry */
42 };
43 
44 
45 #define HASHTAB struct hash_table
46 
47 HASHTAB {
48   size_t size;			/* size of this table */
49   HASHENT *table[1];		/* table */
50 };
51 
52 
53 /* KLUDGE ALERT!!!
54  *
55  * Yes, write() is overridden here instead of in osdep.  This
56  * is because misc.h is one of the last files that most things #include, so
57  * this should avoid problems with some system #include file.
58  */
59 
60 #define write safe_write
61 
62 
63 /* Some C compilers have these as macros */
64 
65 #undef min
66 #undef max
67 
68 
69 /* And some C libraries have these as int functions */
70 
71 #define min Min
72 #define max Max
73 
74 
75 /* Compatibility definitions */
76 
77 #define pmatch(s,pat) \
78   pmatch_full (s,pat,NIL)
79 
80 /* Function prototypes */
81 
82 unsigned char *ucase (unsigned char *string);
83 unsigned char *lcase (unsigned char *string);
84 char *cpystr (const char *string);
85 char *cpytxt (SIZEDTEXT *dst,char *text,unsigned long size);
86 char *textcpy (SIZEDTEXT *dst,SIZEDTEXT *src);
87 char *textcpystring (SIZEDTEXT *text,STRING *bs);
88 char *textcpyoffstring (SIZEDTEXT *text,STRING *bs,unsigned long offset,
89 			unsigned long size);
90 unsigned long find_rightmost_bit (unsigned long *valptr);
91 long min (long i,long j);
92 long max (long i,long j);
93 long search (unsigned char *base,long basec,unsigned char *pat,long patc);
94 long ssearch (unsigned char *base,long basec,unsigned char *pat,long patc);
95 HASHTAB *hash_create (size_t size);
96 void hash_destroy (HASHTAB **hashtab);
97 void hash_reset (HASHTAB *hashtab);
98 unsigned long hash_index (HASHTAB *hashtab,char *key);
99 void **hash_lookup (HASHTAB *hashtab,char *key);
100 HASHENT *hash_add (HASHTAB *hashtab,char *key,void *data,long extra);
101 void **hash_lookup_and_add (HASHTAB *hashtab,char *key,void *data,long extra);
102 unsigned char hex2byte (unsigned char c1,unsigned char c2);
103 int compare_ulong (unsigned long l1,unsigned long l2);
104 int compare_uchar (unsigned char c1,unsigned char c2);
105 int compare_string (unsigned char *s1,unsigned char *s2);
106 int compare_cstring (unsigned char *s1,unsigned char *s2);
107 int compare_csizedtext (unsigned char *s1,SIZEDTEXT *s2);
108