00001
00002 #include "NueAna/ParticlePID/Analysis/DirectoryHelpers.h"
00003
00004 using namespace std;
00005
00006
00007 void DirectoryHelpers::Tokenize(const string& str,
00008 vector<string>& tokens,
00009 const string& delimiters)
00010 {
00011
00012 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00013
00014 string::size_type pos = str.find_first_of(delimiters, lastPos);
00015
00016
00017
00018 while (string::npos != pos || string::npos != lastPos)
00019 {
00020
00021 string t = str.substr(lastPos, pos - lastPos);
00022 if(t!="")tokens.push_back(t);
00023
00024
00025
00026 lastPos = str.find_first_not_of(delimiters, pos);
00027
00028 pos = str.find_first_of(delimiters, lastPos);
00029 }
00030
00031 }
00032
00033
00034
00035 TDirectory * DirectoryHelpers::GetDirectory(TDirectory *f, string dir, int create)
00036 {
00037 if(!create)
00038 {
00039 TDirectory * r = f->GetDirectory(dir.c_str());
00040 if(r)return r;
00041
00042
00043 vector<string> paths;
00044 Tokenize(dir,paths,"/");
00045
00046 r = f->GetDirectory("/");
00047 for(int i=0;i<(int)paths.size();i++)
00048 {
00049 r=r->GetDirectory(paths[i].c_str());
00050 if(!r)break;
00051 }
00052 return r;
00053
00054 }
00055
00056
00057
00058 vector<string> paths;
00059 Tokenize(dir,paths,"/");
00060
00061
00062 TDirectory *tmp=f;
00063 TDirectory *tmpNew=0;
00064 for(int i=0;i<(int)paths.size();i++)
00065 {
00066 tmpNew=tmp->GetDirectory(paths[i].c_str());
00067 if(!tmpNew)
00068 tmp->mkdir(paths[i].c_str());
00069 tmpNew=tmp->GetDirectory(paths[i].c_str());
00070 tmp=tmpNew;
00071 }
00072
00073
00074
00075 if(create==2)
00076 {
00077 tmp->Close();
00078 tmp->Write();
00079 }
00080
00081 return tmp;
00082 }
00083
00084
00085 TDirectory * DirectoryHelpers::GetDirectory(TFile *f, string dir, int create)
00086 {
00087 TDirectory *ret = GetDirectory(f->GetDirectory("/"),dir,create);
00088
00089 if(!ret)printf("failed to get directory %s\n",dir.c_str());
00090
00091 return ret;
00092 }
00093
00094
00095
00096
00097 int DirectoryHelpers::MakeTrueDirectory(vector<string> paths, int pos)
00098 {
00099
00100 if(paths.size()>0)
00101 {
00102 string str ="/";
00103 if(pos > (int)paths.size())return 1;
00104
00105 for(int i=0;i<pos;i++)str+=paths[i]+"/";
00106
00107 int res = mkdir(str.c_str(),0755);
00108
00109 return MakeTrueDirectory(paths,pos+1) || res;
00110
00111 }
00112
00113 return 10;
00114 }
00115
00116
00117 int DirectoryHelpers::MakeTrueDirectory(string dir)
00118 {
00119 printf("Making directory %s\n",dir.c_str());
00120
00121 vector<string> paths;
00122 Tokenize(dir,paths,"/");
00123
00124 return MakeTrueDirectory(paths,1);
00125 }
00126
00127
00128