Skip to content

Commit e9ff7ad

Browse files
doczyJan Doczy
doczy
authored and
Jan Doczy
committed
Initial commit
0 parents  commit e9ff7ad

File tree

8 files changed

+879
-0
lines changed

8 files changed

+879
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.csv
2+
*~*
3+
.so
4+
.o
5+
.a
6+
.swp

LICENSE.txt

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

Makefile

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# simle makefile used to build csv dynamic | static library
2+
3+
##include Config.mk
4+
ifeq ($(CC),)
5+
CC=gcc
6+
endif
7+
ifeq ($(AR),)
8+
AR=ar
9+
endif
10+
11+
CC_FILES = csv.c
12+
SHARED_DIR = ./shared
13+
STATIC_DIR = ./static
14+
TEST_DIR = ./test
15+
SHARED_OBJ := $(CC_FILES:%.c=$(SHARED_DIR)/%.o)
16+
STATIC_OBJ := $(CC_FILES:%.c=$(STATIC_DIR)/%.o)
17+
SHARED_LIB := $(SHARED_DIR)/csv.so
18+
STATIC_LIB := $(STATIC_DIR)/csv.a
19+
TEST_BIN := $(TEST_DIR)/test
20+
CFLAGS= -O3 -Wall -ansi -pedantic -g
21+
DEFINES = -D_FILE_OFFSET_BITS=64
22+
23+
# make both, shared and static + test
24+
all: make_outdir $(SHARED_LIB) $(STATIC_LIB) $(TEST_BIN) runtest
25+
shared: make_outdir $(SHARED_LIB)
26+
static: make_outdir $(STATIC_LIB)
27+
28+
make_outdir:
29+
$(shell mkdir -p $(SHARED_DIR) $(STATIC_DIR))
30+
31+
# shared library target
32+
$(SHARED_LIB): CFLAGS += -fPIC
33+
$(SHARED_LIB): $(SHARED_OBJ)
34+
$(CC) $^ -shared -o $@
35+
36+
$(STATIC_LIB): $(STATIC_OBJ)
37+
$(AR) rcs $@ $^
38+
39+
# compile test binary
40+
$(TEST_BIN): CFLAGS=-O3 -Wall -pedantic
41+
$(TEST_BIN): test.c
42+
$(CC) $(CFLAGS) $^ $(STATIC_LIB) -lrt -o $@
43+
44+
# all shared objs pass
45+
$(SHARED_DIR)/%.o: %.c
46+
$(CC) $^ $(CFLAGS) -c -o $@
47+
48+
# all static
49+
$(STATIC_DIR)/%.o: %.c
50+
$(CC) $^ $(CFLAGS) -c -o $@
51+
52+
# runtests
53+
runtest:
54+
./test/runtest.sh
55+
56+
57+
# try clean both static and dynamic
58+
clean:
59+
rm -fR $(SHARED_DIR)
60+
rm -fR $(STATIC_DIR)
61+
rm -f $(TEST_BIN) $(TEST_DIR)/*.csv
62+

README.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)