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

ExtractValueBytes.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 // Last modified on Sun Feb 16 10:06:07 EST 2003 by ken@xorian.net  
00020 //      modified on Sat Feb 12 13:11:57 PST 2000 by mann  
00021 //      modified on Sun Aug 23 11:38:57 PDT 1998 by heydon
00022 
00023 // ExtractValueBytes -- print the contents of a MultiPKFile
00024 
00025 #include <Basics.H>
00026 #include <FS.H>
00027 #include <CacheArgs.H>
00028 #include <CacheConfig.H>
00029 #include <SMultiPKFileRep.H>
00030 
00031 static bool OpenFile(const Text fname, /*OUT*/ ifstream &ifs)
00032   throw (FS::Failure)
00033 /* Open the file named "fname" for reading. If successful, set "ifs" to a
00034    stream on the file and return true. If the file does not exist, print an
00035    error message and return false. If any other error occurs, throw
00036    "FS::Failure".
00037 */
00038 {
00039     try {
00040         FS::OpenReadOnly(fname, ifs);
00041     }
00042     catch (FS::DoesNotExist) {
00043         cerr << "Error: unable to open '" << fname << "' for reading." << endl;
00044         return false;
00045     }
00046     return true;
00047 }
00048 
00049 static void WriteValueFile(CacheEntry::Index ci, const VestaVal::T *value)
00050 {
00051   assert(value != 0);
00052   assert(value->bytes != 0);
00053 
00054   char vale_fname[100];
00055   sprintf(vale_fname, "value.ci%d", ci);
00056 
00057   ofstream value_file;
00058   FS::OpenForWriting(vale_fname, value_file);
00059 
00060   FS::Write(value_file, value->bytes, value->len);
00061 
00062   FS::Close(value_file);
00063 }
00064 
00065 static void ExtarctFromFile(const char *nm, const char *func = 0)
00066   throw (SMultiPKFileRep::BadMPKFile, FS::EndOfFile, FS::Failure)
00067 /* Open and read the MultiPKFile named by the file "nm", and extarct
00068    the bytes of each cache entry into a file named
00069    "value.ci<index>". */
00070 {
00071     // form the filename
00072     Text fname(nm);
00073     if (nm[0] != '/') {
00074         fname = Config_SCachePath + '/' + fname;
00075     }
00076 
00077     // open the file
00078     ifstream ifs;
00079     if (!OpenFile(fname, /*OUT*/ ifs)) return;
00080 
00081     try {
00082         // read it (including the PKFiles)
00083         SMultiPKFileRep::Header hdr(ifs);
00084         hdr.ReadEntries(ifs);
00085         hdr.ReadPKFiles(ifs);
00086         FS::Close(ifs);
00087 
00088         // Loop over the PKFiles
00089         for (int i = 0; i < hdr.num; i++) {
00090             SMultiPKFileRep::HeaderEntry *he;
00091             bool inTbl = hdr.pkTbl.Get(*(hdr.pkSeq[i]), he);
00092             assert(inTbl);
00093 
00094             if (he->pkfile != (SPKFile *)NULL) {
00095 
00096               // If a function limiter was specified, only proceed if
00097               // this PKFile matches it.
00098               if((func != 0) &&
00099                  ((he->pkfile->SourceFunc() == 0) ||
00100                   (he->pkfile->SourceFunc()->FindText(func) == -1)))
00101                 {
00102                   continue;
00103                 }
00104 
00105               // Loop over the CFP groups in the PKFile
00106               SPKFile::CFPEntryIter cfp_it(he->pkfile->OldEntries());
00107               FP::Tag cfp;
00108               CE::List *entries;
00109               while(cfp_it.Next(cfp, entries))
00110                 {
00111                   // Loop over the entries in the CFP group
00112                   while(entries != 0)
00113                     {
00114                       CE::T *entry = entries->Head();
00115                       assert(entry != 0);
00116 
00117                       // Write out the bytes for this value into a new
00118                       // file.
00119                       WriteValueFile(entry->CI(), entry->Value());
00120 
00121                       entries = entries->Tail();
00122                     }
00123                 }
00124             }
00125         }
00126     } catch (...) {
00127         FS::Close(ifs);
00128         throw;
00129     }
00130 }
00131 
00132 static const char *g_program_name = 0;
00133 
00134 static void ExitProgram(char *msg) throw ()
00135 {
00136     cerr << "Fatal error: " << msg << endl;
00137     cerr << "Syntax: " << g_program_name << " [-func <function>] file ..." << endl;
00138     exit(1);
00139 }
00140 
00141 int main(int argc, char *argv[])
00142 {
00143   g_program_name = argv[0];
00144 
00145     // process command-line switch(es). (@@@ Should allow the user to
00146     // specify the CIs to be extracted.)
00147     int arg = 1;
00148     const char *func = 0;
00149 
00150     for (/*SKIP*/; arg < argc && *argv[arg] == '-'; arg++) {
00151         if (CacheArgs::StartsWith(argv[arg], "-func")) {
00152           if(func != 0)
00153             {
00154               ExitProgram("only one -func allowed");
00155             }
00156           func = argv[++arg];
00157         } else {
00158             ExitProgram("unrecognized command-line option");
00159         }
00160     }
00161 
00162 
00163     // process file arguments
00164     if (argc - arg < 1) {
00165         ExitProgram("no file arguments specified");
00166     }
00167     bool firstFile = true; // determines whether to print a separating newline
00168     for (/*SKIP*/; arg < argc; arg++) {
00169         try {
00170             if (firstFile) firstFile = false; else cout << endl;
00171             ExtarctFromFile(argv[arg], func);
00172         }
00173         catch (SMultiPKFileRep::BadMPKFile) {
00174             cerr << "Error: '" << argv[arg]
00175                  << "' is a bad MultiPKFile" << endl;
00176         }
00177         catch (FS::EndOfFile) {
00178             cerr << "Fatal error: unexpected end-of-file" << endl;
00179         }
00180         catch (const FS::Failure &f) {
00181             cerr << f;
00182         }
00183     }
00184     return 0;
00185 }

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