Interfaces*, Themes - Lecture Notes | CS 265, Study notes of Computer Science

Material Type: Notes; Class: Advanced Programming Tools and Techniques; Subject: Computer Science; University: Drexel University; Term: Winter 2004;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-l3a-1
koofers-user-l3a-1 🇺🇸

10 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objective: To discuss the considerations that must
be addressed when designing an interface. To
illustrate these design issues with a simple yet
useful example.
“The essence of design is to balance competing
goals and constraints. Although there are many
tradeoffs when one is writing a small self-
contained system, the ramifications of particular
choices remain within the system and affect only
the individual programmer. But when code is to
be used by others, decisions have wider
repercussions.”
*The examples in these slides come from
Brian W. Kernighan and Rob Pike,
“The Practice of Programming”, Addison-Wesley, 1999.
CSV
Interfaces*
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Interfaces*, Themes - Lecture Notes | CS 265 and more Study notes Computer Science in PDF only on Docsity!

•^

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.

CSV

Interfaces*

Themes

  • Interfaces: what services and access are

provided?^ – The interface is in effect a contract between the

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.

Themes

  • Resource Management: who is responsible

for managing memory and other resources?^ – Here, the main problems are allocating and

freeing storage, and managing shared copies ofinformation.

Themes

  • Error handling: who detects errors, who

reports them, and how? When an error isdetected, what recovery is attempted?

Topics

•^

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

Comma Separated Values (CSV) •^

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

  • “Bad Student”,CS265,”Advanced Programming Tools

and Techniques”,D

Prototype

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; }

Prototype

/* 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; }

Decisions Made in Prototype

•^

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

Specification

•^

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

Specification

•^

char *csvfield(int n);^ – fields are numbered from 0.^ – returns n-th field from last line read by csvgetline

  • returns NULL if n<0 or beyond last field.
    • fields are separated by commas. – fields may be surrounded by “…”, such quotes are

removed; within “…”, “” is replaced by “ and comma isnot a separator

  • in unquoted fields, quotes are regular characters – there can be an arbitrary number of fields of any length;
    • returns NULL if memory limit exceeded.
      • fields must be treated as read-only storage;
        • caller must make a copy to preserve or change contents
          • behavior undefined if called before csvgetline is called.

Specification

  • int csvnfield(void);
    • returns number of fields on last line read by

csvgetline.

  • behavior undefined if called before csvgetline is

called.

Internal Variables

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 */

Internal Functions

/* 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)