Btrieve 2
Loading...
Searching...
No Matches
btest.c

This is an example of file creation, load, access, and deletion written in C.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include "btrieveC.h"
static char *btrieveFileName = (char *)"squaresAndSquareRoots.btr";
#define MIN_X 0
#define MAX_X 255
#pragma pack(1)
typedef struct {
uint8_t x;
uint16_t xSquared;
double xSquareRoot;
} record_t;
#pragma pack()
createFile(btrieve_client_t btrieveClient)
{
btrieve_file_attributes_t btrieveFileAttributes = NULL;
record_t record;
// If BtrieveFileAttributesAllocate() failed.
if ((status = BtrieveFileAttributesAllocate(&btrieveFileAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileAttributesAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveFileAttributesSetFixedRecordLength() failed.
if ((status = BtrieveFileAttributesSetFixedRecordLength(btrieveFileAttributes, sizeof(record))) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileAttributesSetFixedRecordLength():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveClientFileCreate() failed.
if ((status = BtrieveClientFileCreate(btrieveClient, btrieveFileAttributes, NULL, btrieveFileName, BTRIEVE_CREATE_MODE_OVERWRITE, BTRIEVE_LOCATION_MODE_NO_PREFERENCE)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClientFileCreate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
leave:
if (btrieveFileAttributes != NULL)
{
BtrieveFileAttributesFree(btrieveFileAttributes);
btrieveFileAttributes = NULL;
}
return status;
}
openFile(btrieve_client_t btrieveClient, btrieve_file_t *btrieveFile)
{
// If BtrieveClientFileOpen() failed.
if ((status = BtrieveClientFileOpen(btrieveClient, btrieveFile, btrieveFileName, NULL, BTRIEVE_OPEN_MODE_NORMAL, BTRIEVE_LOCATION_MODE_NO_PREFERENCE)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClientFileOpen():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
return status;
leave:
if (btrieveFile != NULL)
{
BtrieveClientFileClose(btrieveClient, *btrieveFile);
*btrieveFile = NULL;
}
return status;
}
loadFile(btrieve_file_t 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(i);
// If BtrieveFileRecordCreate() failed.
if ((status = BtrieveFileRecordCreate(btrieveFile, (char *)&record, sizeof(record))) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileRecordCreate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
}
leave:
return status;
}
closeFile(btrieve_client_t btrieveClient, btrieve_file_t btrieveFile)
{
// If BtrieveClientFileClose() failed.
if ((status = BtrieveClientFileClose(btrieveClient, btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClientFileClose():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
btrieveFile = NULL;
leave:
return status;
}
deleteFile(btrieve_client_t btrieveClient)
{
// If BtrieveClientFileDelete() failed.
if ((status = BtrieveClientFileDelete(btrieveClient, btrieveFileName)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClientFileDelete():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
leave:
return status;
}
createIndex(btrieve_file_t btrieveFile)
{
btrieve_index_attributes_t btrieveIndexAttributes = NULL;
btrieve_key_segment_t btrieveKeySegment = NULL;
// If BtrieveKeySegmentAllocate() failed.
if ((status = BtrieveKeySegmentAllocate(&btrieveKeySegment)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveKeySegmentAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveKeySegmentSetField() failed.
{
printf("Error: BtrieveKeySegmentSetField():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveIndexAttributesAllocate() failed.
if ((status = BtrieveIndexAttributesAllocate(&btrieveIndexAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveIndexAttributesAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveIndexAttributesAddKeySegment() failed.
if ((status = BtrieveIndexAttributesAddKeySegment(btrieveIndexAttributes, btrieveKeySegment)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveIndexAttributesAddKeySegment():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If BtrieveFileIndexCreate() failed.
if ((status = BtrieveFileIndexCreate(btrieveFile, btrieveIndexAttributes)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveFileIndexCreate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
leave:
if (btrieveIndexAttributes != NULL)
{
BtrieveIndexAttributesFree(btrieveIndexAttributes);
btrieveIndexAttributes = NULL;
}
if (btrieveKeySegment != NULL)
{
BtrieveKeySegmentFree(btrieveKeySegment);
btrieveKeySegment = NULL;
}
return status;
}
retrieveRecord(btrieve_file_t btrieveFile, uint8_t *key)
{
record_t record;
if (BtrieveFileRecordRetrieve(btrieveFile, BTRIEVE_COMPARISON_EQUAL, BTRIEVE_INDEX_1, (char *)key, sizeof(&key), (char *)&record, sizeof(record), BTRIEVE_LOCK_MODE_NONE) != sizeof(record))
{
status = BtrieveFileGetLastStatusCode(btrieveFile);
printf("Error: BtrieveFileRecordRetrieve():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
printf("record: (%u, %u, %f)\n", record.x, record.xSquared, record.xSquareRoot);
leave:
return status;
}
int
main(int argc, char *argv[])
{
static const char *usage = "Usage: %s uint8_value\n";
btrieve_client_t btrieveClient = NULL;
btrieve_file_t btrieveFile = NULL;
uint8_t key;
record_t record;
uint64_t integerValue;
if (argc != 2)
{
printf(usage, argv[0]);
goto leave;
}
integerValue = atoi(argv[1]);
if ((integerValue < MIN_X) || (integerValue > MAX_X))
{
printf(usage, argv[0]);
goto leave;
}
key = (uint8_t)integerValue;
// If BtrieveClientAllocate() failed.
if ((status = BtrieveClientAllocate(&btrieveClient, 0, 0)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClientAllocate():%d:%s.\n", status, BtrieveStatusCodeToString(status));
goto leave;
}
// If createFile() failed.
if ((status = createFile(btrieveClient)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If openFile() failed.
if ((status = openFile(btrieveClient, &btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If loadFile() failed.
if ((status = loadFile(btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If createIndex() failed.
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() failed.
if ((status = closeFile(btrieveClient, btrieveFile)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
btrieveFile = NULL;
// If deleteFile() failed.
if ((status = deleteFile(btrieveClient)) != BTRIEVE_STATUS_CODE_NO_ERROR)
{
goto leave;
}
leave:
if (btrieveFile != NULL)
{
BtrieveClientFileClose(btrieveClient, btrieveFile);
btrieveFile = NULL;
}
if (btrieveClient != NULL)
{
BtrieveClientFree(btrieveClient);
btrieveClient = NULL;
}
{
return 0;
}
return 1;
}
@ BTRIEVE_INDEX_1
Index 1.
Definition btrieveC.h:828
@ BTRIEVE_DATA_TYPE_UNSIGNED_BINARY
Unsigned binary.
Definition btrieveC.h:1087
btrieve_status_code_t BtrieveIndexAttributesAllocate(btrieve_index_attributes_t *btrieveIndexAttributesPtr)
Allocate a Btrieve index attributes.
struct btrieve_file_attributes * btrieve_file_attributes_t
A Btrieve file attributes.
Definition btrieveC.h:1309
btrieve_status_code_t BtrieveClientFileDelete(btrieve_client_t client, const char *fileName)
File delete for a Btrieve client.
btrieve_status_code_t BtrieveFileAttributesAllocate(btrieve_file_attributes_t *btrieveFileAttributesPtr)
Allocate a Btrieve file attributes.
btrieve_status_code_t BtrieveClientFileOpen(btrieve_client_t client, btrieve_file_t *btrieveFilePtr, const char *fileName, const char *ownerName, btrieve_open_mode_t openMode, btrieve_location_mode_t locationMode)
File open for a Btrieve client.
struct btrieve_file * btrieve_file_t
A Btrieve file.
Definition btrieveC.h:1302
btrieve_status_code_t BtrieveKeySegmentAllocate(btrieve_key_segment_t *btrieveKeySegmentPtr)
Allocate a Btrieve key segment.
btrieve_status_code_t BtrieveIndexAttributesAddKeySegment(btrieve_index_attributes_t indexAttributes, btrieve_key_segment_t keySegment)
segment for a Btrieve index attributes Addkey
btrieve_status_code_t BtrieveClientFree(btrieve_client_t client)
Free a Btrieve client.
@ BTRIEVE_CREATE_MODE_OVERWRITE
Overwrite.
Definition btrieveC.h:708
btrieve_status_code_t BtrieveClientFileClose(btrieve_client_t client, btrieve_file_t file)
File close for a Btrieve client.
@ BTRIEVE_OPEN_MODE_NORMAL
Normal.
Definition btrieveC.h:753
const char * BtrieveStatusCodeToString(btrieve_status_code_t statusCode)
Return a Btrieve status code as a string.
btrieve_status_code_t BtrieveFileAttributesFree(btrieve_file_attributes_t fileAttributes)
Free a Btrieve file attributes.
int BtrieveFileRecordRetrieve(btrieve_file_t file, btrieve_comparison_t comparison, btrieve_index_t index, char *key, int keyLength, char *record, int recordSize, btrieve_lock_mode_t lockMode)
Record retrieve for a Btrieve file.
btrieve_status_code_t
The Btrieve status codes.
Definition btrieveC.h:40
@ BTRIEVE_STATUS_CODE_UNKNOWN
-7 - Unknown.
Definition btrieveC.h:521
@ BTRIEVE_STATUS_CODE_NO_ERROR
0 - No error.
Definition btrieveC.h:42
struct btrieve_index_attributes * btrieve_index_attributes_t
A Btrieve index attributes.
Definition btrieveC.h:1324
btrieve_status_code_t BtrieveKeySegmentFree(btrieve_key_segment_t keySegment)
Free a Btrieve key segment.
btrieve_status_code_t BtrieveClientFileCreate(btrieve_client_t client, const btrieve_file_attributes_t fileAttributes, const btrieve_index_attributes_t indexAttributes, const char *fileName, btrieve_create_mode_t createMode, btrieve_location_mode_t locationMode)
File create for a Btrieve client.
@ BTRIEVE_LOCATION_MODE_NO_PREFERENCE
No preference.
Definition btrieveC.h:720
btrieve_status_code_t BtrieveFileRecordCreate(btrieve_file_t file, char *record, int recordLength)
Record create for a Btrieve file.
btrieve_status_code_t BtrieveFileGetLastStatusCode(btrieve_file_t file)
Get last status code from a Btrieve file
@ BTRIEVE_LOCK_MODE_NONE
None.
Definition btrieveC.h:735
@ BTRIEVE_COMPARISON_EQUAL
Equal.
Definition btrieveC.h:587
btrieve_status_code_t BtrieveFileAttributesSetFixedRecordLength(btrieve_file_attributes_t fileAttributes, int fixedRecordLength)
Set fixed record length for a Btrieve file attributes.
btrieve_status_code_t BtrieveClientAllocate(btrieve_client_t *btrieveClientPtr, int serviceAgentIdentifier, int clientIdentifier)
Allocate a Btrieve client.
btrieve_status_code_t BtrieveKeySegmentSetField(btrieve_key_segment_t keySegment, int offset, int length, btrieve_data_type_t dataType)
Set field for a Btrieve key segment.
struct btrieve_key_segment * btrieve_key_segment_t
A Btrieve key segment.
Definition btrieveC.h:1331
btrieve_status_code_t BtrieveIndexAttributesFree(btrieve_index_attributes_t indexAttributes)
Free a Btrieve index attributes.
struct btrieve_client * btrieve_client_t
A Btrieve client.
Definition btrieveC.h:1296
btrieve_status_code_t BtrieveFileIndexCreate(btrieve_file_t file, const btrieve_index_attributes_t indexAttributes)
Index create for a Btrieve file.