


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Advanced Programming Tools and Techniques; Subject: Computer Science; University: Drexel University; Term: Winter 2004;
Typology: Study notes
1 / 34
This page cannot be seen from the preview
Don't miss anything!



























Objective: To discuss the considerations that mustbe addressed when designing an interface. Toillustrate these design issues with a simple yetuseful example.
-^
“The essence of design is to balance competinggoals and constraints. Although there are manytradeoffs when one is writing a small self-contained system, the ramifications of particularchoices remain within the system and affect onlythe individual programmer. But when code is tobe used by others, decisions have widerrepercussions.”
*The examples in these slides come fromBrian W. Kernighan and Rob Pike,“The Practice of Programming”, Addison-Wesley, 1999.
supplier and the customer. The desire is toprovide services that are uniform andconvenient, with enough functionality to beeasy to use but not so much as to becomeunwieldy.
freeing storage, and managing shared copies ofinformation.
Interface Principles^ 1. Hide implementation details^ 2. Choose a small orthogonal set of primitives^ 3. Don't reach behind the user's back^ 4. Do the same thing the same way everywhere
-^
Resource Management^ 1. Free a resource in the same layer that allocated it
-^
Abort, Retry, Fail?^ 1. Detect errors at a low level, handle them at a high level^ 2. Use exceptions only for exceptional situations
A natural and widely used format for tabular data^ – Each row is a line of text^ – The fields on each line are separated by commas^ – Text format used by many spreadsheets
-^
Example^ – “Good Student”,CS265,”Advanced Programming Tools
and Techniques”,A
and Techniques”,D
char buf[200];
/* input line buffer */
char *field[20];
/* fields */
/* csvgetline: read and parse line, return field count / / sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625 */ int csvgetline(FILE *fin) {
int nfield; char *p, *q; if (fgets(buf, sizeof(buf), fin) == NULL)
return -1; nfield = 0; for (q = buf; (p=strtok(q, ",\n\r")) != NULL; q = NULL)
field[nfield++] = unquote(p); return nfield; }
/* unquote: remove leading and trailing quote */ char *unquote(char *p) {
if (p[0] == '"') {
if (p[strlen(p)-1] == '"')
p[strlen(p)-1] = '\0'; p++; } return p; } /* csvtest main: test csvgetline function */ int main(void) {
int i, nf; while ((nf = csvgetline(stdin)) != -1)
for (i = 0; i < nf; i++)
printf("field[%d] = `%s'\n", i, field[i]);
return 0; }
Global variables make code unsuitable to multi-threaded environment or interleaved calls
-^
Caller must open and close files
-^
Input and splitting are inextricably linked
-^
The return value is the number of fields (each linemust be split to compute this value)
-^
Each decision is interwoven into the code. Thereis no way to change any of these propertieswithout changing the code
Fields are separated by commas
-^
A field may be enclosed in double-quotes
-^
A quoted field may contain commas but notnewlines
-^
A quoted field may contain double-quotes,represented by “”
-^
Fields may be empty; “” and empty string bothrepresent an empty field
-^
Leading and trailing white space is preserved
char *csvfield(int n);^ – fields are numbered from 0.^ – returns n-th field from last line read by csvgetline
removed; within “…”, “” is replaced by “ and comma isnot a separator
csvgetline.
called.
enum { NOMEM = -2 };
/* out of memory signal */
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 */
/* endofline: check for and consume \r, \n, \r\n, or EOF */ static int endofline(FILE fin, int c) / reset: set variables back to starting values / static void reset(void) / split: split line into fields / static int split(void) / advquoted: quoted field; return pointer to next separator */ static char *advquoted(char *p)