File: coda-matlab-traverse.c

package info (click to toggle)
coda 2.25.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 10,168 kB
  • sloc: ansic: 121,489; javascript: 6,788; java: 2,369; python: 1,695; yacc: 1,007; makefile: 598; lex: 204; sh: 105; fortran: 60; xml: 5
file content (237 lines) | stat: -rw-r--r-- 7,947 bytes parent folder | download | duplicates (2)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) 2007-2024 S[&]T, The Netherlands.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "coda-matlab.h"

#include <string.h>

/*
 * Depending on the contents of arg, arg_type is set to
 *   0 : index -> index[] is set to the value in arg and length is set to the number of dimensions
 *   1 : name -> *name is set to the value in arg and length is set to the length of the string
 *  -1 : invalid -> nothing is changed.
 *     arg is considered invalid if it doesn't contain a valid string or
 *     if it doesn't contain a value array of indices.
 *
 *     The user should pass index as an integer array of size = CODA_MAX_N_DIMS.
 *
 *     If *name != NULL then *name contains a string that was allocated with
 *     mxCalloc and the caller of this function should free it with mxFree.
 */
static void coda_matlab_parse_arg(const mxArray *arg, int *arg_type, long *index, char **name, int *length)
{
    int arg_num_dims;
    const mwSize *arg_dim;

    mxAssert(arg != NULL, "Arguments array pointer is zero");
    mxAssert(arg_type != NULL, "Pointer to 'type' argument is zero");
    mxAssert(index != NULL, "Pointer to 'index' argument is zero");
    mxAssert(name != NULL, "Pointer to 'name' argument is zero");
    mxAssert(length != NULL, "Pointer to 'lenght' argument is zero");

    *arg_type = -1;

    arg_num_dims = (int)mxGetNumberOfDimensions(arg);
    arg_dim = mxGetDimensions(arg);

    if (mxGetClassID(arg) == mxCHAR_CLASS)
    {
        /* arg contains string */

        if (arg_num_dims == 2 && arg_dim[0] == 1 && arg_dim[1] > 0)
        {
            *length = (int)(arg_dim[0] * arg_dim[1] * sizeof(mxChar)) + 1;
            *name = mxCalloc(*length, 1);
            if (mxGetString(arg, *name, *length) != 0)
            {
                mexErrMsgTxt("Error copying string");
            }

            *arg_type = 1;
        }
    }
    else if (arg_num_dims == 2 && arg_dim[0] == 1 && arg_dim[1] > 0 && arg_dim[1] <= CODA_MAX_NUM_DIMS)
    {
        void *data;
        mwSize i;

        /* arg contains value array */

        data = mxGetData(arg);

        for (i = 0; i < arg_dim[1]; i++)
        {
            switch (mxGetClassID(arg))
            {
                case mxDOUBLE_CLASS:
                    index[i] = (int)((double *)data)[i];
                    break;
                case mxINT32_CLASS:
                    index[i] = (int)((int32_t *)data)[i];
                    break;
                default:
                    mexErrMsgTxt("Index parameter not of type double or int32");
                    break;
            }
        }

        *length = (int)arg_dim[1];
        *arg_type = 0;
    }
}

void coda_matlab_traverse_data(int nrhs, const mxArray *prhs[], coda_cursor *cursor, coda_MatlabCursorInfo *info)
{
    int arg_idx;
    long index[CODA_MAX_NUM_DIMS];

    if (info != NULL)
    {
        info->intermediate_cursor_flag = 0;     /* Final cursor unless otherwise specified */
    }

    for (arg_idx = 0; arg_idx < nrhs; arg_idx++)
    {
        char *name = NULL;
        int arg_type;
        int length;

        coda_matlab_parse_arg(prhs[arg_idx], &arg_type, index, &name, &length);

        if (arg_type == 0)
        {
            coda_type_class type_class;
            long local_index[CODA_MAX_NUM_DIMS];
            int i;

            if (coda_cursor_get_type_class(cursor, &type_class) != 0)
            {
                coda_matlab_coda_error();
            }

            if (type_class != coda_array_class)
            {
                mexErrMsgTxt("Using numeric index for type that is not an array");
            }

            for (i = 0; i < length; i++)
            {
                if (index[i] != -1)
                {
                    index[i]--;
                }
                else if (info != NULL)
                {
                    info->intermediate_cursor_flag = 1;
                }
            }

            if ((info != NULL) && (info->intermediate_cursor_flag))
            {
                info->argument_index = arg_idx;
                info->num_variable_indices = length;
                for (i = 0; i < length; i++)
                {
                    info->variable_index[i] = index[i];
                }
                return; /* Return intermediate cursor */
            }

            if (length == 1 && index[0] == 0)
            {
                coda_type *type;
                int num_dims;

                /* convert to zero dimensional index if needed */
                if (coda_cursor_get_type(cursor, &type) != 0)
                {
                    coda_matlab_coda_error();
                }
                if (coda_type_get_array_num_dims(type, &num_dims) != 0)
                {
                    coda_matlab_coda_error();
                }
                if (num_dims == 0)
                {
                    length = 0;
                }
            }

            for (i = 0; i < length; i++)
            {
                local_index[i] = coda_env.option_swap_dimensions ? index[i] : index[length - i + 1];
            }
            if (coda_cursor_goto_array_element(cursor, length, local_index) != 0)
            {
                if (coda_errno == CODA_ERROR_ARRAY_NUM_DIMS_MISMATCH)
                {
                    mexErrMsgTxt("Error in parameter (array dimensions mismatch)");
                }
                if (coda_errno == CODA_ERROR_ARRAY_OUT_OF_BOUNDS)
                {
                    mexErrMsgTxt("Error in parameter (array index out of bounds)");
                }
                coda_matlab_coda_error();
            }

        }
        else if (arg_type == 1)
        {
            if (coda_cursor_goto(cursor, name) != 0)
            {
                coda_matlab_coda_error();
            }
        }
        else
        {
            mexErrMsgTxt("Error in parameter (not a string or integer)");
        }

        if (name != NULL)
        {
            mxFree(name);
        }
    }
}

void coda_matlab_traverse_product(coda_product *pf, int nrhs, const mxArray *prhs[], coda_cursor *cursor,
                                  coda_MatlabCursorInfo *info)
{
    mxAssert(pf != NULL, "Productfile pointer is zero");
    mxAssert(cursor != NULL, "Coda Cursor pointer is zero");

    if (coda_cursor_set_product(cursor, pf) != 0)
    {
        coda_matlab_coda_error();
    }

    coda_matlab_traverse_data(nrhs, prhs, cursor, info);
}