Added tdb_store_by_string() and tdb_fetch_by_string() functions to store
[samba.git] / source / tdb / tdbutil.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    tdb utility functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /* these are little tdb utility functions that are meant to make
25    dealing with a tdb database a little less cumbersome in Samba */
26
27
28 /* fetch a value by a arbitrary blob key, return -1 if not found */
29 int tdb_get_int_byblob(TDB_CONTEXT *tdb, char *keyval, size_t len)
30 {
31         TDB_DATA key, data;
32         int ret;
33
34         key.dptr = keyval;
35         key.dsize = len;
36         data = tdb_fetch(tdb, key);
37         if (!data.dptr || data.dsize != sizeof(int)) return -1;
38         
39         memcpy(&ret, data.dptr, sizeof(int));
40         free(data.dptr);
41         return ret;
42 }
43
44 /* fetch a value by string key, return -1 if not found */
45 int tdb_get_int(TDB_CONTEXT *tdb, char *keystr)
46 {
47         return tdb_get_int_byblob(tdb, keystr, strlen(keystr));
48 }
49
50 /* store a value by an arbitary blob key, return 0 on success, -1 on failure */
51 int tdb_store_int_byblob(TDB_CONTEXT *tdb, char *keystr, size_t len, int v)
52 {
53         TDB_DATA key, data;
54
55         key.dptr = keystr;
56         key.dsize = len;
57         data.dptr = (void *)&v;
58         data.dsize = sizeof(int);
59
60         return tdb_store(tdb, key, data, TDB_REPLACE);
61 }
62
63 /* store a value by string key, return 0 on success, -1 on failure */
64 int tdb_store_int(TDB_CONTEXT *tdb, char *keystr, int v)
65 {
66         return tdb_store_int_byblob(tdb, keystr, strlen(keystr), v);
67 }
68
69 /* Store a buffer by a null terminated string key.  Return 0 on success, -1
70    on failure */
71 int tdb_store_by_string(TDB_CONTEXT *tdb, char *keystr, void *buffer, int len)
72 {
73     TDB_DATA key, data;
74
75     key.dptr = keystr;
76     key.dsize = strlen(keystr) + 1;
77
78     data.dptr = buffer;
79     data.dsize = len;
80
81     return tdb_store(tdb, key, data, TDB_REPLACE);
82 }
83
84 /* Fetch a buffer using a null terminated string key.  Don't forget to call
85    free() on the result dptr. */
86 TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, char *keystr)
87 {
88     TDB_DATA key;
89
90     key.dptr = keystr;
91     key.dsize = strlen(keystr) + 1;
92
93     return tdb_fetch(tdb, key);
94 }