DOC PREVIEW
DREXEL CS 265 - Interfaces

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Interfaces*ThemesThemesThemesThemesTopicsTopicsComma Separated Values (CSV)Prototype LibraryPrototypePrototypeDecisions Made in PrototypeDecisions Made in PrototypeSpecificationSpecificationSpecificationSpecificationHeader FileInternal VariablesInternal FunctionsExampleMain Programcsvgetlinecsvgetline (cont)resetendoflinesplitadvquotedcsvfield & csvnfieldC++ VersionPublic InterfacePrivate InterfaceMain Program• 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 fromBrian W. Kernighan and Rob Pike,“The Practice of Programming”, Addison-Wesley, 1999.CSVInterfaces*Themes• Interfaces: what services and access are provided?– The interface is in effect a contract between the supplier and the customer. The desire is to provide services that are uniform and convenient, with enough functionality to be easy to use but not so much as to become unwieldy.Themes• Information Hiding: What information is visible and what is private?– An interface must provide straightfowardaccess to the components while hiding the details of the implementation so they can be changed without affecting users.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 of information.Themes• Error handling: who detects errors, who reports them, and how? When an error is detected, what recovery is attempted?Topics• CSV - Comma separated values • CSV - Prototype library • CSV - A library for others • CSV - A C++ implementationTopics• 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 situationsComma 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”,DPrototype Library• “plan to throw one away; you will, anyhow” –Frederick Brooks• “It is not usually until you’ve built and used a version of the program that you understand the issues well enough to get the design right.”• First version– ignore many of the difficulties– complete enough to be useful and to gain some familiarity with the problemPrototypechar 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• Doesn’t handle long input lines or lots of fields• Lines terminated by newlines• Fields are separated by commas and surrounding quotes removed – no embedded quotes• Input line not preserved – overwritten when creating fields• No data saved from one input line to the next• Access to fields through global variable (does not prevent access beyond last field)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 line must be split to compute this value)• Each decision is interwoven into the code. There is no way to change any of these properties without changing the codeSpecification• Fields are separated by commas• A field may be enclosed in double-quotes• A quoted field may contain commas but not newlines• A quoted field may contain double-quotes, represented by “”• Fields may be empty; “” and empty string both represent an empty field• Leading and trailing white space is preservedSpecification• char *csvgetline(FILE *f);– reads one line from open input file f;• assumes that input lines are terminated by \r, \n, \r\n, or EOF– returns pointer to line, with terminator removed, or NULL if EOF occurred.– line may be of arbitrary length; returns NULL if memory limit exceeded.– line must be treated as read-only storage;• caller must make a copy to preserve or change contentsSpecification• 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 is not 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.Header Fileextern char *csvgetline(FILE *f); /* read next input line */extern char *csvfield(int n); /* return field n */extern int


View Full Document

DREXEL CS 265 - Interfaces

Download Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?