dbf.datafile

Module to load and save DBF files

Authors Carlos Santander Bernal
Version 0.5
Date February 25, 2013
History
  • Version 0.5 - February 25, 2013
    • Updated for D2
    • If the DBF is constructed using a file name, the underlying stream is closed after loading/saving
    • Added DBF.getRecordCount, DBF.getFieldCount, DBF.getFieldName, DBF.addDateField
  • Version 0.4n - May 24, 2006
    • Added support for non-integer numerical fields
    • Added DBF.removeRecord and DBF.removeAllRecords
  • Version 0.3n - May 23, 2006: Removed Mango dependency
  • Version 0.3 - May 22, 2006
    • Added DBF.addEmptyRecord
    • Added DBF.this (Buffer) and proper support for it
    • Removed alias DBF.read
    • Versioned out DBF.this (Stream)
    • Better unittesting
  • Version 0.2 - May 18, 2006
    • Renamed public DBF.read to DBF.load (added a deprecated alias just in case)
    • Renamed private DBF.read to DBF.readText
    • Added DBF.save, DBF.addField, DBF.addNumericField, DBF.removeField , DBF.getFieldIndex, DBF.opApply, DBF.addRecord
    • Renamed FieldDescriptor to Field (added a deprecated alias just in case), moved to dbf.field
    • Added Field.this and static Date.opCall
    • Renamed DataTypes to DataType
    • Changed type of Field.fieldType to DataType
    • Added DBFException, moved to dbf.exception
    • Moved getAndSwap, getAndSwap, swapAndPut, swapAndPut to dbf.internal
    • Moved Boolean to dbf.boolean
    • Moved Date to dbf.date
    • Moved Record to dbf.record
    • Moved DataType to dbf.datatype
  • Version 0.1 - May 14, 2006: First public release
License zlib/libpng
See Also
class DBF;

Simple class to access a DBF file.

Loading and saving are performed from wherever the stream is at that moment, and the stream is not closed after loading or saving. All this means that if you have a Stream which contains more than just the DBF, you can keep using it.

Bugs
  • Only works for data types: L, C, N, D
  • No character conversion is made, so non-Unicode strings will generate exceptions
Examples
 string file = args[1];
 DBF dbf = new DBF (file);
 dbf.read();

 ...

 void foo (Stream stream)
 {
 	(new DBF(stream)).load();
 }
See Also Mango
this(Stream stream);

Initializes the DBF from a Stream

Parameters
Stream stream the Stream containing the DBF or where the DBF should be saved
this(string file);

Initializes the DBF from a file

Parameters
string file the file containing the DBF or where the DBF should be saved
void load();

Loads the DBF from the specified source

void save();

Saves the DBF to the specified source

auto getRecordCount();

Returns the number of records

auto getFieldCount();

Returns the number of fields

auto getFieldName(uint index);

Returns the name of a field by its index

auto getFieldIndex(string name);

Returns the index of a field by its name

void removeField(string name);

Removes a field from this DBF

Throws DBFException if there's no field named name
void removeField(uint index);

Removes a field from this DBF

Throws DBFException if index is greater than the number of fields
void addField(string fname, DataType dataType, ushort flength);

Adds a field to this DBF

If type == DataType.Number, flength represents the fieldLength, and the decimalCount is set to 0.

If type == DataType.Date, flength is ignored and automatically set to 8.

This method fails if there're already records in the DBF. To modify the fields in the DBF, you must first remove all its records.

Parameters
string fname the name of the field
DataType dataType the type of the field
ushort flength the length of the field
Throws DBFException if there're records in the DBF or if there's already a field with this name
void addNumericField(string fname, ubyte flength, ubyte fdecimals);

Adds a numeric field to this DBF

This method fails if there're already records in the DBF. To modify the fields in the DBF, you must first remove all its records.

Parameters
string fname the name of the field
ubyte flength the length of the field
ubyte fdecimals the decimal count of the field
Throws DBFException if there're records in the DBF or if there's already a field with this name
void addDateField(string fname);

Adds a date field to this DBF

This method fails if there're already records in the DBF. To modify the fields in the DBF, you must first remove all its records.

Parameters
string fname the name of the field
Throws DBFException if there're records in the DBF or if there's already a field with this name
int opApply(int delegate(Field) dg);
int opApply(int delegate(uint, Field) dg);
int opApply(int delegate(string, Field) dg);

Foreach the fields of this DBF

int opApply(int delegate(Record) dg);
int opApply(int delegate(uint, Record) dg);

Foreach the records of this DBF

void removeRecord(uint index);

Removes a record from this DBF

Throws DBFException if index is greater than the number of records
void removeAllRecords();

Removes all records from this DBF

Record addEmptyRecord();

Adds a record to this DBF

This record contains no data and the fields must be explicitly filled by the user. However, it already belongs to the DBF.

void addRecord(...);

Adds a record to this DBF

The types passed here must be compatible with those defined by the fields.