Created
October 19, 2015 13:42
-
-
Save nikopol/03bb050b290f594cf7c3 to your computer and use it in GitHub Desktop.
LIGTHNING FAST CSV ROW COUNT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
LIGTHNING FAST CSV ROW COUNT | |
syntax: csvcount filename | |
niko 2015 | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <ctype.h> | |
typedef int bool; | |
#define true 1 | |
#define false 0 | |
#define BUFSIZE 32768 | |
#define LF 10 | |
int main(int argc, char const *argv[]){ | |
if( argc<2 ){ | |
printf("syntax %s filename\n", argv[0]); | |
return 1; | |
} | |
FILE *csv = fopen(argv[1], "r"); | |
if( csv == NULL ){ | |
printf("error opening %s\n", argv[1]); | |
return 2; | |
} | |
char buf[BUFSIZE]; | |
bool wasquote = false; | |
bool inquote = false; | |
long nbrows = 0; | |
long n; | |
int i, pass = 0; | |
char c; | |
size_t done = 0; | |
fseek(csv, 0L, SEEK_END); | |
size_t todo = ftell(csv); | |
fseek(csv, 0L, SEEK_SET); | |
printf("reading %s (%lu bytes) ...\n", argv[1], todo); | |
while( (n = fread(&buf, 1, BUFSIZE, csv)) > 0 ){ | |
done += n; | |
if( todo > 0 && (pass++ % 10) == 0 ) | |
printf(" %d%%\r", (100 * done)/todo); | |
for( i = 0; i < n; ++i ){ | |
c = buf[i]; | |
if( c == '"' ) { | |
if( !inquote && !wasquote ) { | |
inquote = true; | |
} else { | |
if( wasquote ) { | |
wasquote = false; | |
inquote = true; | |
} else { | |
wasquote = true; | |
inquote = false; | |
} | |
} | |
} else { | |
if( wasquote ) wasquote = false; | |
if( !inquote && c == LF ) ++nbrows; | |
} | |
} | |
} | |
fclose(csv); | |
printf("%lu rows found \n", nbrows); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gcc -o csvcount csvcount.c