Skip to content

Commit

Permalink
Cache isAscii flag in FileDescriptor. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jul 4, 2013
1 parent 7f3aabd commit 248c704
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ namespace Mantid
* @returns A reference to a const string containing the file extension
*/
inline const std::string & extension() const { return m_extension; }
/**
* Returns true if the descriptor is looking at an ascii file
*/
inline bool isAscii() const { return m_ascii; }
/**
* Access the open file stream. DO NOT CLOSE IT
* @returns The current stream
*/
inline std::ifstream & data() { return m_file; }

inline std::istream & data() { return m_file; }
/// Reset the file stream to the start of the file
void resetStreamToStart();

Expand All @@ -84,6 +87,8 @@ namespace Mantid
std::string m_extension;
/// Open file stream
std::ifstream m_file;
/// Flag indicating the file is pure ascii
bool m_ascii;
};


Expand Down
4 changes: 3 additions & 1 deletion Code/Mantid/Framework/Kernel/src/FileDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace Mantid
* @throws std::invalid_argument if the filename is empty or the file does not exist
*/
FileDescriptor::FileDescriptor(const std::string & filename) :
m_filename(), m_extension(), m_file(NULL)
m_filename(), m_extension(), m_file(NULL), m_ascii(false)
{
if(filename.empty())
{
Expand Down Expand Up @@ -132,6 +132,8 @@ namespace Mantid

m_file.open(m_filename.c_str(), std::ios::in | std::ios::binary);
if(!m_file) throw std::runtime_error("FileDescriptor::initialize - Cannot open file '" + filename + "' for reading");

m_ascii = FileDescriptor::isAscii(m_file);
}

} // namespace Kernel
Expand Down
16 changes: 8 additions & 8 deletions Code/Mantid/Framework/Kernel/test/FileDescriptorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ class FileDescriptorTest : public CxxTest::TestSuite
//===================== Success cases ============================================
void test_isAscii_Returns_True_For_Ascii_Filename()
{
TS_ASSERT(FileDescriptor::isAscii(m_testAsciiPath));
TS_ASSERT(FileDescriptor::isAscii(m_testAsciiPath)); //static method

FileDescriptor descr(m_testAsciiPath);
TS_ASSERT(descr.isAscii());
}

void test_isAscii_Returns_False_For_Binary_Filename()
{
TS_ASSERT(!FileDescriptor::isAscii(m_testNonNexusPath));
TS_ASSERT(!FileDescriptor::isAscii(m_testNonNexusPath)); //static method

FileDescriptor descr(m_testNonNexusPath);
TS_ASSERT(!descr.isAscii());
}

void test_isAscii_Returns_True_For_Stream_Pointing_At_Ascii_File_And_Stream_Is_Returned_To_Position_On_Entry()
Expand Down Expand Up @@ -83,12 +89,6 @@ class FileDescriptorTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(".nxs", descr.extension());
}

void test_File_Is_Opened_After_Object_Is_Constructed()
{
FileDescriptor descr(m_testNexusPath);
TS_ASSERT(descr.data().is_open());
}

void test_Intial_Stream_Is_Positioned_At_Start_Of_File()
{
const std::string filename = m_testNexusPath;
Expand Down

0 comments on commit 248c704

Please sign in to comment.