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

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