Fix the mess with ldb includes.
[ira/wip.git] / source4 / lib / ldb / examples / ldifreader.c
1 /* 
2    example code for the ldb database library
3
4    Copyright (C) Brad Hards (bradh@frogmouth.net) 2005-2006
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /** \example ldifreader.c
25
26 The code below shows a simple LDB application.
27
28 It lists / dumps the entries in an LDIF file to standard output.
29
30 */
31
32 #include "ldb.h"
33
34 /*
35   ldb_ldif_write takes a function pointer to a custom output
36   function. This version is about as simple as the output function can
37   be. In a more complex example, you'd likely be doing something with
38   the private data function (e.g. holding a file handle).
39 */
40 static int vprintf_fn(void *private_data, const char *fmt, ...)
41 {
42         int retval;
43         va_list ap;
44
45         va_start(ap, fmt);
46         /* We just write to standard output */
47         retval = vprintf(fmt, ap);
48         va_end(ap);
49         /* Note that the function should return the number of 
50            bytes written, or a negative error code */
51         return retval;
52 }
53   
54 int main(int argc, const char **argv)
55 {
56         struct ldb_context *ldb;
57         FILE *fileStream;
58         struct ldb_ldif *ldifMsg;
59
60         if (argc != 2) {
61                 printf("Usage %s filename.ldif\n", argv[0]);
62                 exit(1);
63         }
64
65         /*
66           This is the always the first thing you want to do in an LDB
67           application - initialise up the context structure.
68
69           Note that you can use the context structure as a parent
70           for talloc allocations as well
71         */
72         ldb = ldb_init(NULL, NULL);
73
74         fileStream = fopen(argv[1], "r");
75         if (0 == fileStream) {
76                 perror(argv[1]);
77                 exit(1);
78         }
79
80         /*
81           We now work through the filestream to get each entry.
82         */
83         while ( (ldifMsg = ldb_ldif_read_file(ldb, fileStream)) ) {
84                 /*
85                   Each message has a particular change type. For Add,
86                   Modify and Delete, this will also appear in the
87                   output listing (as changetype: add, changetype:
88                   modify or changetype:delete, respectively).
89                 */
90                 switch (ldifMsg->changetype) {
91                 case LDB_CHANGETYPE_NONE:
92                         printf("ChangeType: None\n");
93                         break;
94                 case LDB_CHANGETYPE_ADD:
95                         printf("ChangeType: Add\n");
96                         break;
97                 case LDB_CHANGETYPE_MODIFY:
98                         printf("ChangeType: Modify\n");
99                         break;
100                 case LDB_CHANGETYPE_DELETE:
101                         printf("ChangeType: Delete\n");
102                         break;
103                 default:
104                         printf("ChangeType: Unknown\n");
105                 }
106
107                 /*
108                   We can now write out the results, using our custom
109                   output routine as defined at the top of this file. 
110                 */
111                 ldb_ldif_write(ldb, vprintf_fn, NULL, ldifMsg);
112
113                 /*
114                   Clean up the message
115                 */
116                 ldb_ldif_read_free(ldb, ldifMsg);
117         }
118
119         /*
120           Clean up the context
121         */
122         talloc_free(ldb);
123
124         return 0;
125 }