Skip to content

Commit

Permalink
Add pathExists & classTypeExists checks to HDFDDescriptor. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jul 4, 2013
1 parent da30646 commit 367b623
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 41 deletions.
11 changes: 9 additions & 2 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/HDFDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "MantidKernel/ClassMacros.h"
#include "MantidKernel/DllConfig.h"

#include <map>
#include <string>

namespace Mantid
Expand Down Expand Up @@ -56,6 +57,9 @@ namespace Mantid
public:
/// Constructor accepting a filename
HDFDescriptor(const std::string & filename);
/// Destructor
~HDFDescriptor();

/**
* Access the filename
* @returns A reference to a const string containing the filename
Expand All @@ -68,7 +72,9 @@ namespace Mantid
inline const std::string & extension() const { return m_extension; }

/// Query if a path exists
bool pathExists(const std::string&) const;
bool pathExists(const std::string& path) const;
/// Query if a given type exists somewhere in the file
bool classTypeExists(const std::string & classType) const;

private:
DISABLE_DEFAULT_CONSTRUCT(HDFDescriptor);
Expand All @@ -77,11 +83,12 @@ namespace Mantid
/// Initialize object with filename
void initialize(const std::string& filename);


/// Full filename
std::string m_filename;
/// Extension
std::string m_extension;
/// Map of types to full path strings.
std::multimap<std::string, std::string> *m_typesToPaths;
};


Expand Down
62 changes: 37 additions & 25 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_filename(), m_extension(), m_typesToPaths(NULL)
{
if(filename.empty())
{
Expand All @@ -108,18 +108,48 @@ namespace Mantid
{
throw std::invalid_argument("HDFDescriptor() - File '" + filename + "' does not exist");
}
initialize(filename);
try
{
initialize(filename);
}
catch(::NeXus::Exception &)
{
throw std::invalid_argument("HDFDescriptor::initialize - File '" + filename + "' does not look like a HDF file.");
}
}

/**
*/
HDFDescriptor::~HDFDescriptor()
{
delete m_typesToPaths;
}


/**
* @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
*/
bool HDFDescriptor::pathExists(const std::string& path) const
{
return true;
auto iend = m_typesToPaths->end();
for(auto it = m_typesToPaths->begin(); it != iend; ++it)
{
if(path == it->second) return true;
}
return false;
}

/**
* @param classType A string name giving a class type
* @return True if the type exists in the file, false otherwise
*/
bool HDFDescriptor::classTypeExists(const std::string & classType) const
{
return (m_typesToPaths->find(classType) != m_typesToPaths->end());
}


//---------------------------------------------------------------------------------------------------------------------------
// HDFDescriptor private methods
//---------------------------------------------------------------------------------------------------------------------------
Expand All @@ -132,30 +162,12 @@ namespace Mantid
m_filename = filename;
m_extension = "." + Poco::Path(filename).getExtension();

try
{
::NeXus::File file(this->filename());
}
catch(::NeXus::Exception &)
{
throw std::invalid_argument("HDFDescriptor::initialize - File '" + filename + "' does not look like a HDF file.");
}
// // Root node has no type and is named "/"
// m_root->name = "/";
//
// addChildren(file, "/", m_root);
//

// auto rootEntries = file.getEntries();
// for(auto it = rootEntries.begin(); rootEntries.end(); ++it)
// {
// auto node = boost::make_shared<Node>();
// node->name = it->first;
// node->type = it->second;
// m_roots.insert(std::make_pair(it->first, node));
// }
::NeXus::File file(this->filename());
m_typesToPaths = file.getTypeMap();
}




} // namespace Kernel
} // namespace Mantid
37 changes: 23 additions & 14 deletions Code/Mantid/Framework/Kernel/test/HDFDescriptorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/HDFDescriptor.h"

#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>

#include <Poco/Path.h>
#include <Poco/File.h>

Expand Down Expand Up @@ -43,6 +46,8 @@ class HDFDescriptorTest : public CxxTest::TestSuite
throw std::runtime_error("Unable to find test files for FileDescriptorTest. "
"The AutoTestData directory needs to be in the search path");
}

m_testHDF5 = boost::make_shared<HDFDescriptor>(m_testHDF5Path);
}

//=================================== Static isHDF methods ======================================
Expand Down Expand Up @@ -81,10 +86,8 @@ class HDFDescriptorTest : public CxxTest::TestSuite

void test_Constructor_Initializes_Object_Correctly_Given_HDF_File()
{
HDFDescriptor descr(m_testHDF5Path);

TS_ASSERT_EQUALS(m_testHDF5Path, descr.filename());
TS_ASSERT_EQUALS(".nxs", descr.extension());
TS_ASSERT_EQUALS(m_testHDF5Path, m_testHDF5->filename());
TS_ASSERT_EQUALS(".nxs", m_testHDF5->extension());
}

void test_Constructor_Throws_With_Empty_filename()
Expand All @@ -104,32 +107,38 @@ class HDFDescriptorTest : public CxxTest::TestSuite

void test_PathExists_Returns_False_For_Path_Not_In_File()
{
HDFDescriptor fd(m_testHDF5Path);

TS_ASSERT(!fd.pathExists("/raw_data_1/bank1"));
TS_ASSERT(!m_testHDF5->pathExists("/raw_data_1/bank1"));
}

void test_PathExists_Returns_False_For_Invalid_Path_Specification()
{
HDFDescriptor fd(m_testHDF5Path);

TS_ASSERT(!fd.pathExists("raw_data_1\\bank1"));
TS_ASSERT(!m_testHDF5->pathExists("raw_data_1\\bank1"));
}


void test_PathExists_Returns_True_For_Valid_Path_In_File()
void test_PathExists_Returns_False_For_Root_Path_Along()
{
HDFDescriptor fd(m_testHDF5Path);
TS_ASSERT(!m_testHDF5->pathExists("/"));
}

TS_ASSERT(fd.pathExists("/entry/bank1"));
void test_PathExists_Returns_True_For_Path_At_Any_Level_In_File()
{
TS_ASSERT(m_testHDF5->pathExists("/entry"));
TS_ASSERT(m_testHDF5->pathExists("/entry/bank1/data_x_y"));
}

void test_classTypeExists_Returns_True_For_Type_At_Any_Level_In_File()
{
TS_ASSERT(m_testHDF5->classTypeExists("NXentry"));
TS_ASSERT(m_testHDF5->classTypeExists("NXevent_data"));
TS_ASSERT(m_testHDF5->classTypeExists("NXlog"));
}

private:

std::string m_testHDF5Path;
std::string m_testHDF4Path;
std::string m_testNonHDFPath;
boost::shared_ptr<HDFDescriptor> m_testHDF5;
};


Expand Down

0 comments on commit 367b623

Please sign in to comment.