DOC PREVIEW
DREXEL CS 265 - Interfaces

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 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 35 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 35 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 35 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 35 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 35 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 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Interfaces*ThemesSlide 3Slide 4Slide 5TopicsSlide 7Comma Separated Values (CSV)Prototype LibraryPrototypeSlide 11Decisions Made in PrototypeSlide 13SpecificationSpecification – csvgetline()Specification – csvfield()Specification – csvfield() (cont)Specification – csvnfield()Header FileInternal VariablesInternal FunctionsExampleMain Programcsvgetlinecsvgetline (cont)resetendoflinesplitSlide 29advquotedcsvfield & csvnfieldC++ VersionPublic InterfacePrivate InterfaceSlide 35Interfaces*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.CSVThemesInterfaces: 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.ThemesInformation Hiding: What information is visible and what is private?An interface must provide straightfoward access to the components while hiding the details of the implementation so they can be changed without affecting users.ThemesResource 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.ThemesError handling: who detects errors, who reports them, and how? When an error is detected, what recovery is attempted?TopicsCSV - Comma separated values CSV - Prototype library CSV - A library for others CSV - A C++ implementationTopicsInterface 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 spreadsheetsExample“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 PrototypeDoesn’t handle long input lines or lots of fieldsLines terminated by newlinesFields are separated by commas and surrounding quotes removed – no embedded quotesInput line not preserved – overwritten when creating fieldsNo data saved from one input line to the nextAccess to fields through global variable (does not prevent access beyond last field)Decisions Made in PrototypeGlobal variables make code unsuitable to multi-threaded environment or interleaved callsCaller must open and close filesInput and splitting are inextricably linkedThe 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 codeSpecificationFields are separated by commasA field may be enclosed in double-quotesA quoted field may contain commas but not newlinesA quoted field may contain double-quotes, represented by “”Fields may be empty; “” and empty string both represent an empty fieldLeading and trailing white space is preservedSpecification – csvgetline()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 – csvfield()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 separatorSpecification – csvfield() (cont)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 – csvnfield()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


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?