bfilter.cpp

This is an example of filter use.

#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include "btrieveCpp.h"
static char *btrieveFileName = (char *)"squaresAndSquareRoots.btr";
#ifndef __GNUC__
#define snprintf _snprintf
#endif
#define MIN_X 0
#define MAX_X 255
#pragma pack(1)
typedef struct {
uint8_t x;
uint16_t xSquared;
double xSquareRoot;
char text[128];
} record_t;
#pragma pack()
typedef char _key_t;
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;
}
retrieveRecord(BtrieveFile *btrieveFile, _key_t *key)
{
BtrieveBulkRetrieveAttributes btrieveBulkRetrieveAttributes;
BtrieveBulkRetrieveResult btrieveBulkRetrieveResult;
BtrieveFilter btrieveFilter;
int i;
int recordCount;
record_t record;
// If SetComparison() fails.
{
printf("Error: BtrieveFilter::SetComparison():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetComparisonConstant() fails.
if ((status = btrieveFilter.SetComparisonConstant((char *)key, 1)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetComparisonConstant():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetConnector() fails.
{
printf("Error: BtrieveFilter::SetConnector():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetField() fails.
if ((status = btrieveFilter.SetField(25, 1, Btrieve::DATA_TYPE_CHAR)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetField():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If AddFilter() fails.
if ((status = btrieveBulkRetrieveAttributes.AddFilter(&btrieveFilter)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetConnector() fails.
{
printf("Error: BtrieveFilter::SetConnector():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetField() fails.
if ((status = btrieveFilter.SetField(32, 1, Btrieve::DATA_TYPE_CHAR)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetField():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetComparisonField() fails.
if ((status = btrieveFilter.SetComparisonField(27)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetComparisonField():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If AddFilter() fails.
if ((status = btrieveBulkRetrieveAttributes.AddFilter(&btrieveFilter)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetCaseSensitivity() fails.
{
printf("Error: BtrieveFilter::SetACSMode():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetComparison() fails.
{
printf("Error: BtrieveFilter::SetComparison():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetConnector() fails.
{
printf("Error: BtrieveFilter::SetConnector():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetField() fails.
if ((status = btrieveFilter.SetField(20, 1, Btrieve::DATA_TYPE_CHAR)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetField():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetComparisonConstant() fails.
if ((status = btrieveFilter.SetComparisonConstant("E", 1)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetComparisonConstant():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If AddFilter() fails.
if ((status = btrieveBulkRetrieveAttributes.AddFilter(&btrieveFilter)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetACSName() fails.
if ((status = btrieveFilter.SetACSName("u54-msft_enus_0")) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetACSName():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetComparison() fails.
{
printf("Error: BtrieveFilter::SetComparison():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetConnector() fails.
{
printf("Error: BtrieveFilter::SetConnector():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetField() fails.
if ((status = btrieveFilter.SetField(19, 1, Btrieve::DATA_TYPE_CHAR)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetField():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetComparisonConstant() fails.
if ((status = btrieveFilter.SetComparisonConstant("R", 1)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFilter::SetComparisonConstant():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If AddFilter() fails.
if ((status = btrieveBulkRetrieveAttributes.AddFilter(&btrieveFilter)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If AddField() fails.
if ((status = btrieveBulkRetrieveAttributes.AddField(0, sizeof(record))) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveBulkRetrieveAttributes::AddField():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetMaximumRecordCount() fails.
if ((status = btrieveBulkRetrieveAttributes.SetMaximumRecordCount(MAX_X - MIN_X + 1)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveBulkRetrieveAttributes::SetMaximumRecordCount():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetMaximumRejectCount() fails.
if ((status = btrieveBulkRetrieveAttributes.SetMaximumRejectCount(MAX_X - MIN_X + 2)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveBulkRetrieveAttributes::SetMaximumRejectCount():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If SetSkipCurrentRecord() fails.
if ((status = btrieveBulkRetrieveAttributes.SetSkipCurrentRecord(false)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveBulkRetrieveAttributes::SetSkipCurrentRecord():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If RecordRetrieveFirst() fails.
if (btrieveFile->RecordRetrieveFirst(Btrieve::INDEX_NONE, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If BulkRetrieveNext() fails.
if ((status = btrieveFile->BulkRetrieveNext(&btrieveBulkRetrieveAttributes, &btrieveBulkRetrieveResult)) != Btrieve::STATUS_CODE_END_OF_FILE)
{
printf("Error: BtrieveFile::BulkRetrieveNext():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
else
{
}
// If GetRecordCount() fails.
if ((recordCount = btrieveBulkRetrieveResult.GetRecordCount()) == -1)
{
status = btrieveBulkRetrieveResult.GetLastStatusCode();
printf("Error: BtrieveBulkRetrieveResult::GetRecordCount():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
for (i = 1; i <= recordCount; i++)
{
// If GetRecord() fails.
if (btrieveBulkRetrieveResult.GetRecord(i, (char *)&record, sizeof(record)) != sizeof(record))
{
status = btrieveBulkRetrieveResult.GetLastStatusCode();
printf("Error: BtrieveBulkRetrieveResult::GetRecord():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If GetRecordCursorPosition() fails.
if (btrieveBulkRetrieveResult.GetRecordCursorPosition(i) < 0)
{
status = btrieveBulkRetrieveResult.GetLastStatusCode();
printf("Error: BtrieveBulkRetrieveResult::GetRecordCursorPosition():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If GetRecordLength() fails.
if (btrieveBulkRetrieveResult.GetRecordLength(i) != sizeof(record))
{
status = btrieveBulkRetrieveResult.GetLastStatusCode();
printf("Error: BtrieveBulkRetrieveResult::GetRecordLength():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("record: (%u, %u, %g, %s)\n", record.x, record.xSquared, record.xSquareRoot, record.text);
}
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 ((i % 2) == 0)
{
snprintf(record.text, sizeof(record.text), "The square of %03d is %05d and its square root is %f.", record.x, record.xSquared, record.xSquareRoot);
}
else
{
snprintf(record.text, sizeof(record.text), "The SQUARE of %03d is %05d and its square root is %f.", record.x, record.xSquared, record.xSquareRoot);
}
record.text[sizeof(record.text) - 1] = '\0';
// 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.
if ((status = btrieveKeySegment.SetField(11, 128, Btrieve::DATA_TYPE_ZSTRING)) != 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 SetACSName() fails.
if ((status = btrieveIndexAttributes.SetACSName("u54-msft_enus_0")) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveIndexAttributes::SetACSName():%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;
}
int
main(int argc, char *argv[])
{
static char *usage = "Usage: %s <digit>\n";
BtrieveClient btrieveClient(0x4232, 0);
BtrieveFile btrieveFile;
_key_t key;
// If the incorrect number of arguments were given.
if (argc != 2)
{
printf(usage, argv[0]);
goto leave;
}
// If digit is out of range.
if ((isdigit(argv[1][0]) == 0) || (argv[1][1] != '\0'))
{
printf(usage, argv[0]);
goto leave;
}
key = (_key_t)argv[1][0];
// 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 createIndex() fails.
if ((status = createIndex(&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;
}