16 #include "ManagedObject.h"
40 typedef std::vector<std::string>
Path;
46 typedef std::map<std::string, IManagedObject *>
ObjMap;
53 Tree(std::string storename,
bool xml =
true)
54 : name(storename), flat(!xml), cwd(
"/"), overwrite(true) {
61 Tree(): name(
""), flat(false), cwd(
"/") {
69 : ITree(dt), name(dt.name), flat(dt.flat), dirs(dt.dirs),
70 objs(dt.objs), cwd(dt.cwd), overwrite(true) {}
74 for ( ObjMap::iterator it = objs.begin(); it != objs.end(); ++it )
92 IManagedObject *
find(
const std::string & path) {
93 ObjMap::const_iterator it = objs.find(path);
94 return it == objs.end()? (IManagedObject *)0: it->second;
111 bool cd(
const std::string & dir) {
112 PathSet::iterator it = dirs.find(purgepath(str2pth(fullpath(sts(dir)))));
113 if ( it == dirs.end() )
return false;
121 bool insert(std::string str, IManagedObject * o) {
122 Path path = purgepath(str2pth(fullpath(str)));
123 if ( dirs.find(path) == dirs.end() ) {
124 std::string fullname = pth2str(path);
126 if ( dirs.find(path) != dirs.end() ) {
127 ObjMap::iterator old = objs.find(fullname);
128 if ( old == objs.end() || overwrite ) {
129 if ( old != objs.end() ) {
154 bool ls(
const std::string & =
".",
bool =
false,
155 std::ostream & = std::cout)
const {
162 std::vector<std::string> listObjectNames(
const std::string & =
".",
163 bool =
false)
const {
164 return std::vector<std::string>();
170 std::vector<std::string> listObjectTypes(
const std::string & =
".",
171 bool =
false)
const {
172 return std::vector<std::string>();
182 bool mkdir(
const std::string & dir) {
183 Path p = purgepath(str2pth(fullpath(sts(dir))));
186 if ( dirs.find(base) == dirs.end() )
return false;
199 return mkdirs(purgepath(str2pth(fullpath(sts(dir)))));
210 if ( dirs.find(p) != dirs.end() )
return true;
222 bool rmdir(
const std::string & dir) {
223 Path path = purgepath(str2pth(fullpath(sts(dir))));
224 if ( dirs.find(path) == dirs.end() )
return false;
225 for ( ObjMap::const_iterator it = objs.begin(); it != objs.end(); ++it )
226 if ( it->first.substr(0, dir.length()) == dir )
return false;
237 bool rm(
const std::string & path) {
238 ObjMap::iterator it = objs.find(fullpath(path));
239 if ( it == objs.end() )
return false;
251 std::string
findPath(
const IManagedObject & o)
const {
252 for ( ObjMap::const_iterator it = objs.begin(); it != objs.end(); ++it )
253 if ( it->second == &o )
return it->first;
264 bool mv(
const std::string & oldp,
const std::string & newp) {
265 Path newpath = purgepath(str2pth(fullpath(sts(newp))));
266 std::string foldp = fullpath(oldp);
267 Path oldpath = purgepath(str2pth(foldp));
268 ObjMap::iterator it = objs.find(foldp);
269 if ( it == objs.end() )
return false;
270 if ( dirs.find(newpath) != dirs.end() )
return false;
271 newpath.push_back(oldpath.back());
272 if ( !insert(pth2str(newpath), it->second) )
return false;
282 std::ofstream of(name.c_str());
283 if ( !of )
return false;
285 <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE aida SYSTEM "
286 <<
"\"http://aida.freehep.org/schemas/3.0/aida.dtd\">\n"
287 <<
"<aida version=\"3.0\">\n"
288 <<
"<implementation version=\"1.0\" package=\"FreeHEP\"/>" << std::endl;
289 for ( ObjMap::const_iterator it = objs.begin(); it != objs.end(); ++it ) {
292 std::string path = it->first.substr(0, it->first.rfind(
'/'));
293 std::string name = it->first.substr(it->first.rfind(
'/') + 1);
299 if ( !flat ) of <<
"</aida>" << std::endl;
314 bool cp(
const std::string &,
const std::string &,
bool =
false) {
322 bool symlink(
const std::string &,
const std::string &) {
330 bool mount(
const std::string &, ITree &,
const std::string &) {
353 void *
cast(
const std::string &)
const {
360 std::string
sts(std::string s)
const {
361 if ( s[s.length() - 1] ==
'/' ) s = s.substr(0, s.length() - 1);
362 if ( s[s.length() - 1] ==
'/' )
return "";
367 std::string
stn(std::string s)
const {
368 std::string::size_type slash = s.rfind(
'/');
369 return s.substr(0, slash);
374 if ( d[0] !=
'/' ) d = cwd +
"/" + d;
375 return pth2str(purgepath(str2pth(d)));
381 std::string::size_type i = s.find_first_not_of(
"/");
382 while ( i != std::string::npos ) {
384 i = s.find_first_of(
"/");
385 pth.push_back(s.substr(0, i));
386 if ( i == std::string::npos )
return pth;
388 i = s.find_first_not_of(
"/");
396 for (
int i = 0, N = pth.size(); i < N; ++i ) str +=
"/" + pth[i];
403 for (
int i = 0, N = pth.size(); i < N; ++i ) {
404 if ( pth[i] ==
".." ) p.pop_back();
405 else if ( pth[i] !=
"." ) p.push_back(pth[i]);