Skip to content

Commit

Permalink
WIP Add the barebones of a reader for a binary stream.
Browse files Browse the repository at this point in the history
Refs #11056
  • Loading branch information
martyngigg committed Dec 1, 2015
1 parent 9f756ea commit 10b3d68
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Framework/Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set ( SRC_FILES
src/ArrayProperty.cpp
src/Atom.cpp
src/BinFinder.cpp
src/BinaryStreamReader.cpp
src/CPUTimer.cpp
src/CatalogInfo.cpp
src/ChecksumHelper.cpp
Expand Down Expand Up @@ -134,6 +135,7 @@ set ( INC_FILES
inc/MantidKernel/Atom.h
inc/MantidKernel/BinFinder.h
inc/MantidKernel/BinaryFile.h
inc/MantidKernel/BinaryStreamReader.h
inc/MantidKernel/BoundedValidator.h
inc/MantidKernel/CPUTimer.h
inc/MantidKernel/Cache.h
Expand Down Expand Up @@ -192,7 +194,6 @@ set ( INC_FILES
inc/MantidKernel/MRUList.h
inc/MantidKernel/MagneticFormFactorTable.h
inc/MantidKernel/MagneticIon.h
inc/MantidKernel/make_unique.h
inc/MantidKernel/MandatoryValidator.h
inc/MantidKernel/MantidVersion.h
inc/MantidKernel/MaskedProperty.h
Expand Down Expand Up @@ -274,6 +275,7 @@ set ( INC_FILES
inc/MantidKernel/WriteLock.h
inc/MantidKernel/XMLInstantiator.h
inc/MantidKernel/cow_ptr.h
inc/MantidKernel/make_unique.h
)

set ( TEST_FILES
Expand All @@ -283,6 +285,7 @@ set ( TEST_FILES
AtomTest.h
BinFinderTest.h
BinaryFileTest.h
BinaryStreamReaderTest.h
BoseEinsteinDistributionTest.h
BoundedValidatorTest.h
CPUTimerTest.h
Expand Down
48 changes: 48 additions & 0 deletions Framework/Kernel/inc/MantidKernel/BinaryStreamReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef MANTID_KERNEL_BINARYSTREAMREADER_H_
#define MANTID_KERNEL_BINARYSTREAMREADER_H_
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "MantidKernel/DllConfig.h"
#include <iosfwd>

namespace Mantid {
namespace Kernel {

/**
* Assists with reading a binary file by providing standard overloads for the
* istream operators (>>) to given types (and vectors of those types)
*
* Copyright &copy; 2015 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
* National Laboratory & European Spallation Source
*
* This file is part of Mantid.
*
* Mantid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Mantid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* File change history is stored at: <https://github.com/mantidproject/mantid>
* Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_KERNEL_DLL BinaryStreamReader {
public:
BinaryStreamReader(std::istream & istrm);
~BinaryStreamReader();
private:
std::istream & m_istrm;
};

} // namespace Kernel
} // namespace Mantid

#endif /* MANTID_KERNEL_BINARYSTREAMREADER_H_ */
33 changes: 33 additions & 0 deletions Framework/Kernel/src/BinaryStreamReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "MantidKernel/BinaryStreamReader.h"
#include "MantidKernel/Exception.h"

#include <istream>

namespace Mantid {
namespace Kernel {

/**
* Constructor taking the stream to read.
* @param istrm An open stream from which data will be read. The object does
* not take ownership of the stream. The caller is responsible for closing
* it.
*/
BinaryStreamReader::BinaryStreamReader(std::istream &istrm) : m_istrm(istrm) {
if (!istrm) {
throw std::runtime_error(
"BinaryStreamReader: Input stream is in a bad state. Cannot continue.");
}
}

/**
* Destructor
* The stream state is left as it was in the last call to a read operation.
* It is up to the caller to close it.
*/
BinaryStreamReader::~BinaryStreamReader() {}

} // namespace Kernel
} // namespace Mantid
79 changes: 79 additions & 0 deletions Framework/Kernel/test/BinaryStreamReaderTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef MANTID_KERNEL_BINARYSTREAMREADERTEST_H_
#define MANTID_KERNEL_BINARYSTREAMREADERTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidKernel/BinaryStreamReader.h"
#include "MantidKernel/ConfigService.h"

#include <Poco/File.h>
#include <Poco/Path.h>

#include <fstream>

using Mantid::Kernel::BinaryStreamReader;

class BinaryFileReaderTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static BinaryFileReaderTest *createSuite() {
return new BinaryFileReaderTest();
}
static void destroySuite(BinaryFileReaderTest *suite) { delete suite; }

BinaryFileReaderTest()
: CxxTest::TestSuite(), m_filename("test_horace_reader.sqw"), m_stream() {
openTestFile();
}

//----------------------------------------------------------------------------
// Successes cases
//----------------------------------------------------------------------------
void test_Constructor_With_Good_Stream_Does_Not_Touch_Stream() {
BinaryStreamReader reader(m_stream);
TS_ASSERT_EQUALS(std::ios_base::beg, m_stream.tellg());
}

//----------------------------------------------------------------------------
// Failure cases
//----------------------------------------------------------------------------
void test_Stream_Marked_Not_Good_Throws_RuntimeError_On_Construction() {
m_stream.seekg(std::ios_base::end);
// read will put it into a 'bad' state
int i(0);
m_stream >> i;
TSM_ASSERT_THROWS("Expected a runtime_error when given a bad stream",
BinaryStreamReader reader(m_stream), std::runtime_error);
}

private:
void openTestFile() {
using Mantid::Kernel::ConfigService;
// The test file should be in the data search path
const auto &dirs = ConfigService::Instance().getDataSearchDirs();
std::string filepath;
for (auto direc : dirs) {
Poco::Path path(direc, m_filename);
if (Poco::File(path).exists()) {
filepath = path.toString();
break;
}
}
if (filepath.empty()) {
throw std::runtime_error(
"Unable to find test file. Check data search paths");
} else {
m_stream.open(filepath, std::ios_base::binary);
if (!m_stream) {
throw std::runtime_error(
"Cannot open test file. Check file permissions.");
}
}
}

std::string m_filename;
std::ifstream m_stream;
};

#endif /* MANTID_KERNEL_BINARYSTREAMREADERTEST_H_ */

0 comments on commit 10b3d68

Please sign in to comment.