Skip to content

Commit

Permalink
Start on general file loader conversion. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jun 29, 2013
1 parent c0b04b5 commit 3f102e2
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 157 deletions.
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/IFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Mantid
{
public:
/// Returns a confidence value that this algorithm can load a file
virtual int confidence(const Kernel::FileDescriptor & descriptor) const = 0;
virtual int confidence(Kernel::FileDescriptor & descriptor) const = 0;
};

} // namespace API
Expand Down
18 changes: 17 additions & 1 deletion Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ namespace Mantid
//----------------------------------------------------------------------------------------------
// Anonymous namespace helpers
//----------------------------------------------------------------------------------------------
/// @cond
template<typename T>
struct DescriptorCallback
{
void apply(T &) {} //general one does nothing
};
template<>
struct DescriptorCallback<Kernel::FileDescriptor>
{
void apply(Kernel::FileDescriptor & descriptor) { descriptor.resetStreamToStart(); }
};
///endcond

/**
* @param descriptor A descriptor object describing the file
* @param names The collection of names to search through
Expand All @@ -22,12 +35,13 @@ namespace Mantid
* was found
*/
template<typename DescriptorType, typename FileLoaderType>
const std::string searchForLoader(const DescriptorType & descriptor,const std::set<std::string> & names,
const std::string searchForLoader(DescriptorType & descriptor,const std::set<std::string> & names,
Kernel::Logger & logger)
{
const auto & factory = AlgorithmFactory::Instance();
std::string bestLoader;
int maxConfidence(0);
DescriptorCallback<DescriptorType> callback;

auto iend = names.end();
for(auto it = names.begin(); it != iend; ++it)
Expand All @@ -39,6 +53,8 @@ namespace Mantid
try
{
const int confidence = alg->confidence(descriptor);
callback.apply(descriptor);

if(confidence > maxConfidence) // strictly greater
{
bestLoader = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/IFileLoader.h"

namespace Mantid
{
Expand Down Expand Up @@ -45,7 +44,7 @@ namespace Mantid
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport LoadAscii :public API::IDataFileChecker
class DLLExport LoadAscii :public API::IFileLoader
{
public:
/// Default constructor
Expand All @@ -56,11 +55,8 @@ namespace Mantid
virtual int version() const { return 1; }
/// The category
virtual const std::string category() const { return "DataHandling\\Text"; }
/// Do a quick check that this file can be loaded
virtual bool quickFileCheck(const std::string& filePath,size_t nread,const file_header& header);
/// check the structure of the file and return a value between
/// 0 and 100 of how much this file can be loaded
virtual int fileCheck(const std::string& filePath);
/// Returns a confidence value that this algorithm can load a file
virtual int confidence(Kernel::FileDescriptor & descriptor) const;

static bool isAscii(FILE *file);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IFileLoader.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/IDataFileChecker.h"
#include <Poco/DOM/Element.h>
#include <Poco/DOM/Node.h>
//----------------------------------------------------------------------
Expand Down Expand Up @@ -54,7 +53,7 @@ namespace Mantid
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport LoadCanSAS1D : public API::IDataFileChecker
class DLLExport LoadCanSAS1D : public API::IFileLoader
{
public:
///default constructor
Expand All @@ -68,10 +67,8 @@ namespace Mantid
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "DataHandling\\XML"; }

/// do a quick check that this file can be loaded
virtual bool quickFileCheck(const std::string& filePath,size_t nread,const file_header& header);
/// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded
virtual int fileCheck(const std::string& filePath);
/// Returns a confidence value that this algorithm can load a file
virtual int confidence(Kernel::FileDescriptor & descriptor) const;

protected:
/// Sets documentation strings for this algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//----------------------------------------------------------------------
#include "LoadCanSAS1D.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/IDataFileChecker.h"
#include <Poco/DOM/Element.h>
#include <Poco/DOM/Node.h>
//----------------------------------------------------------------------
Expand Down
81 changes: 17 additions & 64 deletions Code/Mantid/Framework/DataHandling/src/LoadAscii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This algorithm cannot load a file created by [[SaveAscii]] if it has X errors wr
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include <fstream>
Expand All @@ -38,10 +38,7 @@ namespace Mantid
{
namespace DataHandling
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadAscii)
//register the algorithm into loadalgorithm factory
DECLARE_LOADALGORITHM(LoadAscii)
DECLARE_FILELOADER_ALGORITHM(LoadAscii);

/// Sets documentation strings for this algorithm
void LoadAscii::initDocs()
Expand All @@ -59,72 +56,28 @@ namespace Mantid
{
}

/** This method does a quick file check by checking the no.of bytes read nread params and header buffer
* @param filePath :: path of the file including name.
* @param nread :: no.of bytes read
* @param header :: The first 100 bytes of the file as a union
* @return true if the given file is of type which can be loaded by this algorithm
*/
bool LoadAscii::quickFileCheck(const std::string& filePath,size_t nread,const file_header& header)
/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not be used
*/
int LoadAscii::confidence(Kernel::FileDescriptor & descriptor) const
{
std::string extn=extension(filePath);
if (!extn.compare("dat")||!extn.compare("csv")|| !extn.compare("txt")|| !extn.compare("")) //If the file is of type ascii then have a go
{
return true;
}
else //See if file looks like Ascii and have a go at doing something about it
{
bool is_ascii (true);
if( filePath.compare(filePath.size()-12,12,"_runinfo.xml") == 0)
is_ascii =false;
else if( filePath.compare(filePath.size()-6,6,".peaks") == 0)
is_ascii =false;
else if( filePath.compare(filePath.size()-10,10,".integrate") == 0)
is_ascii =false;
for(size_t i=0; i<nread; i++)
{
if (!isascii(header.full_hdr[i]))
is_ascii =false;
}
return (is_ascii);
}
}
const std::string & filePath = descriptor.filename();
const size_t filenameLength = filePath.size();

/**
* Checks the file by opening it and reading few lines
* @param filePath name of the file including its path
* @return an integer value how much this algorithm can load the file
*/
int LoadAscii::fileCheck(const std::string& filePath)
{
FILE* file = fopen(filePath.c_str(), "rb");
if (!file)
{
g_log.error("Unable to open file: " + filePath);
throw Exception::FileError("Unable to open file: " , filePath);
}

// Avoid some known file types that have different loaders
int confidence(0);
if( filePath.compare(filePath.size()-12,12,"_runinfo.xml") == 0)
{
fclose(file);
return confidence;
}
else if( filePath.compare(filePath.size()-6,6,".peaks") == 0)
{
fclose(file);
return confidence;
}
else if( filePath.compare(filePath.size()-10,10,".integrate") == 0)
if( filePath.compare(filenameLength - 12,12,"_runinfo.xml") == 0 ||
filePath.compare(filenameLength - 6,6,".peaks") == 0 ||
filePath.compare(filenameLength - 10,10,".integrate") == 0 )
{
fclose(file);
return confidence;
confidence = 0;
}
if (isAscii(file))
else if(Kernel::FileDescriptor::isAscii(descriptor.data()))
{
confidence = 10; //Lower because should load other files first
confidence = 10; // Low so that others may try
}
fclose(file);
return confidence;
}

Expand Down
107 changes: 45 additions & 62 deletions Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ If the file contains mulitple SASentry elements a workspace group will be create
//----------------------------------------------------------------------
#include "MantidDataHandling/LoadCanSAS1D.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/ConfigService.h"
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidDataObjects/Workspace2D.h"

#include <Poco/Path.h>
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/NodeList.h>
#include <Poco/DOM/Text.h>
#include <Poco/SAX/InputSource.h>

#include <boost/lexical_cast.hpp>
//-----------------------------------------------------------------------
Expand All @@ -44,11 +45,7 @@ namespace Mantid
namespace DataHandling
{

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(LoadCanSAS1D)

//register the algorithm into loadalgorithm factory
DECLARE_LOADALGORITHM(LoadCanSAS1D)
DECLARE_FILELOADER_ALGORITHM(LoadCanSAS1D);

/// Sets documentation strings for this algorithm
void LoadCanSAS1D::initDocs()
Expand All @@ -66,6 +63,47 @@ LoadCanSAS1D::LoadCanSAS1D() : m_groupNumber(0)
LoadCanSAS1D::~LoadCanSAS1D()
{}

/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not be used
*/
int LoadCanSAS1D::confidence(Kernel::FileDescriptor & descriptor) const
{
const std::string & extn = descriptor.extension();
if(extn.compare(".xml") != 0) return 0;

std::istream & is = descriptor.data();
int confidence(0);

{// start of inner scope
Poco::XML::InputSource src(is);
// Set up the DOM parser and parse xml file
DOMParser pParser;
Document* pDoc;
try
{
pDoc = pParser.parse(&src);
}
catch (...)
{
throw Kernel::Exception::FileError("Unable to parse File:", descriptor.filename());
}
// Get pointer to root element
Element* pRootElem = pDoc->documentElement();
if(pRootElem)
{
if(pRootElem->tagName().compare("SASroot") == 0)
{
confidence = 80;
}
}
pDoc->release();
}// end of inner scope

return confidence;
}

/// Overwrites Algorithm Init method.
void LoadCanSAS1D::init()
{
Expand Down Expand Up @@ -323,60 +361,5 @@ void LoadCanSAS1D::createLogs(const Poco::XML::Element * const sasEntry, API::Ma
}


/**This method does a quick file check by checking the no.of bytes read nread params and header buffer
* @param filePath- path of the file including name.
* @param nread :: no.of bytes read
* @param header :: The first 100 bytes of the file as a union
* @return true if the given file is of type which can be loaded by this algorithm
*/
bool LoadCanSAS1D::quickFileCheck(const std::string& filePath,size_t nread,const file_header& header)
{
std::string extn=extension(filePath);
bool bspice2d(false);
(!extn.compare("xml"))?bspice2d=true:bspice2d=false;

const char* xml_header="<?xml version=";
const char* full_hdr = reinterpret_cast<const char*>(header.full_hdr);
if ( ((unsigned)nread >= strlen(xml_header)) &&
!strncmp(full_hdr, xml_header, strlen(xml_header)) )
{
}
return(bspice2d?true:false);
}

/**checks the file by opening it and reading few lines
* @param filePath :: name of the file inluding its path
* @return an integer value how much this algorithm can load the file
*/
int LoadCanSAS1D::fileCheck(const std::string& filePath)
{
// Set up the DOM parser and parse xml file
DOMParser pParser;
Document* pDoc;
try
{
pDoc = pParser.parse(filePath);
}
catch (...)
{
throw Kernel::Exception::FileError("Unable to parse File:", filePath);
}
int confidence(0);
// Get pointer to root element
Element* pRootElem = pDoc->documentElement();
if(pRootElem)
{
if(pRootElem->tagName().compare("SASroot") == 0)
{
confidence = 80;
}
}
pDoc->release();
return confidence;

}



}
}

0 comments on commit 3f102e2

Please sign in to comment.