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

StageinlistExpander.cpp

Go to the documentation of this file.
00001 #include "cafe/StageinlistExpander.hpp"
00002 #include "cafe/Config.hpp"
00003 
00004 #include <cstdio>
00005 #include <iostream>
00006 #include <fstream>
00007 #include <stdexcept>
00008 #include <cassert>
00009 
00010 namespace cafe {
00011 
00012     StageinlistExpander::StageinlistExpander(const char *url): _localFile("")
00013     {
00014         Config config("cafe");
00015 
00016         _stageinCommand = config.get("Stager","cp");
00017         _stageinDirectory = config.get("StagingDirectory",".");
00018         _host             = config.get("Host","");
00019         if(!_host.empty()) {
00020             _host += ':';
00021         }
00022 
00023         const char *ptr = strchr(url, ':') + 1;
00024         _file.open(ptr);
00025         if(!_file) {
00026             throw std::runtime_error(std::string("StageinlistExpander: cannot find ") + url);
00027         }
00028     }
00029 
00030     StageinlistExpander::~StageinlistExpander()
00031     {
00032         clean();
00033         _file.close();
00034     }
00035 
00036     bool StageinlistExpander::fileExists(const std::string& fileName) const
00037     {
00038         // inspired from io_packages/RemoteFileExpander 
00039         std::ifstream file(fileName.c_str());
00040         bool result = file.good() && file.is_open();
00041         return result;
00042     }
00043 
00044     void StageinlistExpander::clean()
00045     {
00046         if ( _localFile.size() > 0 && fileExists(_localFile) ) std::remove(_localFile.c_str());
00047         _localFile = "";
00048     }
00049 
00050     std::string StageinlistExpander::nextFile()
00051     {
00052         using namespace std;
00053         clean();
00054 
00055         string url = "";
00056         if ( _file && _file >> url ) {
00057 
00058             string::size_type n = url.find(':');
00059             string remote_path = "";
00060             if (n != string::npos ) remote_path = url.substr(n+1);
00061             else remote_path = url;
00062             assert(remote_path.size() > 0);
00063             string::size_type m = remote_path.rfind('/');
00064             if(m < string::npos) {
00065                 ++m;
00066             } else {
00067                 m = 0;
00068             }
00069             _localFile = _stageinDirectory + '/' + remote_path.substr(m);
00070 
00071             if ( fileExists(_localFile) ) std::remove(_localFile.c_str());
00072             std::string cmd = _stageinCommand + ' ' + _host + url + ' ' + _localFile;
00073             std::cout << cmd << std::endl;
00074 
00075             int ok = system(cmd.c_str());
00076             if ( ok != 0 || !fileExists(_localFile)) {
00077                 clean();
00078                 std::string msg("StageinlistExpander: cannot find ");
00079                 msg+=_localFile;
00080                 throw std::runtime_error(msg);
00081             }
00082         }
00083 
00084         return _localFile;
00085     }
00086 }
00087 
00088 ClassImp(cafe::StageinlistExpander);

Generated on Tue Mar 28 10:13:04 2006 for CAF by doxygen 1.3.4