bkeyonly.cpp

This is an example of a key only file.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include "btrieveCpp.h"
static char *btrieveFileName = (char *)"primes.btr";
#define MIN_X 1
#define MAX_X 255
typedef uint32_t _key_t;
createFile(BtrieveClient *btrieveClient)
{
BtrieveFileAttributes btrieveFileAttributes;
BtrieveIndexAttributes btrieveIndexAttributes;
BtrieveKeySegment btrieveKeySegment;
// If SetField() fails.
if ((status = btrieveKeySegment.SetField(0, sizeof(_key_t), Btrieve::DATA_TYPE_UNSIGNED_BINARY)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveKeySegment::SetField():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If AddKeySegment() fails.
if ((status = btrieveIndexAttributes.AddKeySegment(&btrieveKeySegment)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetKeyOnly() fails.
if ((status = btrieveFileAttributes.SetKeyOnly(true)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileAttributes::SetKeyOnly():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetFixedRecordLength() fails.
if ((status = btrieveFileAttributes.SetFixedRecordLength(sizeof(_key_t))) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If FileCreate() fails.
if ((status = btrieveClient->FileCreate(&btrieveFileAttributes, &btrieveIndexAttributes, 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)
{
_key_t key;
int i;
int j;
_key_t record;
for (i = MIN_X; i <= MAX_X; i++)
{
record = (_key_t)i;
// If RecordCreate() fails.
if ((status = btrieveFile->RecordCreate((char *)&record, sizeof(record))) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFile::RecordCreate():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
}
for (j = 2; j <= (MAX_X / 2); j++)
{
for (i = j * 2; i <= MAX_X; i += j)
{
key = (_key_t)i;
// If RecordRetrieve() fails.
if (btrieveFile->RecordRetrieve(Btrieve::COMPARISON_EQUAL, Btrieve::INDEX_1, (char *)&key, sizeof(key), (char *)&record, sizeof(record)) != sizeof(record))
{
continue;
}
// If RecordDelete() fails.
if ((status = btrieveFile->RecordDelete()) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFile::RecordDelete():%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, _key_t *key)
{
// If KeyRetrieve() fails.
if ((status = btrieveFile->KeyRetrieve(Btrieve::COMPARISON_EQUAL, Btrieve::INDEX_1, (char *)key, sizeof(*key))) == Btrieve::STATUS_CODE_NO_ERROR)
{
printf("%u is prime.\n", *key);
goto leave;
}
{
printf("%u is not prime.\n", *key);
goto leave;
}
printf("Error: BtrieveClient::KeyRetrieve():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
leave:
return status;
}
int
main(int argc, char *argv[])
{
BtrieveClient btrieveClient(0x4232, 0);
BtrieveFile btrieveFile;
_key_t key;
uint64_t integerValue;
// If the incorrect number of arguments were given.
if (argc != 2)
{
printf("Usage: %s <uint8_value greater than zero>\n", argv[0]);
goto leave;
}
integerValue = atoi(argv[1]);
// If integerValue is out of range.
if ((integerValue < MIN_X) || (integerValue > MAX_X))
{
printf("Usage: %s <uint8_value greater than zero>\n", argv[0]);
goto leave;
}
key = (_key_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;
}