3a1f57cc433bd62c1e0c503808bf696da071896f
[p5sagit/p5-mst-13.2.git] / ext / SDBM_File / sdbm / sdbm.h
1 /*
2  * sdbm - ndbm work-alike hashed database library
3  * based on Per-Ake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
4  * author: oz@nexus.yorku.ca
5  * status: public domain. 
6  */
7 #define DBLKSIZ 4096
8 #define PBLKSIZ 1024
9 #define PAIRMAX 1008                    /* arbitrary on PBLKSIZ-N */
10 #define SPLTMAX 10                      /* maximum allowed splits */
11                                         /* for a single insertion */
12 #ifdef VMS
13 #define DIRFEXT ".sdbm_dir"
14 #else
15 #define DIRFEXT ".dir"
16 #endif
17 #define PAGFEXT ".pag"
18
19 #ifdef WIN32
20 #undef  __declspec
21 #define __declspec(t)                   /* kludge for EXTCONST below */
22 #endif
23
24 typedef struct {
25         int dirf;                      /* directory file descriptor */
26         int pagf;                      /* page file descriptor */
27         int flags;                     /* status/error flags, see below */
28         long maxbno;                   /* size of dirfile in bits */
29         long curbit;                   /* current bit number */
30         long hmask;                    /* current hash mask */
31         long blkptr;                   /* current block for nextkey */
32         int keyptr;                    /* current key for nextkey */
33         long blkno;                    /* current page to read/write */
34         long pagbno;                   /* current page in pagbuf */
35         char pagbuf[PBLKSIZ];          /* page file block buffer */
36         long dirbno;                   /* current block in dirbuf */
37         char dirbuf[DBLKSIZ];          /* directory file block buffer */
38 } DBM;
39
40 #define DBM_RDONLY      0x1            /* data base open read-only */
41 #define DBM_IOERR       0x2            /* data base I/O error */
42
43 /*
44  * utility macros
45  */
46 #define sdbm_rdonly(db)         ((db)->flags & DBM_RDONLY)
47 #define sdbm_error(db)          ((db)->flags & DBM_IOERR)
48
49 #define sdbm_clearerr(db)       ((db)->flags &= ~DBM_IOERR)  /* ouch */
50
51 #define sdbm_dirfno(db) ((db)->dirf)
52 #define sdbm_pagfno(db) ((db)->pagf)
53
54 typedef struct {
55         char *dptr;
56         int dsize;
57 } datum;
58
59 EXTCONST datum nullitem
60 #ifdef DOINIT
61                         = {0, 0}
62 #endif
63                                    ;
64
65 #if defined(__STDC__) || defined(__cplusplus) || defined(CAN_PROTOTYPE)
66 #define proto(p) p
67 #else
68 #define proto(p) ()
69 #endif
70
71 /*
72  * flags to sdbm_store
73  */
74 #define DBM_INSERT      0
75 #define DBM_REPLACE     1
76
77 /*
78  * ndbm interface
79  */
80 extern DBM *sdbm_open proto((char *, int, int));
81 extern void sdbm_close proto((DBM *));
82 extern datum sdbm_fetch proto((DBM *, datum));
83 extern int sdbm_delete proto((DBM *, datum));
84 extern int sdbm_store proto((DBM *, datum, datum, int));
85 extern datum sdbm_firstkey proto((DBM *));
86 extern datum sdbm_nextkey proto((DBM *));
87
88 /*
89  * other
90  */
91 extern DBM *sdbm_prep proto((char *, char *, int, int));
92 extern long sdbm_hash proto((char *, int));
93
94 #ifndef SDBM_ONLY
95 #define dbm_open sdbm_open
96 #define dbm_close sdbm_close
97 #define dbm_fetch sdbm_fetch
98 #define dbm_store sdbm_store
99 #define dbm_delete sdbm_delete
100 #define dbm_firstkey sdbm_firstkey
101 #define dbm_nextkey sdbm_nextkey
102 #define dbm_error sdbm_error
103 #define dbm_clearerr sdbm_clearerr
104 #endif
105
106 /* Most of the following is stolen from perl.h. */
107 #ifndef H_PERL  /* Include guard */
108
109 /*
110  * The following contortions are brought to you on behalf of all the
111  * standards, semi-standards, de facto standards, not-so-de-facto standards
112  * of the world, as well as all the other botches anyone ever thought of.
113  * The basic theory is that if we work hard enough here, the rest of the
114  * code can be a lot prettier.  Well, so much for theory.  Sorry, Henry...
115  */
116
117 #include <errno.h>
118 #ifdef HAS_SOCKET
119 #   ifdef I_NET_ERRNO
120 #     include <net/errno.h>
121 #   endif
122 #endif
123
124 #if defined(__STDC__) || defined(_AIX) || defined(__stdc__) || defined(__cplusplus)
125 # define STANDARD_C 1
126 #endif
127
128 #include <stdio.h>
129 #include <ctype.h>
130 #include <setjmp.h>
131
132 #if defined(I_UNISTD)
133 #include <unistd.h>
134 #endif
135
136 #ifdef VMS
137 #  include <file.h>
138 #  include <unixio.h>
139 #endif
140
141 #if !defined(MSDOS) && !defined(WIN32) && !defined(VMS)
142 #   ifdef PARAM_NEEDS_TYPES
143 #       include <sys/types.h>
144 #   endif
145 #   include <sys/param.h>
146 #endif
147
148 #ifndef _TYPES_         /* If types.h defines this it's easy. */
149 #   ifndef major                /* Does everyone's types.h define this? */
150 #       include <sys/types.h>
151 #   endif
152 #endif
153
154 #include <sys/stat.h>
155
156 #ifndef SEEK_SET
157 # ifdef L_SET
158 #  define SEEK_SET      L_SET
159 # else
160 #  define SEEK_SET      0  /* Wild guess. */
161 # endif
162 #endif
163
164 /* Use all the "standard" definitions? */
165 #if defined(STANDARD_C) && defined(I_STDLIB)
166 #   include <stdlib.h>
167 #endif /* STANDARD_C */
168
169 #define MEM_SIZE Size_t
170
171 /* This comes after <stdlib.h> so we don't try to change the standard
172  * library prototypes; we'll use our own instead. */
173
174 #if defined(MYMALLOC) && (defined(HIDEMYMALLOC) || defined(EMBEDMYMALLOC))
175
176 #   ifdef HIDEMYMALLOC
177 #       define malloc  Mymalloc
178 #       define calloc  Mycalloc
179 #       define realloc Myremalloc
180 #       define free    Myfree
181 #   endif
182 #   ifdef EMBEDMYMALLOC
183 #       define malloc  Perl_malloc
184 #       define calloc  Perl_calloc
185 #       define realloc Perl_realloc
186 #       define free    Perl_free
187 #   endif
188
189     Malloc_t malloc proto((MEM_SIZE nbytes));
190     Malloc_t calloc proto((MEM_SIZE elements, MEM_SIZE size));
191     Malloc_t realloc proto((Malloc_t where, MEM_SIZE nbytes));
192     Free_t   free proto((Malloc_t where));
193
194 #endif /* MYMALLOC && (HIDEMYMALLOC || EMBEDMYMALLOC) */
195
196 #ifdef I_STRING
197 #include <string.h>
198 #else
199 #include <strings.h>
200 #endif
201
202 #ifdef I_MEMORY
203 #include <memory.h>
204 #endif      
205
206 #ifdef __cplusplus
207 #define HAS_MEMCPY
208 #endif
209
210 #ifdef HAS_MEMCPY
211 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
212 #    ifndef memcpy
213         extern char * memcpy proto((char*, char*, int));
214 #    endif
215 #  endif
216 #else
217 #   ifndef memcpy
218 #       ifdef HAS_BCOPY
219 #           define memcpy(d,s,l) bcopy(s,d,l)
220 #       else
221 #           define memcpy(d,s,l) my_bcopy(s,d,l)
222 #       endif
223 #   endif
224 #endif /* HAS_MEMCPY */
225
226 #ifdef HAS_MEMSET
227 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
228 #    ifndef memset
229         extern char *memset proto((char*, int, int));
230 #    endif
231 #  endif
232 #  define memzero(d,l) memset(d,0,l)
233 #else
234 #   ifndef memzero
235 #       ifdef HAS_BZERO
236 #           define memzero(d,l) bzero(d,l)
237 #       else
238 #           define memzero(d,l) my_bzero(d,l)
239 #       endif
240 #   endif
241 #endif /* HAS_MEMSET */
242
243 #if defined(mips) && defined(ultrix) && !defined(__STDC__)
244 #   undef HAS_MEMCMP
245 #endif
246
247 #if defined(HAS_MEMCMP) && defined(HAS_SANE_MEMCMP)
248 #  if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY)
249 #    ifndef memcmp
250         extern int memcmp proto((char*, char*, int));
251 #    endif
252 #  endif
253 #  ifdef BUGGY_MSC
254   #  pragma function(memcmp)
255 #  endif
256 #else
257 #   ifndef memcmp
258         /* maybe we should have included the full embedding header... */
259 #       ifdef NO_EMBED
260 #           define memcmp my_memcmp
261 #       else
262 #           define memcmp Perl_my_memcmp
263 #       endif
264 #ifndef __cplusplus
265         extern int memcmp proto((char*, char*, int));
266 #endif
267 #   endif
268 #endif /* HAS_MEMCMP */
269
270 #ifndef HAS_BCMP
271 #   ifndef bcmp
272 #       define bcmp(s1,s2,l) memcmp(s1,s2,l)
273 #   endif
274 #endif /* !HAS_BCMP */
275
276 #ifdef HAS_MEMCMP
277 #  define memNE(s1,s2,l) (memcmp(s1,s2,l))
278 #  define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
279 #else
280 #  define memNE(s1,s2,l) (bcmp(s1,s2,l))
281 #  define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
282 #endif
283
284 #ifdef I_NETINET_IN
285 #  ifdef VMS
286 #    include <in.h>
287 #  else
288 #    include <netinet/in.h>
289 #  endif
290 #endif
291
292 #endif /* Include guard */
293