C DataIO API¶
Defines
-
ASCIIFILE_INCLUDED¶ Flag for checking if AsciiFile.h has already been included.
-
LINE_SIZE_MAX¶ Maximum line size.
Typedefs
-
typedef struct asciiFile_t
asciiFile_t¶ Structure containing information about an ASCII text file.
Functions
-
static int
af_is_open(const asciiFile_t t)¶ Determine if the file is open.
- Return
- int 1 if open, 0 if closed.
- Parameters
t: constant asciiFile_t file structure.
-
static int
af_open(asciiFile_t *t)¶ Open the file.
- Return
- int 0 if opened successfully, -1 otherwise.
- Parameters
t: constant asciiFile_t file structure.
-
static int
af_close(asciiFile_t *t)¶ Close the file.
- Return
- int 0 if closed successfully, -1 otherwise.
- Parameters
t: constant asciiFile_t file structure.
-
static int
af_is_comment(const asciiFile_t t, const char *line)¶ Check if string starts with a comment.
- Return
- int 1 if line starts with a comment, 0 otherwise.
- Parameters
t: constant asciiFile_t file structure.line: constant character pointer to string that should be checked.
-
static int
af_readline_full(const asciiFile_t t, char **line, size_t *n)¶ Read a single line from the file.
- Return
- int On success, the number of characters read, -1 on failure.
- Parameters
t: constant asciiFile_t file structure.line: constant character pointer to pointer to buffer where the read line should be stored. If line is not large enough to hold the read line, it will be reallocated.n: Pointer to size of allocated buffer. If line is not large enough to hold the read line and is reallocated, n will be changed to the new size.
-
static int
af_writeline_full(const asciiFile_t t, const char *line)¶ Write a single line to the file.
- Return
- int On success, the number of characters written, -1 on failure.
- Parameters
t: constant asciiFile_t file structure.line: constant character pointer to string that should be written.
-
static asciiFile_t
asciiFile(const char *filepath, const char *io_mode, const char *comment, const char *newline)¶ Constructor for asciiFile_t structure.
- Return
- asciiFile_t File structure.
- Parameters
filepath: constant character pointer to file path.io_mode: const character pointer to I/O mode. “r” for read, “w” for write.comment: const character pointer to character(s) that should indicate a comment. If NULL, comment is set to “# ”.newline: const character pointer to character(s) that should indicate a newline. If NULL, newline is set to “\n”.
-
struct
asciiFile_t¶ - #include <AsciiFile.h>
Structure containing information about an ASCII text file.
Typedefs
-
typedef struct asciiTable_t
asciiTable_t¶ Structure containing information about an ASCII table.
Enums
Functions
-
static int
compile_regex(regex_t *r, const char *regex_text)¶ Create a regex from a character array. Adapted from https://www.lemoda.net/c/unix-regex/.
- Return
- static int Success or failure of compilation.
- Parameters
r: pointer to regex_t. Resutling regex expression.regex_text: constant character pointer to text that should be compiled.
-
static int
count_matches(const char *regex_text, const char *to_match)¶ Count the number of times a regular expression is matched in a string.
- Return
- int Number of matches found. -1 is returned if the regex could not be compiled.
- Parameters
regex_text: constant character pointer to string that should be compiled into a regex.to_match: constant character pointer to string that should be checked for matches.
-
static int
count_formats(const char *fmt_str)¶ Count how many % format specifiers there are in format string. Formats are found by counting the number of matches to the regular expression adapted from https://stackoverflow.com/questions/446285/validate-sprintf-format-from-input-field-with-regex.
- Return
- int Number of format specifiers found.
- Parameters
fmt_str: constant character pointer to string that should be searched for format specifiers.
-
static int
at_open(asciiTable_t *t)¶ Open the file.
- Return
- int 0 if opened successfully, -1 otherwise.
- Parameters
t: asciiTable_t table structure.
-
static void
at_close(asciiTable_t *t)¶ Close the file.
- Return
- int 0 if ocloseded successfully, -1 otherwise.
- Parameters
t: asciiTable_t table structure.
-
static int
at_vreadline(const asciiTable_t t, va_list ap)¶ Read a line from the file and parse it.
- Return
- int On success, the number of characters read. -1 on failure.
- Parameters
t: constant asciiTable_t table structure.ap: va_list Pointers to variables where parsed arguments should be stored.
-
static int
at_vwriteline(const asciiTable_t t, va_list ap)¶ Format arguments to form a line and write it to the file.
- Return
- int On success, the number of characters written. -1 on failure.
- Parameters
t: constant asciiTable_t table structure.ap: va_list Variables that should be formatted using the format string to create a line in the table.
-
static int
at_readline(const asciiTable_t t, ...)¶ Read a line from the file and parse it.
- Return
- int On success, the number of characters read. -1 on failure.
- Parameters
t: constant asciiTable_t table structure....: Pointers to variables where parsed arguments should be stored.
-
static int
at_writeline(const asciiTable_t t, ...)¶ Format arguments to form a line and write it to the file.
- Return
- int On success, the number of characters written. -1 on failure.
- Parameters
t: constant asciiTable_t table structure....: Variables that should be formatted using the format string to create a line in the table.
-
static int
at_writeformat(const asciiTable_t t)¶ Write the format string the the file, prepending it with a comment.
- Return
- int On success, the number of characters written. -1 on failure.
- Parameters
t: constant asciiTable_t table structure.
-
static int
at_discover_format_str(asciiTable_t *t)¶ Try to find the format string in the file. The format string is assumed to start with a comment.
- Return
- 0 on success, -1 on failure.
- Parameters
t: constant asciiTable_t table structure.
-
static int
at_set_ncols(asciiTable_t *t)¶ Set the number of columns by counting the format specifiers.
- Return
- int The number of columns counted. Negative values indicate errors.
- Parameters
t: constant asciiTable_t table structure.
-
static int
at_set_format_typ(asciiTable_t *t)¶ Determine the column types by parsing the format string.
- Return
- int 0 on success, -1 on failure. TODO: switch to regex
- Parameters
t: constant asciiTable_t table structure.
-
static int
at_vbytes_to_array(const asciiTable_t t, const char *data, const int data_siz, va_list ap)¶ Convert data into arrays for columns.
- Return
- int Number of rows read on success, -1 on failure.
- Parameters
t: constant asciiTable_t table structure.data: constant character pointer to memory containing data that should be parsed.data_siz: constant int Size of data in bytes.ap: va_list Pointers to pointers to memory where columns should be stored.
-
static int
at_varray_to_bytes(const asciiTable_t t, char **data, int nrows, va_list ap)¶ Encode a set of arrays as bytes.
- Parameters
t: constant asciiTable_t table structure.data: Pointer to pointer to memory where encoded arrays should be stored. It does not need to be allocate, only declared.nrows: int Number of rows in each column array.ap: va_list Pointers to memory where column data is stored.
-
static int
at_bytes_to_array(const asciiTable_t t, char *data, int data_siz, ...)¶ Convert data into arrays for columns.
- Return
- int Number of rows read on success, -1 on failure.
- Parameters
t: constant asciiTable_t table structure.data: constant character pointer to memory containing data that should be parsed.data_siz: constant int Size of data in bytes....: Pointers to pointers to memory where columns should be stored.
-
static int
at_array_to_bytes(const asciiTable_t t, char **data, int nrows, ...)¶ Encode a set of arrays as bytes.
- Parameters
t: constant asciiTable_t table structure.data: Pointer to pointer to memory where encoded arrays should be stored. It does not need to be allocate, only declared.nrows: int Number of rows in each column array....: Pointers to memory where column data is stored.
-
static void
at_cleanup(asciiTable_t *t)¶ Deallocate and clean up asciiTable_t structure.
- Parameters
t: asciiTable_t table structure.
-
static asciiTable_t
asciiTable(const char *filepath, const char *io_mode, const char *format_str, const char *comment, const char *column, const char *newline)¶ Constructor for asciiTable_t structure.
- Return
- asciiTable_t table structure.
- Parameters
filepath: constant character pointer to file path.io_mode: constant character pointer to I/O mode. “r” for read, “w” for write.format_str: constant character pointer to string describing the format of the table roads. Required for io_mode == “w”, but if set to NULL for io_mode == “r”, it will attempt to be read from the table.comment: const character pointer to character(s) that should indicate a comment. If NULL, comment is set to “# ”.column: const character pointer to character(s) that should separate columns in the table. If NULL, column is set to “\t”.newline: const character pointer to character(s) that should indicate a newline. If NULL, newline is set to “\n”.
-
struct
asciiTable_t¶ - #include <AsciiTable.h>
Structure containing information about an ASCII table.
Public Members
-
asciiFile_t
f¶ ASCII file structure.
-
char
format_str[LINE_SIZE_MAX]¶ Format string for rows.
-
char
column[64]¶ Character(s) used to seperate columns.
-
int
ncols¶ Number of columns in the table.
-
int *
format_typ¶ Array of ncols integers specifying column types.
-
int *
format_siz¶ Array of ncols sizes for elements in each column.
-
int
row_siz¶ Size of an entire row in bytes.
-
int
status¶ Negative if format_str has not been set yet.
-
asciiFile_t