// Fig. 16.23: viewcart.cpp // Program to view books in the shopping cart. #include using std::cerr; using std::cout; using std::cin; using std::ios; #include #include using std::ifstream; using std::ofstream; #include using std::string; #include void outputBooks( const string &, const string & ); int main() { // variable to store query string char query[ 1024 ] = ""; char *cartData; // variable to hold contents of cart string dataString = ""; string cookieString = ""; string isbnEntered = ""; int contentLength = 0; // retrieve cookie data if ( getenv( "HTTP_COOKIE" ) ) { cartData = getenv( "HTTP_COOKIE" ); cookieString = cartData; } // end if // data was entered if ( getenv( "CONTENT_LENGTH" ) ) { contentLength = atoi( getenv( "CONTENT_LENGTH" ) ); cin.read( query, contentLength ); dataString = query; // find location of isbn value int addLocation = dataString.find( "add=" ) + 4; int endAdd = dataString.find( "&isbn" ); int isbnLocation = dataString.find( "isbn=" ) + 5; // retrieve isbn number to add to cart isbnEntered = dataString.substr( isbnLocation ); // write cookie string expires = "Friday, 14-MAY-10 16:00:00 GMT"; int cartLocation = cookieString.find( "CART=" ) + 5; // cookie exists if ( cartLocation > 0 ) cookieString = cookieString.substr( cartLocation ); // no cookie data exists if ( cookieString == "" ) cookieString = isbnEntered; // cookie data exists else cookieString = cookieString + "," + isbnEntered; // set cookie cout << "set-cookie: CART=" << cookieString << "; expires=" << expires << "; path=\n"; } // end if // output header cout << "Content-type: text/html\n\n"; // output XML declaration and DOCTYPE cout << "" << ""; // output html element and some of its contents cout << "" << "Shopping Cart" << "
" << "

Here is your current order:

"; // cookie data exists if ( cookieString != "" ) outputBooks( cookieString, isbnEntered ); cout << "\n"; return 0; } // end main // function to output books in catalog.txt void outputBooks( const string &cookieRef, const string &isbnRef ) { char book[ 50 ] = ""; char year[ 50 ] = ""; char isbn[ 50 ] = ""; char price[ 50 ] = ""; string bookString = ""; string yearString = ""; string isbnString = ""; string priceString = ""; // open file for input ifstream userData( "catalog.txt", ios::in ); // file could not be opened if ( !userData ) { cerr << "Could not open database."; exit( 1 ); } // end if // output link to log out and table to display books cout << "Sign Out"; cout << "

"; cout << ""; // file is open while ( userData ) { // retrieve book information userData.getline( book, 50 ); bookString = book; // retrieve year information userData.getline( year, 50 ); yearString = year; // retrieve isbn number userData.getline( isbn, 50 ); isbnString = isbn; // retrieve price userData.getline( price, 50 ); priceString = price; int match = cookieRef.find( isbn ); // match has been made if ( match > 0 || isbnRef == isbnString ) { // output table row with book information cout << "" << "" << "" << "" << "" << ""; } // end if cout << ""; } // end while // output link to add more books cout << "Back to book list"; } // end outputBooks /************************************************************************** * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * * Hall. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/
" << bookString << "" << yearString << "" << isbnString << "" << priceString << "