shapereader
Read ESRI shapefiles
Data Structures | Typedefs | Enumerations | Functions
dbf.h File Reference

Go to the source code of this file.

Data Structures

struct  dbf_field_t
 Field. More...
 
struct  dbf_header_t
 File header. More...
 
struct  dbf_record_t
 Record. More...
 
struct  dbf_file_t
 File handle. More...
 

Typedefs

typedef int(* dbf_header_callback_t) (dbf_file_t *fh, const dbf_header_t *header)
 Handle the file header. More...
 
typedef int(* dbf_record_callback_t) (dbf_file_t *fh, const dbf_header_t *header, const dbf_record_t *record, size_t file_offset)
 Handle a record. More...
 

Enumerations

enum  dbf_version_t {
  DBF_VERSION_UNKNOWN = 0x00 , DBF_VERSION_DBASE2 = 0x02 , DBF_VERSION_DBASE3 = 0x03 , DBF_VERSION_DBASE4 = 0x04 ,
  DBF_VERSION_DBASE5 = 0x05 , DBF_VERSION_VISUAL_OBJECTS = 0x07 , DBF_VERSION_VISUAL_FOXPRO = 0x30 , DBF_VERSION_VISUAL_FOXPRO_AUTO = 0x31 ,
  DBF_VERSION_VISUAL_FOXPRO_VARIFIELD = 0x32 , DBF_VERSION_DBASE3_MEMO = 0x83 , DBF_VERSION_VISUAL_OBJECTS_MEMO = 0x87 , DBF_VERSION_DBASE4_MEMO = 0x8b ,
  DBF_VERSION_DBASE7 = 0x8c , DBF_VERSION_FOXPRO_MEMO = 0xf5
}
 Versions. More...
 
enum  dbf_type_t {
  DBF_TYPE_AUTOINCREMENT = '+' , DBF_TYPE_BINARY_OR_DOUBLE = 'B' , DBF_TYPE_BLOB = 'W' , DBF_TYPE_CHARACTER = 'C' ,
  DBF_TYPE_CURRENCY = 'Y' , DBF_TYPE_DATE = 'D' , DBF_TYPE_DATETIME = 'T' , DBF_TYPE_DOUBLE = 'O' ,
  DBF_TYPE_FLOAT = 'F' , DBF_TYPE_GENERAL = 'G' , DBF_TYPE_INTEGER = 'I' , DBF_TYPE_LOGICAL = 'L' ,
  DBF_TYPE_MEMO = 'M' , DBF_TYPE_NULLFLAGS = '0' , DBF_TYPE_NUMBER = 'N' , DBF_TYPE_PICTURE = 'P' ,
  DBF_TYPE_TIMESTAMP = '@' , DBF_TYPE_VARBINARY = 'Q' , DBF_TYPE_VARCHAR = 'V'
}
 Field types. More...
 

Functions

void dbf_jd_to_tm (int32_t jd, int32_t jt, struct tm *tm)
 Convert a Julian date into a tm structure. More...
 
int dbf_yyyymmdd_to_tm (const char *ymd, size_t n, struct tm *tm)
 Converts a date string in the format "YYYYMMDD" into a tm structure. More...
 
dbf_file_tdbf_init_file (dbf_file_t *fh, FILE *stream, void *user_data)
 Initialize a file handle. More...
 
void dbf_set_error (dbf_file_t *fh, const char *format,...)
 Set an error message. More...
 
int dbf_read (dbf_file_t *fh, dbf_header_callback_t handle_header, dbf_record_callback_t handle_record)
 Read a data file. More...
 
int dbf_read_header (dbf_file_t *fh, dbf_header_t **pheader)
 Read the file header. More...
 
int dbf_read_record (dbf_file_t *fh, dbf_record_t **precord)
 Read a record. More...
 
int dbf_seek_record (dbf_file_t *fh, size_t file_offset, dbf_record_t **precord)
 Read a record by record number. More...
 

Typedef Documentation

◆ dbf_header_callback_t

typedef int(* dbf_header_callback_t) (dbf_file_t *fh, const dbf_header_t *header)

A callback function that is called for the file header.

Parameters
fha file handle.
headera pointer to a dbf_header_t structure.
Return values
1on sucess.
0to stop the processing.
-1on error.

◆ dbf_record_callback_t

typedef int(* dbf_record_callback_t) (dbf_file_t *fh, const dbf_header_t *header, const dbf_record_t *record, size_t file_offset)

A callback function that is called for each record.

Parameters
fha file handle.
headera pointer to a dbf_header_t structure.
recorda pointer to a dbf_record_t structure.
file_offsetthe record's position in the file.
Return values
1on sucess.
0to stop the processing.
-1on error.

Enumeration Type Documentation

◆ dbf_version_t

Enumerator
DBF_VERSION_UNKNOWN 

Unknown.

DBF_VERSION_DBASE2 

dBase II

DBF_VERSION_DBASE3 

dBase III

DBF_VERSION_DBASE4 

dBase IV

DBF_VERSION_DBASE5 

dBase V

DBF_VERSION_VISUAL_OBJECTS 

Visual Objects.

DBF_VERSION_VISUAL_FOXPRO 

Visual FoxPro.

DBF_VERSION_VISUAL_FOXPRO_AUTO 

Visual FoxPro with Autoincrement field.

Visual FoxPro with Varchar or Varbinary field

DBF_VERSION_DBASE3_MEMO 

dBase III with memo file

DBF_VERSION_VISUAL_OBJECTS_MEMO 

Visual Objects with memo file.

DBF_VERSION_DBASE4_MEMO 

dBase IV with memo file

DBF_VERSION_DBASE7 

dBase 7

DBF_VERSION_FOXPRO_MEMO 

FoxPro with memo file.

◆ dbf_type_t

enum dbf_type_t
Enumerator
DBF_TYPE_AUTOINCREMENT 

Autoincrement (4 bytes)

DBF_TYPE_BINARY_OR_DOUBLE 

Binary (integer stored as a string).

Double (8 bytes) in FoxPro

DBF_TYPE_BLOB 

Blob (integer stored as a string)

DBF_TYPE_CHARACTER 

String.

DBF_TYPE_CURRENCY 

Decimal number (8 bytes)

DBF_TYPE_DATE 

Date (stored as "YYYYMMDD")

DBF_TYPE_DATETIME 

Date and time (8 bytes)

DBF_TYPE_DOUBLE 

Double (8 bytes)

DBF_TYPE_FLOAT 

Number (stored as a string)

DBF_TYPE_GENERAL 

OLE (integer stored as a string)

DBF_TYPE_INTEGER 

Integer (4 bytes)

DBF_TYPE_LOGICAL 

Logical (1 byte)

DBF_TYPE_MEMO 

Memo (integer stored as a string)

DBF_TYPE_NULLFLAGS 

_NullFlags (bytes)

DBF_TYPE_NUMBER 

Number (stored as a string)

DBF_TYPE_PICTURE 

Picture (integer stored as a string)

DBF_TYPE_TIMESTAMP 

Timestamp (8 bytes)

DBF_TYPE_VARBINARY 

Varbinary.

DBF_TYPE_VARCHAR 

Varchar.

Function Documentation

◆ dbf_jd_to_tm()

void dbf_jd_to_tm ( int32_t  jd,
int32_t  jt,
struct tm *  tm 
)

Calculates the calendar date from a Julian date and the time since midnight.

The tm_isdst member of the tm structure is always set to -1.

Parameters
jddays since 1 January -4712.
jtmilliseconds since midnight.
[out]tmthe converted date.
See also
"Astronomical Algorithms" [4], p. 63 for a description of the algorithm.

◆ dbf_yyyymmdd_to_tm()

int dbf_yyyymmdd_to_tm ( const char *  ymd,
size_t  n,
struct tm *  tm 
)

Fills a tm structure with the day, month and year from a date string.

The tm_wday member is only valid after 15 October 1582 in the Gregorian calendar.

The tm_isdst member is always set to -1.

Parameters
ymda date string in the format "YYYYMMDD".
nthe string length
[out]tmthe converted date.
Returns
true on success, otherwise false.

◆ dbf_init_file()

dbf_file_t * dbf_init_file ( dbf_file_t fh,
FILE *  stream,
void *  user_data 
)

Initializes a dbf_file_t structure.

Parameters
fhan uninitialized file handle.
streama FILE pointer.
user_datacallback data or NULL.
Returns
the initialized file handle.

◆ dbf_set_error()

void dbf_set_error ( dbf_file_t fh,
const char *  format,
  ... 
)

Formats and sets an error message.

Parameters
fha file handle.
formata printf format string followed by a variable number of arguments.

◆ dbf_read()

int dbf_read ( dbf_file_t fh,
dbf_header_callback_t  handle_header,
dbf_record_callback_t  handle_record 
)

Reads a file that has the file extension ".dbf" and calls functions for the file header and each record.

The data that is passed to the callback functions is only valid during the function call. Do not keep pointers to the data.

Example

int handle_header(dbf_file_t *fh, const dbf_header_t *header) {
mydata_t *mydata = (mydata_t *) fh->user_data;
// Do something
return 1;
}
int handle_record(dbf_file_t *fh, const dbf_header_t *header,
const dbf_record_t *record, size_t file_offset) {
mydata_t *mydata = (mydata_t *) fh->user_data;
// Do something
return 1;
}
dbf_init_file(fh, stream, mydata)
rc = dbf_read(fh, handle_header, handle_record);
int dbf_read(dbf_file_t *fh, dbf_header_callback_t handle_header, dbf_record_callback_t handle_record)
Read a data file.
Definition: dbf.c:1200
dbf_file_t * dbf_init_file(dbf_file_t *fh, FILE *stream, void *user_data)
Initialize a file handle.
Definition: dbf.c:55
File handle.
Definition: dbf.h:475
void * user_data
Callback data.
Definition: dbf.h:487
File header.
Definition: dbf.h:119
Record.
Definition: dbf.h:135
Parameters
fha file handle.
handle_headera function that is called for the file header.
handle_recorda function that is called for each record.
Return values
1on success.
0on end of file.
-1on error.
See also
the "Xbase File Format Description" [1] for information on the file format.

◆ dbf_read_header()

int dbf_read_header ( dbf_file_t fh,
dbf_header_t **  pheader 
)

Reads the header from a file that has the file extension ".dbf".

Parameters
fha file handle.
[out]pheaderon sucess, a pointer to a dbf_header_t structure. Free the header with free() when you are done.
Return values
1on success.
0on end of file.
-1on error.
See also
dbf_read_record

◆ dbf_read_record()

int dbf_read_record ( dbf_file_t fh,
dbf_record_t **  precord 
)

Reads a record from a file that has the file extension ".dbf".

Example

dbf_header_t *header;
dbf_record_t *record;
if ((rc = dbf_read_header(fh, &header)) > 0) {
while ((rc = dbf_read_record(fh, &record)) > 0) {
// Do something
free(record);
}
free(header);
}
int dbf_read_record(dbf_file_t *fh, dbf_record_t **precord)
Read a record.
Definition: dbf.c:1107
int dbf_read_header(dbf_file_t *fh, dbf_header_t **pheader)
Read the file header.
Definition: dbf.c:1060
Parameters
fha file handle.
[out]precordon sucess, a pointer to a dbf_record_t structure. Free the record with free() when you are done.
Return values
1on success.
0on end of file.
-1on error.
See also
dbf_read_header

◆ dbf_seek_record()

int dbf_seek_record ( dbf_file_t fh,
size_t  file_offset,
dbf_record_t **  precord 
)

Sets the file position to the specified record number and reads the requested record.

Example

size_t i;
dbf_header_t *header;
dbf_record_t *record;
if ((rc = dbf_read_header(fh, &header)) > 0) {
i = header->num_records;
while (i-- > 0) {
if ((rc = dbf_seek_record(fh, i, &record)) > 0) {
// Do something
free(record);
}
}
free(header);
}
int dbf_seek_record(dbf_file_t *fh, size_t file_offset, dbf_record_t **precord)
Read a record by record number.
Definition: dbf.c:1172
size_t num_records
Number of records.
Definition: dbf.h:124
Parameters
fha file handle.
record_numbera zero-based record number.
[out]precordon success, a pointer to a dbf_record_t structure. Free the record with free() when you are done.
Return values
1on success.
0on end of file.
-1on error.