Btrieve 2
Loading...
Searching...
No Matches
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;
}
leave:
return status;
}
int
main(int argc, char* argv[])
{ //Establish a session with the engine:
BtrieveClient btrieveClient;
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;
}
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 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 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.
Btrieve::StatusCode IndexCreate(BtrieveIndexAttributes *btrieveIndexAttributes)
Create an index.
int RecordRetrieveLast(Btrieve::Index index, char *record, int recordSize, Btrieve::LockMode lockMode=Btrieve::LOCK_MODE_NONE)
Retrieve the last record.
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.
Btrieve::StatusCode SetModifiable(bool enableModifiable)
Set modifiable.
The Btrieve key segment class.
Definition btrieveCpp.h:3874
Btrieve::StatusCode SetField(int offset, int length, Btrieve::DataType dataType)
Set the field.
static const char * StatusCodeToString(StatusCode statusCode)
Return a status code as a string.
@ LOCK_MODE_NONE
None.
Definition btrieveCpp.h:575
@ CREATE_MODE_NO_OVERWRITE
No overwrite.
Definition btrieveCpp.h:74
StatusCode
The Btrieve status codes.
Definition btrieveCpp.h:713
@ STATUS_CODE_FILE_ALREADY_EXISTS
59 - File already exists.
Definition btrieveCpp.h:833
@ 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
static long long UnixEpochMicrosecondsToTimestamp(long long microseconds)
Convert a Unix Epoch based timestamp in microseconds to a TIMESTAMP.
@ DATA_TYPE_TIMESTAMP
Timestamp.
Definition btrieveCpp.h:225
@ INDEX_1
Index 1.
Definition btrieveCpp.h:311