| 1 | /* |
| 2 | * Copyright (c) 2015-2026, The matio contributors |
| 3 | * Copyright (c) 2012-2014, Christopher C. Hulbert |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | * list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer in the documentation |
| 14 | * and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "matio_private.h" |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | /** @brief Returns a pointer to the Cell array at a specific index |
| 33 | * |
| 34 | * Returns a pointer to the Cell Array Field at the given 1-relative index. |
| 35 | * MAT file must be a version 5 matlab file. |
| 36 | * @ingroup MAT |
| 37 | * @param matvar Pointer to the Cell Array MAT variable |
| 38 | * @param index linear index of cell to return |
| 39 | * @return Pointer to the Cell Array Field on success, NULL on error |
| 40 | */ |
| 41 | matvar_t * |
| 42 | Mat_VarGetCell(const matvar_t *matvar, int index) |
| 43 | { |
| 44 | size_t nelems = 1; |
| 45 | matvar_t *cell = NULL; |
| 46 | int err; |
| 47 | |
| 48 | if ( matvar == NULL ) |
| 49 | return NULL; |
| 50 | |
| 51 | err = Mat_MulDims(matvar, &nelems); |
| 52 | if ( err ) |
| 53 | return NULL; |
| 54 | |
| 55 | if ( 0 <= index && (size_t)index < nelems ) |
| 56 | cell = *((matvar_t **)matvar->data + index); |
| 57 | |
| 58 | return cell; |
| 59 | } |
| 60 | |
| 61 | /** @brief Indexes a cell array |
| 62 | * |
| 63 | * Finds cells of a cell array given a start, stride, and edge for each. |
| 64 | * dimension. The cells are placed in a pointer array. The cells should not |
| 65 | * be freed, but the array of pointers should be. If copies are needed, |
| 66 | * use Mat_VarDuplicate on each cell. |
| 67 | * |
| 68 | * Note that this function is limited to structure arrays with a rank less than |
| 69 | * 10. |
| 70 | * |
| 71 | * @ingroup MAT |
| 72 | * @param matvar Cell Array matlab variable |
| 73 | * @param start vector of length rank with 0-relative starting coordinates for |
| 74 | * each dimension. |
| 75 | * @param stride vector of length rank with strides for each dimension. |
| 76 | * @param edge vector of length rank with the number of elements to read in |
| 77 | * each dimension. |
| 78 | * @returns an array of pointers to the cells |
| 79 | */ |
| 80 | matvar_t ** |
| 81 | Mat_VarGetCells(const matvar_t *matvar, const int *start, const int *stride, const int *edge) |
| 82 | { |
| 83 | int i, j, N, I; |
| 84 | size_t idx[10] = |
| 85 | { |
| 86 | 0, |
| 87 | }, |
| 88 | cnt[10] = |
| 89 | { |
| 90 | 0, |
| 91 | }, |
| 92 | dimp[10] = { |
| 93 | 0, |
| 94 | }; |
| 95 | matvar_t **cells; |
| 96 | |
| 97 | if ( matvar == NULL || start == NULL || stride == NULL || edge == NULL ) { |
| 98 | return NULL; |
| 99 | } else if ( matvar->rank > 9 ) { |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | dimp[0] = matvar->dims[0]; |
| 104 | N = edge[0]; |
| 105 | I = start[0]; |
| 106 | idx[0] = start[0]; |
| 107 | for ( i = 1; i < matvar->rank; i++ ) { |
| 108 | idx[i] = start[i]; |
| 109 | dimp[i] = dimp[i - 1] * matvar->dims[i]; |
| 110 | N *= edge[i]; |
| 111 | I += start[i] * dimp[i - 1]; |
| 112 | } |
| 113 | cells = (matvar_t **)malloc(N * sizeof(matvar_t *)); |
| 114 | for ( i = 0; i < N; i += edge[0] ) { |
| 115 | for ( j = 0; j < edge[0]; j++ ) { |
| 116 | cells[i + j] = *((matvar_t **)matvar->data + I); |
| 117 | I += stride[0]; |
| 118 | } |
| 119 | idx[0] = start[0]; |
| 120 | I = idx[0]; |
| 121 | cnt[1]++; |
| 122 | idx[1] += stride[1]; |
| 123 | for ( j = 1; j < matvar->rank; j++ ) { |
| 124 | if ( cnt[j] == (size_t)edge[j] ) { |
| 125 | cnt[j] = 0; |
| 126 | idx[j] = start[j]; |
| 127 | if ( j < matvar->rank - 1 ) { |
| 128 | cnt[j + 1]++; |
| 129 | idx[j + 1] += stride[j + 1]; |
| 130 | } |
| 131 | } |
| 132 | I += idx[j] * dimp[j - 1]; |
| 133 | } |
| 134 | } |
| 135 | return cells; |
| 136 | } |
| 137 | |
| 138 | /** @brief Indexes a cell array |
| 139 | * |
| 140 | * Finds cells of a cell array given a linear indexed start, stride, and edge. |
| 141 | * The cells are placed in a pointer array. The cells themself should not |
| 142 | * be freed as they are part of the original cell array, but the pointer array |
| 143 | * should be. If copies are needed, use Mat_VarDuplicate on each of the cells. |
| 144 | * MAT file version must be 5. |
| 145 | * @ingroup MAT |
| 146 | * @param matvar Cell Array matlab variable |
| 147 | * @param start starting index |
| 148 | * @param stride stride |
| 149 | * @param edge Number of cells to get |
| 150 | * @returns an array of pointers to the cells |
| 151 | */ |
| 152 | matvar_t ** |
| 153 | Mat_VarGetCellsLinear(const matvar_t *matvar, int start, int stride, int edge) |
| 154 | { |
| 155 | matvar_t **cells = NULL; |
| 156 | |
| 157 | if ( matvar != NULL ) { |
| 158 | int i, I; |
| 159 | cells = (matvar_t **)malloc(edge * sizeof(matvar_t *)); |
| 160 | I = start; |
| 161 | for ( i = 0; i < edge; i++ ) { |
| 162 | cells[i] = *((matvar_t **)matvar->data + I); |
| 163 | I += stride; |
| 164 | } |
| 165 | } |
| 166 | return cells; |
| 167 | } |
| 168 | |
| 169 | /** @brief Sets the element of the cell array at the specific index |
| 170 | * |
| 171 | * Sets the element of the cell array at the given 0-relative index to @c cell. |
| 172 | * @ingroup MAT |
| 173 | * @param matvar Pointer to the cell array variable |
| 174 | * @param index 0-relative linear index of the cell to set |
| 175 | * @param cell Pointer to the cell to set |
| 176 | * @return Pointer to the previous cell element, or NULL if there was no |
| 177 | * previous cell element or error. |
| 178 | */ |
| 179 | matvar_t * |
| 180 | Mat_VarSetCell(matvar_t *matvar, int index, matvar_t *cell) |
| 181 | { |
| 182 | size_t nelems = 1; |
| 183 | matvar_t **cells, *old_cell = NULL; |
| 184 | int err; |
| 185 | |
| 186 | if ( matvar == NULL || matvar->rank < 1 ) |
| 187 | return NULL; |
| 188 | |
| 189 | err = Mat_MulDims(matvar, &nelems); |
| 190 | if ( err ) |
| 191 | return NULL; |
| 192 | |
| 193 | cells = (matvar_t **)matvar->data; |
| 194 | if ( 0 <= index && (size_t)index < nelems ) { |
| 195 | old_cell = cells[index]; |
| 196 | cells[index] = cell; |
| 197 | } |
| 198 | |
| 199 | return old_cell; |
| 200 | } |