dirfilemanip.C

Go to the documentation of this file.
00001 #include "dirfilemanip.h"
00002 #include "TSystem.h"
00003 #include "emcDataManager.h"
00004 #include <fstream>
00005 
00006 bool createDirectory(const std::string& dir)
00007 {
00008   char* stmp = gSystem->ExpandPathName(dir.c_str());
00009   std::string sdir = stmp;
00010   delete[] stmp;
00011 
00012   void* pdir = gSystem->OpenDirectory(sdir.c_str());
00013   if (!pdir)
00014     {
00015       emcDataManager* dm = emcDataManager::GetInstance();
00016       if ( dm->GetVerboseLevel() )
00017         {
00018           std::cout << __FILE__ << " Trying to create directory "
00019                     << sdir << std::endl;
00020         }
00021       // The trailing slash makes ROOT returning failure = -1
00022       // though the directory *is* created... Strange, but we
00023       // have to live with it, so remove any traling slash.
00024       if ( sdir[sdir.size()-1] == '/' )
00025         {
00026           sdir = sdir.substr(0,sdir.size()-1);
00027         }
00028 
00029       int bad = gSystem->mkdir(sdir.c_str(), true);
00030       if (bad)
00031         {
00032           std::cerr << __FILE__ << " Could not create directory "
00033                     << sdir << std::endl;
00034           return false;
00035         }
00036       else
00037         {
00038           if ( dm->GetVerboseLevel() )
00039             {
00040               std::cout << sdir << " successfully created." << std::endl;
00041             }
00042           return true;
00043         }
00044     }
00045   else
00046     {
00047       gSystem->FreeDirectory(pdir);
00048       return true;
00049     }
00050 }
00051 
00052 bool checkFile(const std::string& file)
00053 {
00054   std::ifstream in(file.c_str());
00055   return (in.good());
00056 }
00057 
00058 std::string expand(const std::string& dir)
00059 {
00060   char* stmp = gSystem->ExpandPathName(dir.c_str());
00061   std::string rv(stmp);
00062   delete[] stmp;
00063   return rv;
00064 }