Manage() provides data management functionality for your application and/or process.
Below is the code for Manage():
/*
* Manage()
*
* This function performs simple record functions of add, delete and gets
*/
#ifdef PROTOTYPE
VOID Manage(VOID)
#else
VOID Manage()
#endif
{
printf("MANAGE\n");
/* delete any existing record */
Delete_Records();
/* populate the table with data */
Add_Records();
/* show contents of table */
Display_Records();
}
/*
* Delete_Records()
*
* This function deletes all the records in the table
*/
#ifdef PROTOTYPE
VOID Delete_Records(VOID)
#else
VOID Delete_Records()
#endif
{
COUNT retval;
NINT empty;
TEXT recbuf[MAX_RECORD_SIZE];
VRLEN reclen;
printf("\tDelete records...\n");
empty = NO;
reclen = sizeof(recbuf);
retval = FirstVRecord(custmast_filno, &recbuf, &reclen);
if (retval != NO_ERROR)
{
if (retval == END_OF_FILE)
empty = YES;
else
Handle_Error("Delete_Records(): FirstVRecord()", 0);
}
while (empty == NO) /* while table is not empty */
{
/* delete record */
if (DeleteVRecord(custmast_filno))
Handle_Error("Delete_Records(): DeleteVRecord()", 0);
/* read next record */
reclen = sizeof(recbuf);
retval = NextVRecord(custmast_filno, &recbuf, &reclen);
if (retval != NO_ERROR)
{
if (retval == END_OF_FILE)
empty = YES;
else
Handle_Error("Delete_Records(): NextVRecord()", 0);
}
}
}
/*
* Add_Records()
*
* This function adds records to a table in the database from an
* array of strings
*/
typedef struct {
pTEXT number, zipcode, state, rating, name, address, city;
} CUSTOMER_DATA;
CUSTOMER_DATA data[] = {
"1000", "92867", "CA", "1", "Bryan Williams", "2999 Regency", "Orange",
"1001", "61434", "CT", "1", "Michael Jordan", "13 Main", "Harford",
"1002", "73677", "GA", "1", "Joshua Brown", "4356 Cambridge", "Atlanta",
"1003", "10034", "MO", "1", "Keyon Dooling", "19771 Park Avenue", "Columbia"
};
#ifdef PROTOTYPE
VOID Add_Records(VOID)
#else
VOID Add_Records()
#endif
{
COUNT i;
COUNT nRecords = sizeof(data) / sizeof(CUSTOMER_DATA);
TEXT recbuf[MAX_RECORD_SIZE];
VRLEN offset;
printf("\tAdd records...\n");
/* add data to table */
for(i = 0; i < nRecords; i++)
{
ctsfill(recbuf, 0, sizeof(recbuf));
/* populate record buffer with data */
offset = 0;
cpybuf(recbuf+offset, data[i].number, strlen(data[i].number));
offset += NUMBLEN;
cpybuf(recbuf+offset, data[i].zipcode, strlen(data[i].zipcode));
offset += ZIPCLEN;
cpybuf(recbuf+offset, data[i].state, strlen(data[i].state));
offset += STATLEN;
cpybuf(recbuf+offset, data[i].rating, strlen(data[i].rating));
offset += RATGLEN;
cpybuf(recbuf+offset, data[i].name, strlen(data[i].name));
offset += strlen(data[i].name) + 1;
cpybuf(recbuf+offset, data[i].address, strlen(data[i].address));
offset += strlen(data[i].address) + 1;
cpybuf(recbuf+offset, data[i].city, strlen(data[i].city));
offset += strlen(data[i].city) + 1;
/* add record */
if (AddVRecord(custmast_filno, recbuf, offset))
Handle_Error("Add_Records(): AddVRecord()", 0);
}
}
/*
* Display_Records()
*
* This function displays the contents of a table. FirstVRecord() and
* NextVRecord() fetch the record. Then each field is parsed and displayed
*/
#ifdef PROTOTYPE
VOID Display_Records(VOID)
#else
VOID Display_Records()
#endif
{
COUNT retval;
TEXT recbuf[MAX_RECORD_SIZE];
VRLEN reclen;
TEXT custnumb[NUMBLEN+1];
TEXT custname[NAMELEN+1];
pTEXT fldptr;
printf("\tDisplay records...");
/* read first record */
reclen = sizeof(recbuf);
retval = FirstVRecord(custmast_filno, &recbuf, &reclen);
if (retval != NO_ERROR)
Handle_Error("Display_Records(): FirstVRecord()", 0);
while (retval != END_OF_FILE) /* while records are found */
{
ctsfill(custnumb, 0, sizeof(custnumb));
ctsfill(custname, 0, sizeof(custname));
/* get customer number from record buffer */
fldptr = recbuf + 0; /* set field pointer to field position */
strncpy(custnumb, fldptr, NUMBLEN); /* copy field from record buffer to display string */
/* get customer name from record buffer */
fldptr = recbuf + FIXED_RECORD_SIZE; /* set field pointer to field position */
strcpy(custname, fldptr); /* copy field from record buffer to display string */
printf("\n\t\t%-8s%10s\n",custnumb, custname);
/* read next record */
reclen = sizeof(recbuf);
retval = NextVRecord(custmast_filno, &recbuf, &reclen);
if (retval == END_OF_FILE)
break; /* reached end of file */
if (retval != NO_ERROR)
Handle_Error("Display_Records(): NextVRecord()", 0);
}
}