00001 #include "IONameArchiveFile.h"
00002 #include "../SctNames.h"
00003
00004 #include <fstream>
00005
00006 namespace Sct {
00007 namespace Archive {
00008
00009 IONameArchiveFile::IONameArchiveFile(const string& fileName) throw(InvalidArgument) : IOName(fileName) {
00010 IONameArchiveFile::parse();
00011 }
00012
00013 IONameArchiveFile::IONameArchiveFile(const UniqueID& uniqueID, const string& className) throw() : IOName(uniqueID, className) {
00014 IONameArchiveFile::construct();
00015 }
00016
00017 IONameArchiveFile::~IONameArchiveFile() throw() {
00018 }
00019
00020 IONameArchiveFile::IONameArchiveFile() {
00021 }
00022
00023 bool IONameArchiveFile::wildcard()const{
00024 return (nameIO.find("*")!=string::npos);
00025 }
00026
00027 void IONameArchiveFile::expandWildcards(){
00028 if (m_wildcardsExpanded || !wildcard() ) return;
00029 m_wildcardsExpanded=true;
00030 std::ostringstream oss; oss << SctNames::getTempDir() << "/ArchivingList.XXXXXX";
00031 char tempfile_template[oss.str().size()];
00032 char* tempfile= tempfile_template;
00033 strcpy(tempfile, oss.str().c_str());
00034 mkstemp(tempfile);
00035 string fullcmd="ls "; fullcmd += nameIO;
00036 fullcmd += " > "; fullcmd += tempfile;
00037 if (system (fullcmd.c_str() ) !=0 ) {
00038 throw Sct::IoError(string("Error executing command :") + fullcmd, __FILE__, __LINE__);
00039 }
00040 std::ifstream f(tempfile);
00041 while (f.good()){
00042 string name; f>> name; if(!f.good()) break;
00043 shared_ptr<IONameArchiveFile> item (new IONameArchiveFile(name) );
00044 m_list.push_back(item);
00045 }
00046 }
00047
00048 void IONameArchiveFile::parse() throw(InvalidArgument) {
00049 int ldot = nameIO.rfind(".gz");
00050 int slash = nameIO.rfind('/');
00051 int dot2 = nameIO.find('.', ++slash);
00052
00053 if (ldot==string::npos || slash==string::npos || dot2==string::npos){
00054 throw InvalidArgument(string("Error parsing IONameArchiveFile ") + nameIO,
00055 __FILE__, __LINE__);
00056 }
00057 className = nameIO.substr(slash, dot2-slash);
00058 uniqueID = UniqueID(nameIO.substr(dot2+1, ldot-dot2-1));
00059 }
00060
00061 void IONameArchiveFile::construct() throw() {
00062 nameIO = SctNames::getPersistentDir();
00063 nameIO += "/" + className;
00064 nameIO += "." + (string) uniqueID;
00065 nameIO += ".gz";
00066 }
00067
00068 }
00069 }