fw4spl
GzArrayWriter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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/writer/GzArrayWriter.hpp"
8 
9 #include "fwDataIO/writer/registry/macros.hpp"
10 
11 #include <fwDataTools/helper/ArrayGetter.hpp>
12 
13 #include <boost/filesystem/path.hpp>
14 
15 #include <zlib.h>
16 
17 #include <iostream>
18 
19 fwDataIOWriterRegisterMacro( ::fwDataIO::writer::GzArrayWriter);
20 
21 namespace fwDataIO
22 {
23 namespace writer
24 {
25 
26 //------------------------------------------------------------------------------
27 
29  ::fwData::location::enableSingleFile< ::fwDataIO::writer::IObjectWriter >(this)
30 {
31 }
32 
33 //------------------------------------------------------------------------------
34 
36 {
37 }
38 
39 //------------------------------------------------------------------------------
40 
42 {
43  assert( getFile().empty() == false );
44 
45  ::fwData::Array::csptr array = this->getConcreteObject();
46 
48  gzFile rawFile = gzopen( this->getFile().string().c_str(), "wb1");
49  if ( rawFile == 0 )
50  {
51  std::string str = "GzArrayWriter::write unable to open ";
52  str += getFile().string();
53  gzclose(rawFile);
54  throw std::ios_base::failure(str);
55  }
56 
57  ::fwDataTools::helper::ArrayGetter arrayHelper(array);
58  // file is OK : process now
59  const size_t arraySizeInBytes = array->getSizeInBytes();
60 
61  const unsigned int uncompressedbyteswrited = gzwrite(rawFile, arrayHelper.getBuffer(), arraySizeInBytes);
62  gzclose(rawFile);
63  if ( uncompressedbyteswrited != arraySizeInBytes )
64  {
65  std::string str = "GzArrayWriter::write unable to write ";
66  str += getFile().string();
67  throw std::ios_base::failure(str);
68  }
69 }
70 
71 //------------------------------------------------------------------------------
72 
74 {
75  return ".raw.gz";
76 }
77 
78 //------------------------------------------------------------------------------
79 
80 } // namespace writer
81 
82 } // namespace fwDataIO
virtual FWDATATOOLS_API const void * getBuffer() const
Getter for the array buffer.
Definition: ArrayGetter.cpp:30
FWDATAIO_API GzArrayWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor. Do nothing.
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
virtual std::shared_ptr< const DataType > getConcreteObject() const
m_object getter.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Array Writer. Write file format .raw.gz.
Helper to manage array buffer. Lock the buffer before to modify it.
Definition: ArrayGetter.hpp:28
virtual FWDATAIO_API std::string extension() override
Defines extension supported by this writer ".raw.gz".
virtual FWDATAIO_API void write() override
Read the file with zlib API.
Base class for all object writers.
Contains the representation of the data objects used in the framework.
virtual FWDATAIO_API ~GzArrayWriter()
Destructor. Do nothing.