DBFLibDBF and FPT files for Ring

Records, memos, maintenance and export

Appending and deleting records, searching, memo fields, pack, export, structure, code page and information.

Record Operations#

append()#

Adds a new blank record at the end of the table and positions the current record pointer on it. All character fields are space-filled. Memo, binary, and integer fields are zero-filled. The record is marked as modified.

Raises if the file is read-only.

Ring
oDbf.append()
oDbf.replace("ID",    42)
oDbf.replace("NAME",  "Khalid Mansour")
oDbf.replace("NOTES", "New hire.")   # memo written transparently

deleteRec()#

Soft-deletes the current record. The record remains in the file until pack() is called.

Raises if the file is read-only.

recall()#

Clears the deletion flag on the current record, restoring it to active status.

Raises if the file is read-only.

blank()#

Resets all fields of the current record to their empty/default values. The deletion flag is preserved.

Raises if the file is read-only.

Searching#

locate(pFieldName, pValue) → true / false#

Performs a sequential search always starting from record 1, regardless of the current position. Matches by trimmed string equality (for string values) or numeric equality (for numeric values). Positions on the first match and returns true. Returns false and sets EOF if no match is found.

Ring
if oDbf.locate("CITY", "Riyadh")
    ? "Found: " + trim(oDbf.fieldGet("NAME"))
ok

locateNext(pFieldName, pValue) → true / false#

Continues a search from the record after the current one.

Ring
nCount = 0
if oDbf.locate("DEPT", "Engineering")
    nCount++
    while oDbf.locateNext("DEPT", "Engineering")
        nCount++
    end
ok
? "Engineering staff: " + nCount

Memo Fields#

Memo (M) and General (G) fields store variable-length text in a companion .fpt file. The library handles all FPT I/O transparently:

No special handling is needed. Use memo fields exactly like any other field.

Ring
# Write
oDbf.replace("NOTES", "Performance review completed. Rating: Excellent.")

# Read
? oDbf.fieldGet("NOTES")

# In a loop — no different from any other field
oDbf.goTop()
while ! oDbf.isEof()
    ? trim(oDbf.fieldGet("NAME")) + ": " + oDbf.fieldGet("NOTES")
    oDbf.skip(1)
end

memoRead(pFieldName) → string#

memoWrite(pFieldName, pText)#

These explicit memo functions remain available as aliases. They produce identical results to fieldGet() and replace() respectively. Use them if you prefer to make FPT access explicit in your code.

Ring
# These pairs are exactly equivalent:
oDbf.replace("NOTES",  "Some text")       # — preferred
oDbf.memoWrite("NOTES", "Some text")      # — explicit alias

cText = oDbf.fieldGet("NOTES")            # — preferred
cText = oDbf.memoRead("NOTES")            # — explicit alias
Note

Note: every write (via either API) appends a new block to the FPT file. Repeated updates to the same memo field accumulate orphaned blocks. Call pack() or packFpt() to reclaim that space.

Maintenance#

pack()#

Permanently removes all soft-deleted records from the .dbf file. When the table has memo fields, also calls packFpt() automatically to compact the .fpt file and remove all orphaned blocks.

Both operations write to a temporary file first, then replace the original via rename — safe against data loss if interrupted.

After pack(), the current record is positioned at record 1, or EOF/BOF if the table is now empty.

Raises if the file is read-only or a temporary file cannot be created.

Ring
# Delete inactive employees and clean up both DBF and FPT
oDbf.goTop()
while ! oDbf.isEof()
    if ! oDbf.fieldGet("ACTIVE")
        oDbf.deleteRec()
    ok
    oDbf.skip(1)
end
oDbf.pack()
? "Records after pack: " + oDbf.recCount()

packFpt()#

Compacts the .fpt memo file independently of the DBF record list. Rewrites only the memo blocks referenced by records currently in the table, discarding all orphaned blocks. Updates the block pointers in every affected DBF record on disk.

Use packFpt() standalone after a heavy memo-update workload where no records have been deleted (in those cases pack() calls packFpt() automatically).

Raises if the file is read-only or a temporary file cannot be created. Does nothing if no FPT file is open.

Ring
# After many replace("NOTES", ...) calls with no record deletions
oDbf.packFpt()

Export#

toList() → list of lists#

Returns all non-deleted records as a list of rows. Each row is a list of field values in field-definition order. Memo fields are included as their text content, just like any other field. The current record position is preserved.

Ring
aData = oDbf.toList()
nRows = len(aData)
for i = 1 to nRows
    ? aData[i][2]   # NAME (field 2)
    ? aData[i][7]   # NOTES memo text (field 7)
next

toMapList() → list of lists of pairs#

Returns all non-deleted records as a list of rows, where each row is a list of [FieldName, Value] pairs. Memo field values are included as text. The current record position is preserved.

Ring
aMap = oDbf.toMapList()
for i = 1 to len(aMap)
    aRow = aMap[i]
    for j = 1 to len(aRow)
        ? aRow[j][1] + " = " + aRow[j][2]
    next
next

Structure#

getStructure() → list of field definitions#

Returns the field definitions in the same format accepted by create(). Each element is [Name, Type, Length, Decimals].

Ring
aStruct = oDbf.getStructure()
for i = 1 to len(aStruct)
    ? aStruct[i][FLD_NAME] + " " + aStruct[i][FLD_TYPE] +
      "(" + aStruct[i][FLD_LEN] + ")"
next

copyStructure(pNewPath) → true#

Creates a new empty .dbf file at pNewPath with the same field definitions as the current table.

Ring
oDbf.copyStructure("employees_backup.dbf")

Code Page#

setCodePage(pCodePage)#

Sets the language driver / code page byte in the DBF header. Use the C_CODEPAGE_* constants.

Ring
oDbf.setCodePage(C_CODEPAGE_WIN_1256)   # Windows Arabic

getCodePage() → number#

Returns the current code page constant value.

getCodePageName() → string#

Returns a human-readable description of the current code page.

Ring
? oDbf.getCodePageName()   # e.g. "Windows Arabic (CP 1256)"

Information#

info() → string#

Returns a formatted multi-line string describing the file: path, version, record count, field count, header size, record size, code page, memo flag, read-only flag, and a field structure table.

Ring
? oDbf.info()

Example output:

DBF File Information
====================
File     : employees.dbf
Version  : 0x30
Records  : 10
Fields   : 7
Header   : 360 bytes
RecSize  : 97 bytes
CodePage : Windows ANSI (CP 1252)
Has Memo : Yes
Read Only: No

Field Structure:
-------------------------------------------------
Name        Type  Len     Dec
-------------------------------------------------
ID          N     6       0
NAME        C     30      0
CITY        C     20      0
SALARY      N     12      2
HIREDATE    D     8       0
ACTIVE      L     1       0
NOTES       M     4       0
DBFLib - DBF and FPT files for Ring, made with ringdoc.