00001 #include "Sct/Serializable.h"
00002 #include <iostream>
00003 #include <list>
00004 #include <fstream>
00005 #include <sstream>
00006 #include "Sct/IS/IOManagerIS.h"
00007 #include "Sct/File/IOManagerFile.h"
00008 #include "Sct/Archive/IOManagerArchiveFile.h"
00009 #include "Sct/IoExceptions.h"
00010 #include "SctData/TestResult.h"
00011 #include "SummaryWriter/SummaryManager.h"
00012 #include "ipc/core.h"
00013
00014 std::string file;
00015 std::list<std::string> names;
00016
00017 shared_ptr<Sct::Serializable> readObject(string name) {
00018 FILE* f = fopen(name.c_str(), "rb");
00019 if (f) {
00020 fclose(f);
00021 if (name.substr(name.size()-3) == ".gz"){
00022 return Sct::Archive::IOManagerArchiveFile::instance().read(name);
00023 } else {
00024 return Sct::File::IOManagerFile::instance().read(name);
00025 }
00026 } else {
00027 return Sct::IS::IOManagerIS::instance().read(name);
00028 }
00029 }
00030
00031 void printHelp() {
00032 std::cerr << "Usage: SummaryExtractor [-file <filename>] <Names>" << std::endl;
00033 std::cerr << "Print the TestSummary information about a test" << std::endl;
00034 std::cerr << "Options: -file <filename> write to named file rather than to standard output" << std::endl;
00035 }
00036
00037 void handleArgs(int argc, char** argv) {
00038 if (argc == 1) {
00039 printHelp();
00040 exit(0);
00041 }
00042 for (int i=1; i<argc; ++i) {
00043 if (string(argv[i])=="-file"){
00044 if (i+1 >= argc) {
00045 std::cerr << "Invalid arguments for option -file" << std::endl;
00046 printHelp();
00047 exit(1);
00048 }
00049 file=argv[i+1];
00050
00051 i++;
00052 }else{
00053
00054 names.push_back(argv[i]);
00055 }
00056 }
00057 }
00058
00059 void writeOut(std::ostream& os){
00060 for (std::list<string>::const_iterator it = names.begin();
00061 it!=names.end(); ++it){
00062 try{
00063 boost::shared_ptr<Sct::Serializable> ob = readObject(*it);
00064 if (!ob.get()) throw Sct::IllegalStateError("readObject returned null", __FILE__, __LINE__);
00065 boost::shared_ptr<SctData::TestResult> tr = boost::dynamic_pointer_cast<SctData::TestResult>(ob);
00066 if (!tr.get()){
00067 std::ostringstream msg;
00068 msg << "Not a TestResult : " << (*it);
00069 throw Sct::IoException(msg.str(), __FILE__, __LINE__);
00070 }
00071 SctData::TestSummary::SummaryManager::instance().write(*tr, os);
00072 }catch(Sct::Throwable& e){
00073 std::cerr << "ERROR:" << e.what() << std::endl;
00074 }
00075 }
00076 }
00077
00078 int main(int argc, char** argv){
00079 IPCCore::init(argc,argv);
00080 setExceptionHandlers(argv[0]);
00081 handleArgs(argc,argv);
00082 if (file==""){
00083 writeOut(std::cout);
00084 }else{
00085 std::ofstream f(file.c_str());
00086 if (!f.good()){
00087 std::cerr << "SummaryExtractor: Bad output file name "
00088 << file << std::endl;
00089 exit(1);
00090 }
00091 writeOut(f);
00092 }
00093 exit (0);
00094 }