.TH "Key :: Basic Methods" 3 "Fri Aug 26 2011" "Elektra Projekt" \" -*- nroff -*- .ad l .nh .SH NAME Key :: Basic Methods \- .PP Key construction and initialization methods. .SS "Functions" .in +1c .ti -1c .RI "Key * \fBkeyNew\fP (const char *keyName,...)" .br .ti -1c .RI "Key * \fBkeyDup\fP (const Key *source)" .br .ti -1c .RI "int \fBkeyCopy\fP (Key *dest, const Key *source)" .br .ti -1c .RI "int \fBkeyDel\fP (Key *key)" .br .ti -1c .RI "ssize_t \fBkeyIncRef\fP (Key *key)" .br .ti -1c .RI "ssize_t \fBkeyDecRef\fP (Key *key)" .br .ti -1c .RI "ssize_t \fBkeyGetRef\fP (const Key *key)" .br .in -1c .SH "Detailed Description" .PP Key construction and initialization methods. To use them: .PP .nf #include .fi .PP .PP A Key is the essential class that encapsulates key \fBname \fP, \fBvalue \fP and \fBmetainfo \fP. Key properties are: .IP "\(bu" 2 \fBKey name \fP .IP "\(bu" 2 \fBKey value \fP .IP "\(bu" 2 \fBData type \fP .IP "\(bu" 2 \fBKey comment \fP .IP "\(bu" 2 \fBKey owner \fP .IP "\(bu" 2 \fBUID, GID and filesystem-like mode permissions \fP .IP "\(bu" 2 \fBMode, change and modification times \fP .PP .PP Described here the methods to allocate and free the key. .SH "Function Documentation" .PP .SS "int keyCopy (Key *dest, const Key *source)"Copy or Clear a key. .PP Most often you may prefer \fBkeyDup()\fP which allocates a new key and returns a duplication of another key. .PP But when you need to copy into an existing key, e.g. because it was passed by a pointer in a function you can do so: .PP .PP .nf int h (Key *k) { // receive key c keyCopy (k, c); // the caller will see the changed key k } .fi .PP .PP The reference counter will not change for the destination key. Affiliation to keysets are also not affected. .PP When you pass a NULL-pointer as source the data of dest will be cleaned completely and you get a fresh dest key. .PP .PP .nf int g (Key *k) { keyCopy (k, 0); // k is now an empty and fresh key } .fi .PP .PP \fBParameters:\fP .RS 4 \fIdest\fP the key which will be written to .br \fIsource\fP the key which should be copied or NULL to clean the destination key .RE .PP \fBReturns:\fP .RS 4 -1 on failure when a NULL pointer was passed for dest or a dynamic property could not be written. .PP 0 when dest was cleaned .PP 1 when source was successfully copied .RE .PP \fBSee also:\fP .RS 4 \fBkeyDup()\fP to get a duplication of a \fBKey :: Basic Methods\fP .RE .PP .SS "ssize_t keyDecRef (Key *key)"Decrement the viability of a key object. .PP The reference counter can't be decremented once it reached 0. In that situation nothing will happen and 0 will be returned. .PP \fBReturns:\fP .RS 4 the value of the new reference counter .PP -1 on null pointer .PP 0 when the key is ready to be freed .RE .PP \fBParameters:\fP .RS 4 \fIkey\fP the key object to work with .RE .PP \fBSee also:\fP .RS 4 \fBkeyGetRef()\fP, \fBkeyDel()\fP, \fBkeyIncRef()\fP .RE .PP .SS "int keyDel (Key *key)"A destructor for Key objects. .PP Every key created by \fBkeyNew()\fP must be deleted with \fBkeyDel()\fP. .PP It is save to delete keys which are in a keyset, the number of references will be returned then. .PP It is save to delete a nullpointer, -1 will be returned then. .PP \fBParameters:\fP .RS 4 \fIkey\fP the key object to delete .RE .PP \fBSee also:\fP .RS 4 \fBkeyNew()\fP, keyInc(), \fBkeyGetRef()\fP .RE .PP \fBReturns:\fP .RS 4 the value of the reference counter if the key is within keyset(s) .PP 0 when the key was freed .PP -1 on null pointers .RE .PP .SS "Key* keyDup (const Key *source)"Return a duplicate of a key. .PP Memory will be allocated as needed for dynamic properties. .PP The new key will not be member of any KeySet and will start with a new reference counter at 0. A subsequent \fBkeyDel()\fP will delete the key. .PP .PP .nf int f (const Key * source) { Key * dup = keyDup (source); // work with duplicate keyDel (dup); // everything related to dup is freed // and source is unchanged } .fi .PP .PP Like for a new key after \fBkeyNew()\fP a subsequent \fBksAppend()\fP makes a KeySet to take care of the lifecycle of the key. .PP .PP .nf int g (const Key * source, KeySet * ks) { Key * dup = keyDup (source); // work with duplicate ksAppendKey (ks, dup); // ksDel(ks) will also free the duplicate // source remains unchanged. } .fi .PP .PP Duplication of keys should be preferred to \fBkeyNew()\fP, because data like owner can be filled with a copy of the key instead of asking the environment. It can also be optimized in the checks, because the keyname is known to be valid. .PP \fBParameters:\fP .RS 4 \fIsource\fP has to be an initializised source Key .RE .PP \fBReturns:\fP .RS 4 0 failure or on NULL pointer .PP a fully copy of source on success .RE .PP \fBSee also:\fP .RS 4 \fBksAppend()\fP, \fBkeyDel()\fP .PP keyClear(), \fBkeyNew()\fP .RE .PP .SS "ssize_t keyGetRef (const Key *key)"Return how many references the key has. .PP The references will be incremented when \fBksAppendKey()\fP or \fBksAppend()\fP uses the key and will be decremented when \fBksPop()\fP is used. .PP \fBkeyDup()\fP will reset the references for dupped key. .PP For your own applications you can use \fBkeyIncRef()\fP and keyDelRef() for reference counting. Keys with zero references will be deleted when using \fBkeyDel()\fP. .PP \fBParameters:\fP .RS 4 \fIkey\fP the key object to work with .RE .PP \fBReturns:\fP .RS 4 the number of references .PP -1 on null pointer .RE .PP \fBSee also:\fP .RS 4 \fBkeyIncRef()\fP and \fBkeyDecRef()\fP .RE .PP .SS "ssize_t keyIncRef (Key *key)"Increment the viability of a key object. .PP This function is intended for applications using their own reference counter for key objects. With it you can increment the reference and thus avoid destruction of the object in a subsequent \fBkeyDel()\fP. .PP .PP .nf Key *k; keyInc (k); function_that_keyDec(k); // work with k keyDel (k); // now really free it .fi .PP .PP The reference counter can't be incremented once it reached SSIZE_MAX. In that situation nothing will happen and SSIZE_MAX will be returned. .PP \fBReturns:\fP .RS 4 the value of the new reference counter .PP -1 on null pointer .PP SSIZE_MAX when maximum exceeded .RE .PP \fBParameters:\fP .RS 4 \fIkey\fP the key object to work with .RE .PP \fBSee also:\fP .RS 4 \fBkeyGetRef()\fP, \fBkeyDecRef()\fP, \fBkeyDel()\fP .RE .PP .SS "Key* keyNew (const char *keyName, ...)"A practical way to fully create a Key object in one step. .PP This function tries to mimic the C++ way for constructors. .PP To just get a key object, simple do: .PP .nf Key *k = keyNew(0); // work with it keyDel (k); .fi .PP .PP If you want the key object to contain a name, value, comment and other meta info read on. .PP \fBNote:\fP .RS 4 When you already have a key with similar properties its easier and cheaper to \fBkeyDup()\fP the key. .RE .PP Due to ABI compatibility, the \fCKey\fP structure is not defined in kdb.h, only declared. So you can only declare \fCpointers\fP to \fCKeys\fP in your program, and allocate and free memory for them with \fBkeyNew()\fP and \fBkeyDel()\fP respectively. See http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN135 .PP You can call it in many different ways depending on the attribute tags you pass as parameters. Tags are represented as the #keyswitch_t values, and tell \fBkeyNew()\fP which Key attribute comes next. .PP The simplest and minimum way to use it is with no tags, only a key name: .PP .nf Key *nullKey,*emptyNamedKey; // Create a key that has no name, is completely empty, but is initialized nullKey=keyNew(0); keyDel (nullKey); // Is the same as above nullKey=keyNew('', KEY_END); keyDel (nullKey); // Create and initialize a key with a name and nothing else emptyNamedKey=keyNew('user/some/example',KEY_END); keyDel (emptyNamedKey); .fi .PP .PP \fBkeyNew()\fP allocates memory for a key object and cleans everything up. After that, it processes the given argument list. .PP The Key attribute tags are the following: .IP "\(bu" 2 keyswitch_t::KEY_TYPE .br Next parameter is a type of the value. Default assumed is KEY_TYPE_UNDEFINED. Set this attribute so that a subsequent KEY_VALUE can toggle to \fBkeySetString()\fP or \fBkeySetBinary()\fP regarding to \fBkeyIsString()\fP or \fBkeyIsBinary()\fP. If you don't use KEY_TYPE but a KEY_VALUE follows afterwards, KEY_TYPE_STRING will be used. .IP "\(bu" 2 keyswitch_t::KEY_SIZE .br Define a maximum length of the value. This is especially useful for setting a binary key. So make sure you use that before you KEY_VALUE for binary keys. .IP "\(bu" 2 keyswitch_t::KEY_VALUE .br Next parameter is a pointer to the value that will be set to the key If no keyswitch_t::KEY_TYPE was used before, keyswitch_t::KEY_TYPE_STRING is assumed. If KEY_TYPE was previously passed with a KEY_TYPE_BINARY, you should have passed KEY_SIZE before! Otherwise it will be cut of with first \\0 in string! .IP "\(bu" 2 keyswitch_t::KEY_UID, \fCkeyswitch_t::KEY_GID\fP .br Next parameter is taken as the UID (uid_t) or GID (gid_t) that will be defined on the key. See \fBkeySetUID()\fP and \fBkeySetGID()\fP. .IP "\(bu" 2 keyswitch_t::KEY_MODE .br Next parameter is taken as mode permissions (mode_t) to the key. See \fBkeySetMode()\fP. .IP "\(bu" 2 keyswitch_t::KEY_DIR .br Define that the key is a directory rather than a ordinary key. This means its executable bits in its mode are set. This option allows the key to have subkeys. See \fBkeySetDir()\fP. .IP "\(bu" 2 keyswitch_t::KEY_OWNER .br Next parameter is the owner. See \fBkeySetOwner()\fP. .IP "\(bu" 2 keyswitch_t::KEY_COMMENT .br Next parameter is a comment. See \fBkeySetComment()\fP. .IP "\(bu" 2 keyswitch_t::KEY_REMOVE .br Mark the key to be removed instead of set it. See \fBkeyRemove()\fP. .IP "\(bu" 2 keyswitch_t::KEY_STAT .br Mark the key to be stated instead of get it. See \fBkeyStat()\fP. .IP "\(bu" 2 keyswitch_t::KEY_END .br Must be the last parameter passed to \fBkeyNew()\fP. It is always required, unless the \fCkeyName\fP is 0. .PP .PP \fBExample:\fP .RS 4 .PP .nf KeySet *ks=ksNew(0); ksAppendKey(ks,keyNew(0)); // an empty key ksAppendKey(ks,keyNew('user/sw', // the name of the key KEY_END)); // no more args ksAppendKey(ks,keyNew('user/tmp/ex1', KEY_VALUE,'some data', // set a string value KEY_END)); // end of args ksAppendKey(ks,keyNew('user/tmp/ex2', KEY_VALUE,'some data', // with a simple value KEY_MODE,0777, // permissions KEY_END)); // end of args ksAppendKey(ks,keyNew('user/tmp/ex4', KEY_TYPE,KEY_TYPE_BINARY, // key type KEY_SIZE,7, // assume binary length 7 KEY_VALUE,'some data', // value that will be truncated in 7 bytes KEY_COMMENT,'value is truncated', KEY_OWNER,'root', // owner (not uid) is root KEY_UID,0, // root uid KEY_END)); // end of args ksAppendKey(ks,keyNew('user/tmp/ex5', KEY_TYPE, KEY_TYPE_DIR | KEY_TYPE_BINARY,// dir key with a binary value KEY_SIZE,7, KEY_VALUE,'some data', // value that will be truncated in 7 bytes KEY_COMMENT,'value is truncated', KEY_OWNER,'root', // owner (not uid) is root KEY_UID,0, // root uid KEY_END)); // end of args ksDel(ks); .fi .PP .RE .PP The reference counter (see \fBkeyGetRef()\fP) will be initialized with 0, that means a subsequent call of \fBkeyDel()\fP will delete the key. If you append the key to a keyset the reference counter will be incremented by one (see keyInc()) and the key can't be be deleted by a \fBkeyDel()\fP. .PP .PP .nf Key *k = keyNew(0); // ref counter 0 ksAppendKey(ks, k); // ref counter of key 1 ksDel(ks); // key will be deleted with keyset * .fi .PP .PP If you increment only by one with keyInc() the same as said above is valid: .PP .PP .nf Key *k = keyNew(0); // ref counter 0 keyIncRef(k); // ref counter of key 1 keyDel(k); // has no effect keyDecRef(k); // ref counter back to 0 keyDel(k); // key is now deleted * .fi .PP .PP If you add the key to more keySets: .PP .PP .nf Key *k = keyNew(0); // ref counter 0 ksAppendKey(ks1, k); // ref counter of key 1 ksAppendKey(ks2, k); // ref counter of key 2 ksDel(ks1); // ref counter of key 1 ksDel(ks2); // k is now deleted * .fi .PP .PP or use keyInc() more than once: .PP .PP .nf Key *k = keyNew(0); // ref counter 0 keyIncRef(k); // ref counter of key 1 keyDel (k); // has no effect keyIncRef(k); // ref counter of key 2 keyDel (k); // has no effect keyDecRef(k); // ref counter of key 1 keyDel (k); // has no effect keyDecRef(k); // ref counter is now 0 keyDel (k); // k is now deleted * .fi .PP .PP they key won't be deleted by a \fBkeyDel()\fP as long refcounter is not 0. .PP The key's sync bit will always be set for any call, except: .PP .nf Key *k = keyNew(0); // keyNeedSync() will be false .fi .PP .PP \fBParameters:\fP .RS 4 \fIkeyName\fP a valid name to the key, or NULL to get a simple initialized, but really empty, object .RE .PP \fBSee also:\fP .RS 4 \fBkeyDel()\fP .RE .PP \fBReturns:\fP .RS 4 a pointer to a new allocated and initialized Key object, or NULL if an invalid \fCkeyName\fP was passed (see \fBkeySetName()\fP). .RE .PP .SH "Author" .PP Generated automatically by Doxygen for Elektra Projekt from the source code.