From 61768feec636cdbf976bbd200204c6ba746a2e53 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Wed, 11 Jan 2006 16:20:16 +0000 Subject: r12847: add some ldb examples from Brad Hards (bradh@frogmouth.net) metze (This used to be commit 4d32d50ccd8bd0bfb3e2d6f5aee23bce38abbc03) --- source4/lib/ldb/examples/ldifreader.c | 129 ++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 source4/lib/ldb/examples/ldifreader.c (limited to 'source4/lib/ldb/examples/ldifreader.c') diff --git a/source4/lib/ldb/examples/ldifreader.c b/source4/lib/ldb/examples/ldifreader.c new file mode 100644 index 0000000000..3b8591e73f --- /dev/null +++ b/source4/lib/ldb/examples/ldifreader.c @@ -0,0 +1,129 @@ +/* + example code for the ldb database library + + Copyright (C) Brad Hards (bradh@frogmouth.net) 2005-2006 + + ** NOTE! The following LGPL license applies to the ldb + ** library. This does NOT imply that all of Samba is released + ** under the LGPL + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301 USA +*/ + +/** \example ldifreader.c + +The code below shows a simple LDB application. + +It lists / dumps the entries in an LDIF file to standard output. + +*/ + +#include "includes.h" +#include "ldb/include/ldb.h" +#include "ldb/include/ldb_errors.h" + +/* + ldb_ldif_write takes a function pointer to a custom output + function. This version is about as simple as the output function can + be. In a more complex example, you'd likely be doing something with + the private data function (e.g. holding a file handle). +*/ +static int vprintf_fn(void *private_data, const char *fmt, ...) +{ + int retval; + va_list ap; + + va_start(ap, fmt); + /* We just write to standard output */ + retval = vprintf(fmt, ap); + va_end(ap); + /* Note that the function should return the number of + bytes written, or a negative error code */ + return retval; +} + +int main(int argc, const char **argv) +{ + struct ldb_context *ldb; + FILE *fileStream; + struct ldb_ldif *ldifMsg; + + if (argc != 2) { + printf("Usage %s filename.ldif\n", argv[0]); + exit(1); + } + + /* + This is the always the first thing you want to do in an LDB + application - initialise up the context structure. + + Note that you can use the context structure as a parent + for talloc allocations as well + */ + ldb = ldb_init(NULL); + + fileStream = fopen(argv[1], "r"); + if (0 == fileStream) { + perror(argv[1]); + exit(1); + } + + /* + We now work through the filestream to get each entry. + */ + while ( (ldifMsg = ldb_ldif_read_file(ldb, fileStream)) ) { + /* + Each message has a particular change type. For Add, + Modify and Delete, this will also appear in the + output listing (as changetype: add, changetype: + modify or changetype:delete, respectively). + */ + switch (ldifMsg->changetype) { + case LDB_CHANGETYPE_NONE: + printf("ChangeType: None\n"); + break; + case LDB_CHANGETYPE_ADD: + printf("ChangeType: Add\n"); + break; + case LDB_CHANGETYPE_MODIFY: + printf("ChangeType: Modify\n"); + break; + case LDB_CHANGETYPE_DELETE: + printf("ChangeType: Delete\n"); + break; + default: + printf("ChangeType: Unknown\n"); + } + + /* + We can now write out the results, using our custom + output routine as defined at the top of this file. + */ + ldb_ldif_write(ldb, vprintf_fn, NULL, ldifMsg); + + /* + Clean up the message + */ + ldb_ldif_read_free(ldb, ldifMsg); + } + + /* + Clean up the context + */ + talloc_free(ldb); + + return 0; +} -- cgit From 52fb06edc25e8538c413df1aaabba18c859a00cf Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sat, 5 May 2007 18:50:56 +0000 Subject: r22681: Fix standalone ldb build when parent directory name != ldb. (This used to be commit 1093875d59f1ea9b8bd82277d4f9d8366e584952) --- source4/lib/ldb/examples/ldifreader.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source4/lib/ldb/examples/ldifreader.c') diff --git a/source4/lib/ldb/examples/ldifreader.c b/source4/lib/ldb/examples/ldifreader.c index 3b8591e73f..c392f311e5 100644 --- a/source4/lib/ldb/examples/ldifreader.c +++ b/source4/lib/ldb/examples/ldifreader.c @@ -31,9 +31,9 @@ It lists / dumps the entries in an LDIF file to standard output. */ -#include "includes.h" -#include "ldb/include/ldb.h" -#include "ldb/include/ldb_errors.h" +#include "ldb_includes.h" +#include "ldb.h" +#include "ldb_errors.h" /* ldb_ldif_write takes a function pointer to a custom output -- cgit From b8d69a7ea2505b706ff7c74d7c97bc89d82dfa07 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 02:46:15 +0000 Subject: r23795: more v2->v3 conversion (This used to be commit 84b468b2f8f2dffda89593f816e8bc6a8b6d42ac) --- source4/lib/ldb/examples/ldifreader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/lib/ldb/examples/ldifreader.c') diff --git a/source4/lib/ldb/examples/ldifreader.c b/source4/lib/ldb/examples/ldifreader.c index c392f311e5..4eb7b5f1e5 100644 --- a/source4/lib/ldb/examples/ldifreader.c +++ b/source4/lib/ldb/examples/ldifreader.c @@ -10,7 +10,7 @@ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. + version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -- cgit From e1c15c74af7366901eac9fb9a8e1e674928855ec Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 03:52:17 +0000 Subject: r23799: updated old Franklin Street FSF addresses to new URL (This used to be commit db92b76a0034899f5f0dc2d012ee7709ff9a6132) --- source4/lib/ldb/examples/ldifreader.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'source4/lib/ldb/examples/ldifreader.c') diff --git a/source4/lib/ldb/examples/ldifreader.c b/source4/lib/ldb/examples/ldifreader.c index 4eb7b5f1e5..926d99f838 100644 --- a/source4/lib/ldb/examples/ldifreader.c +++ b/source4/lib/ldb/examples/ldifreader.c @@ -18,9 +18,7 @@ Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301 USA + License along with this library; if not, see . */ /** \example ldifreader.c -- cgit From 929adc9efa5cf985f0585214d30d18521aa1a821 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sat, 14 Jun 2008 11:24:17 -0400 Subject: Make up the right dependencies now that ldb depends on libevents (This used to be commit 3b8eec7ca334528cad3cdcd5e3fc5ee555d8d0e0) --- source4/lib/ldb/examples/ldifreader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source4/lib/ldb/examples/ldifreader.c') diff --git a/source4/lib/ldb/examples/ldifreader.c b/source4/lib/ldb/examples/ldifreader.c index 926d99f838..12e7a1a6fd 100644 --- a/source4/lib/ldb/examples/ldifreader.c +++ b/source4/lib/ldb/examples/ldifreader.c @@ -71,7 +71,7 @@ int main(int argc, const char **argv) Note that you can use the context structure as a parent for talloc allocations as well */ - ldb = ldb_init(NULL); + ldb = ldb_init(NULL, NULL); fileStream = fopen(argv[1], "r"); if (0 == fileStream) { -- cgit