mariadb/bdb/examples_c/ex_access.c

163 lines
3.5 KiB
C
Raw Normal View History

2001-03-04 19:42:05 -05:00
/*-
* See the file LICENSE for redistribution information.
*
2002-10-30 15:57:05 +04:00
* Copyright (c) 1997-2002
2001-03-04 19:42:05 -05:00
* Sleepycat Software. All rights reserved.
*
2002-10-30 15:57:05 +04:00
* $Id: ex_access.c,v 11.22 2002/09/03 12:54:26 bostic Exp $
2001-03-04 19:42:05 -05:00
*/
#include <sys/types.h>
2002-10-30 15:57:05 +04:00
#include <stdio.h>
2001-03-04 19:42:05 -05:00
#include <stdlib.h>
#include <string.h>
2002-10-30 15:57:05 +04:00
#ifdef _WIN32
extern int getopt(int, char * const *, const char *);
#else
2001-03-04 19:42:05 -05:00
#include <unistd.h>
#endif
#include <db.h>
#define DATABASE "access.db"
2002-10-30 15:57:05 +04:00
int main __P((int, char *[]));
int usage __P((void));
2001-03-04 19:42:05 -05:00
int
main(argc, argv)
int argc;
char *argv[];
{
extern int optind;
2002-10-30 15:57:05 +04:00
DB *dbp;
DBC *dbcp;
DBT key, data;
u_int32_t len;
int ch, ret, rflag;
char *database, *p, *t, buf[1024], rbuf[1024];
const char *progname = "ex_access"; /* Program name. */
2001-03-04 19:42:05 -05:00
2002-10-30 15:57:05 +04:00
rflag = 0;
while ((ch = getopt(argc, argv, "r")) != EOF)
2001-03-04 19:42:05 -05:00
switch (ch) {
2002-10-30 15:57:05 +04:00
case 'r':
rflag = 1;
break;
2001-03-04 19:42:05 -05:00
case '?':
default:
2002-10-30 15:57:05 +04:00
return (usage());
2001-03-04 19:42:05 -05:00
}
argc -= optind;
argv += optind;
2002-10-30 15:57:05 +04:00
/* Accept optional database name. */
database = *argv == NULL ? DATABASE : argv[0];
2001-03-04 19:42:05 -05:00
2002-10-30 15:57:05 +04:00
/* Optionally discard the database. */
if (rflag)
(void)remove(database);
2001-03-04 19:42:05 -05:00
/* Create and initialize database object, open the database. */
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
fprintf(stderr,
"%s: db_create: %s\n", progname, db_strerror(ret));
2002-10-30 15:57:05 +04:00
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
dbp->set_errfile(dbp, stderr);
dbp->set_errpfx(dbp, progname);
if ((ret = dbp->set_pagesize(dbp, 1024)) != 0) {
dbp->err(dbp, ret, "set_pagesize");
goto err1;
}
if ((ret = dbp->set_cachesize(dbp, 0, 32 * 1024, 0)) != 0) {
dbp->err(dbp, ret, "set_cachesize");
goto err1;
}
2002-10-30 15:57:05 +04:00
if ((ret = dbp->open(dbp,
NULL, database, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
dbp->err(dbp, ret, "%s: open", database);
2001-03-04 19:42:05 -05:00
goto err1;
}
/*
* Insert records into the database, where the key is the user
* input and the data is the user input in reverse order.
*/
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
for (;;) {
printf("input> ");
fflush(stdout);
if (fgets(buf, sizeof(buf), stdin) == NULL)
break;
2002-10-30 15:57:05 +04:00
if (strcmp(buf, "exit\n") == 0 || strcmp(buf, "quit\n") == 0)
break;
2001-03-04 19:42:05 -05:00
if ((len = strlen(buf)) <= 1)
continue;
for (t = rbuf, p = buf + (len - 2); p >= buf;)
*t++ = *p--;
*t++ = '\0';
key.data = buf;
data.data = rbuf;
data.size = key.size = len - 1;
switch (ret =
dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE)) {
case 0:
break;
default:
dbp->err(dbp, ret, "DB->put");
if (ret != DB_KEYEXIST)
goto err1;
break;
}
}
printf("\n");
/* Acquire a cursor for the database. */
if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
dbp->err(dbp, ret, "DB->cursor");
goto err1;
}
/* Initialize the key/data pair so the flags aren't set. */
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
/* Walk through the database and print out the key/data pairs. */
while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0)
printf("%.*s : %.*s\n",
(int)key.size, (char *)key.data,
(int)data.size, (char *)data.data);
if (ret != DB_NOTFOUND) {
dbp->err(dbp, ret, "DBcursor->get");
goto err2;
}
/* Close everything down. */
if ((ret = dbcp->c_close(dbcp)) != 0) {
dbp->err(dbp, ret, "DBcursor->close");
goto err1;
}
if ((ret = dbp->close(dbp, 0)) != 0) {
fprintf(stderr,
"%s: DB->close: %s\n", progname, db_strerror(ret));
2002-10-30 15:57:05 +04:00
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}
2002-10-30 15:57:05 +04:00
return (EXIT_SUCCESS);
2001-03-04 19:42:05 -05:00
err2: (void)dbcp->c_close(dbcp);
err1: (void)dbp->close(dbp, 0);
2002-10-30 15:57:05 +04:00
return (EXIT_FAILURE);
}
int
usage()
{
(void)fprintf(stderr, "usage: ex_access [-r] [database]\n");
return (EXIT_FAILURE);
2001-03-04 19:42:05 -05:00
}