summaryrefslogtreecommitdiff
path: root/modules/hdf5/src/cpp/H5VlenData.cpp
blob: eb59326ae8783e39097e15c6d29b2a88b2a0613d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
 *
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

#include "H5VlenData.hxx"
#include "H5DataFactory.hxx"

namespace org_modules_hdf5
{

H5VlenData::H5VlenData(H5Object & _parent, const hsize_t _totalSize, const hsize_t _dataSize, const hsize_t _ndims, const hsize_t * _dims, char * _data, hid_t vlenType, const hsize_t _stride, const size_t _offset, const bool _dataOwner) : H5BasicData<char>(_parent, _totalSize, _dataSize, _ndims, _dims, _data, _stride, _offset, _dataOwner), cumprod(H5Object::getCumProd(_ndims, dims))
{
    type = H5Tget_super(vlenType);
    baseSize = H5Tget_size(type);
    if (H5Tget_class(type) == H5T_STRING && !H5Tis_variable_str(type))
    {
        // We have a C-string so it is null terminated
        baseSize++;
    }
}

H5VlenData::~H5VlenData()
{
    delete[] cumprod;
    H5Tclose(type);
}

bool H5VlenData::isVlen() const
{
    return true;
}

H5Object & H5VlenData::getData(const unsigned int size, const unsigned int * index) const
{
    unsigned int pos = 0;
    hsize_t * _dims;
    hvl_t * x = 0;

    for (unsigned int i = 0; i < size; i++)
    {
        pos += (int)cumprod[i] * index[i];
    }

    if (pos >= totalSize)
    {
        throw H5Exception(__LINE__, __FILE__, _("Invalid index."));
    }

    x = reinterpret_cast<hvl_t *>(static_cast<char *>(data) + offset + pos * (stride ? stride : dataSize));
    _dims = new hsize_t[1];
    *_dims = (hsize_t)x->len;

    return H5DataFactory::getObjectData(*const_cast<H5VlenData *>(this), *_dims, baseSize, type, 1, _dims, x->p, 0, 0, false);
}

std::string H5VlenData::toString(const unsigned int indentLevel) const
{
    std::ostringstream os;
    std::string indentString = H5Object::getIndentString(indentLevel + 1);

    os << H5Object::getIndentString(indentLevel) << "HDF5 Variable length data" << std::endl
       << indentString << _("Dimensions") << ": [";

    if (ndims == 0)
    {
        os << "1 x 1]";
    }
    else if (ndims == 1)
    {
        os << "1 x " << dims[0] << "]";
    }
    else
    {
        for (unsigned int i = 0; i < ndims - 1; i++)
        {
            os << dims[i] << " x ";
        }
        os << dims[ndims - 1] << "]" << std::endl;
    }

    return os.str();
}

std::string H5VlenData::dump(std::map<haddr_t, std::string> & alreadyVisited, const unsigned int indentLevel) const
{
    return H5DataConverter::dump(alreadyVisited, indentLevel, (int)ndims, dims, *this, false);
}

void H5VlenData::printData(std::ostream & os, const unsigned int pos, const unsigned int indentLevel) const
{
    hvl_t * x = reinterpret_cast<hvl_t *>(static_cast<char *>(data) + offset + pos * (stride ? stride : dataSize));

    if (!x || !x->p)
    {
        os << "()";
        return;
    }

    hsize_t * _dims = new hsize_t[1];

    *_dims = (hsize_t)x->len;
    H5Data & hdata = H5DataFactory::getObjectData(*const_cast<H5VlenData *>(this), *_dims, baseSize, type, 1, _dims, x->p, 0, 0, false);

    os << "(";
    for (unsigned int i = 0; i < *_dims - 1; i++)
    {
        hdata.printData(os, i, indentLevel + 1);
        os << ", ";
    }
    hdata.printData(os, (int)*_dims - 1, indentLevel + 1);
    os << ")";

    delete &hdata;
}

void H5VlenData::toScilab(void * pvApiCtx, const int lhsPosition, int * parentList, const int listPosition, const bool flip) const
{
    H5Object::toScilab(pvApiCtx, lhsPosition, parentList, listPosition, flip);
}
}