Skip to content

Commit

Permalink
Add root attribute query methods to HDFDescriptor. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jun 29, 2013
1 parent eb79a4e commit 4293cc3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/HDFDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "MantidKernel/DllConfig.h"

#include <map>
#include <set>
#include <string>

namespace Mantid
Expand Down Expand Up @@ -71,6 +72,8 @@ namespace Mantid
*/
inline const std::string & extension() const { return m_extension; }

/// Query if the given attribute exists on the root node
bool hasRootAttr(const std::string &name) const;
/// Query if a path exists
bool pathExists(const std::string& path) const;
/// Query if a path exists of a given type
Expand All @@ -89,6 +92,8 @@ namespace Mantid
std::string m_filename;
/// Extension
std::string m_extension;
/// Root attributes
std::set<std::string> m_rootAttrs;
/// Map of types to full path strings.
std::multimap<std::string, std::string> *m_typesToPaths;
};
Expand Down
19 changes: 15 additions & 4 deletions Code/Mantid/Framework/Kernel/src/HDFDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace Mantid
* involves simply checking for the signature if a HDF file at the start of the file
*/
HDFDescriptor::HDFDescriptor(const std::string & filename)
: m_filename(), m_extension(), m_typesToPaths(NULL)
: m_filename(), m_extension(), m_rootAttrs(), m_typesToPaths(NULL)
{
if(filename.empty())
{
Expand Down Expand Up @@ -126,6 +126,15 @@ namespace Mantid
}


/**
* @param name The name of an attribute
* @return True if the attribute exists, false otherwise
*/
bool HDFDescriptor::hasRootAttr(const std::string &name) const
{
return (m_rootAttrs.count(name) == 1);
}

/**
* @param path A string giving a path using UNIX-style path separators (/), e.g. /raw_data_1, /entry/bank1
* @return True if the path exists in the file, false otherwise
Expand Down Expand Up @@ -179,11 +188,13 @@ namespace Mantid
m_extension = "." + Poco::Path(filename).getExtension();

::NeXus::File file(this->filename());
auto attrInfos = file.getAttrInfos();
for(size_t i = 0; i < attrInfos.size(); ++i)
{
m_rootAttrs.insert(attrInfos[i].name);
}
m_typesToPaths = file.getTypeMap();
}




} // namespace Kernel
} // namespace Mantid
10 changes: 10 additions & 0 deletions Code/Mantid/Framework/Kernel/test/HDFDescriptorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ class HDFDescriptorTest : public CxxTest::TestSuite
TS_ASSERT_THROWS(HDFDescriptor fd(m_testNonHDFPath), std::invalid_argument);
}

void test_hasRootAttr_Returns_True_For_Existing_Attr()
{
TS_ASSERT(m_testHDF5->hasRootAttr("file_time"));
}

void test_hasRootAttr_Returns_False_For_Non_Existing_Attr()
{
TS_ASSERT(!m_testHDF5->hasRootAttr("not_attr"));
}

void test_PathExists_Returns_False_For_Path_Not_In_File()
{
TS_ASSERT(!m_testHDF5->pathExists("/raw_data_1/bank1"));
Expand Down

0 comments on commit 4293cc3

Please sign in to comment.