Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

754 ADatabase Library Chapter 20


1#ifndef _APUE_DB_H
2#define _APUE_DB_H

3typedef void*DBHANDLE;

4DBHANDLE db_open(const char *, int, ...);
5void db_close(DBHANDLE);
6char *db_fetch(DBHANDLE, const char *);
7int db_store(DBHANDLE, const char *, const char *, int);
8int db_delete(DBHANDLE, const char *);
9void db_rewind(DBHANDLE);
10 char *db_nextrec(DBHANDLE, char *);

11 /*
12 * Flags for db_store().
13 */
14 #define DB_INSERT 1 /* insert new record only */
15 #define DB_REPLACE 2 /* replace existing record */
16 #define DB_STORE 3 /* replace or insert */

17 /*
18 * Implementation limits.
19 */
20 #define IDXLEN_MIN 6 /* key, sep, start, sep, length, \n */
21 #define IDXLEN_MAX 1024 /* arbitrary */
22 #define DATLEN_MIN 2 /* data byte, newline */
23 #define DATLEN_MAX 1024 /* arbitrary */

24 #endif /* _APUE_DB_H */

[1 – 3] We use the_APUE_DB_Hsymbol to ensurethat the contents of the header file
areincluded only once. TheDBHANDLEtype represents an active reference to
the database and is used to isolate applications from the implementation
details of the database. Comparethis technique with the way the standard
I/O library exposes theFILEstructure to applications.
[4 – 10] Next, we declarethe prototypes for the database library’s public functions.
Since this header is included by applications that want to use the library,we
don’t declarethe prototypes for the library’s private functions here.
[11–24] The legal flags that can be passed to thedb_storefunction aredefined next,
followed by fundamental limits of the implementation. These limits can be
changed, if desired, to support bigger databases.
The minimum index recordlength is specified by IDXLEN_MIN.This
represents a 1-byte key,a1-byte separator,a1-byte starting offset, another
1 - byte separator,a1-byte length, and a terminating newline character.(Recall
the format of an index recordfromFigure20.2.) An index recordwill usually
be larger thanIDXLEN_MINbytes, but this is the bareminimum size.
Free download pdf