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()
createFile(btrieve_client_t btrieveClient)
{
btrieve_file_attributes_t btrieveFileAttributes = NULL;
record_t record;
// If BtrieveFileAttributesAllocate() failed.
if ((status = BtrieveFileAttributesAllocate(&btrieveFileAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileAttributesAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveFileAttributesSetFixedRecordLength() failed.
if ((status = BtrieveFileAttributesSetFixedRecordLength(btrieveFileAttributes, sizeof(record))) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: 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("Error: BtrieveClientFileCreate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
leave:
if (btrieveFileAttributes != NULL)
{
BtrieveFileAttributesFree(btrieveFileAttributes);
btrieveFileAttributes = NULL;
}
return status;
}
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("Error: BtrieveClientFileOpen():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
return status;
leave:
if (btrieveFile != NULL)
{
BtrieveClientFileClose(btrieveClient, *btrieveFile);
*btrieveFile = NULL;
}
return status;
}
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("Error: BtrieveFileRecordCreate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
}
leave:
return status;
}
closeFile(btrieve_client_t btrieveClient, btrieve_file_t btrieveFile)
{
// If BtrieveClientFileClose() failed.
if ((status = BtrieveClientFileClose(btrieveClient, btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClientFileClose():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
btrieveFile = NULL;
leave:
return status;
}
deleteFile(btrieve_client_t btrieveClient)
{
// If BtrieveClientFileDelete() failed.
if ((status = BtrieveClientFileDelete(btrieveClient, btrieveFileName)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClientFileDelete():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
leave:
return status;
}
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("Error: BtrieveKeySegmentAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveKeySegmentSetField() failed.
{
printf("Error: BtrieveKeySegmentSetField():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveIndexAttributesAllocate() failed.
if ((status = BtrieveIndexAttributesAllocate(&btrieveIndexAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveIndexAttributesAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveIndexAttributesAddKeySegment() failed.
if ((status = BtrieveIndexAttributesAddKeySegment(btrieveIndexAttributes, btrieveKeySegment)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveIndexAttributesAddKeySegment():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveFileIndexCreate() failed.
if ((status = BtrieveFileIndexCreate(btrieveFile, btrieveIndexAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: 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;
}
retrieveRecord(btrieve_file_t btrieveFile, uint8_t *key)
{
record_t record;
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("Error: BtrieveFileRecordRetrieve():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
printf("record: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
leave:
return status;
}
int
main(int argc, char *argv[])
{
static const char *usage = "Usage: %s uint8_value\n";
btrieve_client_t btrieveClient = NULL;
btrieve_file_t btrieveFile = NULL;
uint8_t key;
record_t record;
uint64_t integerValue;
if (argc != 2)
{
printf(usage, argv[0]);
goto leave;
}
integerValue = atoi(argv[1]);
if ((integerValue < MIN_X) || (integerValue > MAX_X))
{
printf(usage, argv[0]);
goto leave;
}
key = (uint8_t)integerValue;
// If BtrieveClientAllocate() failed.
if ((status = BtrieveClientAllocate(&btrieveClient, 0, 0)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: 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 retrieveRecord() fails.
if ((status = retrieveRecord(btrieveFile, &key)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
// 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;
}