home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / dbm / include / mcom_db.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.3 KB  |  427 lines

  1. /*- 
  2.  * Copyright (c) 1990, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  *    @(#)db.h    8.7 (Berkeley) 6/16/94
  34.  */
  35.  
  36. #ifndef _DB_H_
  37. #define    _DB_H_
  38.  
  39. #include "prtypes.h"
  40.  
  41. #ifdef __sgi
  42. #define BYTE_ORDER BIG_ENDIAN
  43. #define BIG_ENDIAN      4321
  44. #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
  45. #define    __BIT_TYPES_DEFINED__
  46. #endif
  47.  
  48. #ifdef __sun
  49. #define BIG_ENDIAN      4321
  50. #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
  51.  
  52. #ifndef SVR4
  53. /* compat.h is only in 4.1.3 machines. - dp */
  54. #include <compat.h>
  55. #endif
  56.  
  57. /* XXX - dp
  58.  * Need to find a general way of defining endian-ness in SunOS 5.3
  59.  * SunOS 5.4 defines _BIG_ENDIAN and _LITTLE_ENDIAN
  60.  * SunOS 5.3 does nothing like this.
  61.  */
  62.  
  63. #ifndef BYTE_ORDER
  64.  
  65. #if defined(_BIG_ENDIAN)
  66. #define BYTE_ORDER BIG_ENDIAN
  67. #elif defined(_LITTLE_ENDIAN)
  68. #define BYTE_ORDER LITTLE_ENDIAN
  69. #elif !defined(SVR4)
  70. /* 4.1.3 is always BIG_ENDIAN as it was released only on sparc platforms. */
  71. #define BYTE_ORDER BIG_ENDIAN
  72. #elif !defined(vax) && !defined(ntohl) && !defined(lint) && !defined(i386)
  73. /* 5.3 big endian. Copied this above line from sys/byteorder.h */
  74. /* Now we are in a 5.3 SunOS rather non 5.4 or above SunOS  */
  75. #define BYTE_ORDER BIG_ENDIAN
  76. #else
  77. #define BYTE_ORDER LITTLE_ENDIAN
  78. #endif
  79.  
  80. #endif /* !BYTE_ORDER */
  81. #endif /* __sun */
  82.  
  83. #ifdef __linux
  84. # include <endian.h>
  85. # ifndef BYTE_ORDER
  86. #  define BYTE_ORDER    __BYTE_ORDER
  87. #  define BIG_ENDIAN    __BIG_ENDIAN
  88. #  define LITTLE_ENDIAN __LITTLE_ENDIAN
  89. # endif
  90. #endif /* __linux */
  91.  
  92. #ifdef SCO
  93. #include <sys/types.h>
  94. #include <sys/byteorder.h>
  95. #include <sys/bitypes.h>
  96. #define MAXPATHLEN     1024              
  97. #endif
  98.  
  99. #ifdef SNI
  100. #include <sys/types.h>
  101. #include <sys/byteorder.h>
  102. /* #include <sys/hetero.h> */
  103. #define BYTE_ORDER BIG_ENDIAN
  104. #define BIG_ENDIAN      4321
  105. #define LITTLE_ENDIAN   1234
  106. #endif
  107.  
  108. #ifdef UNIXWARE
  109. #include <sys/types.h>
  110. #include <sys/byteorder.h>
  111. #endif
  112.  
  113. #ifdef NCR
  114. #include <sys/types.h>
  115. #include <sys/byteorder.h>
  116. #include <sys/endian.h>
  117. #endif
  118.  
  119. #ifdef macintosh
  120. #include <unix.h>
  121. #endif
  122.  
  123. #ifndef macintosh
  124. #include <fcntl.h>
  125. #endif
  126.  
  127. #ifdef _WINDOWS
  128. #include <stdio.h>
  129. #include <io.h>
  130. #include <limits.h>
  131. #define MAXPATHLEN     1024              
  132.  
  133. #define    EFTYPE        EINVAL        /* POSIX 1003.1 format errno. */
  134.  
  135. #ifndef    STDERR_FILENO
  136. #define    STDIN_FILENO    0        /* ANSI C #defines */
  137. #define    STDOUT_FILENO    1
  138. #define    STDERR_FILENO    2
  139. #endif
  140.  
  141. #ifndef O_ACCMODE            /* POSIX 1003.1 access mode mask. */
  142. #define    O_ACCMODE    (O_RDONLY|O_WRONLY|O_RDWR)
  143. #endif
  144.  
  145. #ifdef BYTE_ORDER
  146. #undef BYTE_ORDER
  147. #endif
  148.  
  149. #define BYTE_ORDER LITTLE_ENDIAN
  150. #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
  151. #define BIG_ENDIAN      4321
  152. #endif
  153.  
  154. #if defined(_WINDOWS) && !defined(_WIN32)
  155. /* 16 bit windows defines */
  156. #define    MAX_PAGE_NUMBER    0xffffffff    /* >= # of pages in a file */
  157. #endif
  158.  
  159.  
  160. #ifdef macintosh
  161. #include <stdio.h>
  162. #include "xp_mcom.h"
  163. #ifndef NSPR20
  164. #include "prmacos.h"
  165. #endif
  166. #define BIG_ENDIAN 4321
  167. #define LITTLE_ENDIAN 1234
  168. #define BYTE_ORDER BIG_ENDIAN
  169. #define O_ACCMODE       3       /* Mask for file access modes */
  170. #define EFTYPE 2000
  171. XP_BEGIN_PROTOS
  172. int mkstemp(const char *path);
  173. XP_END_PROTOS
  174. #endif    /* MACINTOSH */
  175.  
  176. #ifndef macintosh
  177. #include <sys/types.h>
  178. #endif
  179.  
  180. #if !defined(_WINDOWS) && !defined(macintosh)
  181. #include <sys/stat.h>
  182. #include <errno.h>
  183. #endif
  184.  
  185. #include "cdefs.h"
  186.  
  187. #ifndef _WINDOWS  /* included above to prevent spurious warnings chouck 12-Sep-95 */
  188. #include <limits.h>
  189. #endif
  190.  
  191. #ifndef MIN
  192. #define MIN(x, y)    (((x) < (y)) ? (x) : (y))
  193. #endif
  194.  
  195. #ifndef MAX
  196. #define MAX(x, y)    (((x) > (y)) ? (x) : (y))
  197. #endif
  198.  
  199. #define    RET_ERROR    -1        /* Return values. */
  200. #define    RET_SUCCESS     0
  201. #define    RET_SPECIAL     1
  202.  
  203. #if defined(__386BSD__) || defined(SCO)
  204. #define    __BIT_TYPES_DEFINED__
  205. #endif
  206.  
  207. #define    MAX_PAGE_NUMBER    0xffffffff    /* >= # of pages in a file */
  208.  
  209. #ifndef __sgi
  210. typedef uint32    pgno_t;
  211. #endif
  212.  
  213. #define    MAX_PAGE_OFFSET    65535        /* >= # of bytes in a page */
  214. typedef uint16    indx_t;
  215. #define    MAX_REC_NUMBER    0xffffffff    /* >= # of records in a tree */
  216. typedef uint32    recno_t;
  217.  
  218. /* define EFTYPE since most don't */
  219. #ifndef EFTYPE
  220. #define EFTYPE      EINVAL      /* POSIX 1003.1 format errno. */
  221. #endif
  222.  
  223. /* Key/data structure -- a Data-Base Thang. */
  224. typedef struct {
  225.     void    *data;            /* data */
  226.     size_t     size;            /* data length */
  227. } DBT;
  228.  
  229. /* Routine flags. */
  230. #define    R_CURSOR    1        /* del, put, seq */
  231. #define    __R_UNUSED    2        /* UNUSED */
  232. #define    R_FIRST        3        /* seq */
  233. #define    R_IAFTER    4        /* put (RECNO) */
  234. #define    R_IBEFORE    5        /* put (RECNO) */
  235. #define    R_LAST        6        /* seq (BTREE, RECNO) */
  236. #define    R_NEXT        7        /* seq */
  237. #define    R_NOOVERWRITE    8        /* put */
  238. #define    R_PREV        9        /* seq (BTREE, RECNO) */
  239. #define    R_SETCURSOR    10        /* put (RECNO) */
  240. #define    R_RECNOSYNC    11        /* sync (RECNO) */
  241.  
  242. typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
  243.  
  244. typedef enum { LockOutDatabase, UnlockDatabase } DBLockFlagEnum;
  245.  
  246. /*
  247.  * !!!
  248.  * The following flags are included in the dbopen(3) call as part of the
  249.  * open(2) flags.  In order to avoid conflicts with the open flags, start
  250.  * at the top of the 16 or 32-bit number space and work our way down.  If
  251.  * the open flags were significantly expanded in the future, it could be
  252.  * a problem.  Wish I'd left another flags word in the dbopen call.
  253.  *
  254.  * !!!
  255.  * None of this stuff is implemented yet.  The only reason that it's here
  256.  * is so that the access methods can skip copying the key/data pair when
  257.  * the DB_LOCK flag isn't set.
  258.  */
  259. #if UINT_MAX > 65535
  260. #define    DB_LOCK        0x20000000    /* Do locking. */
  261. #define    DB_SHMEM    0x40000000    /* Use shared memory. */
  262. #define    DB_TXN        0x80000000    /* Do transactions. */
  263. #else
  264. #define    DB_LOCK            0x2000    /* Do locking. */
  265. #define    DB_SHMEM        0x4000    /* Use shared memory. */
  266. #define    DB_TXN            0x8000    /* Do transactions. */
  267. #endif
  268.  
  269. /* Access method description structure. */
  270. typedef struct __db {
  271.     DBTYPE type;            /* Underlying db type. */
  272.     int (*close)    (struct __db *);
  273.     int (*del)    (const struct __db *, const DBT *, uint);
  274.     int (*get)    (const struct __db *, const DBT *, DBT *, uint);
  275.     int (*put)    (const struct __db *, DBT *, const DBT *, uint);
  276.     int (*seq)    (const struct __db *, DBT *, DBT *, uint);
  277.     int (*sync)    (const struct __db *, uint);
  278.     void *internal;            /* Access method private. */
  279.     int (*fd)    (const struct __db *);
  280. } DB;
  281.  
  282. #define    BTREEMAGIC    0x053162
  283. #define    BTREEVERSION    3
  284.  
  285. /* Structure used to pass parameters to the btree routines. */
  286. typedef struct {
  287. #define    R_DUP        0x01    /* duplicate keys */
  288.     uint32    flags;
  289.     uint    cachesize;    /* bytes to cache */
  290.     int    maxkeypage;    /* maximum keys per page */
  291.     int    minkeypage;    /* minimum keys per page */
  292.     uint    psize;        /* page size */
  293.     int    (*compare)    /* comparison function */
  294.         (const DBT *, const DBT *);
  295.     size_t    (*prefix)    /* prefix function */
  296.         (const DBT *, const DBT *);
  297.     int    lorder;        /* byte order */
  298. } BTREEINFO;
  299.  
  300. #define    HASHMAGIC    0x061561
  301. #define    HASHVERSION    2
  302.  
  303. /* Structure used to pass parameters to the hashing routines. */
  304. typedef struct {
  305.     uint    bsize;        /* bucket size */
  306.     uint    ffactor;    /* fill factor */
  307.     uint    nelem;        /* number of elements */
  308.     uint    cachesize;    /* bytes to cache */
  309.     uint32        /* hash function */
  310.         (*hash) (const void *, size_t);
  311.     int    lorder;        /* byte order */
  312. } HASHINFO;
  313.  
  314. /* Structure used to pass parameters to the record routines. */
  315. typedef struct {
  316. #define    R_FIXEDLEN    0x01    /* fixed-length records */
  317. #define    R_NOKEY        0x02    /* key not required */
  318. #define    R_SNAPSHOT    0x04    /* snapshot the input */
  319.     uint32    flags;
  320.     uint    cachesize;    /* bytes to cache */
  321.     uint    psize;        /* page size */
  322.     int    lorder;        /* byte order */
  323.     size_t    reclen;        /* record length (fixed-length records) */
  324.     uint8    bval;        /* delimiting byte (variable-length records */
  325.     char    *bfname;    /* btree file name */ 
  326. } RECNOINFO;
  327.  
  328. /* #ifdef __DBINTERFACE_PRIVATE */
  329. /*
  330.  * Little endian <==> big endian 32-bit swap macros.
  331.  *    M_32_SWAP    swap a memory location
  332.  *    P_32_SWAP    swap a referenced memory location
  333.  *    P_32_COPY    swap from one location to another
  334.  */
  335. #define    M_32_SWAP(a) {                            \
  336.     uint32 _tmp = a;                        \
  337.     ((char *)&a)[0] = ((char *)&_tmp)[3];                \
  338.     ((char *)&a)[1] = ((char *)&_tmp)[2];                \
  339.     ((char *)&a)[2] = ((char *)&_tmp)[1];                \
  340.     ((char *)&a)[3] = ((char *)&_tmp)[0];                \
  341. }
  342. #define    P_32_SWAP(a) {                            \
  343.     uint32 _tmp = *(uint32 *)a;                \
  344.     ((char *)a)[0] = ((char *)&_tmp)[3];                \
  345.     ((char *)a)[1] = ((char *)&_tmp)[2];                \
  346.     ((char *)a)[2] = ((char *)&_tmp)[1];                \
  347.     ((char *)a)[3] = ((char *)&_tmp)[0];                \
  348. }
  349. #define    P_32_COPY(a, b) {                        \
  350.     ((char *)&(b))[0] = ((char *)&(a))[3];                \
  351.     ((char *)&(b))[1] = ((char *)&(a))[2];                \
  352.     ((char *)&(b))[2] = ((char *)&(a))[1];                \
  353.     ((char *)&(b))[3] = ((char *)&(a))[0];                \
  354. }
  355.  
  356. /*
  357.  * Little endian <==> big endian 16-bit swap macros.
  358.  *    M_16_SWAP    swap a memory location
  359.  *    P_16_SWAP    swap a referenced memory location
  360.  *    P_16_COPY    swap from one location to another
  361.  */
  362. #define    M_16_SWAP(a) {                            \
  363.     uint16 _tmp = a;                        \
  364.     ((char *)&a)[0] = ((char *)&_tmp)[1];                \
  365.     ((char *)&a)[1] = ((char *)&_tmp)[0];                \
  366. }
  367. #define    P_16_SWAP(a) {                            \
  368.     uint16 _tmp = *(uint16 *)a;                \
  369.     ((char *)a)[0] = ((char *)&_tmp)[1];                \
  370.     ((char *)a)[1] = ((char *)&_tmp)[0];                \
  371. }
  372. #define    P_16_COPY(a, b) {                        \
  373.     ((char *)&(b))[0] = ((char *)&(a))[1];                \
  374.     ((char *)&(b))[1] = ((char *)&(a))[0];                \
  375. }
  376. /* #endif */
  377.  
  378. __BEGIN_DECLS
  379. #if defined(__WATCOMC__) || defined(__WATCOM_CPLUSPLUS__)
  380. extern DB *
  381. #else
  382. PR_EXTERN(DB *)
  383. #endif
  384. dbopen (const char *, int, int, DBTYPE, const void *);
  385.  
  386. /* set or unset a global lock flag to disable the
  387.  * opening of any DBM file
  388.  */
  389. void dbSetOrClearDBLock(DBLockFlagEnum type);
  390.  
  391. /* #ifdef __DBINTERFACE_PRIVATE */
  392. DB    *__bt_open (const char *, int, int, const BTREEINFO *, int);
  393. DB    *__hash_open (const char *, int, int, const HASHINFO *, int);
  394. DB    *__rec_open (const char *, int, int, const RECNOINFO *, int);
  395. void     __dbpanic (DB *dbp);
  396. /* #endif */
  397.  
  398. __END_DECLS
  399.  
  400. #ifdef linux
  401. #if BYTE_ORDER != LITTLE_ENDIAN
  402. #define BYTE_ORDER LITTLE_ENDIAN
  403. #endif
  404. #endif
  405.  
  406. #if defined(__hpux) || defined(__hppa)
  407. #define BYTE_ORDER BIG_ENDIAN
  408. #define BIG_ENDIAN      4321
  409. #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
  410. #endif
  411.  
  412. #if defined(AIXV3)
  413. /* BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN are all defined here */
  414. #include <sys/machine.h>
  415. #endif
  416.  
  417. #if defined(AIX)
  418. /* BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN are all defined here */
  419. #include <sys/machine.h>
  420. #endif
  421.  
  422. #ifdef __alpha
  423. #include <endian.h>
  424. #endif
  425.  
  426. #endif /* !_DB_H_ */
  427.