CSV-Filereader

Supports reading a CSV text file.

Summary
CSV-FilereaderSupports reading a CSV text file.
CopyrightThis program is free software.
Files
C-kern/api/io/reader/csvfilereader.hHeader file CSV-Filereader.
C-kern/io/reader/csvfilereader.cImplementation file CSV-Filereader impl.
Types
struct csvfilereader_tExport csvfilereader_t into global namespace.
Functions
test
unittest_io_reader_csvfilereaderTest csvfilereader_t functionality.
csvfilereader_tReads a CSV file and allows to access the values.
fileThe read file content mapped to memory.
nrcolumnsThe number of columns (Data fields) per row.
nrrowsThe number of rows of data.
allocated_rowsThe number of allocated rows of tablevalues.
tablevaluesTable of strings indexing memory mapped file.
lifetime
csvfilereader_FREEStatic initializer.
init_csvfilereaderOpens file and reads all contained values.
free_csvfilereaderCloses file and frees memory for parsed values.
query
nrcolumns_csvfilereaderThe number of columns (data fields) per row contained in the input data.
nrrows_csvfilereaderThe number of rows (data records) contained in the input data.
colname_csvfilereaderThe name of a column.
colvalue_csvfilereaderReturns the value of a single column in a certain row.
description
CSV-File-FormatIt is a file format for storing tabular data as plain text.
inline implementation
Macros
colname_csvfilereaderImplements csvfilereader_t.colname_csvfilereader.
nrcolumns_csvfilereaderImplements csvfilereader_t.nrcolumns_csvfilereader.
nrrows_csvfilereaderImplements csvfilereader_t.nrrows_csvfilereader.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2012 Jörg Seebohn

Files

C-kern/api/io/reader/csvfilereader.h

Header file CSV-Filereader.

C-kern/io/reader/csvfilereader.c

Implementation file CSV-Filereader impl.

Types

struct csvfilereader_t

typedef struct csvfilereader_t csvfilereader_t

Export csvfilereader_t into global namespace.

Functions

test

unittest_io_reader_csvfilereader

int unittest_io_reader_csvfilereader(void)

Test csvfilereader_t functionality.

csvfilereader_t

struct csvfilereader_t

Reads a CSV file and allows to access the values.  The file is accessed with help of mmfile_t.

CSV File Format

See CSV-File-Format.

Summary
fileThe read file content mapped to memory.
nrcolumnsThe number of columns (Data fields) per row.
nrrowsThe number of rows of data.
allocated_rowsThe number of allocated rows of tablevalues.
tablevaluesTable of strings indexing memory mapped file.
lifetime
csvfilereader_FREEStatic initializer.
init_csvfilereaderOpens file and reads all contained values.
free_csvfilereaderCloses file and frees memory for parsed values.
query
nrcolumns_csvfilereaderThe number of columns (data fields) per row contained in the input data.
nrrows_csvfilereaderThe number of rows (data records) contained in the input data.
colname_csvfilereaderThe name of a column.
colvalue_csvfilereaderReturns the value of a single column in a certain row.
description
CSV-File-FormatIt is a file format for storing tabular data as plain text.

file

mmfile_t file

The read file content mapped to memory.

nrcolumns

size_t nrcolumns

The number of columns (Data fields) per row.

nrrows

size_t nrrows

The number of rows of data.

allocated_rows

size_t allocated_rows

The number of allocated rows of tablevalues.

tablevalues

struct string_t * tablevalues/*[nrrows][nrcolumns]*/

Table of strings indexing memory mapped file.  The allocated table size is determined by allocated_rows and nrcolumns.  The valid values are determined by nrrows and nrcolumns.

lifetime

csvfilereader_FREE

#define csvfilereader_FREE { mmfile_FREE, 0, 0, 0, 0 }

Static initializer.

init_csvfilereader

int init_csvfilereader(/*out*/csvfilereader_t *csvfile,
const char *filepath)

Opens file and reads all contained values.  All rows must have the same number of columns.

TODO: Return error information in extra struct ! Genral Parsing Error Framework ?

free_csvfilereader

int free_csvfilereader(csvfilereader_t *csvfile)

Closes file and frees memory for parsed values.

query

nrcolumns_csvfilereader

size_t nrcolumns_csvfilereader(const csvfilereader_t *csvfile)

The number of columns (data fields) per row contained in the input data.

nrrows_csvfilereader

size_t nrrows_csvfilereader(const csvfilereader_t *csvfile)

The number of rows (data records) contained in the input data.

colname_csvfilereader

int colname_csvfilereader(const csvfilereader_t *csvfile,
size_t column/*0..nrcolumns-1*/,
/*out*/struct string_t *colname)

The name of a column.  This name is defined in the first row of data and is the same for all following rows.

colvalue_csvfilereader

int colvalue_csvfilereader(const csvfilereader_t *csvfile,
size_t row/*1..nrrows-1*/,
size_t column/*0..nrcolumns-1*/,
/*out*/struct string_t *colvalue)

Returns the value of a single column in a certain row.  The index of the columns a value between 0 and nrcolumns_csvfilereader-1.  The index of the row is a value between 1 and nrrows_csvfilereader-1.  A row index of 0 is the same as calling colname_csvfilereader.

description

CSV-File-Format

It is a file format for storing tabular data as plain text.  A text line contains one data row (record) they are separated by new lines (‘\n’).  Any additional carriage return ‘\r’ is ignored.  Every row contains the same number of columns (data fields).  A single value in a row is separated by comma from another value.  It must be enclosed in double quotes.

Example

# The first line contains the column names
"filename",        "proglang",    "authors"
# The following lines defines the data
"test_file_1.cpp", "C++",         "MR. X1, MR. X2"
"test_file_2.c",   "C",           "MR. Y"
"test_file_3.sh",  "shell",       "MR. Z"

A double quote or new line character as part of a value is not supported.  It must be represented by an escape sequence of a higher level abstraction like “\x22” or “\042” used in C-strings.

First Row

The values of the first row are considered to be the header names of the corresponding column.

Comments

Lines beginning with a ‘#’ are recognized as comments and are ignored.

Character Encoding

The reader assumes that comma, white space and double quotes are encoded in ASCII.  UTF-8 is supported or other code pages if comma, white space and double quotes uses the same codes as in ASCII.

Macros

colname_csvfilereader

#define colname_csvfilereader(
   csvfile,
   column,
   colname
) (colvalue_csvfilereader((csvfile), 0, (column), (colname)))

Implements csvfilereader_t.colname_csvfilereader.

nrcolumns_csvfilereader

#define nrcolumns_csvfilereader(csvfile) ((csvfile)->nrcolumns)

Implements csvfilereader_t.nrcolumns_csvfilereader.

nrrows_csvfilereader

#define nrrows_csvfilereader(csvfile) ((csvfile)->nrrows)

Implements csvfilereader_t.nrrows_csvfilereader.

Supports reading a CSV text file.
Implements CSV-Filereader.
typedef struct csvfilereader_t csvfilereader_t
Export csvfilereader_t into global namespace.
struct csvfilereader_t
Reads a CSV file and allows to access the values.
int unittest_io_reader_csvfilereader(void)
Test csvfilereader_t functionality.
mmfile_t file
The read file content mapped to memory.
size_t nrcolumns
The number of columns (Data fields) per row.
size_t nrrows
The number of rows of data.
size_t allocated_rows
The number of allocated rows of tablevalues.
struct string_t * tablevalues/*[nrrows][nrcolumns]*/
Table of strings indexing memory mapped file.
#define csvfilereader_FREE { mmfile_FREE, 0, 0, 0, 0 }
Static initializer.
int init_csvfilereader(/*out*/csvfilereader_t *csvfile,
const char *filepath)
Opens file and reads all contained values.
int free_csvfilereader(csvfilereader_t *csvfile)
Closes file and frees memory for parsed values.
size_t nrcolumns_csvfilereader(const csvfilereader_t *csvfile)
The number of columns (data fields) per row contained in the input data.
size_t nrrows_csvfilereader(const csvfilereader_t *csvfile)
The number of rows (data records) contained in the input data.
int colname_csvfilereader(const csvfilereader_t *csvfile,
size_t column/*0..nrcolumns-1*/,
/*out*/struct string_t *colname)
The name of a column.
int colvalue_csvfilereader(const csvfilereader_t *csvfile,
size_t row/*1..nrrows-1*/,
size_t column/*0..nrcolumns-1*/,
/*out*/struct string_t *colvalue)
Returns the value of a single column in a certain row.
#define colname_csvfilereader(
   csvfile,
   column,
   colname
) (colvalue_csvfilereader((csvfile), 0, (column), (colname)))
Implements csvfilereader_t.colname_csvfilereader.
#define nrcolumns_csvfilereader(csvfile) ((csvfile)->nrcolumns)
Implements csvfilereader_t.nrcolumns_csvfilereader.
#define nrrows_csvfilereader(csvfile) ((csvfile)->nrrows)
Implements csvfilereader_t.nrrows_csvfilereader.
It is a file format for storing tabular data as plain text.
Close