Functions

Prologue

For these examples, assume the following variables are declared, and that the sample csv data below is populated in a string variable called myData.

// Decalare variables
CSVCore_typ csv;
CSVPredef_typ predef;
timestamp,      position,   velocity,   acceleration
2017-02-20,     0,          0,          0
2017-02-21,     100.57,     10,         6.7
2017-02-22,     267.21,     11.34,      7.40
2017-02-23,     439.17,     15.89,      0.57
2017-02-24,     561.98,     11.51,      -6.3
2017-02-25,     601.74,     3.25,       -3.4

Basic Functionality

csvInitData()

Sets pointer csv data and size of data container. Note: This also resets current row being read or written.

Direction Name Type Description
In pCsv CSVCore_typ* Pointer to CSV core structure
In pData UDINT Pointer to buffer containing CSV data or where the CSV data should be written to
In dataSize UDINT Size of data buffer
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: None.

if(dataLoaded) {
    csvInitData(&csv, &myData, sizeof(myData));
}

csvInitPredef()

Initializes predef. If 0 is passed for any of pColumns, pTypes, pStatus, they will be auto allocated. Specifing number of columns is required and can not be changed for an auto allocated predef.

Direction Name Type Description
In pPredef CSVPredef_typ* Pointer to predef to be initialized
In numColumns UDINT Number of columns the predef will handle
In ?pColumns UDINT Pointer to array of INT with minimum size of numColumns. Array will be allocated if 0 is passed.
In ?pTypes UDINT Pointer to array of CSV_CORE_TYPE_enum with minimum size of numColumns. Array will be allocated if 0 is passed.
In ?pStatus UDINT Pointer to array of CSV_CORE_ERR_enum with minimum size of numColumns. Array will be allocated if 0 is passed.
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: INVALID_INPUT, NUM_COLUMNS.

csvInitPredef(&predef, 3, 0 , 0, 0);

After this fn call predef will be configured to handle 3 columns and have pColumns, pTypes, and pStatus allocated as arrays length 3.

csvFilterByColumn()

Will set predef to read from specified columns. If column is not found that column will be ignored. The number of arguments specified for …index must be equal to numColumns specified in pPredef.

Direction Name Type Description
In pPredef CSVPredef_typ* Pointer to CSV core structure
In …index INT Column index to be read by this predef
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: INVALID_INPUT, NUM_COLUMNS.

csvInitData(&csv, &myData, sizeof(myData));
csvInitPredef(&csv.predef, 3, 0, 0, 0);

csvFilterByColumn(&csv.predef, 1, 2, 3);

In this example csv.predef is configured to read 3 columns: Position, Velocity, and Acceleration. Column timestamp will be ignored.

Note: Function requires CsvCoreAPI.h to be included. See Usage.

csvCastAs()

Sets types columns should be cast to before populating application variable during a read call. The number of arguments specified for …type must be equal to numColumns specified in pPredef.

Direction Name Type Description
In pPredef CSVPredef_typ* Pointer to predef to be initialized
In …type CSV_CORE_TYPE_enum Type column should be cast to
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: INVALID_INPUT, NUM_COLUMNS.

csvInitData(&csv, &myData, sizeof(myData));
csvInitPredef(&csv.predef, 3, 0, 0, 0);
csvFilterByColumn(&csv.predef, 1, 2, 3);

csvCastAs(&csv.predef, CSV_CORE_TYPE_REAL, CSV_CORE_TYPE_REAL, CSV_CORE_TYPE_REAL);

After this code is ran csv.predef is configured to read 3 columns: Position, Velocity, and Acceleration. Then typecast them to REALs.

For strings you can specify the length of the string container to prevent overflows by using CSV_CORE_TYPE_STRINGN instead of CSV_CORE_STRING. See the following example.

STRING timestamp[80];

csvInitPredef(&timestampPredef, 1, 0, 0, 0);
csvFilterByColumn(&timestampPredef, 1, 2, 3);

csvCastAs(&timestampPredef, CSV_CORE_TYPE_STRINGN + sizeof(timestamp));

After this code is ran csv.predef is configured to read 1 column: Timestamp and it will truncate after sizeof(timestamp).

Note: Function requires CsvCoreAPI.h to be included. See Usage.

csvCastAllAs()

Sets all types columns should be cast to before populating application variable during a read call.

Direction Name Type Description
In pPredef CSVPredef_typ* Pointer to predef to be initialized
In type CSV_CORE_TYPE_enum Type columns should be cast to
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: INVALID_INPUT.

csvSetPredef()

Sets contents of pPredef into pCsv->predef. Note this copies the pointers.

csvInitPredef(&predef, 3, 0, 0, 0);
csvFilterByColumn(&csv.predef, 1, 2, 3);

csvSetPredef(&csv, &predef);

Potential errors: INVALID_INPUT.

After this code is ran csv.predef is configured to read 3 columns: Position, Velocity, and Acceleration.

Reading

Read functions will populate status for each column in predef with one the following values: OK, INVALID_INPUT, COLUMN_NOT_FOUND, INVALID_TYPE, BUFFER_FULL, UNSUPPORTED_TYPE, INVALID_VALUE.

csvFilterByHeader()

The number of arguments specified for …name must be equal to numColumns specified in pPredef. The location of the header must be specified prior to calling csvFilterByHeader using csv.cfg.headerRow.

Direction Name Type Description
In pCsv CSVCore_typ* Pointer to CSV core structure
In pPredef CSVPredef_typ* Pointer to predef to be initialized
In …name UDINT Name of column to be used specified by the header
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: INVALID_INPUT, NUM_COLUMNS, HEADER_INDEX, END_OF_DATA.

csvInitData(&csv, &myData, sizeof(myData));
csv.cfg.headerRow = 1;
csvInitPredef(&csv.predef, 3, 0, 0, 0);

csvFilterByHeader(&csv, &csv.predef, "Position", "Velocity", "Acceleration");

In this example csv.predef is configured to read 3 columns: Position, Velocity, and Acceleration. Column timestamp will be ignored.

Note: Function requires CsvCoreAPI.h to be included. See Usage.

csvReadNextRow()

Finds next row then parses it using default predef (csv.predef). The number of arguments provided for …pValue must be equal to the numColumns specified in csv.predef.

Direction Name Type Description
In pCsv CSVCore_typ* Pointer to CSV core structure
In …pValue UDINT Pointer to variable to be populated with value. Typeof variable should be same as specified with csvCastAs fn
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: NO_NEWLINE, INTERNAL, END_OF_DATA.

Note: Function requires CsvCoreAPI.h to be included. See Usage.

csvReadRowAs()

ReParses current row using provided predef. The number of arguments provided for …pValue must be equal to the numColumns specified in predef.

Direction Name Type Description
In pCsv CSVCore_typ* Pointer to CSV core structure
In pPredef CSVPredef_typ* Pointer to predef
In …pValue UDINT Pointer to variable to be populated with value. Typeof variable should be same as specified with csvCastAs fn
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: NO_NEWLINE, INTERNAL, END_OF_DATA.

Note: Function requires CsvCoreAPI.h to be included. See Usage.

csvReadNextRowAs()

Finds next row then parses it using provided predef. The number of arguments provided for …pValue must be equal to the numColumns specified in predef.

Direction Name Type Description
In pCsv CSVCore_typ* Pointer to CSV core structure
In pPredef CSVPredef_typ* Pointer to predef
In …value UDINT Pointer to variable to be populated with value. Typeof variable should be same as specified with csvCastAs fn
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: NO_NEWLINE, INTERNAL, END_OF_DATA.

Note: Function requires CsvCoreAPI.h to be included. See Usage.

Writing

Functions used to build csv data strings. CSV is built with line endings of “\r\n”. Write functions will populate status for each column in predef with one of the following values: OK, INTERNAL, BUFFER_FULL, UNSUPPORTED_TYPE, INVALID_TYPE.

csvBuildRow()

Builds next row using default predef (csv.predef). The number of arguments provided for …value must be equal to the numColumns specified in csv.predef. Strings will be escaped in the following situations:

  • Configuration variable forceEscaping is set to TRUE
  • String contains an escapable character
  • String starts or ends with whitespace

Escapable characters include: ‘, “, \f, \n, \r, \t, \v and configured delimiator

Direction Name Type Description
In pCsv CSVCore_typ* Pointer to CSV core structure
In …pValue UDINT Pointer to variable to be populated with value. Typeof variable should be same as specified with csvCastAs fn
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: BUFFER_FULL.

Note: Function requires CsvCoreAPI.h to be included. See Usage.

csvBuildRowAs()

Builds next row using provided predef. The number of arguments provided for …value must be equal to the numColumns specified in predef. Strings may be escaped, see csvBuildRow function for escaping rules.

Direction Name Type Description
In pCsv CSVCore_typ* Pointer to CSV core structure
In pPredef CSVPredef_typ* Pointer to predef
In …value UDINT Pointer to variable to be populated with value. Typeof variable should be same as specified with csvCastAs fn
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: BUFFER_FULL.

Note: Function requires CsvCoreAPI.h to be included. See Usage.

csvBuildComment()

Builds next row as a comment. Comments containing escapable characters will be escaped but not quoted.

Direction Name Type Description
In pCsv CSVCore_typ* Pointer to CSV core structure
In pComment UDINT Pointer to string containing comment
Return status DINT Function call status. Refers to CSV_CORE_ERR_enum

Potential errors: BUFFER_FULL.

csvBuildCell()

  • pCsv
  • pVar
  • dataType
  • appendEndOfLine

Not implemented

csvAppendRow()

  • pCsv
  • pRow

Not implemented