summaryrefslogtreecommitdiff
path: root/modules/hdf5/src/cpp/H5StringData.cpp
blob: fcbf2a0d2fcd7be3df83a019cdcbbff1456122cf (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * 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 "H5StringData.hxx"

namespace org_modules_hdf5
{

H5StringData::H5StringData(H5Object & _parent, const hsize_t _totalSize, const hsize_t _stringSize, const hsize_t _ndims, const hsize_t * _dims, char * _data, const hsize_t _stride, const size_t _offset, const bool _dataOwner) : H5Data(_parent, _totalSize, _stringSize, _ndims, _dims, _data, _stride == 0 ? _stringSize : _stride, _offset, _dataOwner)
{
    char * __data = static_cast<char *>(_data);
    transformedData = new char*[totalSize];

    if (stride == 0)
    {
        *transformedData = __data;
        for (unsigned int i = 1; i < (unsigned int)totalSize; i++)
        {
            transformedData[i] = transformedData[i - 1] + dataSize;
        }
    }
    else
    {
        for (unsigned int i = 0; i < (unsigned int)totalSize; i++)
        {
            transformedData[i] = __data + offset;
            __data += stride;
        }
    }
}

H5StringData::H5StringData(H5Object & _parent, const hsize_t _totalSize, const hsize_t _stringSize, const hsize_t _ndims, const hsize_t * _dims, char ** _data, const hsize_t _stride, const size_t _offset, const bool _dataOwner) : H5Data(_parent, _totalSize, _stringSize, _ndims, _dims, _data, _stride == 0 ? _stringSize : _stride, _offset, _dataOwner), transformedData(0)
{

}

H5StringData::~H5StringData()
{

    if (transformedData)
    {
        delete[] transformedData;
    }
    else
    {
        char ** _data = reinterpret_cast<char **>(getData());
        hid_t space = H5Screate_simple(1, &totalSize, 0);
        hid_t type = H5Tcopy(H5T_C_S1);
        H5Tset_size(type, H5T_VARIABLE);
        H5Tset_strpad(type, H5T_STR_NULLTERM);

        herr_t err = H5Dvlen_reclaim(type, space, H5P_DEFAULT, _data);
        if (err < 0)
        {
            throw H5Exception(__LINE__, __FILE__, _("Cannot free the memory associated with String data"));
        }

        H5Tclose(type);
        H5Sclose(space);
    }
}

void * H5StringData::getData() const
{
    if (transformedData)
    {
        return transformedData;
    }
    else
    {
        return H5Data::getData();
    }
}

void H5StringData::printData(std::ostream & os, const unsigned int pos, const unsigned int indentLevel) const
{
    char * str = static_cast<char **>(getData())[pos];
    if (str)
    {
        os << "\"" << str << "\"";
    }
    else
    {
        os << "NULL";
    }
}

void H5StringData::toScilab(void * pvApiCtx, const int lhsPosition, int * parentList, const int listPosition, const bool flip) const
{
    static char EMPTY[] = { '\0' };

    char ** _tdata = 0;
    char ** _data = static_cast<char **>(getData());

    if (!transformedData)
    {
        // It is possible to have a nil pointer (Scilab doesn't like that)
        // so we replace nil ptr by an empty string...

        _tdata = new char *[totalSize];
        for (hsize_t i = 0; i < totalSize; i++)
        {
            if (_data[i])
            {
                _tdata[i] = _data[i];
            }
            else
            {
                _tdata[i] = static_cast<char *>(EMPTY);
            }
        }
        _data = _tdata;
    }

    std::cout << ndims << std::endl;

    if (ndims == 0)
    {
        H5BasicData<char *>::create(pvApiCtx, lhsPosition, 1, 1, _data, parentList, listPosition);
    }
    else if (ndims == 1)
    {
        H5BasicData<char *>::create(pvApiCtx, lhsPosition, 1, (int)*dims, _data, parentList, listPosition);
    }
    else
    {
        char ** newData = new char *[totalSize];
        if (ndims == 2)
        {
            H5DataConverter::C2FHypermatrix(2, dims, 0, _data, newData, flip);
            if (flip)
            {
                H5BasicData<char *>::create(pvApiCtx, lhsPosition, (int)dims[1], (int)dims[0], newData, parentList, listPosition);
            }
            else
            {
                H5BasicData<char *>::create(pvApiCtx, lhsPosition, (int)dims[0], (int)dims[1], newData, parentList, listPosition);
            }
        }
        else
        {
            int * list = getHypermatrix(pvApiCtx, lhsPosition, parentList, listPosition, flip);
            H5DataConverter::C2FHypermatrix((int)ndims, dims, totalSize, _data, newData, flip);
            H5BasicData<char *>::create(pvApiCtx, lhsPosition, (int)totalSize, 1, newData, list, 3);
        }
        delete[] newData;
    }

    if (_tdata)
    {
        delete[] _tdata;
    }
}

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