cards

alternative to Anki for the command-line
git clone https://github.com/jennydoe/cards.git
Log | Files | Refs | Feed | README

commit ff36cdef19c6fd1e5b44e4499b5cebf0d42cdf5c
parent cdfde1ce852ac0cb8f41ecb3530e4d32c6ff41e7
Author: Jenny Doe <tng@soykaf.me>
Date:   Tue,  9 Apr 2019 15:35:54 +0200

removed unnecessary parts of the csv library

Diffstat:
MMakefile | 5+++--
Mcsv.h | 7+------
Dfread_csv_line.c | 108-------------------------------------------------------------------------------
Dsplit.c | 86-------------------------------------------------------------------------------
4 files changed, 4 insertions(+), 202 deletions(-)

diff --git a/Makefile b/Makefile @@ -2,7 +2,8 @@ CC=cc STD=c99 CFLAGS=-g -Wall BIN=a.out -SRC=cards.c csv.c split.c fread_csv_line.c +SRC=cards.c csv.c +HEADERS=csv.h -$(BIN): $(SRC) +$(BIN): $(SRC) $(HEADERS) $(CC) --std=$(STD) $(CFLAGS) -o $(BIN) $(SRC) diff --git a/csv.h b/csv.h @@ -1,12 +1,7 @@ -#ifndef CSV_DOT_H_INCLUDE_GUARD -#define CSV_DOT_H_INCLUDE_GUARD +#pragma once #define CSV_ERR_LONGLINE 0 #define CSV_ERR_NO_MEMORY 1 char **parse_csv( const char *line ); void free_csv_line( char **parsed ); -char **split_on_unescaped_newlines(const char *txt); -char *fread_csv_line(FILE *fp, int max_line_size, int *done, int *err); - -#endif diff --git a/fread_csv_line.c b/fread_csv_line.c @@ -1,107 +0,0 @@ -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include "csv.h" - -#define READ_BLOCK_SIZE 65536 -#define QUICK_GETC( ch, fp )\ -do\ -{\ - if ( read_ptr == read_end )\ - {\ - fread_len = fread( read_buf, sizeof(char), READ_BLOCK_SIZE, fp );\ - if ( fread_len < READ_BLOCK_SIZE )\ - read_buf[fread_len] = '\0';\ - read_ptr = read_buf;\ - }\ - ch = *read_ptr++;\ -}\ -while(0) - -/* - * Given a file pointer, read a CSV line from that file. - * File may include newlines escaped with "double quotes". - * - * Warning: This function is optimized for the use case where - * you repeatedly call it until the file is exhausted. It is - * very suboptimal for the use case of just grabbing one single - * line of CSV and stopping. Also, this function advances the - * file position (in the fseek/ftell sense) unpredictably. You - * should not change the file position between calls to - * fread_csv_line (e.g., don't use "getc" on the file in between - * calls to fread_csv_line). - * - * Other arguments: - * size_t max_line_size: Maximum line size, in bytes. - * int *done: Pointer to an int that will be set to 1 when file is exhausted. - * int *err: Pointer to an int where error code will be written. - * - * Warning: Calling this function on an exhausted file (as indicated by the - * 'done' flag) is undefined behavior. - * - * See csv.h for definitions of error codes. - */ -char *fread_csv_line(FILE *fp, int max_line_size, int *done, int *err) { - static FILE *bookmark; - static char read_buf[READ_BLOCK_SIZE], *read_ptr, *read_end; - static int fread_len, prev_max_line_size = -1; - static char *buf; - char *bptr, *limit; - char ch; - int fQuote; - - if ( max_line_size > prev_max_line_size ) { - if ( prev_max_line_size != -1 ) { - free( buf ); - } - buf = malloc( max_line_size + 1 ); - if ( !buf ) { - *err = CSV_ERR_NO_MEMORY; - prev_max_line_size = -1; - return NULL; - } - prev_max_line_size = max_line_size; - } - bptr = buf; - limit = buf + max_line_size; - - if ( bookmark != fp ) { - read_ptr = read_end = read_buf + READ_BLOCK_SIZE; - bookmark = fp; - } - - for ( fQuote = 0; ; ) { - QUICK_GETC(ch, fp); - - if ( !ch || (ch == '\n' && !fQuote)) { - break; - } - - if ( bptr >= limit ) { - free( buf ); - *err = CSV_ERR_LONGLINE; - return NULL; - } - *bptr++ = ch; - - if ( fQuote ) { - if ( ch == '\"' ) { - QUICK_GETC(ch, fp); - - if ( ch != '\"' ) { - if ( !ch || ch == '\n' ) { - break; - } - fQuote = 0; - } - *bptr++ = ch; - } - } else if ( ch == '\"' ) { - fQuote = 1; - } - } - - *done = !ch; - *bptr = '\0'; - return strdup(buf); -}- \ No newline at end of file diff --git a/split.c b/split.c @@ -1,85 +0,0 @@ -#include <stdlib.h> -#include <string.h> - -/* - * Given a string which might contain unescaped newlines, split it up into - * lines which do not contain unescaped newlines, returned as a - * NULL-terminated array of malloc'd strings. - */ -char **split_on_unescaped_newlines(const char *txt) { - const char *ptr, *lineStart; - char **buf, **bptr; - int fQuote, nLines; - - /* First pass: count how many lines we will need */ - for ( nLines = 1, ptr = txt, fQuote = 0; *ptr; ptr++ ) { - if ( fQuote ) { - if ( *ptr == '\"' ) { - if ( ptr[1] == '\"' ) { - ptr++; - continue; - } - fQuote = 0; - } - } else if ( *ptr == '\"' ) { - fQuote = 1; - } else if ( *ptr == '\n' ) { - nLines++; - } - } - - buf = malloc( sizeof(char*) * (nLines+1) ); - - if ( !buf ) { - return NULL; - } - - /* Second pass: populate results */ - lineStart = txt; - for ( bptr = buf, ptr = txt, fQuote = 0; ; ptr++ ) { - if ( fQuote ) { - if ( *ptr == '\"' ) { - if ( ptr[1] == '\"' ) { - ptr++; - continue; - } - fQuote = 0; - continue; - } else if ( *ptr ) { - continue; - } - } - - if ( *ptr == '\"' ) { - fQuote = 1; - } else if ( *ptr == '\n' || !*ptr ) { - size_t len = ptr - lineStart; - - if ( len == 0 ) { - *bptr = NULL; - return buf; - } - - *bptr = malloc( len + 1 ); - - if ( !*bptr ) { - for ( bptr--; bptr >= buf; bptr-- ) { - free( *bptr ); - } - free( buf ); - return NULL; - } - - memcpy( *bptr, lineStart, len ); - (*bptr)[len] = '\0'; - - if ( *ptr ) { - lineStart = ptr + 1; - bptr++; - } else { - bptr[1] = NULL; - return buf; - } - } - } -}- \ No newline at end of file