shapereader
Read ESRI shapefiles
dbf.h
Go to the documentation of this file.
1/*
2 * Read ESRI shapefiles
3 *
4 * Copyright (C) 2023 Andreas Vögele
5 *
6 * This library is free software; you can redistribute it and/or modify it
7 * under either the terms of the ISC License or the same terms as Perl.
8 */
9
10/* SPDX-License-Identifier: ISC OR Artistic-1.0-Perl OR GPL-1.0-or-later */
11
16#ifndef _SHAPEREADER_DBF_H
17#define _SHAPEREADER_DBF_H
18
19#include <stddef.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <time.h>
23
27typedef enum dbf_version_t {
45 DBF_VERSION_VISUAL_FOXPRO_VARIFIELD = 0x32,
57
61typedef enum dbf_type_t {
101
105typedef struct dbf_field_t {
107 char name[32];
109 size_t length;
111 unsigned char reserved[14];
112 size_t size; /* Size in bytes */
113 size_t offset; /* Position in the record buffer */
115
119typedef struct dbf_header_t {
121 int year;
122 int month;
123 int day;
124 size_t num_records;
125 size_t header_size;
126 size_t record_size;
127 unsigned char reserved[20];
131
135typedef struct dbf_record_t {
136 char *bytes; /* Raw data of length record_size */
138
154extern void dbf_jd_to_tm(int32_t jd, int32_t jt, struct tm *tm);
155
171extern int dbf_yyyymmdd_to_tm(const char *ymd, size_t n, struct tm *tm);
172
184extern void dbf_record_bytes(const dbf_record_t *record,
185 const dbf_field_t *field, const char **pbytes,
186 size_t *len);
187
205extern int dbf_record_date(const dbf_record_t *record,
206 const dbf_field_t *field, struct tm *tm);
207
221extern int dbf_record_datetime(const dbf_record_t *record,
222 const dbf_field_t *field, struct tm *tm);
223
235extern int dbf_record_double(const dbf_record_t *record,
236 const dbf_field_t *field, double *value);
237
249extern int dbf_record_int32(const dbf_record_t *record,
250 const dbf_field_t *field, int32_t *value);
251
267extern int dbf_record_int64(const dbf_record_t *record,
268 const dbf_field_t *field, int64_t *value);
269
279extern int dbf_record_is_deleted(const dbf_record_t *record);
280
293extern int dbf_record_is_null(const dbf_record_t *record,
294 const dbf_field_t *field);
295
312extern int dbf_record_logical(const dbf_record_t *record,
313 const dbf_field_t *field);
314
327extern int dbf_record_logical_is_false(const dbf_record_t *record,
328 const dbf_field_t *field);
329
342extern int dbf_record_logical_is_true(const dbf_record_t *record,
343 const dbf_field_t *field);
344
356extern char *dbf_record_strdup(const dbf_record_t *record,
357 const dbf_field_t *field);
358
371extern void dbf_record_string(const dbf_record_t *record,
372 const dbf_field_t *field, const char **pstr,
373 size_t *len);
374
387extern int dbf_record_strtod(const dbf_record_t *record,
388 const dbf_field_t *field, double *value);
389
403extern int dbf_record_strtol(const dbf_record_t *record,
404 const dbf_field_t *field, int base, long *value);
405
418extern int dbf_record_strtold(const dbf_record_t *record,
419 const dbf_field_t *field, long double *value);
420
434extern int dbf_record_strtoll(const dbf_record_t *record,
435 const dbf_field_t *field, int base,
436 long long *value);
437
451extern int dbf_record_strtoul(const dbf_record_t *record,
452 const dbf_field_t *field, int base,
453 unsigned long *value);
454
468extern int dbf_record_strtoull(const dbf_record_t *record,
469 const dbf_field_t *field, int base,
470 unsigned long long *value);
471
475typedef struct dbf_file_t {
476 /* File pointer */
477 void *stream;
478 /* Read bytes from the stream */
479 size_t (*fread)(struct dbf_file_t *fh, void *buf, size_t count);
480 /* Test the stream's end-of-file indicator */
481 int (*feof)(struct dbf_file_t *fh);
482 /* Test the stream's error indicator */
483 int (*ferror)(struct dbf_file_t *fh);
484 /* Set the stream's file position */
485 int (*fsetpos)(struct dbf_file_t *fh, size_t offset);
489 size_t num_bytes;
491 char error[128];
492 /* Header size */
493 size_t header_size;
494 /* Record size */
495 size_t record_size;
496} dbf_file_t;
497
508extern dbf_file_t *dbf_init_file(dbf_file_t *fh, FILE *stream,
509 void *user_data);
510
520#ifdef __GNUC__
521extern void dbf_set_error(dbf_file_t *fh, const char *format, ...)
522 __attribute__((format(printf, 2, 3)));
523#else
524extern void dbf_set_error(dbf_file_t *fh, const char *format, ...);
525#endif
526
539 const dbf_header_t *header);
540
555 const dbf_header_t *header,
556 const dbf_record_t *record,
557 size_t file_offset);
558
598extern int dbf_read(dbf_file_t *fh, dbf_header_callback_t handle_header,
599 dbf_record_callback_t handle_record);
600
615extern int dbf_read_header(dbf_file_t *fh, dbf_header_t **pheader);
616
646extern int dbf_read_record(dbf_file_t *fh, dbf_record_t **precord);
647
681extern int dbf_seek_record(dbf_file_t *fh, size_t file_offset,
682 dbf_record_t **precord);
683
684#endif
dbf_type_t
Field types.
Definition: dbf.h:61
@ DBF_TYPE_CHARACTER
String.
Definition: dbf.h:69
@ DBF_TYPE_LOGICAL
Logical (1 byte)
Definition: dbf.h:85
@ DBF_TYPE_DATE
Date (stored as "YYYYMMDD")
Definition: dbf.h:73
@ DBF_TYPE_VARBINARY
Varbinary.
Definition: dbf.h:97
@ DBF_TYPE_NUMBER
Number (stored as a string)
Definition: dbf.h:91
@ DBF_TYPE_BLOB
Blob (integer stored as a string)
Definition: dbf.h:67
@ DBF_TYPE_FLOAT
Number (stored as a string)
Definition: dbf.h:79
@ DBF_TYPE_VARCHAR
Varchar.
Definition: dbf.h:99
@ DBF_TYPE_AUTOINCREMENT
Autoincrement (4 bytes)
Definition: dbf.h:63
@ DBF_TYPE_DOUBLE
Double (8 bytes)
Definition: dbf.h:77
@ DBF_TYPE_BINARY_OR_DOUBLE
Binary (integer stored as a string).
Definition: dbf.h:65
@ DBF_TYPE_MEMO
Memo (integer stored as a string)
Definition: dbf.h:87
@ DBF_TYPE_CURRENCY
Decimal number (8 bytes)
Definition: dbf.h:71
@ DBF_TYPE_PICTURE
Picture (integer stored as a string)
Definition: dbf.h:93
@ DBF_TYPE_TIMESTAMP
Timestamp (8 bytes)
Definition: dbf.h:95
@ DBF_TYPE_INTEGER
Integer (4 bytes)
Definition: dbf.h:83
@ DBF_TYPE_NULLFLAGS
_NullFlags (bytes)
Definition: dbf.h:89
@ DBF_TYPE_GENERAL
OLE (integer stored as a string)
Definition: dbf.h:81
@ DBF_TYPE_DATETIME
Date and time (8 bytes)
Definition: dbf.h:75
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
dbf_version_t
Versions.
Definition: dbf.h:27
@ DBF_VERSION_VISUAL_OBJECTS_MEMO
Visual Objects with memo file.
Definition: dbf.h:49
@ DBF_VERSION_VISUAL_OBJECTS
Visual Objects.
Definition: dbf.h:39
@ DBF_VERSION_FOXPRO_MEMO
FoxPro with memo file.
Definition: dbf.h:55
@ DBF_VERSION_VISUAL_FOXPRO
Visual FoxPro.
Definition: dbf.h:41
@ DBF_VERSION_DBASE4_MEMO
dBase IV with memo file
Definition: dbf.h:51
@ DBF_VERSION_DBASE5
dBase V
Definition: dbf.h:37
@ DBF_VERSION_DBASE4
dBase IV
Definition: dbf.h:35
@ DBF_VERSION_VISUAL_FOXPRO_AUTO
Visual FoxPro with Autoincrement field.
Definition: dbf.h:43
@ DBF_VERSION_DBASE3
dBase III
Definition: dbf.h:33
@ DBF_VERSION_DBASE2
dBase II
Definition: dbf.h:31
@ DBF_VERSION_UNKNOWN
Unknown.
Definition: dbf.h:29
@ DBF_VERSION_DBASE3_MEMO
dBase III with memo file
Definition: dbf.h:47
@ DBF_VERSION_DBASE7
dBase 7
Definition: dbf.h:53
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.
Definition: dbf.c:253
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
void dbf_set_error(dbf_file_t *fh, const char *format,...)
Set an error message.
Definition: dbf.c:75
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.
Definition: dbf.h:554
int(* dbf_header_callback_t)(dbf_file_t *fh, const dbf_header_t *header)
Handle the file header.
Definition: dbf.h:538
void dbf_jd_to_tm(int32_t jd, int32_t jt, struct tm *tm)
Convert a Julian date into a tm structure.
Definition: dbf.c:195
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
Field.
Definition: dbf.h:105
struct dbf_field_t * next
Next field or NULL.
Definition: dbf.h:106
size_t length
Number of bytes.
Definition: dbf.h:109
unsigned char reserved[14]
Reserved bytes.
Definition: dbf.h:111
char name[32]
Name.
Definition: dbf.h:107
size_t decimal_places
Number of decimal places in a number.
Definition: dbf.h:110
dbf_type_t type
Type.
Definition: dbf.h:108
File handle.
Definition: dbf.h:475
char error[128]
Error message.
Definition: dbf.h:491
void * user_data
Callback data.
Definition: dbf.h:487
size_t num_bytes
Number of bytes read.
Definition: dbf.h:489
File header.
Definition: dbf.h:119
dbf_field_t * fields
The fields in each record.
Definition: dbf.h:129
unsigned char reserved[20]
Reserved bytes.
Definition: dbf.h:127
size_t num_records
Number of records.
Definition: dbf.h:124
int month
Month.
Definition: dbf.h:122
dbf_version_t version
Table version.
Definition: dbf.h:120
int year
Year since 1900.
Definition: dbf.h:121
int num_fields
Number of fields in each record.
Definition: dbf.h:128
size_t header_size
Number of bytes in the header.
Definition: dbf.h:125
size_t record_size
Number of bytes in a record.
Definition: dbf.h:126
int day
Day.
Definition: dbf.h:123
Record.
Definition: dbf.h:135
int dbf_record_date(const dbf_record_t *record, const dbf_field_t *field, struct tm *tm)
Get a date.
Definition: dbf.c:326
int dbf_record_strtod(const dbf_record_t *record, const dbf_field_t *field, double *value)
Convert a string to double representation.
Definition: dbf.c:590
int dbf_record_double(const dbf_record_t *record, const dbf_field_t *field, double *value)
Get a double value.
Definition: dbf.c:364
void dbf_record_string(const dbf_record_t *record, const dbf_field_t *field, const char **pstr, size_t *len)
Get a string.
Definition: dbf.c:578
int dbf_record_strtoll(const dbf_record_t *record, const dbf_field_t *field, int base, long long *value)
Convert a string to a long long integer.
Definition: dbf.c:671
int dbf_record_datetime(const dbf_record_t *record, const dbf_field_t *field, struct tm *tm)
Get a date and a time.
Definition: dbf.c:341
int dbf_record_logical_is_false(const dbf_record_t *record, const dbf_field_t *field)
Check if a logical value is false.
Definition: dbf.c:516
int dbf_record_int64(const dbf_record_t *record, const dbf_field_t *field, int64_t *value)
Get a 64-bit integer value.
Definition: dbf.c:408
int dbf_record_strtold(const dbf_record_t *record, const dbf_field_t *field, long double *value)
Convert a string to long double representation.
Definition: dbf.c:644
void dbf_record_bytes(const dbf_record_t *record, const dbf_field_t *field, const char **pbytes, size_t *len)
Get bytes.
Definition: dbf.c:314
int dbf_record_strtoul(const dbf_record_t *record, const dbf_field_t *field, int base, unsigned long *value)
Convert a string to an unsigned long integer.
Definition: dbf.c:698
char * dbf_record_strdup(const dbf_record_t *record, const dbf_field_t *field)
Duplicate a string.
Definition: dbf.c:558
int dbf_record_is_null(const dbf_record_t *record, const dbf_field_t *field)
Check if a field is null.
Definition: dbf.c:449
int dbf_record_logical(const dbf_record_t *record, const dbf_field_t *field)
Get a logical value.
Definition: dbf.c:498
int dbf_record_is_deleted(const dbf_record_t *record)
Check if a record is deleted.
Definition: dbf.c:430
int dbf_record_strtol(const dbf_record_t *record, const dbf_field_t *field, int base, long *value)
Convert a string to a long integer.
Definition: dbf.c:617
int dbf_record_strtoull(const dbf_record_t *record, const dbf_field_t *field, int base, unsigned long long *value)
Convert a string to an unsigned long long integer.
Definition: dbf.c:725
int dbf_record_logical_is_true(const dbf_record_t *record, const dbf_field_t *field)
Check if a logical value is true.
Definition: dbf.c:537
int dbf_record_int32(const dbf_record_t *record, const dbf_field_t *field, int32_t *value)
Get a 32-bit integer value.
Definition: dbf.c:386