2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
24 /* these are little tdb utility functions that are meant to make
25 dealing with a tdb database a little less cumbersome in Samba */
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)
36 data = tdb_fetch(tdb, key);
37 if (!data.dptr || data.dsize != sizeof(int)) return -1;
39 memcpy(&ret, data.dptr, sizeof(int));
44 /* fetch a value by string key, return -1 if not found */
45 int tdb_get_int(TDB_CONTEXT *tdb, char *keystr)
47 return tdb_get_int_byblob(tdb, keystr, strlen(keystr));
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)
57 data.dptr = (void *)&v;
58 data.dsize = sizeof(int);
60 return tdb_store(tdb, key, data, TDB_REPLACE);
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)
66 return tdb_store_int_byblob(tdb, keystr, strlen(keystr), v);
69 /* Store a buffer by a null terminated string key. Return 0 on success, -1
71 int tdb_store_by_string(TDB_CONTEXT *tdb, char *keystr, void *buffer, int len)
76 key.dsize = strlen(keystr) + 1;
81 return tdb_store(tdb, key, data, TDB_REPLACE);
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)
91 key.dsize = strlen(keystr) + 1;
93 return tdb_fetch(tdb, key);