libsdfio 1.0.0
Reader and writer for ISO 25178-71 SDF surface data files in C
Loading...
Searching...
No Matches
reader.h File Reference

Reader of a file, memory or a stream. More...

#include <sdfio/header.h>
#include <sdfio/layout.h>
#include <sdfio/sdfio_export.h>
#include <sdfio/types.h>
#include <stddef.h>

Go to the source code of this file.

Typedefs

typedef struct sdfio_reader sdfio_reader
 Opaque handle of a reader.

Functions

sdfio_status sdfio_reader_create (const sdfio_allocator *allocator, sdfio_reader **reader)
 Creates a reader.
sdfio_status sdfio_reader_add_binary (sdfio_reader *reader)
 Adds the binary format to the formats that the reader reads, the version numbers bISO-1.0 and bISO-2.0.
sdfio_status sdfio_reader_add_ascii (sdfio_reader *reader)
 Adds the ASCII format to the formats that the reader reads, the version numbers aISO-1.0 and aISO-2.0.
sdfio_status sdfio_reader_set_buffer (sdfio_reader *reader, size_t size)
 Sets the size in bytes of the buffer between the reader and its stream.
sdfio_status sdfio_reader_set_chunk (sdfio_reader *reader, size_t count)
 Sets the number of values that the reader converts at a time.
sdfio_status sdfio_reader_set_time_zone (sdfio_reader *reader, const sdfio_time_zone *time_zone)
 Sets the conversion of the dates of ISO-1.0 files.
size_t sdfio_reader_magic_count (const sdfio_reader *reader)
 Returns the number of magics that a file can start with for the formats that the reader adds: two for each format, one for each revision.
const char * sdfio_reader_magic (const sdfio_reader *reader, size_t index)
 Returns a magic that a file can start with, for example "bISO-2.0": the letter b or a for the binary or the ASCII format, and the revision.
sdfio_status sdfio_reader_reset (sdfio_reader *reader)
 Returns a reader to the state new, so that it can open another file, block of memory or stream.
void sdfio_reader_free (sdfio_reader *reader)
 Releases a reader and closes the file that sdfio_open_file() opened, if any.
sdfio_status sdfio_open_memory (sdfio_reader *reader, const void *data, size_t size)
 Opens a block of memory as the source and reads the header.
sdfio_status sdfio_open_stream (sdfio_reader *reader, const sdfio_stream *stream)
 Opens a stream as the source and reads the header.
const sdfio_header * sdfio_reader_header (const sdfio_reader *reader)
 Returns the header of the opened file, or null if no file is open.
sdfio_status sdfio_read_data (sdfio_reader *reader, const sdfio_layout *layout, void *dst, size_t rows)
 Reads the next rows of the data area as the file codes them.
sdfio_status sdfio_read_heights (sdfio_reader *reader, const sdfio_layout *layout, void *dst)
 Reads the remaining rows as heights in metres into a buffer that lies in memory as the layout describes.
sdfio_status sdfio_read_trailer (sdfio_reader *reader, char *buffer, size_t capacity, size_t *size)
 Reads the next part of the trailer, the free text after the data area, into a buffer of the caller.
const char * sdfio_reader_error_message (const sdfio_reader *reader)
 Returns a description of the most recent failure of the reader, or an empty string if none occurred.

Detailed Description

Reader of a file, memory or a stream.

Process
A reader reads one file at a time, from the start to the end:
  1. sdfio_reader_create() creates the reader.
  2. sdfio_reader_add_binary() and sdfio_reader_add_ascii() add the formats that it reads, at least one. sdfio_reader_set_buffer(), sdfio_reader_set_chunk() and sdfio_reader_set_time_zone() set the options.
  3. sdfio_open_file(), sdfio_open_memory() or sdfio_open_stream() opens the file and reads its header, which sdfio_reader_header() returns.
  4. sdfio_read_heights() reads the whole data area, or sdfio_read_data() reads it in parts of one row or more, in the order of the file.
  5. sdfio_read_trailer() reads the trailer, in parts, until it returns 0 bytes.
  6. sdfio_reader_free() releases the reader. sdfio_reader_reset() instead returns it to the state new, so that it can open another source.
States
A reader is in one of four states. It is new until a file is opened, then in the state data. The first call of sdfio_read_trailer() moves it to trailer, and the rows that were not read are discarded. An error that loses the position in the stream moves it to failed: SDFIO_ERR_IO, SDFIO_ERR_FORMAT, or SDFIO_ERR_RANGE in a call that reads. The header stays available in every state after the opening.
  • Formats and options can be set in the state new only. The first attempt to open a file fixes them, also after sdfio_reader_reset().
  • A call that the state does not allow returns SDFIO_ERR_STATE, and an invalid argument returns SDFIO_ERR_ARGUMENT. Neither changes the state or the position in the file.
  • An opening that fails leaves the reader new, so it can open another file.

Function Documentation

◆ sdfio_open_memory()

sdfio_status sdfio_open_memory ( sdfio_reader * reader,
const void * data,
size_t size )

Opens a block of memory as the source and reads the header.

The memory is not copied: it must remain valid and unchanged while the reader is in use.

Parameters
[in,out]readerThe reader.
[in]dataThe first byte of the file.
sizeThe size of the file in bytes.
Return values
SDFIO_ERR_ARGUMENTA pointer is null.
SDFIO_ERR_STATEThe reader is open already.
SDFIO_ERR_FORMATThe data is not an SDF file or is truncated.
SDFIO_ERR_UNSUPPORTEDThe reader format of the file has not been added.
SDFIO_ERR_MEMORYAn allocation failed.

◆ sdfio_open_stream()

sdfio_status sdfio_open_stream ( sdfio_reader * reader,
const sdfio_stream * stream )

Opens a stream as the source and reads the header.

The structure is copied, and the callbacks and the object that user refers to must remain valid while the reader is in use. The stream is read once, from the start.

Parameters
[in,out]readerThe reader.
[in]streamThe stream; read is required.
Return values
SDFIO_ERR_ARGUMENTA pointer is null or read is not set.
SDFIO_ERR_STATEThe reader is open already.
SDFIO_ERR_IOThe stream failed.
SDFIO_ERR_FORMATThe data is not an SDF file or is truncated.
SDFIO_ERR_UNSUPPORTEDThe reader format of the file has not been added.
SDFIO_ERR_MEMORYAn allocation failed.
Examples
read_stream.c.

◆ sdfio_read_data()

sdfio_status sdfio_read_data ( sdfio_reader * reader,
const sdfio_layout * layout,
void * dst,
size_t rows )

Reads the next rows of the data area as the file codes them.

dst receives rows times header->columns coded values of header->data_type in the byte order of the machine, in the order of the file. The bad value is not changed. ASCII values are parsed into the data type of the file, so that an ASCII file of float values is rounded to single precision. Use sdfio_decode_heights() to obtain heights in metres.

The buffer lies in memory as the layout describes, whose type has to be the data type of the file: the element (y, x) is stored at dst + y * layout.row_stride + x * layout.element_stride, where y counts the rows from the first row that is read. sdfio_layout_packed() describes values that follow each other. The bad value of the data type marks a point that is not measured.

The first row read is the first row stored in the file, the row with y = 0.

After an error the reader rejects further reads.

Parameters
[in,out]readerThe reader.
[in]layoutThe layout of the buffer.
[out]dstThe buffer for the coded values.
rowsThe number of rows to read.
Return values
SDFIO_ERR_ARGUMENTA pointer is null, rows exceeds the rows remaining, the type of the layout is not the data type of the file, or the strides are invalid.
SDFIO_ERR_STATENo file is open, or an earlier error ended the reading.
SDFIO_ERR_IOThe stream failed.
SDFIO_ERR_FORMATThe data area is damaged or truncated.
Examples
read_coded.c, and read_stream.c.

◆ sdfio_read_heights()

sdfio_status sdfio_read_heights ( sdfio_reader * reader,
const sdfio_layout * layout,
void * dst )

Reads the remaining rows as heights in metres into a buffer that lies in memory as the layout describes.

sdfio_layout_packed() describes a plain array.

The element (y, x) is stored at dst + y * layout.row_stride + x * layout.element_stride. The row y = 0 is the first row. A negative row stride, with dst at the first element of the row y = 0, which is the last row in memory, stores the matrix upside down. A point that is not measured becomes NaN.

The function reads from the first row of the data area, so no rows may have been read before.

Parameters
[in,out]readerThe reader.
[in]layoutThe layout of the buffer.
[out]dstThe buffer for the heights.
Return values
SDFIO_ERR_ARGUMENTA pointer is null or the strides are invalid.
SDFIO_ERR_STATENo file is open, an earlier error ended the reading, or rows were read already.
SDFIO_ERR_RANGEA height does not fit the type of the buffer.
SDFIO_ERR_IOThe stream failed.
SDFIO_ERR_FORMATThe data area is damaged or truncated.
Examples
eigen.cpp, opencv.cpp, read_heights.c, and vtk.cpp.

◆ sdfio_read_trailer()

sdfio_status sdfio_read_trailer ( sdfio_reader * reader,
char * buffer,
size_t capacity,
size_t * size )

Reads the next part of the trailer, the free text after the data area, into a buffer of the caller.

The call writes at most capacity bytes. A trailer that is longer than the buffer is read by calling the function again, and *size is 0 when the trailer is at its end, also for a file without one. The text is bytes, not terminated, and its encoding is not checked: the standard asks for 7-bit ASCII. Rows of the data area that have not been read are read and discarded by the first call.

Every line end of the text is LF, whether the file has CRLF or LF, in ASCII and in binary files. Nothing else of the text changes: the white space before it and the line ends at its end are part of it. In an ASCII file the trailer ends at the line that consists of *.

Parameters
[in,out]readerThe reader.
[out]bufferReceives the text.
capacityThe size of buffer in bytes, at least 1.
[out]sizeReceives the number of bytes written, 0 at the end of the trailer.
Return values
SDFIO_ERR_ARGUMENTA pointer is null or the capacity is 0.
SDFIO_ERR_STATENo file is open, or an earlier error ended the reading.
SDFIO_ERR_IOThe stream failed.
SDFIO_ERR_FORMATThe file is damaged or truncated.
Examples
read_heights.c.

◆ sdfio_reader_add_ascii()

sdfio_status sdfio_reader_add_ascii ( sdfio_reader * reader)

Adds the ASCII format to the formats that the reader reads, the version numbers aISO-1.0 and aISO-2.0.

The rules of sdfio_reader_add_binary() apply.

Parameters
[in,out]readerThe reader.
Return values
SDFIO_ERR_ARGUMENTThe reader is null or has added the format already.
SDFIO_ERR_STATEA file opening was attempted earlier.
Examples
custom_allocator.c, eigen.cpp, opencv.cpp, read_heights.c, read_stream.c, and vtk.cpp.

◆ sdfio_reader_add_binary()

sdfio_status sdfio_reader_add_binary ( sdfio_reader * reader)

Adds the binary format to the formats that the reader reads, the version numbers bISO-1.0 and bISO-2.0.

A reader needs at least one format before a file is opened. The formats are tried in the order in which they are added, and sdfio_reader_magic() lists their magics in that order.

Like the other options of a reader, a format is added before the first attempt to open a file.

Parameters
[in,out]readerThe reader.
Return values
SDFIO_ERR_ARGUMENTThe reader is null or has added the format already.
SDFIO_ERR_STATEA file opening was attempted earlier.
Examples
custom_allocator.c, custom_timezone.c, eigen.cpp, opencv.cpp, read_coded.c, read_heights.c, read_stream.c, and vtk.cpp.

◆ sdfio_reader_create()

sdfio_status sdfio_reader_create ( const sdfio_allocator * allocator,
sdfio_reader ** reader )

Creates a reader.

The reader reads nothing until a source is opened.

Only the handle is allocated here. The formats are added with the sdfio_reader_add_* functions, the options are optional, and the buffers are allocated when a file is opened. Release the reader with sdfio_reader_free().

Parameters
[in]allocatorThe allocator for all memory of the reader, for example sdfio_default_allocator(). The structure is copied, so it need not outlive the call. Both callbacks must be set.
[out]readerReceives the reader, or null if the call fails.
Return values
SDFIO_ERR_ARGUMENTA pointer is null, or the allocator lacks a callback.
SDFIO_ERR_MEMORYAn allocation failed.
Examples
custom_allocator.c, custom_timezone.c, eigen.cpp, opencv.cpp, read_coded.c, read_heights.c, read_stream.c, and vtk.cpp.

◆ sdfio_reader_error_message()

const char * sdfio_reader_error_message ( const sdfio_reader * reader)

Returns a description of the most recent failure of the reader, or an empty string if none occurred.

The result is never null, and a string of the library, valid for the life of the program.

Parameters
[in]readerThe reader.
Examples
custom_allocator.c, eigen.cpp, opencv.cpp, read_coded.c, read_heights.c, read_stream.c, and vtk.cpp.

◆ sdfio_reader_free()

void sdfio_reader_free ( sdfio_reader * reader)

Releases a reader and closes the file that sdfio_open_file() opened, if any.

A null pointer is ignored.

Parameters
[in,out]readerThe reader.
Examples
custom_allocator.c, custom_timezone.c, eigen.cpp, opencv.cpp, read_coded.c, read_heights.c, read_stream.c, and vtk.cpp.

◆ sdfio_reader_header()

const sdfio_header * sdfio_reader_header ( const sdfio_reader * reader)

Returns the header of the opened file, or null if no file is open.

The pointer remains valid until the reader is freed.

Parameters
[in]readerThe reader.
Examples
custom_allocator.c, custom_timezone.c, eigen.cpp, opencv.cpp, read_coded.c, read_heights.c, read_stream.c, and vtk.cpp.

◆ sdfio_reader_magic()

const char * sdfio_reader_magic ( const sdfio_reader * reader,
size_t index )

Returns a magic that a file can start with, for example "bISO-2.0": the letter b or a for the binary or the ASCII format, and the revision.

The text is terminated by a null character. The first bytes of a file can be compared with each magic to find out whether the reader reads it.

Parameters
[in]readerThe reader.
indexA number below sdfio_reader_magic_count().
Returns
The magic, or null if the index is not below the count.

◆ sdfio_reader_magic_count()

size_t sdfio_reader_magic_count ( const sdfio_reader * reader)

Returns the number of magics that a file can start with for the formats that the reader adds: two for each format, one for each revision.

Parameters
[in]readerThe reader. May be null, which has no formats.

◆ sdfio_reader_reset()

sdfio_status sdfio_reader_reset ( sdfio_reader * reader)

Returns a reader to the state new, so that it can open another file, block of memory or stream.

A file that sdfio_open_file() opened is closed, and the header and the position in the data are dropped. The formats, the options and the buffers stay. The function works in every state, also after an error.

Parameters
[in,out]readerThe reader.
Return values
SDFIO_ERR_ARGUMENTThe reader is null.

◆ sdfio_reader_set_buffer()

sdfio_status sdfio_reader_set_buffer ( sdfio_reader * reader,
size_t size )

Sets the size in bytes of the buffer between the reader and its stream.

The default is 16384. A value below 64 is raised to 64. A smaller buffer causes more calls of the stream callbacks.

Parameters
[in,out]readerThe reader.
sizeThe size, not 0.
Return values
SDFIO_ERR_ARGUMENTThe reader is null or the size is 0.
SDFIO_ERR_STATEA file opening was attempted earlier.

◆ sdfio_reader_set_chunk()

sdfio_status sdfio_reader_set_chunk ( sdfio_reader * reader,
size_t count )

Sets the number of values that the reader converts at a time.

The default is

  1. The chunk occupies up to 8 bytes per value.
Parameters
[in,out]readerThe reader.
countThe number of values, not 0 and not so large that the chunk cannot be counted in bytes.
Return values
SDFIO_ERR_ARGUMENTThe reader is null or the number is not valid.
SDFIO_ERR_STATEA file opening was attempted earlier.

◆ sdfio_reader_set_time_zone()

sdfio_status sdfio_reader_set_time_zone ( sdfio_reader * reader,
const sdfio_time_zone * time_zone )

Sets the conversion of the dates of ISO-1.0 files.

The structure is copied, so it need not outlive the call. Null selects no conversion, which is the default.

Parameters
[in,out]readerThe reader.
[in]time_zoneThe time zone, or null.
Return values
SDFIO_ERR_ARGUMENTThe reader is null.
SDFIO_ERR_STATEA file opening was attempted earlier.
Examples
custom_timezone.c.