fw4spl
GzArrayReader.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/GzArrayReader.hpp"
8 
9 #include "fwDataIO/reader/registry/macros.hpp"
10 
11 #include <fwDataTools/helper/Array.hpp>
12 
13 #include <zlib.h>
14 
15 #include <iostream>
16 
17 fwDataIOReaderRegisterMacro( ::fwDataIO::reader::GzArrayReader );
18 
19 namespace fwDataIO
20 {
21 
22 namespace reader
23 {
24 
25 //------------------------------------------------------------------------------
26 
28  ::fwData::location::enableSingleFile< IObjectReader >(this)
29 {
30 }
31 
32 //------------------------------------------------------------------------------
33 
35 {
36 }
37 
38 //------------------------------------------------------------------------------
39 
41 {
42  assert( ::fwData::location::SingleFile::dynamicCast(m_location) );
43  ::boost::filesystem::path file = ::fwData::location::SingleFile::dynamicCast(m_location)->getPath();
44 
45  assert( file.empty() == false );
46 
47  ::fwData::Array::sptr array = this->getConcreteObject();
48  size_t arraySizeInBytes = array->resize(array->getSize());
49  ::fwDataTools::helper::Array helper(array);
50  void* buff = helper.getBuffer();
51 
52  gzFile rawFile = gzopen(file.string().c_str(), "rb");
53  if ( rawFile == 0 )
54  {
55  gzclose(rawFile);
56  std::string str = "Unable to open ";
57  str += file.string();
58  throw std::ios_base::failure(str);
59  }
60 
61  unsigned int uncompressedBytesReaded = gzread(rawFile, buff, arraySizeInBytes);
62  gzclose(rawFile);
63  if ( uncompressedBytesReaded != arraySizeInBytes )
64  {
65  std::string str = "Unable to read ";
66  str += file.string();
67  throw std::ios_base::failure(str);
68  }
69 }
70 
71 //------------------------------------------------------------------------------
72 
74 {
75  return (".raw.gz");
76 }
77 
78 //------------------------------------------------------------------------------
79 
80 } // namespace reader
81 } // namespace fwDataIO
FWDATAIO_API GzArrayReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor. Do nothing.
virtual FWDATAIO_API ~GzArrayReader()
Destructor. Do nothing.
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.
virtual FWDATAIO_API void read() override
Read the file with zlib API.
virtual FWDATATOOLS_API void * getBuffer()
Getter for the array buffer.
FWDATAIO_API std::string extension() override
Defines extension supported by this reader ".raw.gz".
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
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.
Array Reader. Read file format .raw.gz.