home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BTREE.ZIP
/
BTA.C
next >
Wrap
Text File
|
1989-08-31
|
13KB
|
456 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "bta.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <errno.h>
#include <btree.h>
#include <ctype.h>
#include <stdio.h>
/* #include <stdlib.h> */
void exit(); /* delete if you have stdlib.h */
#define EXIT_SUCCESS (0)
#define EXIT_FAILURE (1)
#define FILENAME ("demo.key")
#define M (4)
#define KS (24)
#define BS ((size_t)10)
int cmp(/* void *buf1, void *buf2, size_t count */);
#define USAGE ("Usage: bta")
/*man---------------------------------------------------------------------------
NAME
bta - btree access
SYNOPSIS
bta
DESCRIPTION
bta is a simple program to directly access a btree. Its purpose is
primarily instructional.
SEE ALSO
btdump, btlist, btree.
NOTES
bta is written for btrees sorted using the strncmp function. To
use it with with keys of a different type, modify the cmp function
in the file bta.c.
------------------------------------------------------------------------------*/
int main(argc, argv)
int argc;
char *argv[];
{
int rs = 0;
btree_t * btp = NULL;
char key[KS];
int c = 0;
/* initialize storage */
memset((void *)key, 0, sizeof(key));
/* process command line arguments */
if (argc != 1) {
printf("%s\n", USAGE);
bexit(EXIT_FAILURE);
}
/* open btree */
btp = btopen(FILENAME, "r+", cmp);
if (btp == NULL) {
if (errno == ENOENT) {
printf("B-tree does not exist. Creating.\n");
rs = btcreate(FILENAME, M, KS, cmp, 0);
if (rs == -1) {
printf("*** Error creating B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
btp = btopen(FILENAME, "r+", cmp);
if (btp == NULL) {
printf("*** Error opening B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
} else {
printf("*** Error opening B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
rs = btsetvbuf(btp, NULL, BS);
if (rs == -1) {
printf("*** Error assigning buffering. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("\n------------------------------\n");
printf("| btree Library Demo Program |\n");
printf("------------------------------\n");
/* main loop */
while (1) {
printf("\n\n");
printf("----------------------\n");
printf("| I - Insert key |\n");
printf("| D - Delete key |\n");
printf("| S - Search for key |\n");
printf("| N - Next key |\n");
printf("| P - Previous key |\n");
printf("| F - First key |\n");
printf("| L - Last key |\n");
printf("| A - list All keys |\n");
printf("| X - eXit |\n");
printf("----------------------\n\n");
printf("Enter selection: ");
c = getchar();
if (getchar() != '\n') {
printf("\a");
continue;
}
c = toupper(c);
if (c == 'X') {
break;
}
switch (c) {
case 'I': /* Insert key */
rs = btlock(btp, BT_WRLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("B-tree currently locked by another process.\n");
break;
}
printf("*** Error write locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("\nEnter key to insert: ");
memset(key, 0, sizeof(key));
if (fgets(key, sizeof(key), stdin) == NULL) {
printf("*** Error reading input.\n");
bexit(EXIT_FAILURE);
}
if (key[strlen(key) - 1] == '\n') {
key[strlen(key) - 1] = '\0';
}
rs = btinsert(btp, key);
if (rs == -1) {
if (errno == BTEDUPKEY) {
printf("Key %s already in tree.\n", key);
} else {
printf("*** Error inserting key into B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
} else {
printf("key %s inserted in tree.\n", key);
}
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'I': */
case 'D': /* */
rs = btlock(btp, BT_WRLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("B-tree currently locked by another process.\n");
break;
}
printf("*** Error write locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("\nEnter key to insert: ");
memset(key, 0, sizeof(key));
if (fgets(key, sizeof(key), stdin) == NULL) {
printf("*** Error reading input.\n");
bexit(EXIT_FAILURE);
}
if (key[strlen(key) - 1] == '\n') {
key[strlen(key) - 1] = '\0';
}
rs = btdelete(btp, key);
if (rs == -1) {
if (errno == BTENKEY) {
printf("Key not found.\n");
} else {
printf("*** Error deleting key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
} else {
printf("Key deleted.\n");
}
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'D': */
case 'S': /* Search for key */
rs = btlock(btp, BT_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("B-tree currently write locked by another process.\n");
break;
}
printf("*** Error read locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("\nEnter key to search for: ");
memset(key, 0, sizeof(key));
if (fgets(key, sizeof(key), stdin) == NULL) {
printf("*** Error reading input.\n");
bexit(EXIT_FAILURE);
}
if (key[strlen(key) - 1] == '\n') {
key[strlen(key) - 1] = '\0';
}
rs = btsearch(btp, (void *)key);
if (rs == -1) {
printf("*** Error searching for key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (rs == 1) {
printf("Key found.\n");
} else {
printf("Key not found.\n");
}
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'S': */
case 'N': /* Next key */
rs = btlock(btp, BT_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("B-tree currently write locked by another process.\n");
break;
}
printf("*** Error read locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (btkeycnt(btp) == 0) {
printf("B-tree is empty.\n");
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break;
}
rs = btsearch(btp, key);
if (rs == -1) {
printf("*** Error searching for key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (rs == 1) {
rs = btnext(btp);
if (rs == -1) {
printf("*** Error finding next key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
if (btcursor(btp) == NULL) {
memset((void *)key, 0, sizeof(key));
printf("That was the last key.\n");
} else {
rs = btgetk(btp, (void *)key);
if (rs == -1) {
printf("*** Error reading key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("The next key is %s.\n", (char *)key);
}
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'N': */
case 'P': /* Previous key */
rs = btlock(btp, BT_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("B-tree currently write locked by another process.\n");
break;
}
printf("*** Error read locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (btkeycnt(btp) == 0) {
printf("B-tree is empty.\n");
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break;
}
rs = btsearch(btp, key);
if (rs == -1) {
printf("*** Error searching for key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (rs == 1) {
rs = btprev(btp);
if (rs == -1) {
printf("*** Error finding previous key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
if (btcursor(btp) == NULL) {
memset((void *)key, 0, sizeof(key));
printf("That was the first key.\n");
} else {
rs = btgetk(btp, (void *)key);
if (rs == -1) {
printf("*** Error reading key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("The previous key is %s.\n", (char *)key);
}
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'P': */
case 'F': /* First key */
rs = btlock(btp, BT_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("B-tree currently write locked by another process.\n");
break;
}
printf("*** Error read locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (btkeycnt(btp) == 0) {
printf("B-tree is empty.\n");
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break;
}
rs = btfirst(btp);
if (rs == -1) {
printf("*** Error finding first key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = btgetk(btp, (void *)key);
if (rs == -1) {
printf("*** Error reading key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("The first key is %s.\n", (char *)key);
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'F': */
case 'L': /* Last key */
rs = btlock(btp, BT_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("B-tree currently write locked by another process.\n");
break;
}
printf("*** Error read locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (btkeycnt(btp) == 0) {
printf("B-tree is empty.\n");
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break;
}
rs = btlast(btp);
if (rs == -1) {
printf("*** Error finding last key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = btgetk(btp, (void *)key);
if (rs == -1) {
printf("*** Error reading key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("The last key is %s.\n", (char *)key);
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'L': */
case 'A':
rs = btlock(btp, BT_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("B-tree currently write locked by another process.\n");
break;
}
printf("*** Error read locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("There are %lu keys in the btree.\n", (unsigned long)btkeycnt(btp));
rs = btsetcur(btp, NULL);
if (rs == -1) {
printf("*** Error setting cursor. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
while (1) {
rs = btnext(btp);
if (rs == -1) {
printf("*** Error finding next key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (btcursor(btp) == NULL) {
break;
}
rs = btgetk(btp, (void *)key);
if (rs == -1) {
printf("Error calling btgetk. errno = %d.\n", errno);
bexit(EXIT_FAILURE);
}
printf("%s\n", key);
}
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'A': */
default:
printf("Invalid selection.\a\n");
break; /* default: */
}
}
/* close btree */
rs = btclose(btp);
if (rs == -1) {
printf("*** Error closing B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
bexit(EXIT_SUCCESS);
}
/* comparison function */
int cmp(buf1, buf2, count)
void *buf1;
void *buf2;
size_t count;
{
return strncmp((char *)buf1, (char *)buf2, count);
}