libsdfio 1.0.0
Reader and writer for ISO 25178-71 SDF surface data files in C
Loading...
Searching...
No Matches
read_stream.c
/* Read an SDF file from a stream row by row. */
#include <sdfio/file.h>
#include <sdfio/sdfio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32)
#include <fcntl.h>
#include <io.h>
#endif
int main(void)
{
#if defined(_WIN32)
/* Windows opens stdin in text mode, which would change the bytes of a
* binary file. */
_setmode(_fileno(stdin), _O_BINARY);
#endif
sdfio_stream stream;
sdfio_stdio_stream(&stream, stdin);
sdfio_reader *reader = NULL;
sdfio_status status = sdfio_open_stream(reader, &stream);
if (status != SDFIO_OK)
{
fprintf(stderr, "%s: %s\n", sdfio_status_string(status),
return EXIT_FAILURE;
}
const sdfio_header *header = sdfio_reader_header(reader);
size_t coded_size = 0;
size_t heights_size = 0;
void *coded = sdfio_data_row_size(header, &coded_size) == SDFIO_OK
? malloc(coded_size)
: NULL;
double *heights =
sdfio_heights_row_size(header, SDFIO_TYPE_DOUBLE, &heights_size)
? malloc(heights_size)
: NULL;
if (coded == NULL || heights == NULL)
{
fprintf(stderr, "a row does not fit in memory\n");
free(coded);
free(heights);
return EXIT_FAILURE;
}
double lowest = INFINITY;
double highest = -INFINITY;
const sdfio_layout coded_layout =
const size_t columns = sdfio_header_columns(header);
const size_t rows = sdfio_header_rows(header);
for (size_t row = 0; row < rows; ++row)
{
status = sdfio_read_data(reader, &coded_layout, coded, 1);
if (status != SDFIO_OK)
{
break;
}
sdfio_decode_heights(header, &heights_layout, coded, columns, heights);
for (size_t column = 0; column < columns; ++column)
{
if (!isnan(heights[column]))
{
lowest = fmin(lowest, heights[column]);
highest = fmax(highest, heights[column]);
}
}
}
if (status != SDFIO_OK)
{
fprintf(stderr, "%s: %s\n", sdfio_status_string(status),
}
else
{
printf("from %g m to %g m\n", lowest, highest);
}
free(coded);
free(heights);
return status == SDFIO_OK ? EXIT_SUCCESS : EXIT_FAILURE;
}
Files by path, and the stream over a C file.
void sdfio_stdio_stream(sdfio_stream *stream, FILE *file)
Creates a stream over a C file.
struct sdfio_header sdfio_header
Header of a file, with the field names of the standard.
Definition header.h:42
sdfio_data_type sdfio_header_data_type(const sdfio_header *header)
Returns the data type of the coded values in the data area.
uint32_t sdfio_header_rows(const sdfio_header *header)
Returns the number of rows N of the data matrix: 1 for a profile.
uint32_t sdfio_header_columns(const sdfio_header *header)
Returns the number of columns M of the data matrix: the points along x.
sdfio_status sdfio_decode_heights(const sdfio_header *header, const sdfio_layout *layout, const void *data, size_t count, void *heights)
Converts values of the data area into heights in metres.
const sdfio_layout * sdfio_layout_packed(sdfio_data_type type)
Returns the layout of a plain array: the elements follow each other and the rows follow each other.
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_reader_add_binary(sdfio_reader *reader)
Adds the binary format to the formats that the reader reads, the version numbers bISO-1....
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_open_stream(sdfio_reader *reader, const sdfio_stream *stream)
Opens a stream as the source and reads the header.
struct sdfio_reader sdfio_reader
Opaque handle of a reader.
Definition reader.h:58
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....
sdfio_status sdfio_reader_create(const sdfio_allocator *allocator, sdfio_reader **reader)
Creates a reader.
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.
void sdfio_reader_free(sdfio_reader *reader)
Releases a reader and closes the file that sdfio_open_file() opened, if any.
Reading and writing of ISO 25178-71 SDF surface data files.
sdfio_status sdfio_heights_row_size(const sdfio_header *header, sdfio_data_type type, size_t *bytes)
Returns the size in bytes of a buffer for one row of heights in a type, for sdfio_decode_heights() an...
sdfio_status sdfio_data_row_size(const sdfio_header *header, size_t *bytes)
Returns the size in bytes of a buffer for one row of the data area, to read or write the data row by ...
Description of a buffer of the caller for heights or for the values of the data area: the type of its...
Definition layout.h:45
Callbacks that provide or accept the bytes of a file.
Definition types.h:213
const char * sdfio_status_string(sdfio_status status)
Returns a constant description of a status.
sdfio_status
Result of a function call.
Definition types.h:31
@ SDFIO_OK
The call succeeded.
Definition types.h:33
const sdfio_allocator * sdfio_default_allocator(void)
Returns the allocator that uses malloc() and free().
@ SDFIO_TYPE_DOUBLE
double: IEEE 754 binary64.
Definition types.h:122