diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/IFileLoader.h b/Code/Mantid/Framework/API/inc/MantidAPI/IFileLoader.h index 57781f4f5c4eeca212dccc7c49eef43c384b2069..65ae5d0bed0aef59eaf3f883dd0c24b950d24310 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/IFileLoader.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/IFileLoader.h @@ -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 diff --git a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp index c62e909e42dd781166c9e73f484f7c25b494c778..6d90dd86ad60b8dd6061131b546400edddd08787 100644 --- a/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp +++ b/Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp @@ -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 @@ -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) @@ -39,6 +53,8 @@ namespace Mantid try { const int confidence = alg->confidence(descriptor); + callback.apply(descriptor); + if(confidence > maxConfidence) // strictly greater { bestLoader = name; diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadAscii.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadAscii.h index 81d703fe6e70d8f1677c3d695ca2a88a51e63d4a..e15ed8996f97ce3d7a0de0b85034fbd32b9d8bb9 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadAscii.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadAscii.h @@ -4,8 +4,7 @@ //---------------------------------------------------------------------- // Includes //---------------------------------------------------------------------- -#include "MantidAPI/Algorithm.h" -#include "MantidAPI/IDataFileChecker.h" +#include "MantidAPI/IFileLoader.h" namespace Mantid { @@ -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 @@ -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); diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadCanSAS1D.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadCanSAS1D.h index 21f67da10862687e6ea7cdaed754c13402f9f2d0..c8fa693b399fd98e441aa3abb090b41b4d073fe1 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadCanSAS1D.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadCanSAS1D.h @@ -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> //---------------------------------------------------------------------- @@ -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 @@ -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 diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadCanSAS1D2.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadCanSAS1D2.h index 7afd76b8789b75409031a48f1e8248e00ab26720..53cbdd9f32c76c0817557b999c4919f27cd91757 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadCanSAS1D2.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadCanSAS1D2.h @@ -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> //---------------------------------------------------------------------- diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadDaveGrp.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadDaveGrp.h index 79e09dd963b95adcaa3bab4bc81f365e2bc4e1f8..7048e83554954f864cea40b876f0dbcde32ab77a 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadDaveGrp.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadDaveGrp.h @@ -1,8 +1,7 @@ #ifndef MANTID_DATAHANDLING_LOADDAVEGRP_H_ #define MANTID_DATAHANDLING_LOADDAVEGRP_H_ -#include "MantidAPI/Algorithm.h" -#include "MantidAPI/IDataFileChecker.h" +#include "MantidAPI/IFileLoader.h" #include <fstream> #include <string> @@ -43,7 +42,7 @@ namespace DataHandling File change history is stored at: <https://github.com/mantidproject/mantid> Code Documentation is available at: <http://doxygen.mantidproject.org> */ -class DLLExport LoadDaveGrp : public API::IDataFileChecker +class DLLExport LoadDaveGrp : public API::IFileLoader { public: /// Constructor @@ -56,24 +55,8 @@ public: virtual int version() const { return (1); } /// Algorithm's category for identification virtual const std::string category() const { return "DataHandling\\Text;Inelastic"; } - /** - * Do a quick check that this file can be loaded - * - * @param filePath the location of and the file to check - * @param nread number of bytes to read - * @param header the first 100 bytes of the file as a union - * @return true if the file can be loaded, otherwise false - */ - virtual bool quickFileCheck(const std::string& filePath, std::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 - * - * @param filePath the location of and the file to check - * @return a confidence level indicator between 0 and 100 - */ - 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; private: /// Sets documentation strings for this algorithm diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadEventPreNexus.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadEventPreNexus.h index 15c6f0d41d82778b5a61341b1d908e477db3c043..44995b590c384234dfb89768db757557c32d79de 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadEventPreNexus.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadEventPreNexus.h @@ -4,7 +4,7 @@ #include <fstream> #include <string> #include <vector> -#include "MantidAPI/IDataFileChecker.h" +#include "MantidAPI/IFileLoader.h" #include "MantidKernel/BinaryFile.h" #include "MantidDataObjects/EventWorkspace.h" #include "MantidDataObjects/Events.h" @@ -96,7 +96,7 @@ struct Pulse #pragma pack(pop) -class DLLExport LoadEventPreNexus : public API::IDataFileChecker +class DLLExport LoadEventPreNexus : public API::IFileLoader { public: /// Constructor @@ -112,12 +112,8 @@ public: /// Algorithm's aliases virtual const std::string alias() const { return "LoadEventPreNeXus"; } - /// Returns the name of the property to be considered as the Filename for Load - virtual const char * filePropertyName() const; - /// do a quick check that this file can be loaded - 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 - int fileCheck(const std::string& filePath); + /// Returns a confidence value that this algorithm can load a file + virtual int confidence(Kernel::FileDescriptor & descriptor) const; private: /// Sets documentation strings for this algorithm diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadEventPreNexus2.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadEventPreNexus2.h index c0add144e4d412a0ddc18a557b1cca1683fdb0ca..7c3a164b96c048b0882b665d760775b34d4c2fdb 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadEventPreNexus2.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadEventPreNexus2.h @@ -4,7 +4,7 @@ #include <fstream> #include <string> #include <vector> -#include "MantidAPI/IDataFileChecker.h" +#include "MantidAPI/IFileLoader.h" #include "MantidKernel/BinaryFile.h" #include "MantidDataObjects/EventWorkspace.h" #include "MantidDataObjects/Events.h" @@ -96,7 +96,7 @@ struct Pulse #pragma pack(pop) -class DLLExport LoadEventPreNexus2 : public API::IDataFileChecker +class DLLExport LoadEventPreNexus2 : public API::IFileLoader { public: /// Constructor @@ -112,12 +112,8 @@ public: /// Algorithm's aliases virtual const std::string alias() const { return "LoadEventPreNeXus2"; } - /// Returns the name of the property to be considered as the Filename for Load - virtual const char * filePropertyName() const; - /// do a quick check that this file can be loaded - 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 - int fileCheck(const std::string& filePath); + /// Returns a confidence value that this algorithm can load a file + virtual int confidence(Kernel::FileDescriptor & descriptor) const; private: /// Sets documentation strings for this algorithm diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadGSS.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadGSS.h index d3730fa7003809ceeabddacef23f7fb551ecc6ba..996724c3b2a01f2f7248bd0d5e188a8b41e920e1 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadGSS.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadGSS.h @@ -4,8 +4,7 @@ //--------------------------------------------------- // Includes //--------------------------------------------------- -#include "MantidAPI/Algorithm.h" -#include "MantidAPI/IDataFileChecker.h" +#include "MantidAPI/IFileLoader.h" namespace Mantid { @@ -37,7 +36,7 @@ namespace DataHandling File change history is stored at: <https://github.com/mantidproject/mantid> Code Documentation is available at: <http://doxygen.mantidproject.org> */ -class DLLExport LoadGSS : public API::IDataFileChecker +class DLLExport LoadGSS : public API::IFileLoader { public: /// (Empty) Constructor @@ -51,10 +50,8 @@ public: /// Algorithm's category for identification virtual const std::string category() const { return "Diffraction;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; private: /// Sets documentation strings for this algorithm diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadPDFgetNFile.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadPDFgetNFile.h index 28b2645b5efa7b807bf69cd61b39fe57b738ea99..71cf00ddc5ba851c56d0c23b9e8437afb1d9ac9b 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadPDFgetNFile.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadPDFgetNFile.h @@ -2,8 +2,7 @@ #define MANTID_DATAHANDLING_LOADPDFGETNFILE_H_ #include "MantidKernel/System.h" -#include "MantidAPI/Algorithm.h" -#include "MantidAPI/IDataFileChecker.h" +#include "MantidAPI/IFileLoader.h" #include "MantidDataObjects/Workspace2D.h" namespace Mantid @@ -33,7 +32,7 @@ namespace DataHandling File change history is stored at: <https://github.com/mantidproject/mantid> Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadPDFgetNFile : public API::IDataFileChecker + class DLLExport LoadPDFgetNFile : public API::IFileLoader { public: LoadPDFgetNFile(); @@ -55,16 +54,14 @@ namespace DataHandling void init(); /// Implement abstract Algorithm methods void exec(); - /// 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; /// Parse PDFgetN data file void parseDataFile(std::string filename); /// Check whether a string starts from a specified sub-string - bool startsWith(std::string s, std::string header); + bool startsWith(const std::string &s, const std::string & header) const; /// Parse column name line staring with \#L void parseColumnNameLine(std::string line); diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadPreNexus.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadPreNexus.h index a1b7bdb75401b3675691f12b866d29daf51fe6c2..d74cfd977c69f82cdcd0f8102a58ccf2462d7983 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadPreNexus.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadPreNexus.h @@ -3,10 +3,9 @@ #include <string> #include <vector> -#include "MantidAPI/IDataFileChecker.h" -#include "MantidKernel/System.h" -#include "MantidAPI/Algorithm.h" +#include "MantidAPI/IFileLoader.h" #include "MantidAPI/IEventWorkspace.h" +#include "MantidKernel/System.h" namespace Mantid { @@ -37,7 +36,7 @@ namespace DataHandling File change history is stored at: <https://github.com/mantidproject/mantid> Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadPreNexus : public API::IDataFileChecker + class DLLExport LoadPreNexus : public API::IFileLoader { public: LoadPreNexus(); @@ -46,10 +45,9 @@ namespace DataHandling virtual const std::string name() const; virtual int version() const; virtual const std::string category() const; - virtual const char * filePropertyName() const; void parseRuninfo(const std::string &runinfo, std::string &dataDir, std::vector<std::string> &eventFilenames); - bool quickFileCheck(const std::string& filePath,size_t nread,const file_header& header); - int fileCheck(const std::string& filePath); + /// Returns a confidence value that this algorithm can load a file + virtual int confidence(Kernel::FileDescriptor & descriptor) const; private: virtual void initDocs(); void init(); diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadQKK.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadQKK.h index e690a73a2540acbf0008447b1a86248dbde5459b..ff29308095b4a4715e9ffc86795405b743e39a3e 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadQKK.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadQKK.h @@ -11,7 +11,7 @@ namespace Mantid namespace DataHandling { /** - Loads a Quokka data file. Implements API::IDataFileChecker and its file check methods to + Loads a Quokka data file. Implements API::IFileLoader and its file check methods to recognise a file as the one containing QUOKKA data. @author Roman Tolchenov, Tessella plc diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadRaw3.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadRaw3.h index f4ad3e8bffd83b8101b9ce026d920702feead684..4b64e76fe145d5d75d86d190e8d15f3fcc51e3fe 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadRaw3.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadRaw3.h @@ -4,10 +4,8 @@ //---------------------------------------------------------------------- // Includes //---------------------------------------------------------------------- -#include "MantidAPI/Algorithm.h" -#include "MantidDataObjects/Workspace2D.h" #include "MantidDataHandling/LoadRawHelper.h" -#include "MantidAPI/IDataFileChecker.h" +#include "MantidDataObjects/Workspace2D.h" #include <climits> //---------------------------------------------------------------------- @@ -61,12 +59,6 @@ namespace Mantid /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling\\Raw"; } - /// 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); - - private: /// Sets documentation strings for this algorithm virtual void initDocs(); diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadRawHelper.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadRawHelper.h index d0019fb52d810ddfac2960d1c6edd79b0d8e4e9d..20c5214dcb71138ce5ee9feb4aea04c98efe76a3 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadRawHelper.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/LoadRawHelper.h @@ -4,11 +4,10 @@ //---------------------------------------------------------------------- // Includes //---------------------------------------------------------------------- -#include "MantidAPI/Algorithm.h" +#include "MantidAPI/IFileLoader.h" #include "MantidDataObjects/Workspace2D.h" #include "MantidDataHandling/ISISRunLogs.h" #include "MantidAPI/Run.h" -#include "MantidAPI/IDataFileChecker.h" #include <climits> //---------------------------------------------------------------------- @@ -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 LoadRawHelper: public API::IDataFileChecker + class DLLExport LoadRawHelper: public API::IFileLoader { public: /// Default constructor @@ -70,10 +69,8 @@ namespace Mantid /// Read in run parameters Public so that LoadRaw2 can use it void loadRunParameters(API::MatrixWorkspace_sptr localWorkspace, ISISRAW * const = NULL) const; - /// 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 if this file can be loaded return a value between 1 and 100 - 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: /// Overwrites Algorithm method. @@ -186,8 +183,6 @@ namespace Mantid /// Overwrites Algorithm method void exec(); - /// Check if the buffer looks like a RAW file header - bool isRawFileHeader(const int nread, const unsigned char* buffer) const; /// convert month label to int string std::string convertMonthLabelToIntStr(std::string month) const; diff --git a/Code/Mantid/Framework/DataHandling/src/LoadAscii.cpp b/Code/Mantid/Framework/DataHandling/src/LoadAscii.cpp index 3bc0fe186ae3f44ced8b47d35a0e48abc529b282..0035fc613c3c84ffeef995a1d158d47cb03f3876 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadAscii.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadAscii.cpp @@ -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> @@ -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() @@ -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; } diff --git a/Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D.cpp b/Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D.cpp index 03360c09608ab33b220cbe0ea0db6d2c34f1fb88..15bd4a5707771ed18d7e9c27025ef0daf28a1ba6 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D.cpp @@ -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> //----------------------------------------------------------------------- @@ -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() @@ -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() { @@ -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; - -} - - - } } diff --git a/Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D2.cpp b/Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D2.cpp index 04e94e8cc6a7991c8ce8ebd391683163890746b4..6e1f20d414b025193537d029bf7ffe6363ecffda 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D2.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadCanSAS1D2.cpp @@ -12,13 +12,13 @@ If the file contains mulitple SASentry elements a workspace group will be create //---------------------------------------------------------------------- #include "MantidDataHandling/LoadCanSAS1D2.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> @@ -44,11 +44,7 @@ namespace Mantid namespace DataHandling { -// Register the algorithm into the AlgorithmFactory -DECLARE_ALGORITHM(LoadCanSAS1D2) - -//register the algorithm into loadalgorithm factory -DECLARE_LOADALGORITHM(LoadCanSAS1D2) +DECLARE_FILELOADER_ALGORITHM(LoadCanSAS1D2); /// constructor LoadCanSAS1D2::LoadCanSAS1D2() : LoadCanSAS1D() diff --git a/Code/Mantid/Framework/DataHandling/src/LoadDaveGrp.cpp b/Code/Mantid/Framework/DataHandling/src/LoadDaveGrp.cpp index c875dbaa46f02eb614dcd0e9b6c0c5361c02e673..bbbaa37e4cae1b82017a3a9473659517db377085 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadDaveGrp.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadDaveGrp.cpp @@ -8,9 +8,9 @@ This algorithm loads data from a DAVE grouped ASCII file. These have been genera #include "MantidDataHandling/LoadDaveGrp.h" #include "MantidDataObjects/Workspace2D.h" #include "MantidAPI/FileProperty.h" -#include "MantidAPI/LoadAlgorithmFactory.h" #include "MantidAPI/NumericAxis.h" #include "MantidAPI/Progress.h" +#include "MantidAPI/RegisterFileLoader.h" #include "MantidKernel/ListValidator.h" #include "MantidKernel/PropertyWithValue.h" #include "MantidKernel/UnitFactory.h" @@ -21,11 +21,7 @@ namespace Mantid { namespace DataHandling { -// Register the algorithm into the AlgorithmFactory -DECLARE_ALGORITHM(LoadDaveGrp) - -// Register the algorithm into the LoadAlgorithmFactory -DECLARE_LOADALGORITHM(LoadDaveGrp) +DECLARE_FILELOADER_ALGORITHM(LoadDaveGrp); /// Sets documentation strings for this algorithm void LoadDaveGrp::initDocs() @@ -38,66 +34,57 @@ LoadDaveGrp::LoadDaveGrp() : ifile(), line(), nGroups(0), xLength(0) { } -bool LoadDaveGrp::quickFileCheck(const std::string& filePath, std::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 LoadDaveGrp::confidence(Kernel::FileDescriptor & descriptor) const { - std::string extn = this->extension(filePath); - bool bdgrp(false); - (!extn.compare("grp") || !extn.compare("sqe") || !extn.compare("txt")) ? bdgrp = true : bdgrp = false; - bool is_ascii(true); - for(std::size_t i = 0; i < nread; i++) - { - if (!isascii(header.full_hdr[i])) - is_ascii = false; - } - return(is_ascii || bdgrp ? true : false); -} + const std::string & extn = descriptor.extension(); + if(extn.compare(".grp") != 0 && extn.compare(".sqe") != 0 && extn.compare(".txt") != 0) return 0; -int LoadDaveGrp::fileCheck(const std::string& filePath) -{ - this->ifile.open(filePath.c_str()); - if (!this->ifile.is_open()) - { - g_log.error("Unable to open file: " + filePath); - throw Kernel::Exception::FileError("Unable to open file: " , filePath); - } + if(!descriptor.isAscii(descriptor.data())) return 0; + + + bool daveGrp(false); + std::string curline; // First line is a comment: # - bool bdgrp(false); - this->readLine(); - if (this->line.substr(0,1) == "#") + std::getline(descriptor.data(), curline); + if (curline.substr(0,1) == "#") { - bdgrp = true; - } else return 0; + daveGrp = true; + } + else return 0; // Second line is an integer - this->readLine(); + std::getline(descriptor.data(), curline); unsigned int value; - std::istringstream is(this->line); + std::istringstream is(curline); is >> value; - bdgrp = bdgrp && !is.fail(); + daveGrp = daveGrp && !is.fail(); + // Third line is a comment: # - this->readLine(); - if (this->line.substr(0,1) == "#") + std::getline(descriptor.data(), curline); + if (curline.substr(0,1) == "#") { - bdgrp = bdgrp && true; - } else return 0; + daveGrp = daveGrp && true; + } + else return 0; // Fourth line is an integer - this->readLine(); + std::getline(descriptor.data(), curline); // Clear all stream bits regardless of what happened before is.clear(); - is.str(this->line); + is.str(curline); is >> value; - bdgrp = bdgrp && !is.fail(); - this->ifile.close(); + daveGrp = daveGrp && !is.fail(); - if (bdgrp) - { - return 80; - } - return 0; + if (daveGrp) return 80; + else return 0; } + void LoadDaveGrp::init() { std::vector<std::string> exts; diff --git a/Code/Mantid/Framework/DataHandling/src/LoadEventPreNexus.cpp b/Code/Mantid/Framework/DataHandling/src/LoadEventPreNexus.cpp index b4432f73278de2c311a3717ba8221873b3c05b06..e18ba097abc1a8b5fe402fb81c2991a80e8b9052 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadEventPreNexus.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadEventPreNexus.cpp @@ -16,7 +16,7 @@ Specific pulse ID and mapping files can be specified if needed; these are guesse #include "MantidDataHandling/LoadEventPreNexus.h" #include "MantidAPI/FileFinder.h" -#include "MantidAPI/LoadAlgorithmFactory.h" +#include "MantidAPI/RegisterFileLoader.h" #include "MantidDataObjects/EventWorkspace.h" #include "MantidDataObjects/EventList.h" #include "MantidKernel/ArrayProperty.h" @@ -52,9 +52,8 @@ namespace Mantid { namespace DataHandling { -// Register the algorithm into the AlgorithmFactory -DECLARE_ALGORITHM(LoadEventPreNexus) -DECLARE_LOADALGORITHM(LoadEventPreNexus) + +DECLARE_FILELOADER_ALGORITHM(LoadEventPreNexus); /// Sets documentation strings for this algorithm void LoadEventPreNexus::initDocs() @@ -103,7 +102,7 @@ static const double TOF_CONVERSION = .1; /// Conversion factor between picoColumbs and microAmp*hours static const double CURRENT_CONVERSION = 1.e-6 / 3600.; -LoadEventPreNexus::LoadEventPreNexus() : Mantid::API::IDataFileChecker(), eventfile(NULL), max_events(0) +LoadEventPreNexus::LoadEventPreNexus() : Mantid::API::IFileLoader(), eventfile(NULL), max_events(0) { } @@ -113,52 +112,25 @@ LoadEventPreNexus::~LoadEventPreNexus() } /** - * Returns the name of the property to be considered as the Filename for Load - * @returns A character string containing the file property's name - */ -const char * LoadEventPreNexus::filePropertyName() const -{ - return EVENT_PARAM.c_str(); -} - -/** - * Do a quick file type check by looking at the first 100 bytes of the file - * @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 + * 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 */ -bool LoadEventPreNexus::quickFileCheck(const std::string& filePath,size_t,const file_header&) +int LoadEventPreNexus::confidence(Kernel::FileDescriptor & descriptor) const { - std::string ext = extension(filePath); - return (ext.rfind("dat") != std::string::npos); -} - -/** - * 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 LoadEventPreNexus::fileCheck(const std::string& filePath) -{ - int confidence(0); - try - { - // If this looks like a binary file where the exact file length is a multiple - // of the DasEvent struct then we're probably okay. - // NOTE: Putting this on the stack gives a segfault on Windows when for some reason - // the BinaryFile destructor is called twice! I'm sure there is something I don't understand there - // but heap allocation seems to work so go for that. - BinaryFile<DasEvent> *event_file = new BinaryFile<DasEvent>(filePath); - confidence = 80; - delete event_file; - } - catch(std::runtime_error &) - { - // This BinaryFile constructor throws if the file does not contain an - // exact multiple of the sizeof(DasEvent) objects. - } - return confidence; + if(descriptor.extension().rfind("dat") == std::string::npos) return 0; + + // If this looks like a binary file where the exact file length is a multiple + // of the DasEvent struct then we're probably okay. + const size_t objSize = sizeof(DasEvent); + auto &handle = descriptor.data(); + // get the size of the file in bytes and reset the handle back to the beginning + handle.seekg(0, std::ios::end); + const size_t filesize = static_cast<size_t>(handle.tellg()); + handle.seekg(0, std::ios::beg); + + if (filesize % objSize != 0) return 80; + else return 0; } diff --git a/Code/Mantid/Framework/DataHandling/src/LoadEventPreNexus2.cpp b/Code/Mantid/Framework/DataHandling/src/LoadEventPreNexus2.cpp index 0e0fe071df43ae4fae3f9a7e3f3cdf0762fc0144..ea1cd74114a94c144e62536e574ab39a4a9a9519 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadEventPreNexus2.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadEventPreNexus2.cpp @@ -26,7 +26,7 @@ The ChunkNumber and TotalChunks properties can be used to load only a section of #include <Poco/Path.h> #include <boost/timer.hpp> #include "MantidAPI/FileFinder.h" -#include "MantidAPI/LoadAlgorithmFactory.h" +#include "MantidAPI/RegisterFileLoader.h" #include "MantidAPI/WorkspaceFactory.h" #include "MantidDataObjects/EventWorkspace.h" #include "MantidDataObjects/EventList.h" @@ -56,9 +56,7 @@ namespace Mantid { namespace DataHandling { -// Register the algorithm into the AlgorithmFactory -DECLARE_ALGORITHM(LoadEventPreNexus2) -DECLARE_LOADALGORITHM(LoadEventPreNexus2) +DECLARE_FILELOADER_ALGORITHM(LoadEventPreNexus2); using namespace Kernel; using namespace API; @@ -190,10 +188,32 @@ static string generateMappingfileName(EventWorkspace_sptr &wksp) } //----------------------------------------------------------------------------- +/** + * 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 LoadEventPreNexus2::confidence(Kernel::FileDescriptor & descriptor) const +{ + if(descriptor.extension().rfind("dat") == std::string::npos) return 0; + + // If this looks like a binary file where the exact file length is a multiple + // of the DasEvent struct then we're probably okay. + const size_t objSize = sizeof(DasEvent); + auto &handle = descriptor.data(); + // get the size of the file in bytes and reset the handle back to the beginning + handle.seekg(0, std::ios::end); + const size_t filesize = static_cast<size_t>(handle.tellg()); + handle.seekg(0, std::ios::beg); + + if (filesize % objSize != 0) return 80; + else return 0; +} + /* * Constructor */ -LoadEventPreNexus2::LoadEventPreNexus2() : Mantid::API::IDataFileChecker(), eventfile(NULL), max_events(0) +LoadEventPreNexus2::LoadEventPreNexus2() : Mantid::API::IFileLoader(), eventfile(NULL), max_events(0) { } @@ -457,57 +477,6 @@ void LoadEventPreNexus2::addToWorkspaceLog(std::string logtitle, size_t mindex){ return; } -/** - * Returns the name of the property to be considered as the Filename for Load - * @returns A character string containing the file property's name - */ -const char * LoadEventPreNexus2::filePropertyName() const -{ - return EVENT_PARAM.c_str(); -} - -/** - * Do a quick file type check by looking at the first 100 bytes of the file - * @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 LoadEventPreNexus2::quickFileCheck(const std::string& filePath,size_t,const file_header&) -{ - std::string ext = extension(filePath); - return (ext.rfind("dat") != std::string::npos); -} - -/** - * 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 LoadEventPreNexus2::fileCheck(const std::string& filePath) -{ - int confidence(0); - try - { - // If this looks like a binary file where the exact file length is a multiple - // of the DasEvent struct then we're probably okay. - // NOTE: Putting this on the stack gives a segfault on Windows when for some reason - // the BinaryFile destructor is called twice! I'm sure there is something I don't understand there - // but heap allocation seems to work so go for that. - BinaryFile<DasEvent> *event_file = new BinaryFile<DasEvent>(filePath); - confidence = 80; - delete event_file; - } - catch(std::runtime_error &) - { - // This BinaryFile constructor throws if the file does not contain an - // exact multiple of the sizeof(DasEvent) objects. - } - return confidence; -} - - - //----------------------------------------------------------------------------- /** Load the instrument geometry File * @param eventfilename :: Used to pick the instrument. diff --git a/Code/Mantid/Framework/DataHandling/src/LoadGSS.cpp b/Code/Mantid/Framework/DataHandling/src/LoadGSS.cpp index d2f321ca13a840c8e699eaf124ed8bd2aa09fa2b..150d9db7060be6482eb967cd3d62d83a6357928f 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadGSS.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadGSS.cpp @@ -13,15 +13,15 @@ Two types of GSAS files are supported // Includes //--------------------------------------------------- #include "MantidDataHandling/LoadGSS.h" +#include "MantidAPI/ISpectrum.h" #include "MantidAPI/FileProperty.h" +#include "MantidAPI/RegisterFileLoader.h" #include "MantidAPI/WorkspaceValidators.h" #include "MantidKernel/UnitFactory.h" -#include "MantidAPI/LoadAlgorithmFactory.h" #include "MantidGeometry/Instrument.h" #include "MantidGeometry/Instrument/Detector.h" #include "MantidGeometry/Instrument/CompAssembly.h" #include "MantidGeometry/Instrument/Component.h" -#include "MantidAPI/ISpectrum.h" #include <boost/math/special_functions/fpclassify.hpp> #include <Poco/File.h> @@ -37,12 +37,37 @@ namespace Mantid { namespace DataHandling { + DECLARE_FILELOADER_ALGORITHM(LoadGSS); - // Register the algorithm into the AlgorithmFactory - DECLARE_ALGORITHM(LoadGSS) + /** + * 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 LoadGSS::confidence(Kernel::FileDescriptor & descriptor) const + { + if(!descriptor.isAscii(descriptor.filename())) return 0; - //register the algorithm into loadalgorithm factory - DECLARE_LOADALGORITHM(LoadGSS) + std::string str; + std::istream & file = descriptor.data(); + std::getline(file, str);//workspace title first line + while (!file.eof()) + { + std::getline(file, str); + // Skip over empty and comment lines, as well as those coming from files saved with the 'ExtendedHeader' option + if (str.empty() || str[0] == '#' || str.compare(0, 8, "Monitor:") == 0) + { + continue; + } + if(str.compare(0, 4,"BANK") == 0 && + (str.find("RALF") != std::string::npos || str.find("SLOG") != std::string::npos) && + (str.find("FXYE") != std::string::npos)) + { + return 80; + } + } + return 0; + } /// Sets documentation strings for this algorithm void LoadGSS::initDocs() @@ -430,67 +455,6 @@ namespace Mantid return rd; } - /**This method does a quick file type check by checking the first 100 bytes of the file - * @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 LoadGSS::quickFileCheck(const std::string& filePath, size_t nread, const file_header& header) - { - // check the file extension - std::string extn = extension(filePath); - bool bascii; - if (extn.compare("gsa")) - bascii = true; - else if (extn.compare("txt")) - bascii = true; - else - bascii = false; - - // check the bit of header - bool is_ascii(true); - for (size_t i = 0; i < nread; i++) - { - if (!isascii(header.full_hdr[i])) - is_ascii = false; - } - return (is_ascii || bascii); - } - - /**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 LoadGSS::fileCheck(const std::string& filePath) - { - std::ifstream file(filePath.c_str()); - if (!file) - { - g_log.error("Unable to open file: " + filePath); - throw Exception::FileError("Unable to open file: ", filePath); - } - std::string str; - getline(file, str);//workspace title first line - while (!file.eof()) - { - getline(file, str); - // Skip over empty and comment lines, as well as those coming from files saved with the 'ExtendedHeader' option - if (str.empty() || str[0] == '#' || !str.substr(0,8).compare("Monitor:") ) - { - continue; - } - if (!str.substr(0, 4).compare("BANK") && (str.find("RALF") != std::string::npos || str.find( - "SLOG") != std::string::npos) && (str.find("FXYE") != std::string::npos)) - { - return 80; - } - return 0; - } - return 0; - - } - /* Create the instrument geometry with Instrument * */ diff --git a/Code/Mantid/Framework/DataHandling/src/LoadMuonNexus.cpp b/Code/Mantid/Framework/DataHandling/src/LoadMuonNexus.cpp index 70ee41a3eafa5f762cf2ad989e9241e5f1a3bae4..1019ffcc1423941a9a7c73c9f531465d807c16e3 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadMuonNexus.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadMuonNexus.cpp @@ -167,7 +167,7 @@ namespace Mantid * @param descriptor A descriptor for the file * @returns An integer specifying the confidence level. 0 indicates it will not be used */ - int LoadMuonNexus::confidence(const Kernel::HDFDescriptor & descriptor) const + int LoadMuonNexus::confidence(const Kernel::HDFDescriptor &) const { return 0; // Not to be used but LoadMuonNexus2, which inherits from this will } diff --git a/Code/Mantid/Framework/DataHandling/src/LoadPDFgetNFile.cpp b/Code/Mantid/Framework/DataHandling/src/LoadPDFgetNFile.cpp index 2fd8f38c41c157e105a042eb9e712e37db729d7d..cef5552b32ea2ab630cb1e91bbfda986127ebe1e 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadPDFgetNFile.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadPDFgetNFile.cpp @@ -17,11 +17,11 @@ The file types include *WIKI*/ #include "MantidDataHandling/LoadPDFgetNFile.h" #include "MantidAPI/FileProperty.h" +#include "MantidAPI/RegisterFileLoader.h" #include "MantidAPI/WorkspaceProperty.h" #include "MantidAPI/WorkspaceFactory.h" #include "MantidKernel/Unit.h" #include "MantidKernel/UnitFactory.h" -#include "MantidAPI/LoadAlgorithmFactory.h" #include <fstream> #include <boost/algorithm/string.hpp> @@ -43,10 +43,7 @@ namespace Mantid namespace DataHandling { - DECLARE_ALGORITHM(LoadPDFgetNFile) - - // register the algorithm into loadalgorithm factory - DECLARE_LOADALGORITHM(LoadPDFgetNFile) + DECLARE_FILELOADER_ALGORITHM(LoadPDFgetNFile); //---------------------------------------------------------------------------------------------- /** Constructor @@ -72,67 +69,31 @@ namespace DataHandling } //---------------------------------------------------------------------------------------------- - /** This method does a quick file type check by checking the first 100 bytes of the file - * @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 + /** + * 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 */ - bool LoadPDFgetNFile::quickFileCheck(const std::string& filePath, size_t nread, const file_header& header) + int LoadPDFgetNFile::confidence(Kernel::FileDescriptor & descriptor) const { - UNUSED_ARG(nread); - UNUSED_ARG(header); - // check the file extension - std::string extn = extension(filePath); - bool bascii; - if (extn.compare("sq")) - bascii = true; - else if (extn.compare("sqa")) - bascii = true; - else if (extn.compare("sqb")) - bascii = true; - else if (extn.compare("gr")) - bascii = true; - else if (extn.compare("ain")) - bascii = true; - else if (extn.compare("braw")) - bascii = true; - else if (extn.compare("bsmo")) - bascii = true; - else - bascii = false; - - /* check the bit of header - bool is_ascii(true); - for (size_t i = 0; i < nread; i++) + const std::string & extn = descriptor.extension(); + // Only allow known file extensions + if(extn.compare("sq") != 0 && extn.compare("sqa") != 0 && extn.compare("sqb") != 0 && + extn.compare("gr") != 0 && extn.compare("ain") != 0 && extn.compare("braw") != 0 && + extn.compare("bsmo")!= 0) { - if (!isascii(header.full_hdr[i])) - is_ascii = false; + return 0; } - return (is_ascii || bascii); - */ - return (bascii); - } + auto & file = descriptor.data(); + if(!Kernel::FileDescriptor::isAscii(file)) return 0; - /** 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 LoadPDFgetNFile::fileCheck(const std::string& filePath) - { - std::ifstream file(filePath.c_str()); - if (!file) - { - g_log.error("Unable to open file: " + filePath); - throw Exception::FileError("Unable to open file: ", filePath); - } std::string str; - getline(file, str);//workspace title first line + std::getline(file, str); //workspace title first line while (!file.eof()) { - getline(file, str); + std::getline(file, str); if (startsWith(str, "#L")) { return 80; @@ -256,7 +217,7 @@ namespace DataHandling //---------------------------------------------------------------------------------------------- /** Check whether the line starts with some specific character */ - bool LoadPDFgetNFile::startsWith(std::string s, std::string header) + bool LoadPDFgetNFile::startsWith(const std::string & s, const std::string& header) const { bool answer = true; diff --git a/Code/Mantid/Framework/DataHandling/src/LoadPreNexus.cpp b/Code/Mantid/Framework/DataHandling/src/LoadPreNexus.cpp index 440e7d880a105cbf1f48b3e820d94911750db41a..1a2233b049fdb3dfabfdf8c05a7cda219e342f20 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadPreNexus.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadPreNexus.cpp @@ -16,7 +16,7 @@ Workflow algorithm to load all of the preNeXus files. #include <Poco/SAX/InputSource.h> #include "MantidAPI/IEventWorkspace.h" #include "MantidAPI/FileProperty.h" -#include "MantidAPI/LoadAlgorithmFactory.h" +#include "MantidAPI/RegisterFileLoader.h" #include "MantidAPI/WorkspaceOpOverloads.h" #include "MantidDataHandling/LoadPreNexus.h" #include "MantidKernel/System.h" @@ -38,9 +38,7 @@ namespace Mantid namespace DataHandling { - // Register the algorithm into the AlgorithmFactory - DECLARE_ALGORITHM(LoadPreNexus) - DECLARE_LOADALGORITHM(LoadPreNexus) + DECLARE_FILELOADER_ALGORITHM(LoadPreNexus); static const string RUNINFO_PARAM("Filename"); static const string MAP_PARAM("MappingFilename"); @@ -86,26 +84,15 @@ namespace DataHandling this->setOptionalMessage("Load a collection of PreNexus files."); } - //---------------------------------------------------------------------------------------------- - /// @copydoc Mantid::API::IDataFileChecker::filePropertyName() - const char * LoadPreNexus::filePropertyName() const - { - return RUNINFO_PARAM.c_str(); - } - - /// @copydoc Mantid::API::IDataFileChecker::quickFileCheck - bool LoadPreNexus::quickFileCheck(const std::string& filePath,size_t nread,const file_header& header) - { - UNUSED_ARG(nread); - UNUSED_ARG(header); - - return (filePath.compare(filePath.size()-12,12,"_runinfo.xml") == 0); - } - - /// @copydoc Mantid::API::IDataFileChecker::fileCheck - int LoadPreNexus::fileCheck(const std::string& filePath) + /** + * 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 LoadPreNexus::confidence(Kernel::FileDescriptor & descriptor) const { - if( filePath.compare(filePath.size()-12,12,"_runinfo.xml") == 0) + const std::string & filename = descriptor.filename(); + if(filename.compare(filename.size()-12,12,"_runinfo.xml") == 0) return 80; else return 0; diff --git a/Code/Mantid/Framework/DataHandling/src/LoadRaw3.cpp b/Code/Mantid/Framework/DataHandling/src/LoadRaw3.cpp index d3e4946daf90d466961275c1c6d412df450ac86a..3b490692e57492841739c5d0cb3df1169629b046 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadRaw3.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadRaw3.cpp @@ -29,6 +29,7 @@ LoadRaw version 1 and 2 are no longer available in Mantid. Version 3 has been v #include "MantidGeometry/Instrument/XMLlogfile.h" #include "MantidAPI/MemoryManager.h" #include "MantidAPI/WorkspaceGroup.h" +#include "MantidAPI/RegisterFileLoader.h" #include "MantidAPI/SpectraAxis.h" #include "MantidKernel/UnitFactory.h" #include "MantidKernel/ConfigService.h" @@ -38,7 +39,6 @@ LoadRaw version 1 and 2 are no longer available in Mantid. Version 3 has been v #include "MantidAPI/FileProperty.h" #include "LoadRaw/isisraw2.h" #include "MantidDataHandling/LoadLog.h" -#include "MantidAPI/LoadAlgorithmFactory.h" #include "MantidAPI/SpectrumDetectorMapping.h" #include <boost/shared_ptr.hpp> @@ -50,9 +50,7 @@ namespace Mantid { namespace DataHandling { - // Register the algorithm into the algorithm factory - DECLARE_ALGORITHM(LoadRaw3) - DECLARE_LOADALGORITHM(LoadRaw3) + DECLARE_FILELOADER_ALGORITHM(LoadRaw3); /// Sets documentation strings for this algorithm void LoadRaw3::initDocs() @@ -725,26 +723,5 @@ 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 LoadRaw3::quickFileCheck(const std::string& filePath,size_t nread,const file_header& header) - { - return LoadRawHelper::quickFileCheck(filePath,nread,header); - - } - /**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 LoadRaw3::fileCheck(const std::string& filePath) - { - return LoadRawHelper::fileCheck(filePath); - } - - } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/Framework/DataHandling/src/LoadRawHelper.cpp b/Code/Mantid/Framework/DataHandling/src/LoadRawHelper.cpp index ac0fb1144c474c4e3ea9d66136eddf54b7f6f00a..32b16817dccac8b0c88211bbf309f6ee340b8281 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadRawHelper.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadRawHelper.cpp @@ -15,7 +15,6 @@ #include "MantidKernel/ListValidator.h" #include "LoadRaw/isisraw2.h" #include "MantidDataHandling/LoadLog.h" -#include "MantidAPI/LoadAlgorithmFactory.h" #include "MantidDataHandling/LoadAscii.h" #include <boost/date_time/gregorian/gregorian.hpp> @@ -1043,75 +1042,26 @@ namespace Mantid } - - /** - * Check if the buffer looks like a RAW file header by looking at - * at the "address of RUN and INST section" attribute - if there, it's an ISIS raw file. - * @param nread The number of bytes in the buffer - * @param buffer A buffer of nread bytes of the file - * @returns True if this looks like an ISIS raw file - */ - bool LoadRawHelper::isRawFileHeader(const int nread, const unsigned char* buffer) const - { - if( nread > 88 && (buffer[84] == 32) && (buffer[88] == 126) ) - { - return true; - } - else return false; - } - - - /**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 LoadRawHelper::quickFileCheck(const std::string& filePath,size_t nread,const file_header& header) - { - std::string extn=extension(filePath); - bool braw = (!extn.compare("raw")||!extn.compare("add")||(extn.length() > 0 && extn[0]=='s')) ? true : false; - if( isRawFileHeader(static_cast<int>(nread), header.full_hdr) || braw ) - { - return true; - } - else - { - return 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 + /** + * 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 LoadRawHelper::fileCheck(const std::string& filePath) + int LoadRawHelper::confidence(Kernel::FileDescriptor & descriptor) const { - /* Open the file and read in the first bufferSize bytes - these will - * be used to determine the type of the file - */ - int bret=0; - FILE* fp = fopen(filePath.c_str(), "rb"); - if (fp == NULL) - { - return bret; - } - file_header header; - int nread(static_cast<int>(fread( - &header,sizeof(unsigned char), IDataFileChecker::g_hdr_bytes, fp))); - header.full_hdr[IDataFileChecker::g_hdr_bytes] = '\0'; - - if (fclose(fp) != 0) - { - } - - if( isRawFileHeader(nread, header.full_hdr) ) + auto & stream = descriptor.data(); + // 85th character is a space & 89th character is a ~ + stream.seekg(84, std::ios::beg); + int c = stream.get(); + int confidence(0); + if(c == 32) { - bret=80; + stream.seekg(3, std::ios::cur); + int c = stream.get(); + if(c == 126) confidence = 80; } - return bret; + return confidence; } - - } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/Framework/DataHandling/src/UpdateInstrumentFromFile.cpp b/Code/Mantid/Framework/DataHandling/src/UpdateInstrumentFromFile.cpp index 522b933f1762a4e7a488a25859d05d4253c58063..a31cecf27c8485d1a87c9e19528e8e72d97f0090 100644 --- a/Code/Mantid/Framework/DataHandling/src/UpdateInstrumentFromFile.cpp +++ b/Code/Mantid/Framework/DataHandling/src/UpdateInstrumentFromFile.cpp @@ -45,6 +45,7 @@ would tell the algorithm to interpret the columns as: #include "MantidAPI/MatrixWorkspace.h" #include "MantidGeometry/Instrument.h" #include "MantidGeometry/Instrument/ComponentHelper.h" +#include "MantidKernel/HDFDescriptor.h" #include <nexus/NeXusFile.hpp> #include <nexus/NeXusException.hpp> #include "LoadRaw/isisraw2.h" @@ -132,27 +133,35 @@ namespace Mantid const bool moveMonitors = getProperty("MoveMonitors"); m_ignoreMonitors = (!moveMonitors); - // Check the file type - LoadRawHelper isisRAW; - LoadAscii ascii; - LoadISISNexus2 isisNexus; - if( isisRAW.fileCheck(filename) > 0 ) + // Check file type + if(HDFDescriptor::isHDF(filename)) { - updateFromRaw(filename); + LoadISISNexus2 isisNexus; + if(isisNexus.confidence(filename) > 0) + { + updateFromNeXus(filename); + return; + } } - else if(ascii.fileCheck(filename) > 0) + + if(FileDescriptor::isAscii(filename)) { updateFromAscii(filename); + return; } - else if(isisNexus.confidence(filename) > 0) + + LoadRawHelper isisRAW; + auto *descriptor = new Kernel::FileDescriptor(filename); + if( isisRAW.confidence(*descriptor) > 0 ) { - updateFromNeXus(filename); + delete descriptor; + updateFromRaw(filename); } else { + delete descriptor; throw std::invalid_argument("File \"" + filename + "\" is not a valid input file."); } - } /** diff --git a/Code/Mantid/Framework/DataHandling/test/LoadRaw3Test.h b/Code/Mantid/Framework/DataHandling/test/LoadRaw3Test.h index d4afcf631313d3f3df39915294076ca69a5f367d..3cd26bcb38c38570540c2be28b7205552f461861 100644 --- a/Code/Mantid/Framework/DataHandling/test/LoadRaw3Test.h +++ b/Code/Mantid/Framework/DataHandling/test/LoadRaw3Test.h @@ -31,6 +31,17 @@ public: inputFile = "HET15869.raw"; } + void testConfidence() + { + using Mantid::Kernel::FileDescriptor; + LoadRaw3 loader; + loader.initialize(); + loader.setPropertyValue("Filename", inputFile); + + FileDescriptor descriptor(loader.getPropertyValue("Filename")); + TS_ASSERT_EQUALS(80, loader.confidence(descriptor)); + } + void testInit() { TS_ASSERT_THROWS_NOTHING( loader.initialize()); diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/BinaryFile.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/BinaryFile.h index 8209cc268deab18abb38cc8a4584fa8de1ed1fb9..c65ddb0d858b7649c75bfef32a57b7264dcfbd81 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/BinaryFile.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/BinaryFile.h @@ -63,7 +63,7 @@ public: * @throw runtime_error if the file size is not an even multiple of the type size * @throw invalid_argument if the file does not exist * */ - void open(std::string filename) + void open(const std::string & filename) { this->handle = NULL; if (!Poco::File(filename).exists()) diff --git a/Code/Mantid/Framework/Kernel/src/FileDescriptor.cpp b/Code/Mantid/Framework/Kernel/src/FileDescriptor.cpp index 54b840cc659df4c78535f87a00f6bf1a2174c10b..7ffa20f72c582c9816b640f602a2f51fbf931761 100644 --- a/Code/Mantid/Framework/Kernel/src/FileDescriptor.cpp +++ b/Code/Mantid/Framework/Kernel/src/FileDescriptor.cpp @@ -106,7 +106,15 @@ namespace Mantid */ void FileDescriptor::resetStreamToStart() { - m_file.seekg(0); + // If the stream was closed, reopen it + if(!m_file.is_open()) + { + m_file.open(m_filename.c_str(), std::ios::in | std::ios::binary); + } + else + { + m_file.seekg(0); + } } //----------------------------------------------------------------------------------------------