Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

dbdemo.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: ex_access.c,v 12.1 2005/06/16 20:22:00 bostic Exp $
00008  */
00009 
00010 #include <sys/types.h>
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 #ifdef _WIN32
00017 extern int getopt(int, char * const *, const char *);
00018 #else
00019 #include <unistd.h>
00020 #endif
00021 
00022 #include <db_config.h>
00023 #include <db_int.h>
00024 
00025 #define DATABASE        "access.db"
00026 int dbdemo_main __P((int, char *[]));
00027 int dbdemo_usage __P((void));
00028 
00029 int
00030 dbdemo(args)
00031         char *args;
00032 {
00033         int argc;
00034         char **argv;
00035 
00036         __db_util_arg("dbdemo", args, &argc, &argv);
00037         return (dbdemo_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
00038 }
00039 
00040 #include <stdio.h>
00041 #define ERROR_RETURN    ERROR
00042 
00043 int
00044 dbdemo_main(argc, argv)
00045         int argc;
00046         char *argv[];
00047 {
00048         extern int optind, __db_getopt_reset;
00049         DB *dbp;
00050         DBC *dbcp;
00051         DBT key, data;
00052         size_t len;
00053         int ch, ret, rflag;
00054         char *database, *p, *t, buf[1024], rbuf[1024];
00055         const char *progname = "dbdemo";                /* Program name. */
00056 
00057         rflag = 0;
00058         __db_getopt_reset = 1;
00059         while ((ch = getopt(argc, argv, "r")) != EOF)
00060                 switch (ch) {
00061                 case 'r':
00062                         rflag = 1;
00063                         break;
00064                 case '?':
00065                 default:
00066                         return (dbdemo_usage());
00067                 }
00068         argc -= optind;
00069         argv += optind;
00070 
00071         /* Accept optional database name. */
00072         database = *argv == NULL ? DATABASE : argv[0];
00073 
00074         /* Optionally discard the database. */
00075         if (rflag)
00076                 (void)remove(database);
00077 
00078         /* Create and initialize database object, open the database. */
00079         if ((ret = db_create(&dbp, NULL, 0)) != 0) {
00080                 fprintf(stderr,
00081                     "%s: db_create: %s\n", progname, db_strerror(ret));
00082                 return (EXIT_FAILURE);
00083         }
00084         dbp->set_errfile(dbp, stderr);
00085         dbp->set_errpfx(dbp, progname);
00086         if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
00087                 dbp->err(dbp, ret, "set_pagesize");
00088                 goto err1;
00089         }
00090         if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0) {
00091                 dbp->err(dbp, ret, "set_cachesize");
00092                 goto err1;
00093         }
00094         if ((ret = dbp->open(dbp,
00095             NULL, database, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
00096                 dbp->err(dbp, ret, "%s: open", database);
00097                 goto err1;
00098         }
00099 
00100         /*
00101          * Insert records into the database, where the key is the user
00102          * input and the data is the user input in reverse order.
00103          */
00104         memset(&key, 0, sizeof(DBT));
00105         memset(&data, 0, sizeof(DBT));
00106         for (;;) {
00107                 printf("input> ");
00108                 fflush(stdout);
00109                 if (fgets(buf, sizeof(buf), stdin) == NULL)
00110                         break;
00111                 if (strcmp(buf, "exit\n") == 0 || strcmp(buf, "quit\n") == 0)
00112                         break;
00113                 if ((len = strlen(buf)) <= 1)
00114                         continue;
00115                 for (t = rbuf, p = buf + (len - 2); p >= buf;)
00116                         *t++ = *p--;
00117                 *t++ = '\0';
00118 
00119                 key.data = buf;
00120                 data.data = rbuf;
00121                 data.size = key.size = (u_int32_t)len - 1;
00122 
00123                 switch (ret =
00124                     dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) {
00125                 case 0:
00126                         break;
00127                 default:
00128                         dbp->err(dbp, ret, "DB->put");
00129                         if (ret != DB_KEYEXIST)
00130                                 goto err1;
00131                         break;
00132                 }
00133         }
00134         printf("\n");
00135 
00136         /* Acquire a cursor for the database. */
00137         if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
00138                 dbp->err(dbp, ret, "DB->cursor");
00139                 goto err1;
00140         }
00141 
00142         /* Initialize the key/data pair so the flags aren't set. */
00143         memset(&key, 0, sizeof(key));
00144         memset(&data, 0, sizeof(data));
00145 
00146         /* Walk through the database and print out the key/data pairs. */
00147         while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0)
00148                 printf("%.*s : %.*s\n",
00149                     (int)key.size, (char *)key.data,
00150                     (int)data.size, (char *)data.data);
00151         if (ret != DB_NOTFOUND) {
00152                 dbp->err(dbp, ret, "DBcursor->get");
00153                 goto err2;
00154         }
00155 
00156         /* Close everything down. */
00157         if ((ret = dbcp->c_close(dbcp)) != 0) {
00158                 dbp->err(dbp, ret, "DBcursor->close");
00159                 goto err1;
00160         }
00161         if ((ret = dbp->close(dbp, 0)) != 0) {
00162                 fprintf(stderr,
00163                     "%s: DB->close: %s\n", progname, db_strerror(ret));
00164                 return (EXIT_FAILURE);
00165         }
00166         return (EXIT_SUCCESS);
00167 
00168 err2:   (void)dbcp->c_close(dbcp);
00169 err1:   (void)dbp->close(dbp, 0);
00170         return (EXIT_FAILURE);
00171 }
00172 
00173 int
00174 dbdemo_usage()
00175 {
00176         (void)fprintf(stderr, "usage: ex_access [-r] [database]\n");
00177         return (EXIT_FAILURE);
00178 }

Generated on Sun Dec 25 12:14:17 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2