Skip to content

Commit

Permalink
Chapter 4: Add implentation for Exercise 4-3
Browse files Browse the repository at this point in the history
Refs #6
  • Loading branch information
asankov committed May 16, 2020
1 parent 9019317 commit e6b9cb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion chapter-4/4.3-a-library-for-others/csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ static char **field = NULL; /* field pointers */
static int maxfield = 0; /* size of fields[] */
static int nfield = 0; /* number of fields in field[] */

static int _initial_maxline = 0; /* initial value of maxline configured by the consumer, or 0 by default */
static int _initial_maxfield = 0; /* initial value of maxfield configured by the consumer, or 0 by default */

void init(int initmaxfield, int initmaxline) /* initialize the library with proper starting values */
{
maxline = _initial_maxline = initmaxline;
maxfield = _initial_maxfield = initmaxfield;
}
/* reset: set variables back to starting values */
static void reset(void)
{
Expand All @@ -23,7 +31,9 @@ static void reset(void)
line = NULL;
sline = NULL;
field = NULL;
maxline = maxfield = nfield = 0;
maxline = _initial_maxline;
maxfield = _initial_maxfield;
nfield = 0;
}

/* endofline: check for and consume \r, \n, \r\n, or EOF */
Expand Down Expand Up @@ -159,6 +169,8 @@ int main(void)
{
char *line;

init(10, 10);

while ((line = csvgetline(stdin, ",")) != NULL)
{
printf("line = '%s'\n", line);
Expand Down
1 change: 1 addition & 0 deletions chapter-4/4.3-a-library-for-others/csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
extern char *csvgetline(FILE *f, char *separator); /* read next input line */
extern char *csvfield(int n); /* return field n */
extern int csvnfield(void); /* return number of fields */
extern void init(int maxfield, int maxline); /* initialize the library with proper starting values */

0 comments on commit e6b9cb4

Please sign in to comment.