Git init
[pkgs/e/elektra.git] / doc / elektra-api / man / man3 / key.3
1 .TH "Key :: Basic Methods" 3 "30 Jun 2009" "Elektra Projekt" \" -*- nroff -*-
2 .ad l
3 .nh
4 .SH NAME
5 Key :: Basic Methods \- Key construction and initialization methods.  
6
7 .PP
8 .SS "Functions"
9
10 .in +1c
11 .ti -1c
12 .RI "Key * \fBkeyNew\fP (const char *keyName,...)"
13 .br
14 .ti -1c
15 .RI "Key * \fBkeyDup\fP (const Key *source)"
16 .br
17 .ti -1c
18 .RI "int \fBkeyCopy\fP (Key *dest, const Key *source)"
19 .br
20 .ti -1c
21 .RI "int \fBkeyDel\fP (Key *key)"
22 .br
23 .ti -1c
24 .RI "ssize_t \fBkeyIncRef\fP (Key *key)"
25 .br
26 .ti -1c
27 .RI "ssize_t \fBkeyDecRef\fP (Key *key)"
28 .br
29 .ti -1c
30 .RI "ssize_t \fBkeyGetRef\fP (const Key *key)"
31 .br
32 .in -1c
33 .SH "Detailed Description"
34 .PP 
35 Key construction and initialization methods. 
36 .PP
37 To use them: 
38 .PP
39 .nf
40 #include <kdb.h>
41
42 .fi
43 .PP
44 .PP
45 A Key is the essential class that encapsulates key \fBname \fP, \fBvalue \fP and \fBmetainfo \fP. Key properties are:
46 .IP "\(bu" 2
47 \fBKey name \fP
48 .IP "\(bu" 2
49 \fBKey value \fP
50 .IP "\(bu" 2
51 \fBData type \fP
52 .IP "\(bu" 2
53 \fBKey comment \fP
54 .IP "\(bu" 2
55 \fBKey owner \fP
56 .IP "\(bu" 2
57 \fBUID, GID and filesystem-like mode permissions \fP
58 .IP "\(bu" 2
59 \fBMode, change and modification times \fP
60 .PP
61 .PP
62 Described here the methods to allocate and free the key. 
63 .SH "Function Documentation"
64 .PP 
65 .SS "int keyCopy (Key * dest, const Key * source)"
66 .PP
67 Copy or Clear a key.
68 .PP
69 Most often you may prefer \fBkeyDup()\fP which allocates a new key and returns a duplication of another key.
70 .PP
71 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:
72 .PP
73 .PP
74 .nf
75 int h (Key *k)
76 {
77         // receive key c
78         keyCopy (k, c);
79         // the caller will see the changed key k
80 }
81 .fi
82 .PP
83 .PP
84 The reference counter will not change for the destination key. Affiliation to keysets are also not affected.
85 .PP
86 When you pass a NULL-pointer as source the data of dest will be cleaned completely and you get a fresh dest key.
87 .PP
88 .PP
89 .nf
90 int g (Key *k)
91 {
92         keyCopy (k, 0);
93         // k is now an empty and fresh key
94 }
95 .fi
96 .PP
97 .PP
98 \fBParameters:\fP
99 .RS 4
100 \fIdest\fP the key which will be written to 
101 .br
102 \fIsource\fP the key which should be copied or NULL to clean the destination key
103 .RE
104 .PP
105 \fBReturns:\fP
106 .RS 4
107 -1 on failure when a NULL pointer was passed for dest or a dynamic property could not be written. 
108 .PP
109 0 when dest was cleaned 
110 .PP
111 1 when source was successfully copied 
112 .RE
113 .PP
114 \fBSee also:\fP
115 .RS 4
116 \fBkeyDup()\fP to get a duplication of a \fBKey :: Basic Methods\fP 
117 .RE
118 .PP
119
120 .SS "ssize_t keyDecRef (Key * key)"
121 .PP
122 Decrement the viability of a key object.
123 .PP
124 The reference counter can't be decremented once it reached 0. In that situation nothing will happen and 0 will be returned.
125 .PP
126 \fBReturns:\fP
127 .RS 4
128 the value of the new reference counter 
129 .PP
130 -1 on null pointer 
131 .PP
132 0 when the key is ready to be freed 
133 .RE
134 .PP
135 \fBParameters:\fP
136 .RS 4
137 \fIkey\fP the key object to work with 
138 .RE
139 .PP
140 \fBSee also:\fP
141 .RS 4
142 \fBkeyGetRef()\fP, \fBkeyDel()\fP, \fBkeyIncRef()\fP 
143 .RE
144 .PP
145
146 .SS "int keyDel (Key * key)"
147 .PP
148 A destructor for Key objects.
149 .PP
150 Every key created by \fBkeyNew()\fP must be deleted with \fBkeyDel()\fP.
151 .PP
152 It is save to delete keys which are in a keyset, the number of references will be returned then.
153 .PP
154 It is save to delete a nullpointer, -1 will be returned then.
155 .PP
156 \fBParameters:\fP
157 .RS 4
158 \fIkey\fP the key object to delete 
159 .RE
160 .PP
161 \fBSee also:\fP
162 .RS 4
163 \fBkeyNew()\fP, keyInc(), \fBkeyGetRef()\fP 
164 .RE
165 .PP
166 \fBReturns:\fP
167 .RS 4
168 the value of the reference counter if the key is within keyset(s) 
169 .PP
170 0 when the key was freed 
171 .PP
172 -1 on null pointers 
173 .RE
174 .PP
175
176 .SS "Key* keyDup (const Key * source)"
177 .PP
178 Return a duplicate of a key.
179 .PP
180 Memory will be allocated as needed for dynamic properties.
181 .PP
182 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.
183 .PP
184 .PP
185 .nf
186 int f (const Key * source)
187 {
188         Key * dup = keyDup (source);
189         // work with duplicate
190         keyDel (dup);
191         // everything related to dup is freed
192         // and source is unchanged
193 }
194 .fi
195 .PP
196 .PP
197 Like for a new key after \fBkeyNew()\fP a subsequent \fBksAppend()\fP makes a KeySet to take care of the lifecycle of the key.
198 .PP
199 .PP
200 .nf
201 int g (const Key * source, KeySet * ks)
202 {
203         Key * dup = keyDup (source);
204         // work with duplicate
205         ksAppendKey (ks, dup);
206         // ksDel(ks) will also free the duplicate
207         // source remains unchanged.
208 }
209 .fi
210 .PP
211 .PP
212 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.
213 .PP
214 \fBParameters:\fP
215 .RS 4
216 \fIsource\fP has to be an initializised source Key 
217 .RE
218 .PP
219 \fBReturns:\fP
220 .RS 4
221 0 failure or on NULL pointer 
222 .PP
223 a fully copy of source on success 
224 .RE
225 .PP
226 \fBSee also:\fP
227 .RS 4
228 \fBksAppend()\fP, \fBkeyDel()\fP 
229 .PP
230 keyClear(), \fBkeyNew()\fP 
231 .RE
232 .PP
233
234 .SS "ssize_t keyGetRef (const Key * key)"
235 .PP
236 Return how many references the key has.
237 .PP
238 The references will be incremented when \fBksAppendKey()\fP or \fBksAppend()\fP uses the key and will be decremented when \fBksPop()\fP is used.
239 .PP
240 \fBkeyDup()\fP will reset the references for dupped key.
241 .PP
242 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.
243 .PP
244 \fBParameters:\fP
245 .RS 4
246 \fIkey\fP the key object to work with 
247 .RE
248 .PP
249 \fBReturns:\fP
250 .RS 4
251 the number of references 
252 .PP
253 -1 on null pointer 
254 .RE
255 .PP
256 \fBSee also:\fP
257 .RS 4
258 \fBkeyIncRef()\fP and \fBkeyDecRef()\fP 
259 .RE
260 .PP
261
262 .SS "ssize_t keyIncRef (Key * key)"
263 .PP
264 Increment the viability of a key object.
265 .PP
266 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.
267 .PP
268 .PP
269 .nf
270 Key *k;
271 keyInc (k);
272 function_that_keyDec(k);
273 // work with k
274 keyDel (k); // now really free it
275 .fi
276 .PP
277 .PP
278 The reference counter can't be incremented once it reached SSIZE_MAX. In that situation nothing will happen and SSIZE_MAX will be returned.
279 .PP
280 \fBReturns:\fP
281 .RS 4
282 the value of the new reference counter 
283 .PP
284 -1 on null pointer 
285 .PP
286 SSIZE_MAX when maximum exceeded 
287 .RE
288 .PP
289 \fBParameters:\fP
290 .RS 4
291 \fIkey\fP the key object to work with 
292 .RE
293 .PP
294 \fBSee also:\fP
295 .RS 4
296 \fBkeyGetRef()\fP, \fBkeyDecRef()\fP, \fBkeyDel()\fP 
297 .RE
298 .PP
299
300 .SS "Key* keyNew (const char * keyName,  ...)"
301 .PP
302 A practical way to fully create a Key object in one step.
303 .PP
304 This function tries to mimic the C++ way for constructors.
305 .PP
306 To just get a key object, simple do: 
307 .PP
308 .nf
309 Key *k = keyNew(0);
310 // work with it
311 keyDel (k);
312
313 .fi
314 .PP
315 .PP
316 If you want the key object to contain a name, value, comment and other meta info read on.
317 .PP
318 \fBNote:\fP
319 .RS 4
320 When you already have a key with similar properties its easier and cheaper to \fBkeyDup()\fP the key.
321 .RE
322 .PP
323 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
324 .PP
325 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.
326 .PP
327 The simplest and minimum way to use it is with no tags, only a key name: 
328 .PP
329 .nf
330 Key *nullKey,*emptyNamedKey;
331
332 // Create a key that has no name, is completely empty, but is initialized
333 nullKey=keyNew(0);
334 keyDel (nullKey);
335
336 // Is the same as above
337 nullKey=keyNew('', KEY_END);
338 keyDel (nullKey);
339
340 // Create and initialize a key with a name and nothing else
341 emptyNamedKey=keyNew('user/some/example',KEY_END);
342 keyDel (emptyNamedKey);
343
344 .fi
345 .PP
346 .PP
347 \fBkeyNew()\fP allocates memory for a key object and cleans everything up. After that, it processes the given argument list.
348 .PP
349 The Key attribute tags are the following:
350 .IP "\(bu" 2
351 keyswitch_t::KEY_TYPE 
352 .br
353  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.
354 .IP "\(bu" 2
355 keyswitch_t::KEY_SIZE 
356 .br
357  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.
358 .IP "\(bu" 2
359 keyswitch_t::KEY_VALUE 
360 .br
361  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!
362 .IP "\(bu" 2
363 keyswitch_t::KEY_UID, \fCkeyswitch_t::KEY_GID\fP 
364 .br
365  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.
366 .IP "\(bu" 2
367 keyswitch_t::KEY_MODE 
368 .br
369  Next parameter is taken as mode permissions (mode_t) to the key. See \fBkeySetMode()\fP.
370 .IP "\(bu" 2
371 keyswitch_t::KEY_DIR 
372 .br
373  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.
374 .IP "\(bu" 2
375 keyswitch_t::KEY_OWNER 
376 .br
377  Next parameter is the owner. See \fBkeySetOwner()\fP.
378 .IP "\(bu" 2
379 keyswitch_t::KEY_COMMENT 
380 .br
381  Next parameter is a comment. See \fBkeySetComment()\fP.
382 .IP "\(bu" 2
383 keyswitch_t::KEY_REMOVE 
384 .br
385  Mark the key to be removed instead of set it. See \fBkeyRemove()\fP.
386 .IP "\(bu" 2
387 keyswitch_t::KEY_STAT 
388 .br
389  Mark the key to be stated instead of get it. See \fBkeyStat()\fP.
390 .IP "\(bu" 2
391 keyswitch_t::KEY_END 
392 .br
393  Must be the last parameter passed to \fBkeyNew()\fP. It is always required, unless the \fCkeyName\fP is 0.
394 .PP
395 .PP
396 \fBExample:\fP.RS 4
397
398 .PP
399 .nf
400 KeySet *ks=ksNew(0);
401
402 ksAppendKey(ks,keyNew(0));       // an empty key
403
404 ksAppendKey(ks,keyNew('user/sw',              // the name of the key
405         KEY_END));                      // no more args
406
407 ksAppendKey(ks,keyNew('user/tmp/ex1',
408         KEY_VALUE,'some data',          // set a string value
409         KEY_END));                      // end of args
410
411 ksAppendKey(ks,keyNew('user/tmp/ex2',
412         KEY_VALUE,'some data',          // with a simple value
413         KEY_MODE,0777,                  // permissions
414         KEY_END));                      // end of args
415
416 ksAppendKey(ks,keyNew('user/tmp/ex4',
417         KEY_TYPE,KEY_TYPE_BINARY,       // key type
418         KEY_SIZE,7,                     // assume binary length 7
419         KEY_VALUE,'some data',          // value that will be truncated in 7 bytes
420         KEY_COMMENT,'value is truncated',
421         KEY_OWNER,'root',               // owner (not uid) is root
422         KEY_UID,0,                      // root uid
423         KEY_END));                      // end of args
424
425 ksAppendKey(ks,keyNew('user/tmp/ex5',
426         KEY_TYPE,
427                 KEY_TYPE_DIR | KEY_TYPE_BINARY,// dir key with a binary value
428         KEY_SIZE,7,
429         KEY_VALUE,'some data',          // value that will be truncated in 7 bytes
430         KEY_COMMENT,'value is truncated',
431         KEY_OWNER,'root',               // owner (not uid) is root
432         KEY_UID,0,                      // root uid
433         KEY_END));                      // end of args
434
435 ksDel(ks);
436
437 .fi
438 .PP
439 .RE
440 .PP
441 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.
442 .PP
443 .PP
444 .nf
445 Key *k = keyNew(0); // ref counter 0
446 ksAppendKey(ks, k); // ref counter of key 1
447 ksDel(ks); // key will be deleted with keyset
448  *
449 .fi
450 .PP
451 .PP
452 If you increment only by one with keyInc() the same as said above is valid:
453 .PP
454 .PP
455 .nf
456 Key *k = keyNew(0); // ref counter 0
457 keyIncRef(k); // ref counter of key 1
458 keyDel(k);    // has no effect
459 keyDecRef(k); // ref counter back to 0
460 keyDel(k);    // key is now deleted
461  *
462 .fi
463 .PP
464 .PP
465 If you add the key to more keySets:
466 .PP
467 .PP
468 .nf
469 Key *k = keyNew(0); // ref counter 0
470 ksAppendKey(ks1, k); // ref counter of key 1
471 ksAppendKey(ks2, k); // ref counter of key 2
472 ksDel(ks1); // ref counter of key 1
473 ksDel(ks2); // k is now deleted
474  *
475 .fi
476 .PP
477 .PP
478 or use keyInc() more than once:
479 .PP
480 .PP
481 .nf
482 Key *k = keyNew(0); // ref counter 0
483 keyIncRef(k); // ref counter of key 1
484 keyDel (k);   // has no effect
485 keyIncRef(k); // ref counter of key 2
486 keyDel (k);   // has no effect
487 keyDecRef(k); // ref counter of key 1
488 keyDel (k);   // has no effect
489 keyDecRef(k); // ref counter is now 0
490 keyDel (k); // k is now deleted
491  *
492 .fi
493 .PP
494 .PP
495 they key won't be deleted by a \fBkeyDel()\fP as long refcounter is not 0.
496 .PP
497 The key's sync bit will always be set for any call, except: 
498 .PP
499 .nf
500 Key *k = keyNew(0);
501 // keyNeedSync() will be false
502
503 .fi
504 .PP
505 .PP
506 \fBParameters:\fP
507 .RS 4
508 \fIkeyName\fP a valid name to the key, or NULL to get a simple initialized, but really empty, object 
509 .RE
510 .PP
511 \fBSee also:\fP
512 .RS 4
513 \fBkeyDel()\fP 
514 .RE
515 .PP
516 \fBReturns:\fP
517 .RS 4
518 a pointer to a new allocated and initialized Key object, or NULL if an invalid \fCkeyName\fP was passed (see \fBkeySetName()\fP). 
519 .RE
520 .PP
521