-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Add the barebones of a reader for a binary stream.
Refs #11056
- Loading branch information
1 parent
9f756ea
commit 10b3d68
Showing
4 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 © 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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |