home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
online
/
source
/
c
/
compilers
/
Tickle-4.0.sit.hqx
/
Tickle-4.0
/
cbtree
/
src
/
cbtest.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-09
|
8KB
|
422 lines
#include <ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <db.h>
#include <btree.h>
#ifdef DOC
typedef struct {
char *dptr;
int dsize;
} datum;
cc ... -ldbm ...
#endif
extern char *malloc();
DB *the_db;
char dirfile[256];
char datafile[256];
char command[256];
char cmd_str[256];
char arg1[256];
char arg2[256];
int
my_btree_cmp(p1, p2)
char *p1, *p2;
{
/*fprintf(stderr, "my_btree_cmp: p1 x%lx '%s' p2 x%lx '%s'\n", p1, p1, p2, p2);*/
return strcmp(p1, p2);
}
main(argc, argv)
int argc;
char *argv[];
{
int done,
count,
args,
strind,
result,
promptlen;
DBT key,
data;
char *prompt = "[f/i/l/r/d/o/q] ",
prefix[128];
BTREEINFO openinfo;
#pragma unused (argc, argv)
strcpy(datafile, argv[1]);
openinfo.flags = R_DUP;
openinfo.cachesize = 0;
openinfo.compare = my_btree_cmp; /* use strcmp() */
openinfo.lorder = BIG_ENDIAN;
openinfo.psize = 4096;
the_db = btree_open(datafile, O_RDWR | O_CREAT, 0666, &openinfo);
if (the_db == NULL) {
fprintf(stderr, "ERROR: opening DB '%s'\n", datafile);
exit(2);
}
promptlen = strlen(prompt);
for (done=0; ! done; ) {
printf(prompt); fflush(stdout);
if (gets(command) == NULL)
break;
strind = 0;
if (strncmp(command, prompt, promptlen) == 0)
strind = promptlen;
args = sscanf(&command[strind], "%s%s%s", cmd_str, arg1, arg2);
switch (cmd_str[0]) {
case 'q': case 'Q':
done = 1;
break;
case 'o': case 'O':
do_other_commands();
break;
case 'f': case 'F':
key.data = arg1;
key.size = strlen(arg1);
result = (* the_db->get)(the_db, &key, &data, 0);
if (result != 0)
fprintf(stderr, "Key '%s' fetch failed (%d).\n", arg1, result);
else
print_datum("FIND", arg1, &data);
break;
case 'i': case 'I':
key.data = arg1;
key.size = strlen(arg1) + 1;
data.data = arg2;
data.size = strlen(arg2) + 1;
result = (* the_db->put)(the_db, &key, &data, R_PUT);
if (result != 0)
fprintf(stderr, "Key '%s' insert failed (%d).\n", arg1, result);
else
printf("Key '%s' inserted.\n", arg1);
break;
case 'd': case 'D':
key.data = arg1;
key.size = strlen(arg1);
result = (* the_db->del)(the_db, &key, R_PUT);
if (result != 0)
fprintf(stderr, "Key '%s' insert failed (%d).\n", arg1, result);
else
printf("Key '%s' inserted.\n", arg1);
break;
case 'r': case 'R':
key.data = arg1;
key.size = strlen(arg1) + 1;
data.data = arg2;
data.size = strlen(arg2) + 1;
result = (* the_db->put)(the_db, &key, &data, R_PUT);
if (result != 0)
fprintf(stderr, "Key '%s' insert failed (%d).\n", arg1, result);
else
printf("Key '%s' inserted.\n", arg1);
break;
case 'l': case 'L':
result = (* the_db->seq) (the_db, &key, &data, R_FIRST);
if (result < 0)
{
fprintf(stderr, "Error reading first key.\n");
}
else if (result > 0)
{
fprintf(stderr, "The database contains no keys.\n");
}
else
{
for (count=1; result == 0 ; )
{
sprintf(prefix, "Item #%02d", count++);
print_datum(prefix, key.data, &data);
key.size = 0;
key.data = "";
result = (* the_db->seq) (the_db, &key, &data, R_NEXT);
}
}
break;
}
fflush(stdout);
}
if ((* the_db->close)(the_db) == -1)
fprintf(stderr, "ERROR closing DB.\n");
}
print_datum(message, key, data)
char *message;
char *key;
DBT *data;
{
printf( "%s%s%s%s ", message,
( key==NULL ? ":" : ": <" ),
( key==NULL ? "" : key ),
( key==NULL ? " " : "> " )
);
printf("[%d]<%.*s>\n", data->size, data->size, data->data);
}
do_other_commands()
{
int done,
args,
strind,
promptlen;
DBT key,
data;
char *prompt = "[h/f/q] ";
promptlen = strlen(prompt);
for (done=0; ! done; ) {
printf(prompt); fflush(stdout);
if (gets(command) == NULL)
break;
strind = 0;
if (strncmp(command, prompt, promptlen) == 0)
strind = promptlen;
args = sscanf(&command[strind], "%s%s%s", cmd_str, arg1, arg2);
switch (cmd_str[0]) {
case 'q': case 'Q':
done = 1;
break;
#ifdef NEVER_DEFINED
#ifdef SDBM
case 'h': case 'H':
hash = 0;
printf("NEED TO IMPLEMENT\n");
break;
#else
case 'h': case 'H':
data.dptr = arg1;
data.dsize = strlen(arg1);
hash = dcalchash(data);
printf("dcalchash(%s) = %d[x%08lX]\n", arg1, hash, hash);
break;
#endif
#endif
case 'f': case 'F':
{
int limit, i, result;
char keystr[32];
char datastr[64];
printf("FILLING DB...\n"); fflush(stdout);
limit = atoi(arg2);
for (i=atoi(arg1); i<=limit; ++i)
{
sprintf(keystr, "Key%d", i);
sprintf(datastr, "DATA --KEY--> <Key%d>", i);
key.data = keystr;
key.size = strlen(keystr) + 1;
data.data = datastr;
data.size = strlen(datastr) + 1;
result = (* the_db->put)(the_db, &key, &data, R_PUT);
if (result != 0)
{
fprintf(stderr, "FAILURE[%d]: Key '%s' store.\n", result, keystr);
break;
}
}
printf("FILLING COMPLETED.\n");
}
break;
}
fflush(stdout);
}
}
typedef struct {
char *ptr;
char *file;
int line;
} MALLOC_ITEM;
static int mptr = 0;
static MALLOC_ITEM mstk[1024];
static int fptr = 0;
static MALLOC_ITEM fstk[1024];
char *
Malloc(size, file, line)
int size;
char *file;
int line;
{
char *ptr;
#pragma unused (file, line)
ptr = malloc(size);
/*fprintf(stderr, "MALLOC: x%08lX %d bytes %s:%d\n", ptr, size, file, line);*/
#ifdef NEVER_DEFINED
if (ptr != NULL)
{
mstk[mptr].ptr = ptr;
mstk[mptr].file = file;
mstk[mptr].line = line;
++mptr;
memset(ptr, 0, size);
}
#endif
return ptr;
}
Free(ptr, file, line)
char *ptr;
char *file;
int line;
{
#pragma unused (file, line)
/*fprintf(stderr, " FREE: x%08lX %s:%d\n", ptr, file, line);*/
free(ptr);
#ifdef NEVER_DEFINED
int i;
for (i=0; i < mptr; ++i)
if (mstk[i].ptr == ptr)
break;
if (i >= mptr)
{
fprintf(stderr, "FREE: x%08lX %s:%d NOT ALLOCATED!!\n", ptr, file, line);
}
else
{
if (i < mptr - 1)
{
memmove(&mstk[i], mstk[i+1], ((mptr - (i + 1)) * sizeof(MALLOC_ITEM)) );
}
--mptr;
free(ptr);
}
#endif
}
#ifdef NEVER_DEFINED
cd {src}bsd-cbtree:
search printf ≈.c
C -mbg full -b3 big.c
C -mbg full -b3 btree.c
C -mbg full -b3 delete.c
C -mbg full -b3 insert.c
C -mbg full -b3 lrucache.c
C -mbg full -b3 lruhash.c
C -mbg full -b3 lrutils.c
C -mbg full -b3 search.c
C -mbg full -b3 seq.c
C -mbg full -b3 split.c
C -mbg full -b3 storage.c
C -mbg full -b3 updutils.c
C -mbg full -b3 utils.c
Lib -o cbtree.o ∂
-sg CBTREE=Main ∂
big.c.o ∂
btree.c.o ∂
delete.c.o ∂
insert.c.o ∂
lrucache.c.o ∂
lruhash.c.o ∂
lrutils.c.o ∂
search.c.o ∂
seq.c.o ∂
split.c.o ∂
storage.c.o ∂
updutils.c.o ∂
utils.c.o
cp cbtree.o {clibraries}cbtree.o
cp cbtree.o {clibraries}cbtreeMDBG.o
search db.h ≈.c
C -mbg full -b3 big.c
C -mbg full -b3 btree.c
C -mbg full -b3 delete.c
C -mbg full -b3 insert.c
C -mbg full -b3 lrucache.c
C -mbg full -b3 lruhash.c
C -mbg full -b3 lrutils.c
C -mbg full -b3 search.c
C -mbg full -b3 seq.c
C -mbg full -b3 split.c
C -mbg full -b3 storage.c
C -mbg full -b3 updutils.c
C -mbg full -b3 utils.c
C -mbg full -b3 {active}
Link -o cbtest -w ∂
-t MPST -c 'MPS ' ∂
{active}.o ∂
big.c.o ∂
btree.c.o ∂
delete.c.o ∂
insert.c.o ∂
lrucache.c.o ∂
lruhash.c.o ∂
lrutils.c.o ∂
search.c.o ∂
seq.c.o ∂
split.c.o ∂
storage.c.o ∂
updutils.c.o ∂
utils.c.o ∂
{clibraries}dprintf_drvr.o ∂
{clibraries}stat.o ∂
{libraries}runtime.o ∂
{libraries}interface.o ∂
{clibraries}stdclib.o
cbtest testME5
#endif