From ec3e85aaf4496543aca35c186d190c2eba46e111 Mon Sep 17 00:00:00 2001 From: Martyn Gigg <martyn.gigg@stfc.ac.uk> Date: Fri, 28 Jun 2013 20:00:59 +0100 Subject: [PATCH] Add root attribute query methods to HDFDescriptor. Refs #7263 --- .../Kernel/inc/MantidKernel/HDFDescriptor.h | 5 +++++ .../Framework/Kernel/src/HDFDescriptor.cpp | 19 +++++++++++++++---- .../Framework/Kernel/test/HDFDescriptorTest.h | 10 ++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/HDFDescriptor.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/HDFDescriptor.h index 80e90657f3c..6b149b90c8a 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/HDFDescriptor.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/HDFDescriptor.h @@ -5,6 +5,7 @@ #include "MantidKernel/DllConfig.h" #include <map> +#include <set> #include <string> namespace Mantid @@ -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 @@ -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; }; diff --git a/Code/Mantid/Framework/Kernel/src/HDFDescriptor.cpp b/Code/Mantid/Framework/Kernel/src/HDFDescriptor.cpp index 242b321e223..483dbd69612 100644 --- a/Code/Mantid/Framework/Kernel/src/HDFDescriptor.cpp +++ b/Code/Mantid/Framework/Kernel/src/HDFDescriptor.cpp @@ -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()) { @@ -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 @@ -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 diff --git a/Code/Mantid/Framework/Kernel/test/HDFDescriptorTest.h b/Code/Mantid/Framework/Kernel/test/HDFDescriptorTest.h index 296dc2c75e4..c599ef1d99c 100644 --- a/Code/Mantid/Framework/Kernel/test/HDFDescriptorTest.h +++ b/Code/Mantid/Framework/Kernel/test/HDFDescriptorTest.h @@ -105,6 +105,16 @@ public: 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")); -- GitLab