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

ex_access.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.h>
00023 
00024 #define DATABASE        "access.db"
00025 int main __P((int, char *[]));
00026 int usage __P((void));
00027 
00028 int
00029 main(argc, argv)
00030         int argc;
00031         char *argv[];
00032 {
00033         extern int optind;
00034         DB *dbp;
00035         DBC *dbcp;
00036         DBT key, data;
00037         size_t len;
00038         int ch, ret, rflag;
00039         char *database, *p, *t, buf[1024], rbuf[1024];
00040         const char *progname = "ex_access";             /* Program name. */
00041 
00042         rflag = 0;
00043         while ((ch = getopt(argc, argv, "r")) != EOF)
00044                 switch (ch) {
00045                 case 'r':
00046                         rflag = 1;
00047                         break;
00048                 case '?':
00049                 default:
00050                         return (usage());
00051                 }
00052         argc -= optind;
00053         argv += optind;
00054 
00055         /* Accept optional database name. */
00056         database = *argv == NULL ? DATABASE : argv[0];
00057 
00058         /* Optionally discard the database. */
00059         if (rflag)
00060                 (void)remove(database);
00061 
00062         /* Create and initialize database object, open the database. */
00063         if ((ret = db_create(&dbp, NULL, 0)) != 0) {
00064                 fprintf(stderr,
00065                     "%s: db_create: %s\n", progname, db_strerror(ret));
00066                 return (EXIT_FAILURE);
00067         }
00068         dbp->set_errfile(dbp, stderr);
00069         dbp->set_errpfx(dbp, progname);
00070         if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
00071                 dbp->err(dbp, ret, "set_pagesize");
00072                 goto err1;
00073         }
00074         if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0) {
00075                 dbp->err(dbp, ret, "set_cachesize");
00076                 goto err1;
00077         }
00078         if ((ret = dbp->open(dbp,
00079             NULL, database, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
00080                 dbp->err(dbp, ret, "%s: open", database);
00081                 goto err1;
00082         }
00083 
00084         /*
00085          * Insert records into the database, where the key is the user
00086          * input and the data is the user input in reverse order.
00087          */
00088         memset(&key, 0, sizeof(DBT));
00089         memset(&data, 0, sizeof(DBT));
00090         for (;;) {
00091                 printf("input> ");
00092                 fflush(stdout);
00093                 if (fgets(buf, sizeof(buf), stdin) == NULL)
00094                         break;
00095                 if (strcmp(buf, "exit\n") == 0 || strcmp(buf, "quit\n") == 0)
00096                         break;
00097                 if ((len = strlen(buf)) <= 1)
00098                         continue;
00099                 for (t = rbuf, p = buf + (len - 2); p >= buf;)
00100                         *t++ = *p--;
00101                 *t++ = '\0';
00102 
00103                 key.data = buf;
00104                 data.data = rbuf;
00105                 data.size = key.size = (u_int32_t)len - 1;
00106 
00107                 switch (ret =
00108                     dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) {
00109                 case 0:
00110                         break;
00111                 default:
00112                         dbp->err(dbp, ret, "DB->put");
00113                         if (ret != DB_KEYEXIST)
00114                                 goto err1;
00115                         break;
00116                 }
00117         }
00118         printf("\n");
00119 
00120         /* Acquire a cursor for the database. */
00121         if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
00122                 dbp->err(dbp, ret, "DB->cursor");
00123                 goto err1;
00124         }
00125 
00126         /* Initialize the key/data pair so the flags aren't set. */
00127         memset(&key, 0, sizeof(key));
00128         memset(&data, 0, sizeof(data));
00129 
00130         /* Walk through the database and print out the key/data pairs. */
00131         while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0)
00132                 printf("%.*s : %.*s\n",
00133                     (int)key.size, (char *)key.data,
00134                     (int)data.size, (char *)data.data);
00135         if (ret != DB_NOTFOUND) {
00136                 dbp->err(dbp, ret, "DBcursor->get");
00137                 goto err2;
00138         }
00139 
00140         /* Close everything down. */
00141         if ((ret = dbcp->c_close(dbcp)) != 0) {
00142                 dbp->err(dbp, ret, "DBcursor->close");
00143                 goto err1;
00144         }
00145         if ((ret = dbp->close(dbp, 0)) != 0) {
00146                 fprintf(stderr,
00147                     "%s: DB->close: %s\n", progname, db_strerror(ret));
00148                 return (EXIT_FAILURE);
00149         }
00150         return (EXIT_SUCCESS);
00151 
00152 err2:   (void)dbcp->c_close(dbcp);
00153 err1:   (void)dbp->close(dbp, 0);
00154         return (EXIT_FAILURE);
00155 }
00156 
00157 int
00158 usage()
00159 {
00160         (void)fprintf(stderr, "usage: ex_access [-r] [database]\n");
00161         return (EXIT_FAILURE);
00162 }

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