libgta 1.2.1 Read and Write Generic Tagged Array (GTA) files

Examples written in C: Stream-based input/output
/* This file is in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <gta/gta.h>
/* This example transforms an input GTA into an output GTA.
* It reads, manipulates and writes the array elements one at a time; the array
* data does not have to fit into memory. */
int main(void)
{
FILE *infile, *outfile;
gta_header_t *inheader, *outheader;
gta_io_state_t *instate, *outstate;
gta_result_t r;
void *element;
uintmax_t i;
/* Initialize input */
infile = fopen("input.gta", "r");
if (!infile) {
return 1;
}
r = gta_create_header(&inheader);
if (r != GTA_OK) {
return 1;
}
r = gta_create_io_state(&instate);
if (r != GTA_OK) {
return 1;
}
/* Initialize output */
outfile = fopen("output.gta", "w");
if (!outfile) {
return 1;
}
r = gta_create_header(&outheader);
if (r != GTA_OK) {
return 1;
}
r = gta_create_io_state(&outstate);
if (r != GTA_OK) {
return 1;
}
/* Copy the GTA header */
r = gta_read_header_from_stream(inheader, infile);
if (r != GTA_OK) {
return 1;
}
r = gta_clone_header(outheader, inheader);
if (r != GTA_OK) {
return 1;
}
r = gta_write_header_to_stream(outheader, outfile);
if (r != GTA_OK) {
return 1;
}
/* Copy the array data */
if (gta_get_element_size(inheader) > SIZE_MAX) {
return 1;
}
element = malloc(gta_get_element_size(inheader));
if (!element) {
return 1;
}
for (i = 0; i < gta_get_elements(inheader); i++) {
r = gta_read_elements_from_stream(inheader, instate, 1, element, infile);
if (r != GTA_OK) {
return 1;
}
/* ... manipulate the element ... */
r = gta_write_elements_to_stream(outheader, outstate, 1, element, outfile);
if (r != GTA_OK) {
return 1;
}
}
/* Cleanup */
free(element);
gta_destroy_io_state(instate);
gta_destroy_header(inheader);
fclose(infile);
gta_destroy_io_state(outstate);
gta_destroy_header(outheader);
fclose(outfile);
return 0;
}
gta.h
The libgta C interface.
gta_write_elements_to_stream
gta_result_t gta_write_elements_to_stream(const gta_header_t *restrict header, gta_io_state_t *restrict io_state, uintmax_t n, const void *restrict buf, FILE *restrict f)
Write array elements.
gta_get_element_size
uintmax_t gta_get_element_size(const gta_header_t *restrict header)
Return the size of an array element.
gta_destroy_io_state
void gta_destroy_io_state(gta_io_state_t *restrict io_state)
Destroy input/output state and free its resources.
gta_create_io_state
gta_result_t gta_create_io_state(gta_io_state_t *restrict *restrict io_state)
Create and initialize a new input/output state.
gta_result_t
gta_result_t
GTA result type.
Definition: gta.h:213
GTA_OK
@ GTA_OK
Success / no error.
Definition: gta.h:214
gta_destroy_header
void gta_destroy_header(gta_header_t *restrict header)
Destroy a GTA header structure and free its resources.
gta_io_state_t
struct gta_internal_io_state_struct gta_io_state_t
State for element-based input/output.
Definition: gta.h:353
gta_write_header_to_stream
gta_result_t gta_write_header_to_stream(const gta_header_t *restrict header, FILE *restrict f)
Write a GTA header to a stream.
gta_header_t
struct gta_internal_header_struct gta_header_t
The GTA header type.
Definition: gta.h:193
gta_get_elements
uintmax_t gta_get_elements(const gta_header_t *restrict header)
Get the total number of elements in the array.
gta_create_header
gta_result_t gta_create_header(gta_header_t *restrict *restrict header)
Create a new GTA header structure and initialize it.
gta_read_elements_from_stream
gta_result_t gta_read_elements_from_stream(const gta_header_t *restrict header, gta_io_state_t *restrict io_state, uintmax_t n, void *restrict buf, FILE *restrict f)
Read array elements.
gta_read_header_from_stream
gta_result_t gta_read_header_from_stream(gta_header_t *restrict header, FILE *restrict f)
Read a GTA header from a stream.
gta_clone_header
gta_result_t gta_clone_header(gta_header_t *restrict dst_header, const gta_header_t *restrict src_header)
Clone a GTA header structure.