CSV Util: csv.h File Reference

# csv.h File Reference

Provides utilities for loading and saving CSV spreadsheet files. More...

Go to the source code of this file.

## Data Structures
struct | CsvTable
| The standard structure used to contain all spreadsheet data. More...
## Functions
CsvTable * | csvLoadBuffer (long len, const unsigned char *data, char cend, char qchr)
| Create a CsvTable structure from a CSV-format data buffer.
CsvTable * | csvLoadFile (const char *file_name, char cend, char qchr)
| Create a CsvTable structure from a file in any supported format.
CsvTable * | csvCreateTable (long width, long height)
| Create a new CsvTable structure in memory.
void | csvFree (CsvTable *csv)
| Free a previously allocated CsvTable structure from memory.
int | csvSaveCSV (const CsvTable *csv, const char *fname, char cend, char qchr)
| Export a CsvTable structure as a CSV file.
int | csvSaveHTML (const CsvTable *csv, const char *file_name)
| Export a CsvTable structure as an HTML file.
int | csvEditCell (const CsvTable *csv, long x, long y, const char *new_text)
| Edit the contents of a CsvTable cell.
long | csvSearchCol (const CsvTable *csv, long col, const char *str)
| Search for a matching string in a particular column of a CsvTable.

## Detailed Description

Provides utilities for loading and saving CSV spreadsheet files.

Additional formats might be supported in the future.

## Function Documentation

CsvTable* csvCreateTable | ( | long | width,
|  | long | height |
| ) |  |  |

Create a new CsvTable structure in memory.

Used internally by the various csvLoad* functions.

Parameters:

  | width | The width of the table in cells.
  | height | The height of the table in cells.

Returns:
  A valid CsvTable on success or NULL on failure.

int csvEditCell | ( | const CsvTable * | csv,
|  | long | x,
|  | long | y,
|  | const char * | new_text |
| ) |  |  |

Edit the contents of a CsvTable cell.

Any previous data in the cell will be freed from memory and replaced.

Parameters:

  | csv | The CsvTable to be edited.
  | x | Cell X position.
  | y | Cell Y position.
  | new_text | The new value of the cell. This can be set to NULL.

Returns:
  Zero on success or non-zero on failure.

void csvFree | ( | CsvTable * | csv | ) |

Free a previously allocated CsvTable structure from memory.

After this function has been called, the CsvTable pointer should be set to NULL.

Parameters:

  | csv | The table to be freed.

CsvTable* csvLoadBuffer | ( | long | len,
|  | const unsigned char * | data,
|  | char | cend,
|  | char | qchr |
| ) |  |  |

Create a CsvTable structure from a CSV-format data buffer.

This function can also be used to import Tab-Seperated or Space-Separated files.

Parameters:

  | len | The length of the data buffer.
  | data | A raw byte buffer containing file data.
  | cend | The col-end character. Usually a comma, but may be a semicolon, etc.
  | qchr | The quote character. Usually a doublequote but may be a singlequote.

Returns:
  A valid CsvTable on success or NULL on failure.

CsvTable* csvLoadFile | ( | const char * | file_name,
|  | char | cend,
|  | char | qchr |
| ) |  |  |

Create a CsvTable structure from a file in any supported format.

This function is essentially a special wrapper for csvLoadBuffer(). It creates a data buffer from a given file, passes it to csvLoadBuffer() and then frees it before returning the result obtained from the csvLoadBuffer() function.

Parameters:

  | file_name | The name and path of the file to be loaded.
  | cend | The col-end character. Usually a comma, but may be a semicolon, etc.
  | qchr | The quote character. Usually a doublequote but may be a singlequote.

Returns:
  A valid CsvTable on success or NULL on failure.

int csvSaveCSV | ( | const CsvTable * | csv,
|  | const char * | fname,
|  | char | cend,
|  | char | qchr |
| ) |  |  |

Export a CsvTable structure as a CSV file.

This function can also be used to export Tab-Seperated or Space-Separated files.

Parameters:

  | csv | The CsvTable to be saved.
  | fname | The filename to use for the exported table.
  | cend | The col-end character. Usually a comma but may be a semicolon, etc.
  | qchr | The quote character. Usually a doublequote but may be a singlequote

Returns:
  Zero on success or non-zero on failure.

int csvSaveHTML | ( | const CsvTable * | csv,
|  | const char * | file_name |
| ) |  |  |

Export a CsvTable structure as an HTML file.

Exported files are HTML 4.01 strict compliant.

Parameters:

  | csv | The CsvTable to be saved.
  | file_name | The filename to use for the exported table.

Returns:
  Zero on success or non-zero on failure.

long csvSearchCol | ( | const CsvTable * | csv,
|  | long | col,
|  | const char * | str |
| ) |  |  |

Search for a matching string in a particular column of a CsvTable.

Returns the first match found, even when there is more than one possible match.

Parameters:

  | csv | The CsvTable to be searched.
  | col | The column to search.
  | str | The value to look for.

Returns:
  a row index on success or a negative value on failure.