Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

ShortIdSetDiff.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Created on Thu Aug 21 22:35:17 PDT 1997 by heydon
00020 // Last modified on Mon Aug  9 18:06:25 EDT 2004 by ken@xorian.net  
00021 //      modified on Sat Feb 12 17:57:32 PST 2000 by mann  
00022 //      modified on Thu Aug 21 23:43:21 PDT 1997 by heydon
00023 
00024 #include <Basics.H>
00025 #include <FS.H>
00026 #include <Generics.H>
00027 #include <SourceOrDerived.H>
00028 
00029 #include "HexReader.H"
00030 
00031 using std::ios;
00032 using std::ostream;
00033 using std::cout;
00034 using std::cerr;
00035 using std::endl;
00036 
00037 static void Syntax(char *msg, char *arg = NULL) throw ()
00038 {
00039     cerr << "Error: " << msg;
00040     if (arg != NULL) cerr << ": '" << arg << "'";
00041     cerr << endl;
00042     cerr << "Syntax: ShortIdSetDiff file1 file2" << endl;
00043     exit(1);
00044 }
00045 
00046 static char *FileName(char *name) throw ()
00047 {
00048     char *res = name;
00049     if (strncmp(name, "0x", 2) == 0) {
00050         // skip initial "0x"
00051         char *arg = name + 2;
00052 
00053         // parse numeric value
00054         ShortId sid;
00055         if (sscanf(arg, "%x", /*OUT*/ &sid) != 1) {
00056             Syntax("bad 'shortId' argument", name);
00057         }
00058 
00059         // convert to filename
00060         res = SourceOrDerived::shortIdToName(sid, /*tailOnly=*/ false);
00061     }
00062     return res;
00063 }
00064 
00065 static void ReadFile2(char *path, /*INOUT*/ IntIntTbl &elts)
00066   throw (FS::DoesNotExist, FS::Failure, HexReader::BadValue)
00067 {
00068     ShortId sid, totalCnt = 0, uniqCnt = 0;
00069     HexReader hexrd(path);
00070     while ((sid = hexrd.Next()) != 0) {
00071         int dummy;
00072         totalCnt++;
00073         if (!elts.Get(sid, /*OUT*/ dummy)) {
00074             uniqCnt++;
00075             bool inTbl = elts.Put(sid, 0); assert(!inTbl);
00076         }
00077     }
00078     cerr << "  Total lines = " << totalCnt;
00079     cerr << "; unique lines = " << uniqCnt << endl;
00080 }
00081 
00082 static void ProcessFile1(char *path, ostream &os, const IntIntTbl &elts)
00083   throw (FS::DoesNotExist, FS::Failure, HexReader::BadValue)
00084 {
00085     // put "os" in correct mode for printing hex results
00086     os.setf(ios::hex,ios::basefield);
00087 
00088     // do work
00089     HexReader hexrd(path);
00090     ShortId sid, totalCnt = 0, uniqCnt = 0, writtenCnt = 0;
00091     IntIntTbl myElts(/*sizeHint=*/ 5000);
00092     while ((sid = hexrd.Next()) != 0) {
00093         totalCnt++;
00094         int dummy;
00095         if (!myElts.Get(sid, /*OUT*/ dummy)) {
00096             uniqCnt++;
00097             bool inTbl = myElts.Put(sid, 0); assert(!inTbl);
00098             if (!elts.Get(sid, /*OUT*/ dummy)) {
00099                 writtenCnt++;
00100                 os.width(8); os << sid << endl;
00101             }
00102         }
00103     }
00104     cerr << "  Total lines = " << totalCnt;
00105     cerr << "; unique lines = " << uniqCnt;
00106     cerr << "; written lines = " << writtenCnt << endl;
00107 }
00108 
00109 int main(int argc, char *argv[]) 
00110 {
00111     if (argc != 3) Syntax("incorrect number of arguments");
00112     try {
00113         char *file2 = FileName(argv[2]);
00114         cerr << "Reading " << file2 << "..." << endl;
00115         IntIntTbl elts(/*sizeHint=*/ 5000);
00116         ReadFile2(file2, /*INOUT*/ elts);
00117 
00118         char *file1 = FileName(argv[1]);
00119         cerr << "Reading " << file1 << "..." << endl;
00120         ProcessFile1(file1, cout, elts);
00121     } catch (const FS::DoesNotExist) {
00122         cerr << "Fatal error: file does not exist" << endl;
00123     } catch (const FS::Failure &f) {
00124         cerr << "Fatal error: " << f << endl;
00125     } catch (const HexReader::BadValue &v) {
00126         cerr << "Fatal error: bad value '" << v.str
00127              << "', line " << v.lineNum << endl;
00128     }
00129     return 0;
00130 }

Generated on Mon May 8 00:48:42 2006 for Vesta by  doxygen 1.4.2