
D Eloquence Library
/*
sample.c
This is a sample program showing usage of eloquence database
library.
compile: cc sample.c -o sample -l eloq
usage : sample
This sample will assume the existence of a database SAD
with the following structure:
ITEMS:
...
PRODUCT-NO, I;
PROD-DESC, X30;
...
SETS:
...
N: PRODUCT, M (/0);
E: PRODUCT-NO,
PROD-DESC;
...
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <eloqdb.h>
int dbid; /* data base id */
int status[10]; /* data base status */
char buf[100]; /* data abse buffer */
struct Product {
short no;
char desc[30];
} product;
typedef struct Packlist {
char *item; /* item name */
void *ptr; /* pointer to item value */
char type; /* item type */
int size; /* item size */
int count; /* subitem count */
};
struct Packlist product_packfmt[] =
{
{ "PRODUCT-NO", &product.no },
{ "PROD-DESC", product.desc },
{ "" }
};
signal_handler()
{
printf("Interrupt\n");
wrapup(3);
}
main(argc, argv)
int argc;
char *argv[];
{
char op;
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
printf("Opening database ...\n");
dbid = idb_open("../sad/SADN", "MANAGER", 3, status);
errorhandler("opening database");
setup_pack_list(product_packfmt);
do {
printf("Add, Update, Delete, Quit ?");
fflush(stdout);
scanf("%1s", buf);
op = *buf;
gets(buf);
switch(op) {
case 'A':
add_product();
break;
case 'U':
update_product();
break;
case 'D':
delete_product();
break;
case 'Q':
break;
default:
printf("Illegal - reenter\n");
}
} while(op != 'Q');
wrapup(0);
}
wrapup(cond)
int cond;
{
if(dbid >= 0) {
printf("closing database ...\n");
idb_close(dbid, NULL, 1, status);
errorhandler("closing database");
}
idb_exit();
exit(cond);
}
errorhandler(action)
char *action;
{
char tmp[80];
if(status[0] != S_OK)
{
fprintf(stderr, "Status error #%d while %s\n", status[0], action);
if(idb_error(status, tmp, NULL) == S_OK)
fprintf(stderr, "%s\n", tmp);
wrapup(1);
}
}
/*
handle product
*/
get_product()
{
printf("Product no ? ");
fflush(stdout);
scanf("%hd", &product.no);
idb_get(dbid, "PRODUCT", 7, status, "@", buf, &product.no);
if(status[0] == S_NOREC) {
printf("Product no %d found\n", product.no);
return(0);
}
errorhandler("get product");
unpack_buffer(product_packfmt);
return(1);
}
add_product()
{
printf("Product no ? ");
fflush(stdout);
scanf("%hd", &product.no);
printf("Description ? ");
fflush(stdout);
scanf("%s", product.desc);
pack_buffer(product_packfmt);
idb_put(dbid, "PRODUCT", 1, status, "@", buf);
if(status[0] == S_DUPL) {
printf("Duplicate product\n");
return(0);
}
errorhandler("adding product");
printf("Product added\n");
return(1);
}
update_product()
{
if(!get_product())
return(0);
printf("Product no : %d\n", product.no);
printf("Description : %.30s\n", product.desc);
printf("Description ? ");
fflush(stdout);
scanf("%s", product.desc);
pack_buffer(product_packfmt);
idb_update(dbid, "PRODUCT", 1, status, "@", buf);
errorhandler("updating product");
printf("Product updated\n");
return(1);
}
delete_product()
{
if(!get_product())
return(0);
printf("Product no : %d\n", product.no);
printf("Description : %.30s\n", product.desc);
idb_delete(dbid, "PRODUCT", 1, status);
errorhandler("deleting product");
printf("Product deleted\n");
return(1);
}
/*
buffer pack/unack utilities
*/
setup_pack_list(listp)
struct Packlist *listp;
{
union info info;
while(*listp->item) {
idb_info(dbid, listp->item, 102, status, &info);
errorhandler("info 102");
listp->type = info.item.type;
listp->size = info.item.size;
listp->count = info.item.count;
listp++;
}
}
pack_buffer(listp)
struct Packlist *listp;
{
int i, ofs;
ofs = 0;
while(*listp->item) {
for(i = 0; i < listp->count; i++) {
if(listp->type == 'X')
{
char *to = (char *)&buf[ofs];
char *from = (char *)listp->ptr + i*listp->size;
int len = listp->size;
while(*from && len) {
*to++ = *from++;
len--;
}
memset(to, ' ', len);
}
else
memcpy(&buf[ofs], (char *)listp->ptr + i*listp->size, listp->size);
ofs += listp->size;
}
listp++;
}
}
unpack_buffer(listp)
struct Packlist *listp;
{
int i, ofs;
ofs = 0;
while(*listp->item) {
for(i = 0; i < listp->count; i++) {
memcpy((char *)listp->ptr + i*listp->size, &buf[ofs], listp->size);
ofs += listp->size;
}
listp++;
}
}