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 {
163 bool =
false)
const {
164 return std::vector<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]);
std::string cwd
The current working directory.
std::string fullpath(std::string d) const
Get proper full path from possibly relative path.
std::map< std::string, IManagedObject * > ObjMap
Map of paths to objects.
ITree * findTree(const std::string &)
LWH cannot get a mounted ITree at a given path in the current ITree.
bool cp(const std::string &, const std::string &, bool=false)
Not implemented in LWH.
std::vector< std::string > listObjectNames(const std::string &=".", bool=false) const
Not implemented in LWH.
bool mkdir(const std::string &dir)
Create a new directory.
std::vector< std::string > Path
A path is a vector of directory names.
bool flat
If true write histograms in FLAT format, otherwise in XML.
std::string storeName() const
Get the name of the store.
bool rmdir(const std::string &dir)
Remove a directory and all the contents underneeth.
bool mkdirs(Path p)
Create a directory recursively.
The "master" factory from which other factories are obtained.
bool mv(const std::string &oldp, const std::string &newp)
Move an IManagedObject or a directory from one directory to another.
virtual bool writeXML(std::ostream &os, std::string path, std::string name)=0
Write out the object to the given stream in XML format.
bool rm(const std::string &path)
Remove and delete an IManagedObject by specifying its path.
IManagedObject * find(const std::string &path)
Get the IManagedObject at a given path in the ITree.
std::string stn(std::string s) const
Strip trailing name.
std::string pth2str(const Path &pth) const
Convert a Path object to a corresponding string.
bool symlink(const std::string &, const std::string &)
Not implemented in LWH.
bool overwrite
Overwrite strategy.
bool mkdirs(const std::string &dir)
Create a directory recursively.
std::string name
The filename to print histograms to.
Tree()
The default constructor.
void * cast(const std::string &) const
Not implemented in LWH.
bool mount(const std::string &, ITree &, const std::string &)
Not implemented in LWH.
bool close()
Calls commit().
std::vector< std::string > listObjectTypes(const std::string &=".", bool=false) const
Not implemented in LWH.
std::set< Path > PathSet
A set of paths.
std::string pwd() const
Get the path of the current working directory.
ObjMap objs
The set of defined objects.
virtual bool writeFLAT(std::ostream &os, std::string path, std::string name)=0
Write out the object to the given stream in simple table format.
bool ls(const std::string &=".", bool=false, std::ostream &=std::cout) const
Not implemented in LWH.
bool cd(const std::string &dir)
Change to a given directory.
bool unmount(const std::string &)
Not implemented in LWH.
void setOverwrite(bool o=true)
Not implemented in LWH.
The LWH namespace contains a Light-Weight Histogram package which implements the most rudimentary his...
bool insert(std::string str, IManagedObject *o)
Insert the ManagedObject o in the tree with the path str.
std::string findPath(const IManagedObject &o) const
Get the full path of an IManagedObject.
bool commit()
Print all histograms to the current filename.
std::string sts(std::string s) const
Strip trailing slash.
Tree(std::string storename, bool xml=true)
The standard constructor.
The Tree class is a simple implementation of the AIDA::ITree interface.
PathSet dirs
The set of defined directories.
virtual ~Tree()
Destructor.
Path str2pth(std::string s) const
Convert a string containing a path to a Path object.
Tree(const Tree &dt)
The copy constructor.
Path purgepath(const Path &pth) const
Remove '..' and '.