Skip to content
Snippets Groups Projects
Commit 308690a0 authored by ys554's avatar ys554
Browse files

lab8

parent ef16f885
Branches main
No related tags found
No related merge requests found
#Yegeon Seo
cc = gcc
EXE = foo
JAVAC = javac
JRE = java
PAGER = less
TEST_OUT = test.out
EXP_OUT = expected.out
.PHONY : clean run-py run-c read run-java save-java test-java
foo.o : foo.c foo.h
$(cc) -c foo.c
main.o : main.c. foo.h
$(cc) -c main.c
$(EXE) : main.o foo.o
$(cc) -o$(EXE) main.o foo.o
run-c : $(EXE)
./$(EXE)
run-py : foo.py
./foo.py
read : README
$(PAGER) README
foo.class : foo.java
$(JAVAC) foo.java
run-java : foo.class
$(JRE) foo
save-java :
$(JRE) foo > $(TEST_OUT)
test-java : $(TEST_OUT) $(EXP_OUT)
-@if diff $(TEST_OUT) $(EXP_OUT) &> /dev/null ; then \
echo "Passed!" ;\
else \
echo "Not the same!" ;\
fi
clean :
rm foo.o main.o $(EXE) foo.class test.out
/*
* csv.c - Implementation for a csv library
*
* Modified from code in Kernighan & Pike, _The Practice of Programming_
* Copyright (C) 1999 Lucent Technologies
* Excerpted from 'The Practice of Programming'
* by Brian W. Kernighan and Rob Pike
*
* Kurt Schmidt
* 3/2018
*
* gcc 5.4.0 20160609 on
* Linux 4.13.0-32-generic
*
* EDITOR: tabstop=2 cols=120
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "csv.h"
static char fieldsep[] = "," ; /* field separator chars */
/***** Prototypes for local helper functions ******/
static char *advquoted( char* ) ;
static int split( csv_t* ) ;
static char *line = NULL ; /* input chars */
static char *sline = NULL ; /* line copy used by split */
static int maxline = 0 ; /* size of line[] and sline[] */
static char **field = NULL ; /* field pointers */
static int maxfield = 0 ; /* size of field[] */
static int nfield = 0 ; /* number of fields in field[] */
static char fieldsep[] = "," ; /* field separator chars */
static char* advquoted( char* ) ;
static int split( void ) ;
/* endofline: check for and consume \r, \n, \r\n, or EOF */
static int endofline( FILE *fin, int c )
{
int eol ;
eol = ( c=='\r' || c=='\n' ) ;
if( c=='\r' ) {
c = getc( fin ) ;
if( c!='\n' && c != EOF )
ungetc( c, fin ) ; /* read too far; put c back */
}
return eol ;
}
/* reset: set variables back to starting values */
static void reset( void )
{
free( line ); /* free(NULL) permitted by ANSI C */
free( sline ) ;
free( field ) ;
line = NULL ;
sline = NULL ;
field = NULL ;
maxline = maxfield = nfield = 0 ;
}
/* csvgetline: get one line, grow as needed */
/* sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625 */
char* csvgetline( FILE *fin )
{
int i, c ;
char *newl, *news ;
if( line==NULL ) { /* allocate on first call */
maxline = maxfield = 1 ;
line = (char*) malloc( maxline ) ;
sline = (char*) malloc( maxline ) ;
field = (char**) malloc( maxfield*sizeof( field[0] )) ;
if( line==NULL || sline==NULL || field==NULL) {
reset() ;
return NULL ; /* out of memory */
}
}
for( i=0; (c=getc( fin ))!=EOF && ! endofline(fin,c); i++ ) {
if( i>=maxline-1 ) { /* grow line */
maxline *= 2; /* double current size */
newl = (char*) realloc( line, maxline ) ;
if( newl==NULL ) {
reset() ;
return NULL ;
}
line = newl ;
news = (char*) realloc( sline, maxline ) ;
if( news==NULL ) {
reset() ;
return NULL ;
}
sline = news ;
}
line[i] = c ;
} /* for i */
line[i] = '\0' ;
if( split()==NOMEM ) {
reset() ;
return NULL; /* out of memory */
}
return (c==EOF && i==0) ? NULL : line ;
}
/* split: split line into fields */
static int split( void )
{
char *p, **newf ;
char *sepp; /* pointer to temporary separator character */
int sepc; /* temporary separator character */
nfield = 0 ;
if( line[0]=='\0' )
return 0 ;
strcpy( sline, line ) ;
p = sline ;
do {
if( nfield>=maxfield ) {
maxfield *= 2; /* double current size */
newf = (char**) realloc( field,
maxfield * sizeof( field[0] )) ;
if( newf==NULL )
return NOMEM ;
field = newf ;
}
if( *p=='"' )
sepp = advquoted( ++p ) ; /* skip initial quote */
else
sepp = p + strcspn( p, fieldsep ) ;
sepc = sepp[0] ;
sepp[0] = '\0' ; /* terminate field */
field[nfield++] = p ;
p = sepp + 1 ;
} while( sepc==',' ) ;
return nfield ;
}
/* advquoted: quoted field; return pointer to next separator */
static char *advquoted( char* p )
{
int i, j ;
for( i=j=0; p[j]!='\0'; ++i, ++j ) {
if( p[j]=='"' && p[++j]!='"' ) {
/* copy up to next separator or \0 */
int k = strcspn( p+j, fieldsep ) ;
memmove( p+i, p+j, k ) ;
i += k ;
j += k ;
break ;
}
p[i] = p[j] ;
}
p[i] = '\0' ;
return p + j ;
}
/* csvfield: return pointer to n-th field */
char* csvfield( int n )
{
if( n<0 || n>=nfield )
return NULL ;
return field[n] ;
}
/* csvnfield: return number of fields */
int csvnfield( void )
{
return nfield ;
}
csv_t* csv_init( FILE *f )
{
csv_t *rv = (csv_t*) malloc( sizeof( csv_t )) ;
rv->fin = f ;
reset( rv ) ;
return( rv ) ;
}
void kill( csv_t* s )
{
reset( s ) ;
free( s ) ;
}
Yegeon Seo
Lab 8
Q1.
Hide implementation details: Implementation that is hidden from the user so that one cannot affected from any change to code
Choose a small orthogonal set of primitives: Limit code to a minimal having functions or methods performing same tasks and capabilities
Don't reach behind the user's back: User interface should not invilve anything that the user is unaware of.
Do the same thing the same way everywhere: User interface should be consistent across all platforms and functionality, along with the programming style.
Q2.
Code is unclear and has bad variable names. It can only have a line buffer with 200 characters because of the allocated memory, along with limited to 20 fields
Q3.
Programming style and variable names are more clear and easier to follow. Also includes comments.
Q4.
You cannot parse 2 different CSV files at the same time because of the way the program is written. You cannot read each line individually.
Q5.
C++ implementation will not solve this problem. Although it will solve the issue of limited allocated memory in the original C program, it still doesn't handle errors empty fields
Q6.
Yes, it does adhere to the guidelines for a good interface.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment