Week 7

Exceptions

exception.cpp
1:	 class Exception {
2:	 public:  
3:	   virtual string getError();
4:	 private:
5:	   string error;
6:	 }
7:	 
8:	 class AlarmException: public Exception {
9:	 public:
10:	   string getError();
11:	 }
12:	 
13:	 class FileException: public Exception {
14:	 public:
15:	   virtual string getError();
16:	 }
17:	 
18:	 class PermissionException: public FileException {
19:	 public:
20:	   string getError();
21:	 }
22:	 
23:	 class FileNotFoundException: public FileException {
24:	 public:
25:	   string getError();
26:	 }
27:	 
28:	 int foo(int a) {
29:	   try {
30:	     bar(a);
31:	   } catch (FileNotFoundException e) {
32:	     cout << "caught thrown filenotfound exception " << e.getError() << endl;
33:	   } catch (FileException e) {
34:	     cout << "caught thrown file exception " << e.getError() << endl;
35:	   } catch (PermissionException e) {
36:	     cout << "caught thrown permission exception " << e.getError() << endl;
37:	   } catch (AlarmException e) {
38:	     cout << "caught thrown alarm exception " << e.getError() << endl;
39:	   } catch (...) {
40:	     throw;
41:	   }
42:	   cout << "At end of foo()" << endl;
43:	 }
44:	 
45:	 int bar(int a) {
46:	   switch (a) {
47:	   case 0:
48:	     throw AlarmException();
49:	   case 1:
50:	     throw FileException();
51:	   case 2:
52:	     throw PermissionException();
53:	   case 3:
54:	     throw FileNotFoundException();
55:	   default:
56:	     throw Exception();
57:	   }
58:	 }          
  1. Where does the control flow if there are no exceptions?
  2. What are the results for foo(0), foo(1), foo(2), foo(3)?
  3. What if bar threw a OutOfMemory exception?
  4. What happens if the catch all (...) was removed?

Linked Lists

ListNode.h
1:	 class ListNode {
2:	  public:
3:	   void add(int i);
4:	   void delete(int index);
5:	   void elementAt(int index);
6:	   int data();
7:	   ListNode* next();
8:	  private:
9:	   int value;
10:	   ListNode* nextNode = 0;
11:	 }

  1. Draw a box and pointer diagram for the list ( 1 ) and ( 1 2 3 ).
  2. What does a empty list look like?
  3. Implement the functions for the ListNode.
  4. How could add() be done faster?
  5. How would you implement the function elementFromBack()?

Programming Assignment 3

XML

3 components

expat

An open-source XML parser. You are free to use others, but this is the most simple.

Building and using expat

  1. Download and unpack expat - tar zxvf expat-1.95.8.tar.gz
  2. Run ./configure --prefix=LIBDIR where LIBDIR is a directory you want to install the library.
  3. Run make, then make install to build and install the library.
  4. Copy LIBDIR/include/*.h and LIBDIR/lib/libexpat.so to your project directory. There are the only files you'll need to statically link against expat.
  5. Modify your CFLAGS in the Makefile to include "-L. -lexpat".
  6. Include the expat.h header file to the code making expat library calls.
expat library functions testnet.xml
1:	 <NETLIST>
2:	 <COMPONENT DESIGNATION="C1" FOOTPRINT="SM/C/0805" LABEL="33nF" />
3:	 <NET NAME="N1">
4:	   <CONNECTION  COMPONENT="C1">
5:	     <PIN>1</PIN>
6:	     <PIN>2</PIN>
7:	   </CONNECTION>
8:	 </NET>
9:	 </NETLIST>
xml.cpp
1:	 #include "expat.h"
2:	 #include <fstream>
3:	 #include <string>
4:	 #include <iostream>
5:	 #include <cctype>
6:	 
7:	 using namespace std;
8:	 
9:	 void print_attr(const char **attr) {
10:	   for(int i=0; attr[i] != 0; i+=2) {
11:	     cout << " " << attr[i] << "=" << "\"" << attr[i+1] << "\"";
12:	   }
13:	   cout << ">" << endl;
14:	 }
15:	 
16:	 static void XMLCALL
17:	 start(void *userdata, const char *el, const char **attr) {
18:	   string e(el);
19:	   if (e == "COMPONENT") {
20:	     cout << "<" << "COMPONENT";
21:	     print_attr(attr);
22:	   } else if (e == "NET") {
23:	     cout << "<" << "NET";
24:	     print_attr(attr);
25:	   } else if (e == "CONNECTION") {
26:	     cout << "<" << "CONNECTION ";
27:	     print_attr(attr);
28:	   } else if (e == "PIN") {
29:	     cout << "<" << "PIN";
30:	     print_attr(attr);
31:	   } else {
32:	     cout << "<" << el << ">" << endl;
33:	   }
34:	 }
35:	 
36:	 static void XMLCALL
37:	 end(void *userdata, const char *el) {
38:	   cout << "" << endl;
39:	 }
40:	 
41:	 static void XMLCALL
42:	 data(void *userdata, const char *s, int len) {
43:	   string ss(s,len);
44:	   while(isspace(ss[0])) {
45:	     ss.erase(0,1);
46:	   }
47:	   if (ss != "") {
48:	     cout << ss << endl;
49:	   }
50:	 }
51:	 
52:	 int main(int argc, char* argv[]) {
53:	   XML_Parser p = XML_ParserCreate(NULL);
54:	   XML_SetElementHandler(p, start, end);
55:	   XML_SetCharacterDataHandler(p, data);
56:	   ifstream f(argv[1]);
57:	   string buf;
58:	   string tmp;
59:	 
60:	   while (!f.eof()) {
61:	     getline(f, tmp);
62:	     buf += tmp;
63:	   }
64:	 
65:	   XML_Parse(p, buf.c_str(), buf.length(), 1);
66:	 }
Submission Instructions

Make sure your program compiles with make. No compile, no credit.
Create a README file containing

Submit your assignment with the following commands:
make clean
handin cs40at pa3 *
Make sure you include the built xml library in your submission. You don't need to submit the sources for the xml library.