DIF, formatted text and dBASE tables
Single edition: lxDif and lxDbf ship in the core HotXLS package (HotXLSLaz.lpk for Lazarus), so they are included in Single, Small Business and Enterprise source and DCU editions; the trial build ships them subject to the usual trial limits
lxDif reads and writes Data Interchange Format workbooks and the application’s formatted text — its space-delimited export. Both shapes came from files the application itself wrote, not from a description of the format.
Where the formats are used
.dif | Open imports it, SaveAs writes it; the sheet name travels in the TABLE header |
.prn | Open imports it and SaveAs writes it: each field is the sheet’s column width in characters |
XlsParseDifText, XlsBuildDifText | the text-level functions, for a caller that already holds the file |
XlsParsePrnText, XlsBuildPrnText | the formatted-text reader and writer on their own |
.dbf | dBASE III/IV tables: Open imports one, writing the field names as the header row; SaveAs writes the active sheet out as a table whose first row becomes the field names |
DIF
A DIF stream is a TABLE/VECTORS/TUPLES/DATA block. Inside DATA each row opens with -1,0 and BOT; a number is 0,<value> followed by the value indicator V, a logical is 0,1 followed by TRUE or 0,0 followed by FALSE, and a string is 1,0 then a quoted line where an embedded quote is doubled. The reader also takes the indicators NA, ERROR and NULL, plus the shape earlier versions wrote: a number with no indicator line and a logical as V, 1,0/0,0 and the word. The application writes a blank cell as 1,0 and "", so the reader turns an empty quoted value into an empty cell rather than a zero-length string.
EXlsDif (derived from EXLSError) is the unit’s reserved error class; at present the text-level parser reports malformed input through its Boolean result instead of raising it
Formatted text
Writing uses the sheet’s own column widths, as the application itself does: each field spans that many characters, a number sits flush right, a logical is centred and cut to TRUE/FALSE, text sits flush left, a value too long for its field is truncated, and a line’s trailing padding is stripped. Numbers go out with a dot as the decimal separator whatever the host locale, because the reader parses with the invariant culture; a workbook written that way reproduces the application’s own file byte for byte on a host whose decimal separator is a dot, at default widths and at caller-set widths.
The file carries no delimiters: columns sit where the header line puts them, two or more spaces marking the split between two columns. Each line is trimmed at those positions and only trailing padding goes, so a value that deliberately starts with spaces keeps them. A value reading as a number or a logical becomes one on import, because the format itself carries no type.
Both readers deal in wide text from end to end. The record splitter never hands the lines to an ANSI container, so a character the host code page cannot represent — a Chinese, Japanese or Korean value, say — comes out of the parse exactly as the writer produced it, whatever locale the machine runs under. Record separators may be a carriage return, a line feed or both, with or without a trailing break
dBASE tables
lxDbf reads a dBASE III/IV table: the 32-byte header plus the 32-byte field descriptors up to the 0x0D terminator, and one fixed-width record per row behind a deletion flag. Character fields treat trailing spaces as padding and hold on to leading ones; numeric and float fields come out as numbers; a logical comes out as a Boolean; a date comes out as a date; the FoxPro I, B, Y and T types are decoded as well. A deleted record stays in the table with Deleted set and skipped on import.
XlsReadDbfFile and XlsReadDbfStream expose the parse; TXlsDbfTable carries FieldCount, Field, RecordCount, Deleted and Value, where Field answers a TXlsDbfField record (Name, the one-letter FieldType, FieldLength, Decimals) describing a single 32-byte field descriptor. Memo contents are read back from the companion file: XlsReadDbfFile opens the sibling .dbt (or .fpt for a FoxPro table) when there is one, and the stream overloads take a memo stream, with AFoxProMemo selecting the FoxPro block layout. With no companion present an M field comes out as the fixed-width text the record stores.
The writer stores text as single bytes in the host ANSI code page — that is what the format carries in place of an encoding — so a value, memo text or field name the page cannot represent is refused with EXlsDbf before anything is written instead of being stored as a replacement character, matching the rule the ANSI text export follows. A field name is likewise limited to the ten bytes of its descriptor slot, so a name that fits in characters but not in bytes is refused rather than overwriting the type, length and decimal bytes that follow it; both checks happen up front, so a refused table leaves the stream empty and the file form deletes the files it created, while an accepted table always reopens. XlsWriteDbfFile writes a sibling .dbt when the table declares a memo field holding text, producing a version 0x83 table whose M fields are block pointers; a table with no memo content produces exactly the single file earlier versions produced. The two-argument XlsWriteDbfStream has nowhere to put memo blocks, so it carries on writing M as fixed-width text
To build a table rather than read one, TXlsDbfTable.Clear empties the field and record arrays; AddField adds a descriptor and fills the new column with Null across every existing row, taking types C, N, F, L, D and M, a length from 1 to 255, decimals from 0 to 15 provided they stay below the field length, and a name of no more than ten characters (the writer additionally requires it to fit in ten bytes of the host code page); AddRow appends a row of Null values and returns its zero-based index, after which SetValue and SetDeleted fill it. A declaration that is invalid raises EXlsDbf before the table is altered.
SaveAs with a .dbf name exports the active sheet: the first row becomes the field names, each column’s type follows its values, and widths are measured in bytes of the host code page so double-byte text fits. A text column whose longest value needs more than 254 bytes becomes a memo column and goes through the .dbt companion rather than being cut.
See also
Compiled reporting and DTO mapping cover the object-model side of import and export.