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

matfile/matfile_concepts/src/Readers/ArraySparseWrapper.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 ARRAYSPARSEWRAPPERREADER_HPP_
00021 #define ARRAYSPARSEWRAPPERREADER_HPP_
00022 
00023 template<class Wrapper>
00024 class Reader<Wrapper, ArraySparseWrapper>
00025 {
00026 
00027 public:
00028   static void read(std::string& in, Wrapper& wrapper) {
00029     std::string uncompressed;
00030     char *p;
00031     miINT32_t *tag = reinterpret_cast<miINT32_t*>(&in[0]);
00032 
00033     if (*tag == miCOMPRESSED) {
00034       // Call zlib
00035       z_stream strm;
00036       strm.zalloc = Z_NULL;
00037       strm.zfree = Z_NULL;
00038       strm.opaque = Z_NULL;
00039       strm.avail_in = 0;
00040       strm.next_in = Z_NULL;
00041       inflateInit(&strm); //TODO: control return value
00042 
00043       miINT32_t matrixtag[2];
00044       strm.next_in = reinterpret_cast<Bytef*>(&in[8]);
00045       strm.avail_in = *(tag+1);
00046       strm.next_out = reinterpret_cast<Bytef*>(matrixtag);
00047       strm.avail_out = 8;
00048       inflate(&strm, Z_NO_FLUSH); //TODO: control return value
00049 
00050       //TODO: control matrixtag[0]
00051       uncompressed.reserve(matrixtag[1]);
00052       strm.next_out = reinterpret_cast<Bytef*>(&uncompressed[0]);
00053       strm.avail_out = matrixtag[1];
00054       inflate(&strm, Z_NO_FLUSH);
00055 
00056       p = &uncompressed[0];
00057     } else {
00058       p = &in[8];
00059     }
00060 
00061     // Flags
00062     const miUINT32_t *flags = reinterpret_cast<miUINT32_t*>(p+8);
00063     bool isComplex = ((*flags & 2048) != 0);
00064     wrapper.isComplex(isComplex);
00065     miUINT32_t nnz = *(flags+1);
00066     wrapper.nnz(nnz);
00067     p += 16;
00068 
00069     // Size
00070     miINT32_t *size = reinterpret_cast<miINT32_t*>(p);
00071     miINT32_t sizeLength = *(size+1);
00072     //TODO: control that matrix is two-dimensional
00073     miINT32_t rows = *(size+2);
00074     miINT32_t cols = *(size+3);
00075     wrapper.size(rows, cols);
00076     p += 8 + (sizeLength+7)/8*8;
00077 
00078     // Name
00079     //TODO: short tags
00080     miINT32_t *nameTag = reinterpret_cast<miINT32_t*>(p);
00081     miINT32_t nameLength = *(nameTag+1);
00082     std::string name(p+8, nameLength);
00083     wrapper.name(name);
00084     p += 8 + (nameLength+7)/8*8;
00085 
00086     // ir
00087     miINT32_t *ir = reinterpret_cast<miINT32_t*>(p+8);
00088     //TODO: control irLength == 4*nnz
00089     memcpy(wrapper.ir(), ir, 4*nnz);
00090     p += 8 + (4*nnz+7)/8*8;
00091 
00092     // jc
00093     miINT32_t *jc = reinterpret_cast<miINT32_t*>(p+8);
00094     //TODO: control jcLength == 4*(cols+1)
00095     memcpy(wrapper.jc(), jc, 4*(cols+1));
00096     p += 8 + (4*(cols+1)+7)/8*8;
00097 
00098     // pr
00099     miINT32_t *prTag = reinterpret_cast<miINT32_t*>(p);
00100     miMATRIX_class realType = *prTag;
00101     wrapper.realType(realType);
00102     miINT32_t prLength = *(prTag+1);
00103     memcpy(wrapper.pr(), p+8, prLength);
00104     p += 8 + (prLength+7)/8*8;
00105 
00106     // pi
00107     if (isComplex && wrapper.pi() != NULL) {
00108       miINT32_t *piTag = reinterpret_cast<miINT32_t*>(p);
00109       miMATRIX_class imagType = *piTag;
00110       wrapper.imagType(imagType);
00111       miINT32_t piLength = *(piTag+1);
00112       memcpy(wrapper.pi(), p+8, piLength);
00113       p += (piLength+7)/8*8;
00114     }
00115 
00116 
00117   }
00118 
00119 };
00120 
00121 #endif /* ARRAYSPARSEWRAPPERREADER_HPP_ */

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