|
| 1 | +# Simple and fast CSV reader written in C |
| 2 | + |
| 3 | +## Description |
| 4 | +Simple and fast library for fast reading of large CSV files using memory-mapped files. |
| 5 | +Purpose of this project was to create fast CSV (comma separated values) reader implementation in C with very simple interface using memory-mapped files. |
| 6 | + |
| 7 | +## Features |
| 8 | +* Simple C interface |
| 9 | +* Very large CSV file support - GBs, TBs |
| 10 | +* Using memory mapped files |
| 11 | +* Supports UNIX and Windows platforms |
| 12 | +* UTF-8 support |
| 13 | +* Supports both Windows CRLF "\r\n" and Unix LF "\n" sequences |
| 14 | +* Supports newlines "\n" in CSV columns |
| 15 | +* Spaces are preserved (e.g "one, two" -> {"one", " two"}) |
| 16 | + |
| 17 | +## How to compile |
| 18 | +You can add ```csv.c``` file to your project or you can use Makefile provided. |
| 19 | +To compile csv library on Linux with GNU Make: |
| 20 | + |
| 21 | +* run ```make all``` from project root to compile all targets and test application |
| 22 | + |
| 23 | +## How to use (trivial example) |
| 24 | +Error handing ommited for brevity |
| 25 | + |
| 26 | +```C++ |
| 27 | +char* row; |
| 28 | +int cols = 0; |
| 29 | +CsvHandle handle = CsvOpen("csvfile.csv"); |
| 30 | + |
| 31 | +while (row = CsvReadNextRow(handle)) |
| 32 | +{ |
| 33 | + /* row = CSV row string */ |
| 34 | + const char* col; |
| 35 | + while (col = CsvReadNextCol(row, handle)) |
| 36 | + cols++; /* col = CSV col string */ |
| 37 | +} |
| 38 | + |
| 39 | +printf("Number of cols %i", cols); |
| 40 | +``` |
| 41 | +
|
| 42 | +## Public API functions |
| 43 | +
|
| 44 | +If you want to read classic CSV files, you can follow this pipeline: |
| 45 | +1. ```CsvOpen()``` to open CSV file |
| 46 | +2. ```CsvReadNextRow()``` to read single CSV line |
| 47 | +3. ```CsvReadNextCol()``` to read single CSV column |
| 48 | +4. ```CsvClose()``` to close opened CSV handle |
| 49 | +
|
| 50 | +### ```CsvOpen(const char* filepath)``` |
| 51 | +Opens a CSV file. |
| 52 | +#### Paramters: |
| 53 | +* filepath, (```const char*```): path to a CSV file |
| 54 | +#### Return value: |
| 55 | +```CsvHandle```: handle to a CSV file on success, NULL otherwise |
| 56 | +
|
| 57 | +### ```CsvOpen2(const char* filepath, char delim, char quote, char escape)``` |
| 58 | +Opens a CSV file. You can specify custom CSV delimeter, quote and escape char. |
| 59 | +#### Parameters: |
| 60 | +* filepath, (```const char*```): path to a CSV file |
| 61 | +* delim (```char```): custom CSV delimeter ASCII character (default ',') |
| 62 | +* quote (```char```): custom CSV quote ASCII character (default '"') |
| 63 | +* escape (```char```): custom CSV escape ASCII character (default '\\') |
| 64 | +#### Return value: |
| 65 | +```CsvHandle```: handle to a CSV file on success, NULL otherwise |
| 66 | +
|
| 67 | +### ```CsvClose(CsvHandle handle)``` |
| 68 | +Releases all resources allocated. |
| 69 | +#### Parameters: |
| 70 | +* handle (```CsvHandle```): handle opened by CsvOpen() or CsvOpen2() |
| 71 | +
|
| 72 | +### ```CsvReadNextRow(CsvHandle handle)``` |
| 73 | +Returns pointer to new line (UTF-8 zero terminated string) or NULL. |
| 74 | +#### Parameters: |
| 75 | +* handle (```CsvHandle```): handle opened by CsvOpen() or CsvOpen2() |
| 76 | +#### Return value: |
| 77 | +```char*```: zero terminated string on success, NULL on EOF or error. |
| 78 | +
|
| 79 | +### ```CsvReadNextCol(CsvHandle handle, char* row)``` |
| 80 | +Returns pointer to column (UTF-8 zero terminated string) or NULL |
| 81 | +#### Parameters: |
| 82 | +* handle (```CsvHandle```): handle opened by CsvOpen() or CsvOpen2() |
| 83 | +#### Return value |
| 84 | +```const char*```: zero terminated string on success, NULL on EOL or error. |
| 85 | +
|
| 86 | +## License |
| 87 | +MIT (see LICENSE.txt) |
0 commit comments