Script started on Mon May 5 18:38:30 2003 [root@Fizzgig /root]# cd C [root@Fizzgig C]# cat makefile updatelist: main.o updlist.o g++ -g main.o updlist.o -o updatelist main.o: main.cpp updlist.hpp g++ -c main.cpp updlist.o: updlist.cpp updlist.hpp g++ -c updlist.cpp [root@Fizzgig C]# cat updlist.hpp #include #include #include #include const int TRANSLEN = 2; //length of file fields const int KEYLEN = 20; const string HIGHVALUE = "zzzzzzzz"; const string INSERT = "I "; const string DELETE = "D "; const string CLEARTRANS = " "; class KeyedItem { public: string trans; string key; string value; KeyedItem(string t, string k, string v) { trans = t; key = k, value = v; } }; typedef list KEYEDLIST; KEYEDLIST runTransactions(ifstream& masterlist, ifstream& translist, int print = 0, int trans = TRANSLEN, int key = KEYLEN); KEYEDLIST runTransactions(KEYEDLIST master, KEYEDLIST trans, int print = 0); KEYEDLIST fileToKeyedList(ifstream& file, int trans = TRANSLEN, int key = KEYLEN); void printKeyedList(KEYEDLIST list, ostream& out = cout); [root@Fizzgig C]# cat updlist.cpp /* Functions for building and processing key-value lists */ /* in order to update a master list through a list of transactions */ /* Author: Zach Tomaszewski */ /* Date: 03 May 2003 */ #include "updlist.hpp" /* Given two valid file stream, Builds the necessary lists and runs the transactions against the master (as per the other runTransactions). Files should contain one record per line. The ints give the length of each section of the line: transaction type and key. Value extends to the end of the line. Returns an updated master. */ KEYEDLIST runTransactions(ifstream& masterfile, ifstream& transfile, int print = 0, int trans = TRANSLEN, int key = KEYLEN){ KEYEDLIST masterlist = fileToKeyedList(masterfile, trans, key); KEYEDLIST translist = fileToKeyedList(transfile, trans, key); return runTransactions(masterlist, translist, print); } /* Takes a input stream. Files should contain one record per line. The ints give the length of each section of the line: transaction type and key. Value extends to the end of the line. Returns the resulting list. */ KEYEDLIST fileToKeyedList(ifstream& file, int trans = TRANSLEN, int key = KEYLEN){ string line, tstr, kstr, vstr; KEYEDLIST list; while(!file.eof()){ getline(file, line); if (line.length() > 0) { tstr = line.substr(0, trans); kstr = line.substr(trans, key); vstr = line.substr(trans + key); list.push_back(KeyedItem(tstr, kstr, vstr)); } } return list; } /* Given a master list and a transaction list, Adds elements to master if item.trans is INSERT Removes elements from from master if item.trans is a DELETE. If print is 0: works silently If print is 1: Prints error message on unfound deletes and repeat inserts. If print is 2: Also prints summary of work to the screen. If print is 3: Also prints resulting master. Returns the new master list. */ KEYEDLIST runTransactions(KEYEDLIST master, KEYEDLIST trans, int print = 0){ int inserts = 0; int deletes = 0; int oldMasterLen = master.size(); //add high values master.push_back(KeyedItem(" ", HIGHVALUE, "")); trans.push_back(KeyedItem(" ", HIGHVALUE, "")); KEYEDLIST::iterator masterIter = master.begin(); KEYEDLIST::iterator transIter = trans.begin(); while((*masterIter).key < HIGHVALUE || (*transIter).key < HIGHVALUE) { if((*masterIter).key < (*transIter).key) { //master is less than trans. //Move ahead in master until find another transaction to do masterIter++; }else if((*masterIter).key > (*transIter).key) { //master is greater than trans //therefore have just passed where the current trans //needs to happen if ((*transIter).trans == DELETE){ //we didn't find the record if (print){ cout << "Could not delete (does not exist in master): "; cout << (*transIter).key << endl; } }else if ((*transIter).trans == INSERT){ //needs to go here (*transIter).trans = CLEARTRANS; masterIter = master.insert(masterIter, *transIter); inserts++; } transIter++; //on to next transaction } else { // they must be equal if ((*transIter).trans == DELETE){ //we found it, so delete it masterIter = master.erase(masterIter); //erasing moves masterIter to next space deletes++; }else if ((*transIter).trans == INSERT){ //record already exists! if (print){ cout << "Could not insert (already exists in master): "; cout<< (*transIter).key << endl; } } transIter++; //on to next transaction } } //all done; remove master's high value master.pop_back(); if (print >= 3) { printKeyedList(master); } if (print >= 2){ cout << oldMasterLen << " entries in original master file." << endl; cout << inserts << " inserts performed." << endl; cout << deletes << " deletes performed." << endl; cout << master.size() << " entries in new master file." << endl; } return master; } /* Prints a KeyedList to the screen. Default is to the screen, but will print to a file if given a stream. */ void printKeyedList(KEYEDLIST list, ostream& out = cout){ KEYEDLIST::iterator iter = list.begin(); while(iter != list.end()){ out << (*iter).trans << (*iter).key << (*iter).value << endl; iter++; } } [root@Fizzgig C]# cat main.cpp /* A program that updates a sorted filelist of CDs by processing a */ /* another sorted list of insertion and deletion transactions. */ /* Author: Zach Tomaszewski */ /* Date: 01 May 2003 */ #include "updlist.hpp" int main(int argc, char* argv[]) { string masterfilename, transfilename, outfilename; //get filenames switch (argc) { case (4): masterfilename = argv[1]; transfilename = argv[2]; outfilename = argv[3]; break; case (3): masterfilename = argv[1]; transfilename = argv[2]; break; default: cout << "This file updates a masterlist "; cout << "by prcocessing a transaction list." << endl; cout << "Enter masterlist filename: "; cin >> masterfilename; cout << "Enter transaction list filename: "; cin >> transfilename; cout << "Enter output filename for new masterlist "; cout << "(enter 0 for none): "; cin >> outfilename; } //open files ifstream masterfile(masterfilename.c_str()); if (!masterfile){ cout << "Could not open masterlist file: "; cout << masterfilename << endl; return(1); } ifstream transfile(transfilename.c_str()); if (!transfile){ cout << "Could not open transaction list file: "; cout << masterfilename << endl; return(1); } ofstream outfile; if (outfilename.length() > 0 && outfilename != "0"){ ofstream outfile(outfilename.c_str()); if (!outfile){ cout << "Could not open output file: "; cout << outfilename << endl; return(1); } } KEYEDLIST out = runTransactions(masterfile, transfile, 3); if (outfile){ printKeyedList(out, outfile); //fix outfile.close(); } return(0); } [root@Fizzgig C]# make g++ -c main.cpp g++ -c updlist.cpp g++ -g main.o updlist.o -o updatelist [root@Fizzgig C]# updatelist CDList.txt trans.txt Could not insert (already exists in master): Faure Could not delete (does not exist in master): Mendelssohn Artie Shaw Begin the Beguine Audra McDonald Way Back to Paradise Bach B Preludes and Fuges (Peter Hurford) Bach C Brandenburg Concertos (Vienna Academy Orchestra) Bach D Goldberg Variations (Claudio Arrau) Beethoven A Sonatas (Wilhelm Kempff) Beethoven B Symphonies No. 5 and No 6 (Berlin Philharmonic Orchestra) Benny Goodman After You've Gone Benny Goodman Orch. Sing, Sing, Sing Chopin A Nocturnes (Artur Rubenstein) Chopin B Concertos No. 1 & No. 2 (Artur Rubenstein) Chopin C Waltzes (Artur Rubenstein) Chopin D Cello Sonatas (Ofra Harnoy) Couperin French Music for Flute and Guitar (Schmidt/Verdery Duo) Dave Brubeck Quiet as the Moon Dave Grusin B A Gershwin Connection Dave Grusin C Collection Debussy B Preludes, Images Book II (Claudio Arrau) Debussy C Prelud es, Images Book I (Claudio Arrau) Duke Ellington Digital Duke Earl Klugh Solo Guitar Eliane Elias Plays Jobim Ella Fitzgerald Love Songs Faure Piano Music (Pascal Roge) Gene Krupa Uptown Gershwin A Rhapsody in Blue (Andre Previn) Gershwin B Gershwin Perfoems Gershwin Glen Miller B In the Digital Mood Grieg Lyric Pieces (Emil Gilels) Harper Brothers Remembrance Hoagy Carmichael Stardust and Much More John Field Nocturnes (John O'Conner) Keiko Matsui Keiko Lena Horne Being Myself Marcus Roberts A As Serenity Approaches Marcus Roberts B Deep in the Shed Marcus Roberts C The Truth is Spoken Here Marcus Roberts D Alone with Three Giants Marcus Roberts E Collection Mori Masako Collection Natalie Cole Unforgettable Patti Austin et al Happy Anniversity, Charlie Brown Rachmaninov Piano Concerto No. 2 (London Symphony Orchestra) Ravel Piano Works (Jean-Yves Thibaudet) Schumann A Humoreske (de Larrocha) Schumann B Piano Works (Wilhelm Klempff) Scott Joplin Piano Rags (Joshua Rifkin) Selena Super Exites Stravinsky Fire Bird Suite (Royal Concertgebouw Orchestra) Tchaikovsky A 1812 Overature (Chicago Symphony Orchestra) Tchaikovsky B Piano Concerto No. 1 (Boston Symphony Orchestra) Van Otter-Costello For the Stars Vaughn Williams Fantasy on Greensleeves (Neville Marriner) Vince Guaraldi Trio A Boy Named Charlie Brown Wynton Marsalis Joe Cool's Blues 49 entries in original master file. 12 inserts performed. 6 deletes performed. 55 entries in new master file. [root@Fizzgig C]# exit Script done on Mon May 5 18:40:05 2003