Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

matfile/matfile_concepts/src/Readers/ArrayDenseWrapper.hh
Go to the documentation of this file.
00001 /*
00002     This file is part of matfile.
00003 
00004     Copyright (C) 2010-2011 Andrea Arteaga <arteagaa@student.ethz.ch>
00005 
00006     matfile is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU Lesser General Public License as published
00008     by the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     matfile is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public License
00017     along with matfile.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef ARRAYDENSEWRAPPERREADER_HPP_
00021 #define ARRAYDENSEWRAPPERREADER_HPP_
00022 
00023 template<class Wrapper>
00024 class Reader<Wrapper, ArrayDenseWrapper>
00025 {
00026 public:
00027   static void read(std::string& in, Wrapper& wrapper) {
00028     std::string uncompressed;
00029     char *p;
00030     miINT32_t *tag = reinterpret_cast<miINT32_t*>(&in[0]);
00031 
00032     if (*tag == miCOMPRESSED) {
00033       // Call zlib
00034       z_stream strm;
00035       strm.zalloc = Z_NULL;
00036       strm.zfree = Z_NULL;
00037       strm.opaque = Z_NULL;
00038       strm.avail_in = 0;
00039       strm.next_in = Z_NULL;
00040       inflateInit(&strm); //TODO: control return value
00041 
00042       miINT32_t matrixtag[2];
00043       strm.next_in = reinterpret_cast<Bytef*>(&in[8]);
00044       strm.avail_in = *(tag+1);
00045       strm.next_out = reinterpret_cast<Bytef*>(matrixtag);
00046       strm.avail_out = 8;
00047       inflate(&strm, Z_NO_FLUSH); //TODO: control return value
00048 
00049       //TODO: control matrixtag[0]
00050       uncompressed.reserve(matrixtag[1]);
00051       strm.next_out = reinterpret_cast<Bytef*>(&uncompressed[0]);
00052       strm.avail_out = matrixtag[1];
00053       inflate(&strm, Z_NO_FLUSH);
00054 
00055       p = &uncompressed[0];
00056     } else {
00057       p = &in[8];
00058     }
00059 
00060     // Flags
00061     const miUINT32_t *flags = reinterpret_cast<miUINT32_t*>(p+8);
00062     bool isComplex = ((*flags & 2048) != 0);
00063     wrapper.isComplex(isComplex);
00064     p += 16;
00065 
00066     // Size
00067     miINT32_t *size = reinterpret_cast<miINT32_t*>(p);
00068     miINT32_t sizeLength = *(size+1);
00069     //TODO: control that matrix is two-dimensional
00070     miINT32_t rows = *(size+2);
00071     miINT32_t cols = *(size+3);
00072     wrapper.size(rows, cols);
00073     p += 8 + (sizeLength+7)/8*8;
00074 
00075     // Name
00076     //TODO: short tags
00077     miINT32_t *nameTag = reinterpret_cast<miINT32_t*>(p);
00078     miINT32_t nameLength = *(nameTag+1);
00079     std::string name(p+8, nameLength);
00080     wrapper.name(name);
00081     p += 8 + (nameLength+7)/8*8;
00082 
00083     // pr
00084     miINT32_t *prTag = reinterpret_cast<miINT32_t*>(p);
00085     miMATRIX_class realType = *prTag;
00086     wrapper.realType(realType);
00087     miINT32_t prLength = *(prTag+1);
00088     memcpy(wrapper.pr(), p+8, prLength);
00089     p += 8 + (prLength+7)/8*8;
00090 
00091     // pi
00092     if (isComplex && wrapper.pi() != NULL) {
00093       miINT32_t *piTag = reinterpret_cast<miINT32_t*>(p);
00094       miMATRIX_class imagType = *piTag;
00095       wrapper.imagType(imagType);
00096       miINT32_t piLength = *(piTag+1);
00097       memcpy(wrapper.pi(), p+8, piLength);
00098       p += (piLength+7)/8*8;
00099     }
00100 
00101   }
00102 };
00103 
00104 #endif /* ARRAYDENSEWRAPPERREADER_HPP_ */

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)