bpercentage.cpp

This is an example of record retrieve both by percentage and fraction 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
#define NUMBER_OF_RECORDS (MAX_X - MIN_X + 1)
#pragma pack(1)
typedef struct {
uint8_t x;
uint16_t xSquared;
double xSquareRoot;
} record_t;
#pragma pack()
createFile(BtrieveClient *btrieveClient)
{
BtrieveFileAttributes btrieveFileAttributes;
// If SetFixedRecordLength() fails.
if ((status = btrieveFileAttributes.SetFixedRecordLength(sizeof(record_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, 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)
{
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((double)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;
}
}
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;
}
createIndex(BtrieveFile *btrieveFile)
{
BtrieveIndexAttributes btrieveIndexAttributes;
BtrieveKeySegment btrieveKeySegment;
// If SetField() fails.
{
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 IndexCreate() fails.
if ((status = btrieveFile->IndexCreate(&btrieveIndexAttributes)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFile::IndexCreate():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
retrieveRecord(BtrieveFile *btrieveFile)
{
long long cursorPosition;
int numerator;
int percentage;
record_t record;
uint8_t key;
// If RecordRetrieveFirst() fails.
if (btrieveFile->RecordRetrieveFirst(Btrieve::INDEX_1, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("First: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
// If RecordRetrieveByPercentage() fails.
if (btrieveFile->RecordRetrieveByPercentage(Btrieve::INDEX_1, 0, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveByPercentage():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("ByPercentage: 0%% record: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
// If RecordRetrieveByFraction() fails.
if (btrieveFile->RecordRetrieveByFraction(Btrieve::INDEX_1, 0, NUMBER_OF_RECORDS, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveByFraction():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("ByFraction: %d/%d record: (%u, %u, %f)\n", 0, NUMBER_OF_RECORDS, record.x, record.xSquared, record.xSquareRoot);
// If RecordRetrieveByPercentage() fails.
if (btrieveFile->RecordRetrieveByPercentage(Btrieve::INDEX_1, 5000, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveByPercentage():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("ByPercentage: 50%% record: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
// If RecordRetrieveByFraction() fails.
if (btrieveFile->RecordRetrieveByFraction(Btrieve::INDEX_1, NUMBER_OF_RECORDS / 2, NUMBER_OF_RECORDS, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveByFraction():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("ByFraction: %d/%d record: (%u, %u, %f)\n", NUMBER_OF_RECORDS / 2, NUMBER_OF_RECORDS, record.x, record.xSquared, record.xSquareRoot);
// If GetCursorPosition() fails.
if ((cursorPosition = btrieveFile->GetCursorPosition()) == -1)
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::GetCursorPosition():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If RecordRetrieveLast() fails.
if (btrieveFile->RecordRetrieveLast(Btrieve::INDEX_1, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveLast():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("Last: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
// If RecordRetrieveByPercentage() fails.
if (btrieveFile->RecordRetrieveByPercentage(Btrieve::INDEX_1, 10000, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveByPercentage():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("ByPercentage: 100.00%% record: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
// If RecordRetrieveByFraction() fails.
if (btrieveFile->RecordRetrieveByFraction(Btrieve::INDEX_1, NUMBER_OF_RECORDS, NUMBER_OF_RECORDS, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveByFraction():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("ByFraction: %d/%d record: (%u, %u, %f)\n", NUMBER_OF_RECORDS, NUMBER_OF_RECORDS, record.x, record.xSquared, record.xSquareRoot);
// If RecordRetrieveByCursorPosition() fails.
if (btrieveFile->RecordRetrieveByCursorPosition(Btrieve::INDEX_1, cursorPosition, (char *)&record, sizeof(record)) != sizeof(record))
{
printf("Error: BtrieveFileAttributes::RecordRetrieveByCursorPosition():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("ByCursorPosition: record: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
key = (MAX_X - MIN_X) / 2;
// If GetPercentage() fails.
if ((percentage = btrieveFile->GetPercentage(Btrieve::INDEX_1, (char *)&key, sizeof(key))) == -1)
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::GetPercentage():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("GetPercentage using Index: %d%% key: %d\n", percentage / 100, key);
// If GetPercentage() fails.
if ((percentage = btrieveFile->GetPercentage(cursorPosition)) == -1)
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::GetPercentage():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("GetPercentage using Cursor Position: %d%%\n", percentage / 100);
// If GetNumerator() fails.
if ((numerator = btrieveFile->GetNumerator(Btrieve::INDEX_1, (char *)&key, sizeof(key), NUMBER_OF_RECORDS)) == -1)
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::GetNumerator():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("GetNumerator using Index: %d/%d key: %d\n", numerator, NUMBER_OF_RECORDS, key);
// If GetNumerator() fails.
if ((numerator = btrieveFile->GetNumerator(cursorPosition, NUMBER_OF_RECORDS)) == -1)
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::GetNumerator():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("GetNumerator using Cursor Position: %d/%d\n", numerator, NUMBER_OF_RECORDS);
leave:
return status;
}
int
main(int argc, char *argv[])
{
static const char *usage = "Usage: %s\n";
BtrieveClient btrieveClient;
BtrieveFile btrieveFile;
// If the incorrect number of arguments were given.
if (argc != 1)
{
printf(usage, argv[0]);
goto leave;
}
// 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 createIndex() fails.
if ((status = createIndex(&btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If retrieveRecord() fails.
if ((status = retrieveRecord(&btrieveFile)) != 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;
}