00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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, ifstream &ifs)
00032 throw (FS::Failure)
00033
00034
00035
00036
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
00068
00069
00070 {
00071
00072 Text fname(nm);
00073 if (nm[0] != '/') {
00074 fname = Config_SCachePath + '/' + fname;
00075 }
00076
00077
00078 ifstream ifs;
00079 if (!OpenFile(fname, ifs)) return;
00080
00081 try {
00082
00083 SMultiPKFileRep::Header hdr(ifs);
00084 hdr.ReadEntries(ifs);
00085 hdr.ReadPKFiles(ifs);
00086 FS::Close(ifs);
00087
00088
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
00097
00098 if((func != 0) &&
00099 ((he->pkfile->SourceFunc() == 0) ||
00100 (he->pkfile->SourceFunc()->FindText(func) == -1)))
00101 {
00102 continue;
00103 }
00104
00105
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
00112 while(entries != 0)
00113 {
00114 CE::T *entry = entries->Head();
00115 assert(entry != 0);
00116
00117
00118
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
00146
00147 int arg = 1;
00148 const char *func = 0;
00149
00150 for (; 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
00164 if (argc - arg < 1) {
00165 ExitProgram("no file arguments specified");
00166 }
00167 bool firstFile = true;
00168 for (; 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 }