<faircom>\drivers\c.lowlevel\tutorials\lowlevel.c
This tutorial will take you through the basic use of the FairCom Low-Level C API.
It is a sample program provided to show you how to use FairCom Low-Level functions. The application is a very simple one; creating and updating a simple database.
The key is a single segment, no duplicates are allowed. The data input functions are extremely simple, and do little or no error checking.
If any value is passed on the command line, this program assumes that you want to create the database. Otherwise it assumes that the database has already been created.
Note: This tutorial uses a function, "toupper", which is not found in all C compilers. It translates any lower case letter into an upper case letter.
Tutorial #1: Introductory - Simple Single Table
The tutorial begins with some basics, such as includes and #defines. You can refer to the source code to see that in detail.
Note our simple main program:
#ifdef PROTOTYPE
NINT main (NINT argc,pTEXT argv[])
#else
NINT main (argc,argv)
NINT argc;
pTEXT argv[];
#endif
{
#ifdef PROTOTYPE
VOID db_create(void), db_init(void), database(void), db_close(void);
#else
VOID db_create(), db_init(), database(), db_close();
#endif
#ifdef ctPortMAC
argc = ccommand(&argv);
#endif
#ifdef ctThrds
{
NINT err;
if ((err = ctThrdInit(2,(LONG) 0, (pctINIT)0))) {
ctrt_printf("\nctThrdInit failed. Error={%d}\n", err);
fflush(stdout);
ctrt_exit(1);
}
}
#endif
Some simple error handling is provided by these functions:
/*********************************************************
* *
* terminate: if an error occurs in a place *
* where we should terminate the program, *
* call this function. *
* *
* You may wish to have a more *
* sophisticated error routine that will *
* look at the error code and give the user *
* more information. *
* *
**********************************************************/
#ifdef PROTOTYPE
VOID terminate(pTEXT termsg,COUNT fil)
#else
VOID terminate(termsg,fil)
pTEXT termsg;
COUNT fil;
#endif
{
printf("\n\n%s",termsg);
func_error(fil);
/* Call the routine that will give you *
* a meaningful message */
STPUSR();
/* This is needed to shut the user down *
* if you are using a file server. If *
* you aren't, it is a good idea to *
* include since it will free up c-tree *
* PLUS(tm)'s memory allocations. */
fflush(stdout);
ctrt_exit(1);
}
/********************************************************
* *
* func_error: A simple error message handler. You can *
* add other error conditions as you need. *
* *
********************************************************/
#ifdef PROTOTYPE
VOID func_error(COUNT fil)
#else
VOID func_error(fil)
COUNT fil;
#endif
{
switch (uerr_cod)
{
case NO_ERROR:
break; /* no error occured, so return */
case DLOK_ERR:
printf("\nCouldn't get lock on record");
break;
case FNOP_ERR:
printf("\nCould not open file %d",fil);
printf("\nIf the files do not exist, you need to create them.");
printf("\nTo do so, type in \"lowlevel create\" on the command line.");
break;
case KCRAT_ERR:
case DCRAT_ERR:
printf("\nCould not create file [%d]",fil);
break;
case KOPN_ERR:
case DOPN_ERR:
printf("\nTried to create existing file [%d]",fil);
break;
case KDUP_ERR:
printf("\nKey already in index");
break;
case FUNK_ERR:
case FCRP_ERR:
printf("\nHeader record of file %d is damaged",fil);
break;
case SEEK_ERR:
case WRITE_ERR:
case READ_ERR:
printf("\nFailure while trying to read or write record");
break;
case DADV_ERR:
printf("\nServer did not find proper lock");
break;
case UDLK_ERR:
printf("\nCannot unlock data record");
break;
default:
printf("\nError %d on file %d",uerr_cod,fil);
break;
} /* end switch */
}