btestchunk.cpp

This is an example of record update and retrieve chunk based on btest.cpp .

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include "btrieveCpp.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;
} chunk_t;
#pragma pack()
createFile(BtrieveClient *btrieveClient)
{
BtrieveFileAttributes btrieveFileAttributes;
// If SetFixedRecordLength() fails.
if ((status = btrieveFileAttributes.SetFixedRecordLength(sizeof(chunk_t))) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetVariableLengthRecordsMode() fails.
{
printf("Error: BtrieveFileAttributes::SetVariableLengthRecordsMode():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If FileCreate() fails.
if ((status = btrieveClient->FileCreate(&btrieveFileAttributes, btrieveFileName, Btrieve::CREATE_MODE_OVERWRITE)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::FileCreate():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
openFile(BtrieveClient *btrieveClient, BtrieveFile *btrieveFile)
{
// If FileOpen() fails.
if ((status = btrieveClient->FileOpen(btrieveFile, btrieveFileName, NULL, Btrieve::OPEN_MODE_NORMAL)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::FileOpen():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
loadFile(BtrieveFile *btrieveFile)
{
chunk_t chunk;
int i;
chunk.x = 0;
chunk.xSquared = 0;
chunk.xSquareRoot = 0;
// If Create() fails.
if ((status = btrieveFile->RecordCreate((char *)&chunk, sizeof(chunk))) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFile::RecordCreate():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
for (i = MIN_X; i <= MAX_X; i++)
{
chunk.x = (uint8_t)i;
chunk.xSquared = (uint16_t)(i * i);
chunk.xSquareRoot = sqrt((double)i);
// If this is the first chunk.
if (i == MIN_X)
{
// If RecordUpdateChunk() fails.
if ((status = btrieveFile->RecordUpdateChunk(0, (char *)&chunk, sizeof(chunk))) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFile::RecordUpdateChunk():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
}
else
{
// If RecordUpdateChunk() fails.
if ((status = btrieveFile->RecordUpdateChunk((char *)&chunk, sizeof(chunk))) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFile::RecordUpdateChunk():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
}
}
leave:
return status;
}
closeFile(BtrieveClient *btrieveClient, BtrieveFile *btrieveFile)
{
// If FileClose() fails.
if ((status = btrieveClient->FileClose(btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::FileClose():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
deleteFile(BtrieveClient *btrieveClient)
{
// If FileDelete() fails.
if ((status = btrieveClient->FileDelete(btrieveFileName)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::FileDelete():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
retrieveRecord(BtrieveFile *btrieveFile, uint8_t *key)
{
chunk_t chunk;
int i;
// If RecordRetrieveFirst() fails.
if (btrieveFile->RecordRetrieveFirst(Btrieve::INDEX_NONE, (char *)&chunk, sizeof(chunk)) != sizeof(chunk))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
for (i = MIN_X; i <= MAX_X; i++)
{
chunk.x = 0;
chunk.xSquared = 0;
chunk.xSquareRoot = 0;
// If this is the first chunk.
if (i == MIN_X)
{
// If RecordRetrieveChunk() fails.
if (btrieveFile->RecordRetrieveChunk(0, sizeof(chunk), (char *)&chunk, sizeof(chunk)) != sizeof(chunk))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveChunk():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
}
else
{
// If RecordRetrieveChunk() fails.
if (btrieveFile->RecordRetrieveChunk(sizeof(chunk), (char *)&chunk, sizeof(chunk)) != sizeof(chunk))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveChunk():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
}
// If key has been found.
if (chunk.x == *key)
{
break;
}
}
printf("chunk: (%u, %u, %f)\n", chunk.x, chunk.xSquared, chunk.xSquareRoot);
leave:
return status;
}
int
main(int argc, char *argv[])
{
static const char *usage = "Usage: %s uint8_value\n";
BtrieveClient btrieveClient;
BtrieveFile btrieveFile;
uint8_t key;
uint64_t integerValue;
// If the incorrect number of arguments were given.
if (argc != 2)
{
printf(usage, argv[0]);
goto leave;
}
integerValue = atoi(argv[1]);
// If integerValue is out of range.
if ((integerValue < MIN_X) || (integerValue > MAX_X))
{
printf(usage, argv[0]);
goto leave;
}
key = (uint8_t)integerValue;
// If createFile() fails.
if ((status = createFile(&btrieveClient)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If openFile() fails.
if ((status = openFile(&btrieveClient, &btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If loadFile() fails.
if ((status = loadFile(&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() fails.
if ((status = closeFile(&btrieveClient, &btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If deleteFile() fails.
if ((status = deleteFile(&btrieveClient)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
leave:
// If there wasn't a failure.
return 0;
return 1;
}