home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 May
/
Pcwk5b98.iso
/
WEBSERVE
/
PI3
/
PI3WEB.EXE
/
DEVEL
/
INCLUDE
/
PIDB.H
< prev
next >
Wrap
C/C++ Source or Header
|
1997-10-19
|
22KB
|
698 lines
/*____________________________________________________________________________*\
*
Copyright (c) 1997 John Roy. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. Redistributions of any form whatsoever and all advertising materials
mentioning features must contain the following
acknowledgment:
"This product includes software developed by John Roy
(http://www.johnroy.com/pi3/)."
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: PIDB.h$
* $Date: Sun Aug 10 06:42:18 1997$
*
Description:
Definition of interface to program database class.
\*____________________________________________________________________________*/
/* $HeaderTop:$ */
#ifndef PIDB_H_
#define PIDB_H_
/*____________________________________________________________________________*\
*
Decription:
Definitions and function prototypes for the program database.
\*____________________________________________________________________________*/
typedef struct _PIDB PIDB;
typedef struct _PIDBIterator PIDBIterator;
typedef struct UserDBType PIUserType;
/* ---
These are the builtin types defined in every DB tree.
--- */
#define PIDBTYPE_INVALID -1
#define PIDBTYPE_TREE 0
#define PIDBTYPE_STRING 1
#define PIDBTYPE_RFC822 2
#define PIDBTYPE_OPAQUE 3
#define PIDBTYPE_USER 4
/* --- this should always be the value of the last builtin type --- */
#define PIDBTYPE_LAST 4
#define PIDBTYPE_BUILTINSIZE (PIDBTYPE_LAST+1)
/* --- Flags --- */
#define PIDBFLAG_NONE 0x0000
#define PIDBFLAG_PROPAGATEUP 0x0001
#define PIDBFLAG_NOCALLBACK 0x0002
#define PIDBFLAG_FASTKEY 0x0004
/* --- text encoding of DB types and flags, for persistence --- */
#define PIDBTYPE_TEXT_TREE "TREE"
#define PIDBTYPE_TEXT_STRING "STRING"
#define PIDBTYPE_TEXT_RFC822 "RFC822"
#define PIDBTYPE_TEXT_OPAQUE "OPAQUE"
#define PIDBTYPE_TEXT_USER "USER"
#define PIDBFLAG_TEXT_PROPAGATEUP "PROPAGATEUP"
#define PIDBFLAG_TEXT_NOCALLBACK "NOCALLBACK"
#define PIDBFLAG_TEXT_FASTKEY "FASTKEY"
/*____________________________________________________________________________*\
*
Name:
PIDB_new
Synopsis:
PIDB *PIDB_new( PIDB *pParent, const char *pName )
Description:
Creates a new PIDB tree under the specified parent node with the
specified name. If pParent is NULL, then the generated node will
be the root of a new PIDB tree hierarchy.
If a new child tree is successfully generated it is automatically
added into the parent tree's list of child nodes with a type of
PIDBTYPE_TREE and a key of pName.
If pParent is NULL then a new root tree node is generated. This tree
should be free'd with PIDB_delete() when it is no longer needed.
Notes:
If memory cannot be allocated for the new tree, a special handler
will be invoked in a method specific to the build and configuration
of the server library. For more information see the specific notes
for the library release.
Return Values:
Returns a pointer to the new child PIDB tree, or NULL if insufficent
memory was available.
Errors:
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIDB *PIDB_new( PIDB *pParent, const char *pName );
/*____________________________________________________________________________*\
*
Name:
PIDBEx_new
Synopsis:
PIDB *PIDBEx_new( PIDB *pParent, const char *pName,
void *(* fnLookup)( PIDB *, int, const char *, int ) )
Description:
Creates a new PIDB of extended type PIDBEx. This tree supports a
lookup callback where lookups on the tree will invoke the user
supplied lookup function.
Notes:
Use of this callback function on instances of this extended tree
type can be overriden by adding the PIDBFLAG_NOCALLBACK flag to the
flags passed to PIDB_lookup() or PIDB_getIterator().
Return Values:
Returns a pointer to the new child PIDB tree on success.
Errors:
Returns NULL if fnLookup is NULL.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIDB *PIDBEx_new( PIDB *pParent, const char *pName,
void *(* fnLookup)( PIDB *, int, const char *, int ) );
/*____________________________________________________________________________*\
*
Name:
PIDB_getParent
Synopsis:
PIDB *PIDB_getParent( PIDB *pTree )
Description:
Returns the parent tree of the specified PIDB tree.
Notes:
Return Values:
Returns a pointer to the parent tree or NULL if the specified tree
is the top of the tree hierarchy.
Errors:
NULL if pTree is NULL
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIDB *PIDB_getParent( PIDB *pThis );
/*____________________________________________________________________________*\
*
Name:
PIDB_add
Synopsis:
int PIDB_add( PIDB *pTree, int iType, const char *pKey, void *pValue,
int iFlags )
Description:
Adds a child element into a PIDB tree. The action performed on the
opaque pointer pValue depends on the value of iType.
<UL>
<ITEM><B>PIDBTYPE_TREE</B><P>
pValue must be a pointer to a valid PIDB tree. This tree
is in specified tree as a child node. Note that child trees are
automatically placed in thier parents tree when they are created,
explicitly adding them a second time may cause serious problems
when the tree is recursively free'd with PIDB_delete(). Use this
only to attach a child tree which was created with a NULL value
for parameter pParent in the call to PIDB_new().
<ITEM><B>PIDBTYPE_STRING</B><P>
pValue must be a pointer to a null terminated character
array. A copy of the string is generated and stored in the
current tree.
<ITEM><B>PIDBTYPE_RFC822</B><P>
This type is created, maintained and used in the same
way as type PIDBTYPE_STRING. The important difference between
PIDBTYPE_STRING and PIDBTYPE_RFC822 is that PIDBTYPE_STRING is
in all repects case sensitive, whereas PIDBTYPE_RFC822 is in all
respects case in-sensitive.
<ITEM><B>PIDBTYPE_OPAQUE</B><P>
pValue is stored in the tree as an unknown pointer. Note
that the user is responsible for free'ing memory allocated
at pValue, if any. This is different from other tree types
where the object referenced by pValue will be cleaned up
and memory free'd when the tree is destroyed.
<ITEM>PIDBTYPE_USER</B><P>
This type requires that pValue is an object of a type
not specified in this interface. Pending this specification
this type is for internal use only.
</UL>
The value iFlags is formed by OR'ing together the following values
<P>
<UL>
<ITEM><B>PIDBFLAG_FASTKEY</B><P> The value pKey is assumed to be a symbol
table entry so PIDB_add() skips the hashing
and offset logic. This improves performance on
repeated DB actions at the cost of initial setup
using PIDB_getFastKey().
</UL>
Notes:
The type PIDBTYPE_USER is under development and its behaviour
is subject to change.
Return Values:
On success PIDB_add returns zero (PIAPI_COMPLETED). Otherwise
an error occured as described below.
Errors:
PIAPI_EINVAL An invalid value for iType was used or pTree was NULL
PIAPI_EXHAUSTED Memory could not be allocated to complete the function.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDB_add( PIDB *pTree, int iType, const char *pKey, void *pV,
int iFlags );
/*____________________________________________________________________________*\
*
Name:
PIDB_replace
Synopsis:
int PIDB_replace( PIDB *pTree, int iType, const char *pKey, void *pValue,
int iFlags )
Description:
Replaces the first instance of the value corresponding to pKey in the
DB pTree. The new entry will be created even if a previous entry could
not be found. PIDB_add() describes the parameters iType and iFlags.
Additionally iFlags may have the following flag set
PIDBFLAG_PROPAGATEUP Search upwards from the current tree to
parent trees to find a match.
Notes:
Return Values:
On success PIDB_replace returns zero (PIAPI_COMPLETED). Otherwise
an error occured as described below.
Errors:
PIAPI_EINVAL An invalid value for iType was used or pTree was NULL
PIAPI_EXHAUSTED Memory could not be allocated to complete the function.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDB_replace( PIDB *pTree, int iType, const char *pKey,
void *pV, int iFlags );
/*____________________________________________________________________________*\
*
Name:
PIDB_lookup
Synopsis:
void *PIDB_lookup( PIDB *pTree, int iType, const char *pKey, int iFlags )
Description:
Searches the PIDB tree for the first occurence of the an element which
matches the key. If pKey is NULL then the first element of the specified
type will be returned. See PIDB_getIterator() for the use of the parameter
iFlags.
Notes:
This function cannot be used to distinguish between an unsuccessful
search and a successful search where the value of the matched element
is NULL. In these cases an PIDBIterator object should be used to
scan the tree, see PIDB_getIterator.
Return Values:
On success the reference of the first element is returned. The type of
the value depends on the specified value of iType. See PIDB_add for
a full description of possible types. See PIDBIterator_current for
a description of appropriate PIDBTYPE to C typecast mappings.
NULL is returned if no match was found.
Errors:
If pTree is NULL or iType is out of range then PIDB_lookup() returns
NULL.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI void *PIDB_lookup( PIDB *pTree, int iType, const char *pKey,
int iFlags );
/*____________________________________________________________________________*\
*
Name:
PIDB_getFastKey
Synopsis:
const char *PIDB_getFastKey( const char *pKey, int iType )
Description:
Returns a pointer to a symbol table entry for the key pKey and
PIDB type iType. This symbol table entry then then be used in calls
to PIDB_lookup() and PIDB_getIterator() with the PIDBFLAG_FASTKEY to
avoid the overhead of hashing and symbol table lookup.
Notes:
This function is useful where a key is used multiple times. The
fast key can be generated once and reused across all invocations
of PIDB_lookup() and PIDB_getIterator(). This significantly improves
performance.
Return Values:
On success PIDB_getFastKey() returns a new key. Returns NULL on
failure.
Errors:
Returns NULL if pKey is NULL or no memory space is available.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI const char *PIDB_getFastKey( const char *pKey, int iType );
/*____________________________________________________________________________*\
*
Name:
PIDB_pidbtypeNameToNumber
Synopsis:
int PIDB_pidbtypeNameToNumber( const char *pType )
Description:
Returns the integer value of the name pType, effectively mapping, the
string pType to return values as follows:-
<CENTER>
<TABLE BORDER=1>
<TH>pType<TH>Return code
<TR><TD>"TREE"<TD>PIDBTYPE_TREE
<TR><TD>"STRING"<TD>PIDBTYPE_STRING
<TR><TD>"RFC822"<TD>PIDBTYPE_RFC822
<TR><TD>"OPAQUE"<TD>PIDBTYPE_OPAQUE
<TR><TD>"USER"<TD>PIDBTYPE_USER
</TABLE>
</CENTER>
Comparision in not case sensitive.
Notes:
Return Values:
If the string pType could not be matched PIDB_pidbtypeNameToNumber()
returns PIDBTYPE_INVALID.
Errors:
On error PIDB_pidbtypeNameToNumber() returns PIDBTYPE_INVALID.
See Also:
PIDB_pidbflagsNameToNumber()
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDB_pidbtypeNameToNumber( const char *pType );
/*____________________________________________________________________________*\
*
Name:
PIDB_pidbflagsNameToNumber
Synopsis:
int PIDB_pidbflagsNameToNumber( const char *pFlags )
Description:
Returns the integer value of the flags represented by the string pFlags.
The string pFlags may contain many flags seperated by the pipe ('|')
character.
<CENTER>
<TABLE BORDER=1>
<TH>pType<TH>Return code
<TR><TD>"PROPAGATEUP"<TD>PIDBFLAG_TREE
<TR><TD>"NOCALLBACK"<TD>PIDBFLAG_NOCALLBACK
<TR><TD>"FASTKEY"<TD>PIDBFLAG_FASTKEY
</TABLE>
</CENTER>
Comparision in not case sensitive.
Notes:
Whitespace in the string pFlags is allowed and will be ignored in
matching the flags.
The flags PIDBFLAG_NOCALLBACK and PIDBTYPE_FASTKEY have little use
in the context of persistence and are included only for
completeness.
Return Values:
Returns the value formed by bitwise OR'ing the flags together. If
no flags are matched or the value pFlags is NULL,
PIDB_pidbflagsNameToNumber returns zero (PIDBFLAG_NONE).
Errors:
This function does not generate an error. Unrecognised flags are
ignored.
See Also:
PIDB_pidbtypeNameToNumber().
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDB_pidbflagsNameToNumber( const char *pFlags );
/*____________________________________________________________________________*\
*
Name:
PIDB_getIterator
Synopsis:
PIDBIterator *PIDB_getIterator( PIDB *pTree, int iType, const char *pKey,
int iFlags )
Description:
Returns a pointer to an PIDBIterator object which to traverse the specified
tree for elements of type iType which match the key pKey. If pKey is
NULL all objects with type iType will be matched. The following flags
may be OR'ed together to form the value iFlags:
PIDBFLAGS_PROPAGATEUP
The current tree will first be searched and then the parent
tree. The recursively search all trees up the tree hierarchy to
the root node.
PIDBFLAG_NOCALLBACK
This flag disables use the the callback functions in
DB tree's created with callback hook functions via PIDBEx_new().
This flag is typically used in the implementation of callback
functions for PIDBEx when PIDB_lookup() or PIDB_getIterator() would
cause a recursive invokation of the callback function.
PIDBFLAG_FASTKEY
This flag indicates that the key pKey is an optimized
key generated by PIDB_getFastKey().
Notes:
The iterator object should be free'd with PIDBIterator_delete() when
it is no longer needed.
Return Values:
On success a pointer to an PIDBIterator object is returned. The
functions PIDBIterator_atValidElement(), PIDBIterator_current(),
PIDBIterator_next() and PIDBIterator_delete() are used to access and
manipulate the iterator.
Errors:
NULL is returned if pTree is NULL, iType invalid or memory
could not be allocated for the PIDBIterator object.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIDBIterator *PIDB_getIterator( PIDB *pTree, int iType,
const char *pKey, int iFlags );
/*____________________________________________________________________________*\
*
Name:
PIDB_delete
Synopsis:
int PIDB_delete( PIDB *pTree )
Description:
Destructs a root PIDB tree created by PIDB_new( NULL ).
Notes:
Return Values:
On success PIDB_delete returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_EINVAL is returned if pTree is NULL. If pTree is a child
node of another tree then PIAPI_ERROR is returned.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDB_delete( PIDB *pTree );
/*____________________________________________________________________________*\
*
Name:
PIDB_getRoot
Synopsis:
int PIDB_getRoot( PIDB *pTree )
Description:
Returns the root node of a PIDB tree hierarchy.
Notes:
Return Values:
On success PIDB_getRoot() returns the root node of the PIDB tree
hierarchy.
Errors:
If pTree is NULL PIDB_getRoot() returns NULL.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI PIDB *PIDB_getRoot( PIDB *pTree );
/*____________________________________________________________________________*\
*
Name:
PIDBIterator_atValidElement
Synopsis:
int PIDBIterator_atValidElement( PIDBIterator *pIter )
Description:
Returns a boolean result indicating whether not the PIDBIterator_current()
may be used to retrieve an element from the iterator.
Notes:
Return Values:
Returns non-zero (PIAPI_TRUE) if an element may be read, otherwise zero
(PIAPI_FALSE) the iterator has exhausted matching elements.
Errors:
Returns PIAPI_FALSE if pIter is NULL.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDBIterator_atValidElement( PIDBIterator *pIter );
/*____________________________________________________________________________*\
*
Name:
PIDBIterator_current
Synopsis:
void *PIDBIterator_current( PIDBIterator *pIter, const char **ppKey )
Description:
Returns the value of the current element of the iterator. If ppKey is
not NULL it will be set to point to the key of the retrieved element.
Notes:
The results are undefined if the iterator does not point at a valid
element. The function PIDBIterator_atValidElement can be used to
verify the status of the iterator object.
In general the iterator object return elements in no particular order.
However, elements with the exact same key will be retrieved in the
order in which they were placed in the tree.
Return Values:
Returns the current object from the iterator. The type of the object
depends on the value of iType. The pointer should be cast to a
pointer of the appropriate type, typically the typecasts would be as
follows
PIDBTYPE_TREE PIDB * a tree node
PIDBTYPE_STRING const char * character array
PIDBTYPE_S_RFC822 const char * character array
PIDBTYPE_C_RFC822 const char * character array
PIDBTYPE_OPAQUE void * generic pointer
PIDBTYPE_USER void not defined
Errors:
Returns NULL is pIter is NULL.
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI void *PIDBIterator_current( PIDBIterator *pIter,
const char **ppKey );
/*____________________________________________________________________________*\
*
Name:
PIDBIterator_next
Synopsis:
int PIDBIterator_next( PIDBIterator *pIter )
Description:
Increments an iterator objects internal pointers to point at the
next valid PIDB element.
Notes:
The results are undefined if the iterator does not point at a valid
element. The function PIDBIterator_atValidElement should be used
at PIIterator_next and before PIDBIterator_next to verify that the
iterator points at a valid PIDB element.
Return Values:
On success PIDBIterator_next() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_EINVAL if pIter is NULL
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDBIterator_next( PIDBIterator *pIter );
/*____________________________________________________________________________*\
*
Name:
PIDBIterator_removeCurrent
Synopsis:
int PIDBIterator_removeCurrent( PIDBIterator *pIter )
Description:
Remove and destroy the current element of the iterator pIter.
Notes:
Return Values:
On success PIDBIterator_removeCurrent() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_EINVAL if pIter is NULL
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDBIterator_removeCurrent( PIDBIterator *pIter );
/*____________________________________________________________________________*\
*
Name:
PIDBIterator_delete
Synopsis:
int PIDBIterator_delete( PIDBIterator *pIter )
Description:
Destroys the iterator object and frees any associated memory.
Notes:
Return Values:
On success PIDBIterator_delete() returns zero (PIAPI_COMPLETED).
Errors:
PIAPI_EINVAL if pIter is NULL
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIDBIterator_delete( PIDBIterator *pIter );
/*____________________________________________________________________________*\
*
Name:
PIUserType_getValue
Synopsis:
void *PIUserType_getValue( PIUserType *pUserType )
Description:
Returns the value associated with a PIUserType data structure.
Notes:
Return Values:
On success PIUserType_getValue returns the value associated with
the PIUserType data structure. Returns NULL if pUserType is not
valid.
Errors:
See Also:
\*____________________________________________________________________________*/
PUBLIC_PIAPI void *PIUserType_getValue( PIUserType *pUserType );
#endif /* PIDB_H_ */