TStruct / TField / TIndex

Source: source/classes/tstruct.prg, source/classes/tfield.prg, source/classes/tindex.prg

These three utility classes provide object-oriented representations for C-style memory structures (TStruct), DBF field definitions (TField), and DBF index definitions (TIndex). They are used when working with low-level data structures, defining DBF schemas programmatically, or creating index files from Harbour code.

TStruct - C Structure Builder

TStruct models a C-language structure in Harbour memory. It maintains a binary buffer and a list of member definitions, allowing members to be added and accessed by name or position.

DATA Members

DATATypeDescription
cBufferCharacterBinary raw memory buffer
aMembersArrayArray of { cName, nType, nLen, nOffset }

Type Constants

ConstantDescription
BYTE / CHAR1-byte unsigned integer
WORD / INT / BOOL2-byte signed integer
LONG / DWORD / PTR / LPSTR4-byte signed integer or pointer
LONGLONG8-byte signed integer

Methods

MethodDescription
New()Create empty structure
AddMember( cName, nType, nLen )Add a member with type and optional length
SetMember( nMember, uData )Set member value by index
GetMember( nMember )Get member value by index
SizeOf()Return total buffer length in bytes

Example: Define and Use a C Structure

#include "FiveWin.ch"
#include "Struct.ch"

function Main()

   local oStru := TStruct():New()

   // Add members: name, type, length
   oStru:AddMember( "nId",   LONG,    4 )
   oStru:AddMember( "cName", CHAR,   20 )
   oStru:AddMember( "nAge",  BYTE      )   // defaults to 1 byte

   ? "Size:", oStru:SizeOf()               // 25

   // Set values
   oStru:SetMember( 1, 12345 )
   oStru:SetMember( 2, "John Smith" )

   // Read them back
   ? oStru:GetMember( 1 )                  // 12345
   ? oStru:GetMember( 2 )                  // "John Smith"

return nil

TField - DBF Field Definition

TField is a simple data container representing a single DBF field. It stores all properties needed to define a field in a DBF file structure.

DATA Members

DATATypeDescription
cDbfNameCharacterName of the DBF file
nOrderNumericField position (1-based)
lIndexedLogicalWhether the field is indexed
cNameCharacterField name
cTypeCharacterField type (C, N, D, L, M)
nWidthNumericField width
nDecimalNumericDecimal places for numeric fields
cCommentsCharacterOptional field description

Method

MethodDescription
New( cDbfName, nOrder, lIndexed, cName, cType, nWidth, nDecimal, cComments )Create a field definition

Example: Define a DBF Field

#include "FiveWin.ch"

function Main()

   local oFld

   oFld := TField():New( "CUSTOMER.DBF", 1, .F., "CODE", "C", 5, 0, "Customer code" )

   ? oFld:cName     // "CODE"
   ? oFld:cType     // "C"
   ? oFld:nWidth    // 5

return nil

TIndex - DBF Index Definition

TIndex represents an index definition for a DBF file. It can create the actual index file on disk (CDX or NTX format) from the definition properties.

DATA Members

DATATypeDescription
cDbfNameCharacterName of the DBF file to index
cNameCharacterIndex file name (without extension)
cTagCharacterTag name for CDX indexes
cExpressionCharacterIndex key expression
lUniqueLogicalUnique index flag
lDescendingLogicalDescending order flag
cCommentsCharacterOptional comments
cTypeCharacterIndex type ("CDX" default, or "NTX")

Methods

MethodDescription
New( cDbfName, cName, cTag, cExpression, lUnique, lDescending, cComments )Create an index definition
Create( cPath )Create the index file on disk at the given path

Example: Create an Index

#include "FiveWin.ch"

function Main()

   local oIdx

   // Define an index on CUSTOMER.DBF by the CODE field
   oIdx := TIndex():New( "CUSTOMER.DBF", "CODE_IDX", "CODE", "CODE", .F., .F., "Index by code" )

   // Create the index file
   oIdx:Create( "C:\data\" )

return nil

Notes

See Also