Mantid
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
Mantid::Kernel::FileDescriptor Class Reference

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...
 
FileDescriptoroperator= (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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ FileDescriptor() [1/3]

Mantid::Kernel::FileDescriptor::FileDescriptor ( const std::string &  filename)

Constructor accepting a filename.

Parameters
filenameA string containing a filename. The file must exist
Exceptions
std::invalid_argumentif 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().

◆ ~FileDescriptor()

Mantid::Kernel::FileDescriptor::~FileDescriptor ( )

Destructor.

Closes the file handle.

Definition at line 143 of file FileDescriptor.cpp.

References m_file.

◆ FileDescriptor() [2/3]

Mantid::Kernel::FileDescriptor::FileDescriptor ( )
delete

Disable default constructor.

◆ FileDescriptor() [3/3]

Mantid::Kernel::FileDescriptor::FileDescriptor ( const FileDescriptor )
delete

Disable copy operator.

Member Function Documentation

◆ data()

std::istream & Mantid::Kernel::FileDescriptor::data ( )
inline

◆ extension()

const std::string & Mantid::Kernel::FileDescriptor::extension ( ) const
inline

◆ filename()

const std::string & Mantid::Kernel::FileDescriptor::filename ( ) const
inline

◆ initialize()

void Mantid::Kernel::FileDescriptor::initialize ( const std::string &  filename)
private

Open the file and cache description elements.

Set the description fields and opens the file.

Parameters
filenameA 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().

◆ isAscii() [1/4]

bool Mantid::Kernel::FileDescriptor::isAscii ( ) const
inline

◆ isAscii() [2/4]

bool Mantid::Kernel::FileDescriptor::isAscii ( const std::string &  filename,
const size_t  nbytes = 256 
)
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.

Parameters
filenameA string pointing to an existing file
nbytesThe number of bytes of the file to check (Default=256)
Returns
True if the file is considered ASCII, false otherwise
Exceptions
std::invalid_argumentif the file cannot be opened
std::runtime_errorif 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().

◆ isAscii() [3/4]

bool Mantid::Kernel::FileDescriptor::isAscii ( FILE *  file,
const size_t  nbytes = 256 
)
static

Returns true if the file is considered ascii.

Check if a file is a text file.

Parameters
file:: The file pointer
nbytesThe number of bytes of the file to check (Default=256)
Returns
true if the file an ascii text file, false otherwise

Definition at line 82 of file FileDescriptor.cpp.

References data().

◆ isAscii() [4/4]

bool Mantid::Kernel::FileDescriptor::isAscii ( std::istream &  data,
const size_t  nbytes = 256 
)
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

Parameters
dataAn input stream opened in binary mode
nbytesThe number of bytes of the file to check (Default=256)
Returns
True if the stream is considered ASCII, false otherwise

Definition at line 51 of file FileDescriptor.cpp.

References data().

◆ isEmpty()

bool Mantid::Kernel::FileDescriptor::isEmpty ( const std::string &  filename)
static

Returns true if the file is empty.

Check whether a file is empty.

Parameters
filename
Returns
true if the file is empty.

Definition at line 111 of file FileDescriptor.cpp.

References filename().

Referenced by Mantid::DataHandling::LoadRawHelper::runLoadLog().

◆ isXML()

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.

Returns
true if the file is of Ascii type and has a ".xml" extension, false otherwise

Definition at line 168 of file FileDescriptor.cpp.

References extension(), and isAscii().

Referenced by Mantid::DataHandling::LoadGeometry::isIDF().

◆ operator=()

FileDescriptor & Mantid::Kernel::FileDescriptor::operator= ( const FileDescriptor )
delete

Disable assignment operator.

◆ resetStreamToStart()

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.

Member Data Documentation

◆ m_ascii

bool Mantid::Kernel::FileDescriptor::m_ascii
private

Flag indicating the file is pure ascii.

Definition at line 90 of file FileDescriptor.h.

Referenced by initialize().

◆ m_extension

std::string Mantid::Kernel::FileDescriptor::m_extension
private

Extension.

Definition at line 86 of file FileDescriptor.h.

Referenced by initialize().

◆ m_file

std::ifstream Mantid::Kernel::FileDescriptor::m_file
private

Open file stream.

Definition at line 88 of file FileDescriptor.h.

Referenced by initialize(), resetStreamToStart(), and ~FileDescriptor().

◆ m_filename

std::string Mantid::Kernel::FileDescriptor::m_filename
private

Full filename.

Definition at line 84 of file FileDescriptor.h.

Referenced by initialize(), and resetStreamToStart().


The documentation for this class was generated from the following files: