Skip to content

Commit

Permalink
Syntax improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
semitrivial committed May 2, 2020
1 parent eec3984 commit 7919b78
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
45 changes: 15 additions & 30 deletions csv.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <stdlib.h>
#include <string.h>

void free_csv_line( char **parsed )
{
void free_csv_line( char **parsed ) {
char **ptr;

for ( ptr = parsed; *ptr; ptr++ ) {
Expand All @@ -12,19 +11,14 @@ void free_csv_line( char **parsed )
free( parsed );
}

static int count_fields( const char *line )
{
static int count_fields( const char *line ) {
const char *ptr;
int cnt, fQuote;

for ( cnt = 1, fQuote = 0, ptr = line; *ptr; ptr++ )
{
if ( fQuote )
{
if ( *ptr == '\"' )
{
if ( ptr[1] == '\"' )
{
for ( cnt = 1, fQuote = 0, ptr = line; *ptr; ptr++ ) {
if ( fQuote ) {
if ( *ptr == '\"' ) {
if ( ptr[1] == '\"' ) {
ptr++;
continue;
}
Expand All @@ -33,8 +27,7 @@ static int count_fields( const char *line )
continue;
}

switch( *ptr )
{
switch( *ptr ) {
case '\"':
fQuote = 1;
continue;
Expand All @@ -58,8 +51,7 @@ static int count_fields( const char *line )
* which are escaped by "double quotes", extract a NULL-terminated
* array of strings, one for every cell in the row.
*/
char **parse_csv( const char *line )
{
char **parse_csv( const char *line ) {
char **buf, **bptr, *tmp, *tptr;
const char *ptr;
int fieldcnt, fQuote, fEnd;
Expand All @@ -78,26 +70,21 @@ char **parse_csv( const char *line )

tmp = malloc( strlen(line) + 1 );

if ( !tmp )
{
if ( !tmp ) {
free( buf );
return NULL;
}

bptr = buf;

for ( ptr = line, fQuote = 0, *tmp = '\0', tptr = tmp, fEnd = 0; ; ptr++ )
{
if ( fQuote )
{
for ( ptr = line, fQuote = 0, *tmp = '\0', tptr = tmp, fEnd = 0; ; ptr++ ) {
if ( fQuote ) {
if ( !*ptr ) {
break;
}

if ( *ptr == '\"' )
{
if ( ptr[1] == '\"' )
{
if ( *ptr == '\"' ) {
if ( ptr[1] == '\"' ) {
*tptr++ = '\"';
ptr++;
continue;
Expand All @@ -111,8 +98,7 @@ char **parse_csv( const char *line )
continue;
}

switch( *ptr )
{
switch( *ptr ) {
case '\"':
fQuote = 1;
continue;
Expand All @@ -122,8 +108,7 @@ char **parse_csv( const char *line )
*tptr = '\0';
*bptr = strdup( tmp );

if ( !*bptr )
{
if ( !*bptr ) {
for ( bptr--; bptr >= buf; bptr-- ) {
free( *bptr );
}
Expand Down
6 changes: 3 additions & 3 deletions fread_csv_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#define QUICK_GETC( ch, fp )\
do\
{\
if ( read_ptr == read_end )\
{\
if ( read_ptr == read_end ) {\
fread_len = fread( read_buf, sizeof(char), READ_BLOCK_SIZE, fp );\
if ( fread_len < READ_BLOCK_SIZE )\
if ( fread_len < READ_BLOCK_SIZE ) {\
read_buf[fread_len] = '\0';\
}\
read_ptr = read_buf;\
}\
ch = *read_ptr++;\
Expand Down

0 comments on commit 7919b78

Please sign in to comment.