4.9.3 Deleting Records



Ddelete(C-3) removes records from an RMSfile. The user program must first establish the current record as described earlier. This is usually done through calls to any of the find functions. One important aspect of ddelete is that the user record is not passed in the call to ddelete. A sample call to ddelete might be:

recno = ddelete (mag);

This deletes the current magazine record at the time of the call. The entire sequence for deleting a record might use the following code:

#include <stdio.h>

#include <cbase/dirio.h>

#include <cbase/dtypes.h>

#include "mag.h"

del_mag (magazine)

char *magazine;

{

/* move magazine code to magazine user record */

strncpy (magbuf.magazine, magazine,

sizeof (magbuf.magazine));

if (dfindk (&magbuf, mag) < 0L) {

printf("No magazine record exists for %s\n",

magazine);

}

else {

if (ddelete (mag) < 0L) {

printf ("%s\ncan't delete magazine record for %s\n",

derrmsg(), magazine);

}

else

printf ("magazine deleted: %s\n", magazine);

}

}

The following example illustrates the use of dinsert, dupdate, and ddelete in a simple subscriber file maintenance program. The function maintenance is the function which does the actual file control; other functions call maintenance to perform certain actions on the RMSfile. The sample main function provided adds a subscriber "conetic", changes the subscriber name to "Conetic Software Systems, Inc.", and then deletes subscriber conetic. Although the function main does not accomplish much, it shows how to call the function maintenance to do the actions.

#include <stdio.h>

 

#include <cbase/dtypes.h>

#include <cbase/dirio.h>

#include "sub.h"

main() /* sample maintenance calling function */

{

maintenance ('o');

maintenance ('a', "conetic", "Conetic Software Systems");

maintenance ('c', "conetic",

"Conetic Software Systems Inc");

maintenance ('d', "conetic");

maintenance ('x');

}

maintenance (action, key, name)

char action;

STRING *key,

*name;

{

switch (action) {

case 'o':

do_open();

break;

case 'a':

do_insert (key, name);

break;

case 'c':

do_update (key, name);

break;

case 'd':

do_delete (key);

break;

case 'x':

closef();

break;

default:

printf ("unknown action code: %c\n", action);

break;

}

}

do_open()

{

if ((sub = dlopen ("sub", "u")) == NULL) {

puts (derrmsg());

printf ("unable to open subscriber file\n");

exit (1);

}

if (drlist (sublist, sub) < 0L) {

puts (derrmsg());

printf ("bad field list for subscriber file\n");

closef();

exit (1);

}

}

do_insert (key, nm)

STRING *key, *nm;

{

char *reason;

reason = NULL;

strncpy (subbuf.subscriber, key,

sizeof (subbuf.subscriber));

if (dfindk (&subbuf, sub) > 0L)

reason = "already in file"

else {

strncpy (subbuf.subscriber, key,

sizeof (subbuf.subscriber));

strncpy (subbuf.name, nm,

sizeof (subbuf.name));

if (dinsert (&subbuf, sub) < 0L)

reason = derrmsg();

}

if (reason) {

printf ("unable to add subscriber: %s,", key);

printf (" reason: %s\n", reason);

}

}

do_update (key, nm)

STRING *key, *nm;

{

char *reason;

reason = NULL;

strncpy (subbuf.subscriber, key,

sizeof (subbuf.subscriber));

if (dfindk (&subbuf, sub) < 0L)

reason = "not in file"

else {

strncpy (subbuf.name, nm,

sizeof (subbuf.name));

if (dupdate (&subbuf, sub) < 0L)

reason = derrmsg();

}

if (reason) {

printf ("unable to change subscriber: %s,", key);

printf ("reason: %s\n", reason);

}

}

do_delete (key)

STRING *key;

{

char *reason;

reason = NULL;

strncpy (subbuf.subscriber, key,

sizeof (subbuf.subscriber));

if (dfindk (&subbuf, sub) < 0L)

reason = "not in file"

else

if (ddelete (sub) < 0L)

reason = derrmsg();

if (reason) {

printf ("unable to delete subscriber: %s,", key);

printf ("reason: %s\n", reason);

}

}

closef()

{

if (sub != NULL) {

dclose (sub);

sub = NULL;

}

}