1 Star 0 Fork 0

aczz / csv-fast-reader

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Simple and fast CSV reader written in C

Description

Simple and fast library for fast reading of large CSV files using memory-mapped files. Purpose of this project was to create fast CSV (comma separated values) reader implementation in C with very simple interface using memory-mapped files.

Features

  • Simple C interface
  • Very large CSV file support - GBs, TBs
  • Using memory mapped files
  • Supports UNIX and Windows platforms
  • UTF-8 support
  • Supports both Windows CRLF "\r\n" and Unix LF "\n" sequences
  • Supports newlines "\n" in CSV columns
  • Spaces are preserved (e.g "one, two" -> {"one", " two"})

How to compile

You can add csv.c file to your project or you can use Makefile provided. To compile csv library on Linux with GNU Make:

  • run make all from project root to compile all targets and test application

How to use (trivial example)

Error handing ommited for brevity

char* row;
int cols = 0;
CsvHandle handle = CsvOpen("csvfile.csv");

while (row = CsvReadNextRow(handle))
{
    /* row = CSV row string */
    const char* col;
    while (col = CsvReadNextCol(row, handle))
        cols++;  /* col = CSV col string */
}

printf("Number of cols %i", cols);

Public API functions

If you want to read classic CSV files, you can follow this pipeline:

  1. CsvOpen() to open CSV file
  2. CsvReadNextRow() to read single CSV line
  3. CsvReadNextCol() to read single CSV column
  4. CsvClose() to close opened CSV handle

CsvOpen(const char* filepath)

Opens a CSV file.

Paramters:

  • filepath, (const char*): path to a CSV file

Return value:

CsvHandle: handle to a CSV file on success, NULL otherwise

CsvOpen2(const char* filepath, char delim, char quote, char escape)

Opens a CSV file. You can specify custom CSV delimeter, quote and escape char.

Parameters:

  • filepath, (const char*): path to a CSV file
  • delim (char): custom CSV delimeter ASCII character (default ',')
  • quote (char): custom CSV quote ASCII character (default '"')
  • escape (char): custom CSV escape ASCII character (default '\')

Return value:

CsvHandle: handle to a CSV file on success, NULL otherwise

CsvClose(CsvHandle handle)

Releases all resources allocated.

Parameters:

  • handle (CsvHandle): handle opened by CsvOpen() or CsvOpen2()

CsvReadNextRow(CsvHandle handle)

Returns pointer to new line (UTF-8 zero terminated string) or NULL.

Parameters:

  • handle (CsvHandle): handle opened by CsvOpen() or CsvOpen2()

Return value:

char*: zero terminated string on success, NULL on EOF or error.

CsvReadNextCol(CsvHandle handle, char* row)

Returns pointer to column (UTF-8 zero terminated string) or NULL

Parameters:

  • handle (CsvHandle): handle opened by CsvOpen() or CsvOpen2()

Return value

const char*: zero terminated string on success, NULL on EOL or error.

License

MIT (see LICENSE.txt)

Copyright (c) 2019 Jan Doczy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
C 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/aczz/csv-fast-reader.git
git@gitee.com:aczz/csv-fast-reader.git
aczz
csv-fast-reader
csv-fast-reader
master

搜索帮助