Btrieve 2
Loading...
Searching...
No Matches
btestvlr.cpp

This is an example of variable length records based on btest.cpp .

#ifndef __GNUC__
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "btrieveCpp.h"
#ifndef __GNUC__
#define snprintf _snprintf
#endif
static char *btrieveFileName = (char *)"squaresAndSquareRoots.btr";
#define MIN_X 0
#define MAX_X 255
#define RECORD_FIXED_SIZE 11
#define RECORD_MAXIMUM_VARIABLE_SIZE 1024
#define RECORD_MAXIMUM_TOTAL_SIZE (RECORD_FIXED_SIZE + RECORD_MAXIMUM_VARIABLE_SIZE)
#pragma pack(1)
typedef struct {
struct
{
uint8_t x;
uint16_t xSquared;
double xSquareRoot;
} fixed;
char text[RECORD_MAXIMUM_VARIABLE_SIZE];
} record_t;
#pragma pack()
createFile(BtrieveClient *btrieveClient)
{
BtrieveFileAttributes btrieveFileAttributes;
// If SetFixedRecordLength() fails.
if ((status = btrieveFileAttributes.SetFixedRecordLength(RECORD_FIXED_SIZE)) != 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)
{
int i;
record_t record;
for (i = MIN_X; i <= MAX_X; i++)
{
record.fixed.x = (uint8_t)i;
record.fixed.xSquared = (uint16_t)(i * i);
record.fixed.xSquareRoot = sqrt((double)i);
snprintf(record.text, RECORD_MAXIMUM_VARIABLE_SIZE, "The square of %d is %d and its square root is %f.", record.fixed.x, record.fixed.xSquared, record.fixed.xSquareRoot);
record.text[RECORD_MAXIMUM_VARIABLE_SIZE - 1] = '\0';
// If RecordCreate() fails.
if ((status = btrieveFile->RecordCreate((char *)&record, RECORD_FIXED_SIZE + (int)strlen(record.text) + 1)) != 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, uint8_t *key)
{
record_t record;
// If RecordRetrieve() fails.
if (btrieveFile->RecordRetrieve(Btrieve::COMPARISON_EQUAL, Btrieve::INDEX_1, (char *)key, sizeof(*key), (char *)&record, RECORD_MAXIMUM_TOTAL_SIZE) < RECORD_FIXED_SIZE)
{
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieve():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("record: (%u, %u, %f, %s)\n", record.fixed.x, record.fixed.xSquared, record.fixed.xSquareRoot, record.text);
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 createIndex() fails.
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() 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;
}
The Btrieve client class.
Definition btrieveCpp.h:1388
Btrieve::StatusCode FileCreate(BtrieveFileAttributes *btrieveFileAttributes, const char *fileName, Btrieve::CreateMode createMode, Btrieve::LocationMode locationMode=Btrieve::LOCATION_MODE_NO_PREFERENCE)
Create a file.
Btrieve::StatusCode FileClose(BtrieveFile *btrieveFile)
Close a file.
Btrieve::StatusCode FileDelete(const char *fileName)
Delete a file.
Btrieve::StatusCode FileOpen(BtrieveFile *btrieveFile, const char *fileName, const char *ownerName, Btrieve::OpenMode openMode, Btrieve::LocationMode locationMode=Btrieve::LOCATION_MODE_NO_PREFERENCE)
Open a file.
The Btrieve file attributes class.
Definition btrieveCpp.h:2448
Btrieve::StatusCode SetVariableLengthRecordsMode(Btrieve::VariableLengthRecordsMode variableLengthRecordsMode)
Set the variable length records mode.
Btrieve::StatusCode SetFixedRecordLength(int fixedRecordLength)
Set the fixed record length.
The Btrieve file class.
Definition btrieveCpp.h:1729
Btrieve::StatusCode RecordCreate(char *record, int recordLength)
Create a record.
int RecordRetrieve(Btrieve::Comparison comparison, Btrieve::Index index, char *key, int keyLength, char *record, int recordSize, Btrieve::LockMode lockMode=Btrieve::LOCK_MODE_NONE)
Retrieve a record using the given comparison, index, and key.
Btrieve::StatusCode IndexCreate(BtrieveIndexAttributes *btrieveIndexAttributes)
Create an index.
Btrieve::StatusCode GetLastStatusCode()
Get the last status code.
The Btrieve index attributes class.
Definition btrieveCpp.h:3716
Btrieve::StatusCode AddKeySegment(BtrieveKeySegment *btrieveKeySegment)
Add a key segment.
The Btrieve key segment class.
Definition btrieveCpp.h:3874
Btrieve::StatusCode SetField(int offset, int length, Btrieve::DataType dataType)
Set the field.
@ VARIABLE_LENGTH_RECORDS_MODE_YES
Yes.
Definition btrieveCpp.h:1243
@ COMPARISON_EQUAL
Equal.
Definition btrieveCpp.h:85
static const char * StatusCodeToString(StatusCode statusCode)
Return a status code as a string.
@ CREATE_MODE_OVERWRITE
Overwrite.
Definition btrieveCpp.h:72
StatusCode
The Btrieve status codes.
Definition btrieveCpp.h:713
@ STATUS_CODE_UNKNOWN
-7 - Unknown.
Definition btrieveCpp.h:1194
@ STATUS_CODE_NO_ERROR
0 - No error.
Definition btrieveCpp.h:715
@ OPEN_MODE_NORMAL
Normal.
Definition btrieveCpp.h:606
@ DATA_TYPE_UNSIGNED_BINARY
Unsigned binary.
Definition btrieveCpp.h:205
@ INDEX_1
Index 1.
Definition btrieveCpp.h:311