Initializing the C data structures

This code segment is an example of how to initialize the C data structures for tableBASE before calling the tableBASE API (TBLBASE) with these data structures.

/*
 * This file contains examples of functions that can be used to
 * initialize the data structs needed for calling tableBASE.
 * The definitions of these structs are in dkh.h
 */
 
 
#include <stdio.h>
#include <string.h>
#include "dkh.h"

void InitTbParm( TbParmStruct * pTbP )
{
    /* TB-PARM */
    
    /*
     * Initialize it all to zero and set individual members as needed.
     */
    memset( pTbP, 0, sizeof(TbParmStruct) );
    
    pTbP->tbParmID[0] = 'T';
    pTbP->tbParmID[1] = 'B';
    pTbP->tbVersion   = '5';
    pTbP->tbFormat    = '0';
}

void InitTbCommandArea( TbCommandAreaStruct * pTbC, char * szTableName )
{
    int tableNameSize = 0, i;

    /* COMMAND-AREA */
    memset( pTbC->tbCommand, ' ', 2 );
    memset( pTbC->tbTable,   ' ', 8 );
    pTbC->tbFound         = ' ';
    pTbC->tbIndirectOpen  = 0;
    pTbC->tbReserved1     = 0;
    pTbC->tbAbendOverride = ' ';
    pTbC->tbError         = 0;
    pTbC->tbCount         = 0;
    memset( pTbC->tbLock, ' ', 8 );
    pTbC->tbRowOverrideLength = 0;
    pTbC->tbRowActualLength   = 0;
    pTbC->tbFgKeyLength       = 0;
    pTbC->tbFunctionID        = 0;
    memset( pTbC->tbDateArea, 0, 8 );
    memset( pTbC->tbReserved2, 0, 20 ); /* 24 bytes for tableBASE v5.x */
    pTbC->tbReturnedAbsGenNo = 0;       /* added in tableBASE v6 */
    pTbC->tbErrorSubcode = 0;           /* added in tableBASE v6 */

    tableNameSize = strlen(szTableName);
    if( tableNameSize > 8 )
    {
        /*
         * Handle the error here, we truncate it for now.
         */
        strncpy( pTbC->tbTable, szTableName, 8 );
    }
    else
    {
        /* tableName length is less than 8, we append it with spaces */
        strncpy( pTbC->tbTable, szTableName, tableNameSize );
        for( i = tableNameSize; i < 8; i++ )
            pTbC->tbTable[i] = ' ';
    }
}

void InitTableDef( TbTableDefinitionStruct * pTbTD )
{
    /* DEFINITION-BLOCK */
    
    /*
     * Initialize it all to zero and set individual members as needed.
     */
    memset( pTbTD, 0, sizeof(TbTableDefinitionStruct) );
    
    pTbTD->org    = ' ';
    pTbTD->method = 'S';
    pTbTD->index  = 'T';
    pTbTD->smc    = 'R';
    memset( pTbTD->readPassword,  ' ', 8 );
    memset( pTbTD->writePassword, ' ', 8 );
    pTbTD->rowSize         = 120;
    pTbTD->keySize         = 11;
    pTbTD->keyLocation     = 1;
    pTbTD->numberOfRows    = 500;
    pTbTD->generations     = 1;
    pTbTD->expansionFactor = 250;
    pTbTD->lowDensity      = 500;
    pTbTD->highDensity     = 800;
    memset( pTbTD->dateTime, ' ', 12 );
    pTbTD->absoluteGenerationNumber = 10;
    memset( pTbTD->datasetName, ' ', 44 );
}

void InitLibList( TbLibListStruct * pTbL )
{
    memset( pTbL, ' ', 80 );
}

void InitTbLibSpace( TbLibSpaceStruct * pTbLS )
{
    pTbLS->libBlocksAllocated = 0;
    pTbLS->libBlocksRemaining = 0;
}

void InitTbDirSpec( TbDirSpecStruct * pTbDS )
{
    memset( pTbDS, 0, sizeof(TbDirSpecStruct) );
}

void InitTbAccDefStruct( TbAccDefStruct * pTbAD )
{
    /*
     * Initialize it all to zero and set individual members as needed.
     */
    memset( pTbAD, 0, sizeof(TbAccDefStruct) );
    
    pTbAD->numberOfRows = 0;
    pTbAD->rowSize      = 100;
    pTbAD->keySize      = 7;
    pTbAD->keyLocation  = 4;
    pTbAD->maxRows      = 1000;
    pTbAD->subscript    = 0;
    pTbAD->org          = 'S';
    pTbAD->action       = 'S';
    pTbAD->searchMethod = 'C';
}

void InitTbStats( TbTableStatsStruct * pTbTS, int nNumberOfTables )
{
    /*
     * The number of tables (nNumberOfTables) must be equal to
     * the value of the field TbListBlockStruct::reqd
     *
     * Do NOT use this function if you set TbListBlockStruct::size to
     * something different than sizeof(TbTableStatsStruct).
     */
    if ( nNumberOfTables > 0 )
        memset( pTbTS, 0, sizeof(TbTableStatsStruct)*nNumberOfTables );
    else
    {
        /*
         * Handle error code here, if necessary.
         */
    }
}

void InitListBlockStruct( TbListBlockStruct * pTbLB )
{
    /*
     * Initialize it all to zero and set individual members as needed.
     */
    memset( pTbLB, 0, sizeof(TbListBlockStruct) );
    
    pTbLB->from = 1;
    pTbLB->reqd = 96;
    
    /*
     * The amount of information returned (by the command LT)
     * for each table can be modified with the next field.
     * If set to something less than sizeof(TbTableStatsStruct),
     * the fourth parameter to TBLBASE() would be an array of
     * a somewhat truncated TbTableStatsStruct.
     * Use this with CAUTION.
     */
    pTbLB->size = sizeof(TbTableStatsStruct);
}

void InitLibraryAllocStruct( TbLibraryAllocStruct * pTbLA )
{
    pTbLA->libShareStatus = 'S';
    memset( pTbLA->libDataSetName, ' ', 44 );
}

void InitAltDefinitionStruct( TbAltDefinitionStruct * pTbAD )
{
    pTbAD->org         = ' ';
    pTbAD->method      = 'S';
    pTbAD->keyCount    = 1;
    pTbAD->keyLocation = 1;
    pTbAD->keySize     = 1;
}

int fixStringLength( char* input, char* output, int len )
{
    int size = 0, i = 0;
    
    size = strlen(input);
    if( size > len )
    {
        /*
         * Handle the error here, we truncate it for now.
         */
        strncpy( output, input, len );
    }
    else
    {
        /*
         * If tableName length is less than len, we append it
         * with spaces.
         */
        strncpy( output, input, size );
        for( i = size; i < len; i++ )
            output[i] = ' ';
    }
    return 0;
}