Manage
The manage step provides data management functionality for your application and/or process. Below is the code for Manage(): /******************************************************** * * * database: This is the main action function. * * * ********************************************************/ #ifdef PROTOTYPE VOID database(void) #else VOID database() #endif {
TEXT choice[2]; #ifdef PROTOTYPE VOID datadd(void), datdel(void); #else VOID datadd(), datdel(); #endif choice[0] = '\0'; while (choice[0] != 'Q') {
printf("\n\nA)dd D)elete Q)uit:");
fflush(stdout); ctsfill(inpbuf,0,INPBUFSIZ); ctrt_gets(inpbuf); choice[0] = (TEXT)toupper((NINT)inpbuf[0]); switch (choice[0]) {
case 'A': datadd(); /* add new entry to database */ break; case 'D': datdel(); /* delete existing entries */ break; default: break; } /* end switch */ } /* end while */ } /******************************************************** * * * dataadd: Add a new record * * * ********************************************************/ #ifdef PROTOTYPE VOID datadd(void) #else VOID datadd() #endif {
printf("\nADD NEW DATA\n\n");
/* Initialize the new record so that all fields are blank */ invent.delete_flag[0] = '\0'; invent.delete_flag[1] = '\0'; invent.item[0] = '\0'; invent.descrip[0] = '\0'; invent.location[0] = '\0'; invent.quantity = 0.0; invent.cost = 0.0; getfld(); /* acquire new item info */ transkey(keyfld,invent.item,sizeof(invent.item)); /* prepare key for use */ /* Look to see if this key is already * * in the file. */ if (GetKey(INVENTIDX,keyfld)) {
printf("\nKey already in file, cannot add");
return; } if (uerr_cod) /* Check for error in EQLKEY call*/ {
func_error(INVENTIDX); return; } /* Get data record from data file */ if ((datarec = NewData(INVENTDAT)) == 0) /* assignment intended */ {
func_error(INVENTDAT); return; } /* Write data record to data file */ if (WriteData(INVENTDAT,datarec,&invent)) {
func_error(INVENTDAT); ReleaseData(INVENTDAT,datarec); /* if we cannot write to * * the record, we will * * return the record to * * the pool. */ LockCtData(INVENTDAT,ctFREE,datarec); /* NewData puts a lock on * * the record automatically* * that we must free */ return; } /* Add key to the index, now that we know* * that we have successfully added the * * record to the data file. */ if (AddKey(INVENTIDX,keyfld,datarec,REGADD)) {
func_error(INVENTIDX); ReleaseData(INVENTDAT,datarec); /* If cannot add key, * * then return the record.*/ LockCtData(INVENTDAT,ctFREE,datarec); /* NewData puts a lock on * * the record automatically* * that we must free */ return; } printf("\nSuccessful Addition");
LockCtData(INVENTDAT,ctFREE,datarec); /* release the lock that NewData placed*/ return; } /********************************************************** * * * transkey: The key field must be properly formatted * * by us, as c-tree does not format it * * automatically with low level functions. * * We are going to do two things to the * * key. First, we will translate all of the * * alpha characters to upper case. Second, * * we will pad the key to its full length. * * This second part is very critical! c-tree * * looks at the entire buffer when comparing * * keys, and if you have garbage after a * * null terminated string in a key buffer, * * that becomes a part of the key! * * * ***********************************************************/ #ifdef PROTOTYPE VOID transkey(pTEXT dest,pTEXT source,NINT len) #else VOID transkey(dest,source,len) pTEXT dest,source; NINT len; #endif {
TEXT ch; len--; /* loop until field filled or null found, * translating any lower case to upper case */ while(*source && len-- > 0) {
ch = *source++; *dest++ = (TEXT)toupper((NINT)ch); } /* fill the rest of the field with nulls, if needed */ while(len-- > 0) *dest++ = '\0'; *dest = '\0'; } /******************************************************** * * * getfld: enter input information * * * ********************************************************/ #ifdef PROTOTYPE VOID getfld(void) #else VOID getfld() #endif {
printf("\nEnter Item ID: ");
fflush(stdout); ctsfill(inpbuf,0,INPBUFSIZ); ctrt_gets(inpbuf); if (inpbuf[0] != '\0') cpybuf(invent.item,inpbuf,sizeof(invent.item)); printf("\nEnter Description: ");
fflush(stdout); ctsfill(inpbuf,0,INPBUFSIZ); ctrt_gets(inpbuf); if (inpbuf[0] != '\0') cpybuf(invent.descrip,inpbuf,sizeof(invent.descrip)); printf("\nEnter Location: ");
fflush(stdout); ctsfill(inpbuf,0,INPBUFSIZ); ctrt_gets(inpbuf); if (inpbuf[0] != '\0') cpybuf(invent.location,inpbuf,sizeof(invent.location)); printf("\nEnter Quantity: ");
fflush(stdout); ctsfill(inpbuf,0,INPBUFSIZ); ctrt_gets(inpbuf); if (inpbuf[0] != '\0') sscanf(inpbuf,"%lf",&invent.quantity); printf("\nEnter Cost: ");
fflush(stdout); ctsfill(inpbuf,0,INPBUFSIZ); ctrt_gets(inpbuf); if (inpbuf[0] != '\0') sscanf(inpbuf,"%lf",&invent.cost); } /******************************************************** * * * datdel: find and delete record * * * ********************************************************/ #ifdef PROTOTYPE VOID datdel(void) #else VOID datdel() #endif {
printf("\nDELETE DATA\n");
printf("\nEnter Item to Delete:");
fflush(stdout); ctsfill(inpbuf,0,INPBUFSIZ); ctrt_gets(inpbuf); transkey(keyfld,inpbuf,sizeof(invent.item)); /* Prepare key for use. It is important to form * this key in the same way as we do keys to be added, * since we will be looking for an exact match */ /* find the key to delete in index */ if (!(datarec = GetKey(INVENTIDX,keyfld))) {
if (uerr_cod) func_error(INVENTIDX); else printf("\nKey not found");
return; } /* read the record to display */ if (ReadData(INVENTDAT,datarec,&invent)) func_error(INVENTDAT); else {
printf("\n\nItem %s",invent.item);
printf("\nDescription %s",invent.descrip);
printf("\nLocation %s",invent.location);
printf("\nQuantity %f",invent.quantity);
printf("\nCost %f",invent.cost);
} /* Delete the key from index */ if (DeleteKey(INVENTIDX,keyfld,datarec)) {
printf("\n\nKey delete failed (code %d).",uerr_cod);
return; } /* Return the data record to pool */ if (ReleaseData(INVENTDAT,datarec)) printf("\n\nData record delete failed (code %d).",uerr_cod);
else printf("\n\nDelete Successful");
} |
|||