Mantid
|
Defines a wrapper around an open file. More...
#include <FileDescriptor.h>
Public Member Functions | |
std::istream & | data () |
Access the open file stream. More... | |
const std::string & | extension () const |
Access the file extension. More... | |
FileDescriptor ()=delete | |
Disable default constructor. More... | |
FileDescriptor (const FileDescriptor &)=delete | |
Disable copy operator. More... | |
FileDescriptor (const std::string &filename) | |
Constructor accepting a filename. More... | |
const std::string & | filename () const |
Access the filename. More... | |
bool | isAscii () const |
Returns true if the descriptor is looking at an ascii file. More... | |
bool | isXML () const |
Returns true if the descriptor is looking at an XML file. More... | |
FileDescriptor & | operator= (const FileDescriptor &)=delete |
Disable assignment operator. More... | |
void | resetStreamToStart () |
Reset the file stream to the start of the file. More... | |
~FileDescriptor () | |
Destructor. More... | |
Static Public Member Functions | |
static bool | isAscii (const std::string &filename, const size_t nbytes=256) |
Returns true if the file is considered ascii. More... | |
static bool | isAscii (FILE *file, const size_t nbytes=256) |
Returns true if the file is considered ascii. More... | |
static bool | isAscii (std::istream &data, const size_t nbytes=256) |
Returns true if the stream is considered ascii. More... | |
static bool | isEmpty (const std::string &filename) |
Returns true if the file is empty. More... | |
Private Member Functions | |
void | initialize (const std::string &filename) |
Open the file and cache description elements. More... | |
Private Attributes | |
bool | m_ascii |
Flag indicating the file is pure ascii. More... | |
std::string | m_extension |
Extension. More... | |
std::ifstream | m_file |
Open file stream. More... | |
std::string | m_filename |
Full filename. More... | |
Defines a wrapper around an open file.
Details of the file such as the filename & extension can be queried. The file is closed when the object is destroyed.
The object stores an opened fstream object that can be accessed using the data() method.
Definition at line 26 of file FileDescriptor.h.
Mantid::Kernel::FileDescriptor::FileDescriptor | ( | const std::string & | filename | ) |
Constructor accepting a filename.
filename | A string containing a filename. The file must exist |
std::invalid_argument | if the filename is empty or the file does not exist |
Definition at line 130 of file FileDescriptor.cpp.
References Mantid::DataHandling::exists(), filename(), and initialize().
Mantid::Kernel::FileDescriptor::~FileDescriptor | ( | ) |
Destructor.
Closes the file handle.
Definition at line 143 of file FileDescriptor.cpp.
References m_file.
|
delete |
Disable default constructor.
|
delete |
Disable copy operator.
|
inline |
Access the open file stream.
DO NOT CLOSE IT
Definition at line 75 of file FileDescriptor.h.
Referenced by Mantid::Crystal::LoadIsawPeaks::confidence(), Mantid::DataHandling::FilterEventsByLogValuePreNexus::confidence(), Mantid::DataHandling::LoadDaveGrp::confidence(), Mantid::DataHandling::LoadEventPreNexus2::confidence(), Mantid::DataHandling::LoadGSS::confidence(), Mantid::DataHandling::LoadPSIMuonBin::confidence(), Mantid::DataHandling::LoadRawHelper::confidence(), Mantid::DataHandling::LoadRKH::confidence(), Mantid::DataHandling::LoadSESANS::confidence(), Mantid::DataHandling::LoadSpice2D::confidence(), Mantid::DataHandling::LoadTBL::confidence(), and isAscii().
|
inline |
Access the file extension.
Defined as the string after and including the last period character
Definition at line 62 of file FileDescriptor.h.
Referenced by Mantid::Crystal::LoadIsawPeaks::confidence(), Mantid::DataHandling::FilterEventsByLogValuePreNexus::confidence(), Mantid::DataHandling::LoadBBY::confidence(), Mantid::DataHandling::LoadDaveGrp::confidence(), Mantid::DataHandling::LoadEmptyInstrument::confidence(), Mantid::DataHandling::LoadEMUTar::confidence(), Mantid::DataHandling::LoadEventPreNexus2::confidence(), Mantid::DataHandling::LoadFITS::confidence(), Mantid::DataHandling::LoadGSS::confidence(), Mantid::DataHandling::LoadNGEM::confidence(), Mantid::DataHandling::LoadSESANS::confidence(), Mantid::DataHandling::LoadSpice2D::confidence(), Mantid::DataHandling::LoadSwans::confidence(), Mantid::MDAlgorithms::LoadDNSSCD::confidence(), Mantid::MDAlgorithms::LoadSQW::confidence(), Mantid::MDAlgorithms::LoadSQW2::confidence(), and isXML().
|
inline |
Access the filename.
Definition at line 56 of file FileDescriptor.h.
Referenced by Mantid::DataHandling::LoadAscii::confidence(), Mantid::DataHandling::LoadAscii2::confidence(), Mantid::DataHandling::LoadBBY::confidence(), Mantid::DataHandling::LoadEmptyInstrument::confidence(), Mantid::DataHandling::LoadEMUTar::confidence(), Mantid::DataHandling::LoadPreNexus::confidence(), Mantid::DataHandling::LoadSpice2D::confidence(), Mantid::DataHandling::LoadTBL::confidence(), FileDescriptor(), initialize(), isAscii(), and isEmpty().
|
private |
Open the file and cache description elements.
Set the description fields and opens the file.
filename | A string pointing to an existing file |
Definition at line 178 of file FileDescriptor.cpp.
References filename(), isAscii(), m_ascii, m_extension, m_file, m_filename, and Mantid::Kernel::Strings::toLower().
Referenced by FileDescriptor().
|
inline |
Returns true if the descriptor is looking at an ascii file.
Definition at line 66 of file FileDescriptor.h.
Referenced by Mantid::DataHandling::LoadRawBin0::exec(), Mantid::DataHandling::LoadRawSpectrum0::exec(), Mantid::DataHandling::UpdateInstrumentFromFile::exec(), initialize(), isAscii(), isXML(), and Mantid::DataHandling::LoadRawHelper::openRawFile().
|
static |
Returns true if the file is considered ascii.
Check if the given file appears to be an ASCII file.
The check searches the first nbytes of the file and returns false if a non-ascii character is found. If the file is shorter than nbytes then it checks until the end of the stream.
filename | A string pointing to an existing file |
nbytes | The number of bytes of the file to check (Default=256) |
std::invalid_argument | if the file cannot be opened |
std::runtime_error | if an error is occurred while reading the stream |
Definition at line 30 of file FileDescriptor.cpp.
References data(), filename(), and isAscii().
Referenced by Mantid::DataHandling::FilterEventsByLogValuePreNexus::confidence(), Mantid::DataHandling::LoadAscii::confidence(), Mantid::DataHandling::LoadAscii2::confidence(), Mantid::DataHandling::LoadDaveGrp::confidence(), Mantid::DataHandling::LoadEmptyInstrument::confidence(), Mantid::DataHandling::LoadEventPreNexus2::confidence(), Mantid::DataHandling::LoadGSS::confidence(), Mantid::DataHandling::LoadRKH::confidence(), Mantid::DataHandling::LoadSESANS::confidence(), Mantid::DataHandling::LoadTBL::confidence(), Mantid::MDAlgorithms::LoadDNSSCD::confidence(), Mantid::MDAlgorithms::LoadSQW::confidence(), Mantid::MDAlgorithms::LoadSQW2::confidence(), and Mantid::DataHandling::LoadGeometry::isNexus().
|
static |
Returns true if the file is considered ascii.
Check if a file is a text file.
file | :: The file pointer |
nbytes | The number of bytes of the file to check (Default=256) |
Definition at line 82 of file FileDescriptor.cpp.
References data().
|
static |
Returns true if the stream is considered ascii.
Check if the given stream appears to point to an ASCII data.
The check searches the next nbytes of the stream and returns false if a non-ascii character is found. If the stream is shorter than nbytes or a reading error occurs then it simply returns the result up to that point The stream is reset to the position is was at when entering the function
data | An input stream opened in binary mode |
nbytes | The number of bytes of the file to check (Default=256) |
Definition at line 51 of file FileDescriptor.cpp.
References data().
|
static |
Returns true if the file is empty.
Check whether a file is empty.
filename |
Definition at line 111 of file FileDescriptor.cpp.
References filename().
Referenced by Mantid::DataHandling::LoadRawHelper::runLoadLog().
bool Mantid::Kernel::FileDescriptor::isXML | ( | ) | const |
Returns true if the descriptor is looking at an XML file.
Check if a file is an XML file.
For now, a file is considered to be an XML file if it is of Ascii type and has a ".xml" extension. Future improvements could include checking inside the file if there are indeed XML tags.
Definition at line 168 of file FileDescriptor.cpp.
References extension(), and isAscii().
Referenced by Mantid::DataHandling::LoadGeometry::isIDF().
|
delete |
Disable assignment operator.
void Mantid::Kernel::FileDescriptor::resetStreamToStart | ( | ) |
Reset the file stream to the start of the file.
Moves the stream pointer back to the start of the file, without reopening the file.
Note that this will affect the stream that has been accessed using the stream() method
Definition at line 150 of file FileDescriptor.cpp.
References m_file, and m_filename.
|
private |
Flag indicating the file is pure ascii.
Definition at line 90 of file FileDescriptor.h.
Referenced by initialize().
|
private |
|
private |
Open file stream.
Definition at line 88 of file FileDescriptor.h.
Referenced by initialize(), resetStreamToStart(), and ~FileDescriptor().
|
private |
Full filename.
Definition at line 84 of file FileDescriptor.h.
Referenced by initialize(), and resetStreamToStart().