Skip to content
Snippets Groups Projects
Commit 5a9186dd authored by Owen Arnold's avatar Owen Arnold
Browse files

refs #6216. Better file type identification.

parent ad838363
No related merge requests found
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
#include "MantidAPI/AlgorithmManager.h" #include "MantidAPI/AlgorithmManager.h"
#include <vtkPVGlyphFilter.h> #include <vtkPVGlyphFilter.h>
#include <boost/algorithm/string.hpp>
vtkCxxRevisionMacro(vtkPeaksReader, "$Revision: 1.0 $"); vtkCxxRevisionMacro(vtkPeaksReader, "$Revision: 1.0 $");
vtkStandardNewMacro(vtkPeaksReader); vtkStandardNewMacro(vtkPeaksReader);
...@@ -137,9 +139,26 @@ void vtkPeaksReader::PrintSelf(ostream& os, vtkIndent indent) ...@@ -137,9 +139,26 @@ void vtkPeaksReader::PrintSelf(ostream& os, vtkIndent indent)
this->Superclass::PrintSelf(os,indent); this->Superclass::PrintSelf(os,indent);
} }
int vtkPeaksReader::CanReadFile(const char* vtkNotUsed(fname)) int vtkPeaksReader::CanReadFile(const char* fname)
{ {
return 1; //TODO: Apply checks here. const std::string fileString(fname);
const int startExtension = fileString.find_last_of('.');
const int endExtension = fileString.length();
if(startExtension >= endExtension)
{
throw std::runtime_error("File has no extension.");
}
std::string extension = fileString.substr(startExtension, endExtension - startExtension);
boost::algorithm::to_lower(extension);
boost::algorithm::trim(extension);
if(extension == ".peaks")
{
return 1;
}
else
{
return 0;
}
} }
unsigned long vtkPeaksReader::GetMTime() unsigned long vtkPeaksReader::GetMTime()
......
...@@ -56,6 +56,7 @@ namespace Mantid ...@@ -56,6 +56,7 @@ namespace Mantid
Mantid::Geometry::IMDDimension_sptr tDimension; Mantid::Geometry::IMDDimension_sptr tDimension;
virtual void appendMetadata(vtkDataSet* visualDataSet, const std::string& wsName) ; virtual void appendMetadata(vtkDataSet* visualDataSet, const std::string& wsName) ;
virtual void extractMetadata(Mantid::API::IMDEventWorkspace_sptr eventWs); virtual void extractMetadata(Mantid::API::IMDEventWorkspace_sptr eventWs);
virtual bool canLoadFileBasedOnExtension(const std::string& filename, const std::string& expectedExtension) const;
virtual bool shouldLoad(); virtual bool shouldLoad();
bool m_isSetup; bool m_isSetup;
double m_time; double m_time;
...@@ -64,6 +65,8 @@ namespace Mantid ...@@ -64,6 +65,8 @@ namespace Mantid
bool m_firstLoad; bool m_firstLoad;
}; };
} }
} }
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include <vtkUnstructuredGrid.h> #include <vtkUnstructuredGrid.h>
namespace Mantid namespace Mantid
{ {
namespace VATES namespace VATES
...@@ -42,6 +41,11 @@ namespace Mantid ...@@ -42,6 +41,11 @@ namespace Mantid
*/ */
bool EventNexusLoadingPresenter::canReadFile() const bool EventNexusLoadingPresenter::canReadFile() const
{ {
if(!canLoadFileBasedOnExtension(m_filename, ".nxs"))
{
return 0;
}
::NeXus::File * file = NULL; ::NeXus::File * file = NULL;
try try
{ {
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "MantidNexusCPP/NeXusFile.hpp" #include "MantidNexusCPP/NeXusFile.hpp"
#include "MantidNexusCPP/NeXusException.hpp" #include "MantidNexusCPP/NeXusException.hpp"
#include "MantidAPI/AlgorithmManager.h" #include "MantidAPI/AlgorithmManager.h"
#include <vtkUnstructuredGrid.h> #include <vtkUnstructuredGrid.h>
namespace Mantid namespace Mantid
...@@ -42,6 +41,12 @@ namespace Mantid ...@@ -42,6 +41,12 @@ namespace Mantid
*/ */
bool MDEWEventNexusLoadingPresenter::canReadFile() const bool MDEWEventNexusLoadingPresenter::canReadFile() const
{ {
// Quick check based on extension.
if(!canLoadFileBasedOnExtension(m_filename, ".nxs"))
{
return 0;
}
::NeXus::File * file = NULL; ::NeXus::File * file = NULL;
file = new ::NeXus::File(this->m_filename); file = new ::NeXus::File(this->m_filename);
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "MantidVatesAPI/MetadataToFieldData.h" #include "MantidVatesAPI/MetadataToFieldData.h"
#include "MantidVatesAPI/RebinningCutterXMLDefinitions.h" #include "MantidVatesAPI/RebinningCutterXMLDefinitions.h"
#include <boost/algorithm/string.hpp>
#include <vtkFieldData.h> #include <vtkFieldData.h>
#include <vtkDataSet.h> #include <vtkDataSet.h>
...@@ -116,6 +116,23 @@ namespace Mantid ...@@ -116,6 +116,23 @@ namespace Mantid
//Return decision. //Return decision.
return bExecute; return bExecute;
} }
/**
Determines wheter the file can be loaded based on it's extension.
@param filename containing the extension
@param expectedExtension expected extension for the file to have
@return TRUE, only if the extension is approved.
*/
bool MDEWLoadingPresenter::canLoadFileBasedOnExtension(const std::string& filename, const std::string& expectedExtension) const
{
// Quick check based on extension.
const int startExtension = filename.find_last_of('.');
const int endExtension = filename.length();
std::string extension = filename.substr(startExtension, endExtension - startExtension);
boost::algorithm::to_lower(extension);
boost::algorithm::trim(extension);
return extension == expectedExtension;
}
/* /*
Append the geometry and function information onto the outgoing vtkDataSet. Append the geometry and function information onto the outgoing vtkDataSet.
......
...@@ -64,6 +64,12 @@ private: ...@@ -64,6 +64,12 @@ private:
return BaseClass::shouldLoad(); return BaseClass::shouldLoad();
} }
bool canLoadFileBasedOnExtension(const std::string& filename, const std::string& extension)
{
//Forwarding method.
return BaseClass::canLoadFileBasedOnExtension(filename, extension);
}
~ConcreteMDEWLoadingPresenter(){} ~ConcreteMDEWLoadingPresenter(){}
}; };
...@@ -164,6 +170,22 @@ void testDepthChanged() ...@@ -164,6 +170,22 @@ void testDepthChanged()
TSM_ASSERT("This is a 4D workspace with an integrated T dimension", presenter.hasTDimensionAvailable()); TSM_ASSERT("This is a 4D workspace with an integrated T dimension", presenter.hasTDimensionAvailable());
} }
void testCanLoadFileBasedOnExtension()
{
MockMDLoadingView* view = new MockMDLoadingView;
ConcreteMDEWLoadingPresenter presenter(view);
// constructive tests
TSM_ASSERT("Should be an exact match", presenter.canLoadFileBasedOnExtension("somefile.nxs", ".nxs"));
TSM_ASSERT("Should lowercase uppercase extension", presenter.canLoadFileBasedOnExtension("somefile.NXS", ".nxs"));
TSM_ASSERT("Should strip off whitespace", presenter.canLoadFileBasedOnExtension("somefile.nxs ", ".nxs"));
// destructive tests
TSM_ASSERT("Extensions do not match, should return false.", !presenter.canLoadFileBasedOnExtension("somefile.nx", ".nxs"));
delete view;
}
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment