btest.c

This is an example of file creation, load, access, and deletion.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include "btrieveC.h"
static char *btrieveFileName = (char *)"squaresAndSquareRoots.btr";
#define MIN_X 0
#define MAX_X 255
#pragma pack(1)
typedef struct {
uint8_t x;
uint16_t xSquared;
double xSquareRoot;
} record_t;
#pragma pack()
typedef uint8_t _key_t;
static int
createFile(btrieve_client_t btrieveClient)
{
btrieve_file_attributes_t btrieveFileAttributes;
record_t record;
// If BtrieveFileAttributesAllocate() failed.
if ((status = BtrieveFileAttributesAllocate(&btrieveFileAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveFileAttributesAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveFileAttributesSetFixedRecordLength() failed.
if ((status = BtrieveFileAttributesSetFixedRecordLength(btrieveFileAttributes, sizeof(record))) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveFileAttributesSetFixedRecordLength():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveClientFileCreate() failed.
if ((status = BtrieveClientFileCreate(btrieveClient, btrieveFileAttributes, NULL, btrieveFileName, BTRIEVE_CREATE_MODE_OVERWRITE, BTRIEVE_LOCATION_MODE_NO_PREFERENCE)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveClientFileCreate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
leave:
if (btrieveFileAttributes != NULL)
{
BtrieveFileAttributesFree(btrieveFileAttributes);
btrieveFileAttributes = NULL;
}
return status;
}
static int
openFile(btrieve_client_t btrieveClient, btrieve_file_t *btrieveFile)
{
// If BtrieveClientFileOpen() failed.
if ((status = BtrieveClientFileOpen(btrieveClient, btrieveFile, btrieveFileName, NULL, BTRIEVE_OPEN_MODE_NORMAL, BTRIEVE_LOCATION_MODE_NO_PREFERENCE)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveClientFileOpen():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
return status;
leave:
if (btrieveFile != NULL)
{
BtrieveClientFileClose(btrieveClient, *btrieveFile);
*btrieveFile = NULL;
}
return status;
}
static int
loadFile(btrieve_file_t btrieveFile)
{
int i;
record_t record;
for (i = MIN_X; i <= MAX_X; i++)
{
record.x = (uint8_t)i;
record.xSquared = (uint16_t)(i * i);
record.xSquareRoot = sqrt(i);
// If BtrieveFileRecordCreate() failed.
if ((status = BtrieveFileRecordCreate(btrieveFile, (char *)&record, sizeof(record))) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveFileRecordCreate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
}
leave:
return status;
}
static int
closeFile(btrieve_client_t btrieveClient, btrieve_file_t btrieveFile)
{
// If BtrieveClientFileClose() failed.
if ((status = BtrieveClientFileClose(btrieveClient, btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveClientFileClose():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
btrieveFile = NULL;
leave:
return status;
}
static int
deleteFile(btrieve_client_t btrieveClient)
{
// If BtrieveClientFileDelete() failed.
if ((status = BtrieveClientFileDelete(btrieveClient, btrieveFileName)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveClientFileDelete():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
leave:
return status;
}
static int
createIndex(btrieve_file_t btrieveFile)
{
btrieve_index_attributes_t btrieveIndexAttributes = NULL;
btrieve_key_segment_t btrieveKeySegment = NULL;
// If BtrieveKeySegmentAllocate() failed.
if ((status = BtrieveKeySegmentAllocate(&btrieveKeySegment)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveKeySegmentAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveKeySegmentSetField() failed.
{
printf("BtrieveKeySegmentSetField():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveIndexAttributesAllocate() failed.
if ((status = BtrieveIndexAttributesAllocate(&btrieveIndexAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveIndexAttributesAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveIndexAttributesAddKeySegment() failed.
if ((status = BtrieveIndexAttributesAddKeySegment(btrieveIndexAttributes, btrieveKeySegment)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveIndexAttributesAddKeySegment():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveFileIndexCreate() failed.
if ((status = BtrieveFileIndexCreate(btrieveFile, btrieveIndexAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveFileIndexCreate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
leave:
if (btrieveIndexAttributes != NULL)
{
BtrieveIndexAttributesFree(btrieveIndexAttributes);
btrieveIndexAttributes = NULL;
}
if (btrieveKeySegment != NULL)
{
BtrieveKeySegmentFree(btrieveKeySegment);
btrieveKeySegment = NULL;
}
return status;
}
int
main(int argc, char *argv[])
{
btrieve_client_t btrieveClient = NULL;
btrieve_file_t btrieveFile = NULL;
_key_t key;
record_t record;
uint64_t integerValue;
if (argc != 2)
{
printf("Usage: %s uint8_value\n", argv[0]);
goto leave;
}
integerValue = atoi(argv[1]);
if ((integerValue < MIN_X) || (integerValue > MAX_X))
{
printf("Usage: %s uint8_value\n", argv[0]);
goto leave;
}
key = (_key_t)integerValue;
// If BtrieveClientAllocate() failed.
if ((status = BtrieveClientAllocate(&btrieveClient, 0x4232, 0)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("BtrieveClientAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If createFile() failed.
if ((status = createFile(btrieveClient)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If openFile() failed.
if ((status = openFile(btrieveClient, &btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If loadFile() failed.
if ((status = loadFile(btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If createIndex() failed.
if ((status = createIndex(btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
if (BtrieveFileRecordRetrieve(btrieveFile, BTRIEVE_COMPARISON_EQUAL, BTRIEVE_INDEX_1, (char *)&key, sizeof(&key), (char *)&record, sizeof(record), BTRIEVE_LOCK_MODE_NONE) != sizeof(record))
{
status = BtrieveFileGetLastStatusCode(btrieveFile);
printf("BtrieveFileRecordRetrieve():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
printf("record: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
// If closeFile() failed.
if ((status = closeFile(btrieveClient, btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
btrieveFile = NULL;
// If deleteFile() failed.
if ((status = deleteFile(btrieveClient)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
leave:
if (btrieveFile != NULL)
{
BtrieveClientFileClose(btrieveClient, btrieveFile);
btrieveFile = NULL;
}
if (btrieveClient != NULL)
{
BtrieveClientFree(btrieveClient);
btrieveClient = NULL;
}
return 0;
return 1;
}