fw4spl
ArrayReader.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwDataIO/reader/ArrayReader.hpp"
8 
9 #include "fwDataIO/reader/registry/macros.hpp"
10 
11 #include <fwCore/Exception.hpp>
12 
13 #include <fwDataTools/helper/Array.hpp>
14 
15 #include <fstream>
16 #include <iostream>
17 
18 fwDataIOReaderRegisterMacro( ::fwDataIO::reader::ArrayReader );
19 
20 namespace fwDataIO
21 {
22 
23 namespace reader
24 {
25 
26 //------------------------------------------------------------------------------
27 
29  ::fwData::location::enableSingleFile<
30  IObjectReader >(this)
31 {
32 }
33 
34 //------------------------------------------------------------------------------
35 
37 {
38 }
39 
40 //------------------------------------------------------------------------------
41 
43 {
44  assert( ::fwData::location::SingleFile::dynamicCast(m_location) );
45  ::boost::filesystem::path file = ::fwData::location::SingleFile::dynamicCast(m_location)->getPath();
46 
47  ::fwData::Array::sptr array = this->getConcreteObject();
48  ::fwDataTools::helper::Array arrayHelper(array);
49 
50  size_t arraySizeInBytes = array->resize(array->getSize());
51  char* buff = arrayHelper.begin();
52 
53  std::ifstream fs(file.string().c_str(), std::ios::in|std::ios::binary|std::ios::ate);
54 
55  FW_RAISE_IF("Unable to read " << file, !fs.good());
56 
57  std::streampos fileSize = fs.tellg();
58  fs.seekg(0, std::ios::beg);
59 
60  FW_RAISE_IF(file << ": Bad file size, expected: " << arraySizeInBytes << ", was: " << fileSize,
61  arraySizeInBytes - fileSize != 0);
62 
63  fs.read(buff, arraySizeInBytes);
64 
65  fs.close();
66 }
67 
68 //------------------------------------------------------------------------------
69 
71 {
72  return (".raw");
73 }
74 
75 //------------------------------------------------------------------------------
76 
77 } // namespace reader
78 } // namespace fwDataIO
FWDATAIO_API std::string extension() override
Defines extension supported by this reader ".raw".
Definition: ArrayReader.cpp:70
FWDATAIO_API ArrayReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor. Do nothing.
Definition: ArrayReader.cpp:28
virtual FWDATAIO_API void read() override
Read the file with zlib API.
Definition: ArrayReader.cpp:42
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Array Reader. Read file format .raw.
Definition: ArrayReader.hpp:29
virtual FWDATATOOLS_API char * begin()
Returns the begining/end of the buffer interpreted as a char buffer.
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
virtual FWDATAIO_API ~ArrayReader()
Destructor. Do nothing.
Definition: ArrayReader.cpp:36
Contains the representation of the data objects used in the framework.
::fwData::location::ILocation::sptr m_location
Object location ( file path, directory path, url, etc )
Helper to manage array buffer. Lock the buffer before to modify it.