Test_BTRV2.cpp

This example stores a given systolic and diastolic value in a Btrieve file.

// Test_BTRV2.cpp : This file contains the 'main' function. Program execution begins and ends there.
// Usage: Test_BTRV2.exe <systolic value> <diastolic value>
// Example: Test_BTRV2.exe 90 70
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctime>
#include "btrieveCpp.h"
// Zen data file name:
static char* btrieveFileName = (char*)"Pressures.btr";
// Zen record structure for the Pressures.btr file:
#pragma pack(1)
typedef struct {
uint64_t timeTaken;
uint16_t systolic;
uint16_t diastolic;
char EvalCode;
} BPrecord_t;
#pragma pack()
createFile(BtrieveClient* btrieveClient)
{
BtrieveFileAttributes btrieveFileAttributes;
// Set Fixed Record Length
if ((status = btrieveFileAttributes.SetFixedRecordLength(sizeof(BPrecord_t))) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// Create the file
status = btrieveClient->FileCreate(&btrieveFileAttributes, btrieveFileName, Btrieve::CREATE_MODE_NO_OVERWRITE);
{
{
printf("Error: BtrieveClient::FileCreate():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
}
leave:
return status;
}
openFile(BtrieveClient* btrieveClient, BtrieveFile* btrieveFile)
{
// Open the file; no owner name provided
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;
}
createIndex(BtrieveFile* btrieveFile)
{
BtrieveIndexAttributes btrieveIndexAttributes;
BtrieveKeySegment btrieveKeySegment;
// Create a non-modifiable timestamp index on the first 8 bytes of the record
if ((status = btrieveKeySegment.SetField(0, 8, Btrieve::DATA_TYPE_TIMESTAMP)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveKeySegment::SetField():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
if ((status = btrieveIndexAttributes.SetModifiable(false)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveIndexAttribute::SetModifiable():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
}
if ((status = btrieveIndexAttributes.AddKeySegment(&btrieveKeySegment)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
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;
}
insertRecord(BtrieveFile* btrieveFile, uint16_t sysdata, uint16_t diasdata)
{
BPrecord_t record;
time_t now = time(0) * 1000000; // Get current system time and convert to microseconds
record.timeTaken = Btrieve::UnixEpochMicrosecondsToTimestamp(now); //Convert time to Btrieve2 Timestamp format
record.systolic = sysdata;
record.diastolic = diasdata;
// Use the systolic and diastolic values to determing the EvaluationCode:
record.EvalCode = 'N'; // Normal
if ((sysdata >= 120 && sysdata < 130) && (diasdata < 80))
record.EvalCode = 'E'; //Elevated blood pressure
if ((sysdata >= 130 && sysdata < 140) || (diasdata >= 80 && diasdata < 90))
record.EvalCode = 'H'; //High blood pressure
if ((sysdata >= 140 && sysdata <= 180) || (diasdata >= 90 && diasdata <= 120))
record.EvalCode = 'V'; //Very high blood pressure
if ((sysdata > 180) || (diasdata > 120))
record.EvalCode = 'C'; //Hypertensive Crisis
// Insert the record
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;
}
retrieveLastRecord(BtrieveFile* btrieveFile)
{
BPrecord_t record;
// Retrieve last inserted record, which should be the last record in the timestamp index
// Notice that record retrieval returns the record length instead of a status.
if (btrieveFile->RecordRetrieveLast(Btrieve::INDEX_1, (char*)& record, sizeof(record), Btrieve::LOCK_MODE_NONE) != sizeof(record))
{
// If we did not get the record size we expected, we can check the status code of the retrieval:
status = btrieveFile->GetLastStatusCode();
printf("Error: BtrieveFile::RecordRetrieve():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// Display the record:
printf("record: (%u, %u, %c)\n", record.systolic, record.diastolic, record.EvalCode);
leave:
return status;
}
closeFile(BtrieveClient* btrieveClient, BtrieveFile* btrieveFile)
{
// Close the file
if ((status = btrieveClient->FileClose(btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::FileClose():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// Close the session with the Zen engine
if ((status = btrieveClient->Reset()) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::Reset() : % d : % s.\n", status, Btrieve::StatusCodeToString(status));
}
leave:
return status;
}
int
main(int argc, char* argv[])
{ //Establish a session with the engine:
BtrieveClient btrieveClient(0x4232, 0);
BtrieveFile btrieveFile;
uint8_t newFile;
uint16_t systolicInput;
uint16_t diastolicInput;
// If the incorrect number of arguments were given.
if (argc != 3)
{
printf("Usage: %s <systolic> <diastolic>\n", argv[0]);
goto leave;
}
systolicInput = atoi(argv[1]);
diastolicInput = atoi(argv[2]);
newFile = 1;
// Create the file
if ((status = createFile(&btrieveClient)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
newFile = 0;
}
// Open the file
if ((status = openFile(&btrieveClient, &btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If the file did NOT already exist, add the index
if (newFile == 1)
{
if ((status = createIndex(&btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
}
// Insert the record
if ((status = insertRecord(&btrieveFile, systolicInput, diastolicInput)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// Retrieve the last record
if ((status = retrieveLastRecord(&btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// Close the File and the session
if ((status = closeFile(&btrieveClient, &btrieveFile)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
leave:
// If there wasn't a failure.
return 0;
return 1;
}