// Copyright (C) 2019 - IIT Bombay - FOSSEE // // 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-en.txt // Author: Rupak Rokade // Organization: FOSSEE, IIT Bombay // Email: toolbox@scilab.in #include #include #include "wchar.h" #include #include extern "C" { #include #include #include #include "localization.h" #include "fun.h" #include #include #include #include "os_string.h" #include static const char fname[] = "octave_fun"; /** * @brief Function to connect to Scilab's API. * * This function will get Data from Scilab, proccess the data * in Octave then return the output back to Scilab using the * API. * * @param env Scialb env * @param nin[in] Number of input arguments * @param in[in] Input Parameters * @param nopt[in] Number of optional parameters * @param opt[in] Optional parameters * @param nout[out] Number of expected output parametets * @param out[out] Array for output data * @return int */ int sci_octave_fun(scilabEnv env, int nin, scilabVar *in, int nopt, scilabOpt *opt, int nout, scilabVar *out) { //DEBUG//printf("nin: %d\n", nin); // Check number of inputs if (nin < 1) { Scierror(999, _("%s: Wrong number of input arguments. Atleast %d expected.\n"), fname, 2); return STATUS_ERROR; } // Declare and initialize the API variables FUNCCALL funcall; FUNCCALL *funptr = &funcall; funcall.n_in_arguments = nin; funcall.n_out_user = nout; FUNCARGS ins[funcall.n_in_arguments * nout]; FUNCARGS *argptr = ins; int i, j; double *d; double *rd = NULL; double *cd = NULL; int size; char str[20]; char *c; double *n = NULL; int row = 0; int col = 0; double *in_real; double *in_img; // Check the data type of input variables and format them into the FUNNCALL for (i = 0; i < nin; i++) { // Check if [in] of type Sci:matrix if (scilab_getType(env, in[i]) == 1) { ins[i].type = TYPE_DOUBLE; // Check if [in] is of type Sci:complex if (scilab_isComplex(env, in[i]) == 1) { //DEBUG//printf("input %d is complex \n", i); ins[i].is_in_cmplx = 1; size = scilab_getDim2d(env, in[i], &row, &col); ins[i].n_in_rows = row; ins[i].n_in_cols = col; scilab_getDoubleComplexArray(env, in[i], &in_real, &in_img); ins[i].in_data_real = malloc(sizeof(double) * size); ins[i].in_data_img = malloc(sizeof(double) * size); rd = (double *)ins[i].in_data_real; cd = (double *)ins[i].in_data_img; ////This code snippet is to flatten matrix row wise and then store it int p, q, k = 0; for (p = 0; p < row; p++) { for (q = 0; q < col; q++) { rd[k] = in_real[p + q * row]; cd[k] = in_img[p + q * row]; k++; //printf("%d\n",in_real[k]); //printf("%d\n",in_img[k]); } } } // [in] is not of type Sci:complex else { //DEBUG//printf("input %d is NOT complex \n", i); ins[i].is_in_cmplx = 0; size = scilab_getDim2d(env, in[i], &row, &col); ins[i].n_in_rows = row; ins[i].n_in_cols = col; scilab_getDoubleArray(env, in[i], &n); ins[i].in_data_real = malloc(sizeof(double) * size); d = (double *)ins[i].in_data_real; //DEBUG//This code snippet is to flatten matrix row wise and then store it int p, q, k = 0; for (p = 0; p < row; p++) { for (q = 0; q < col; q++) { d[k] = n[p + q * row]; k++; //printf("%f\n",d[j]); } } } ///////////////////////////////////////// } // Check if [in] of type SCI:Matrix of strings else if (scilab_getType(env, in[i]) == 10) { ins[i].is_in_cmplx = 0; wchar_t *in1 = 0; scilab_getString(env, in[i], &in1); //printf("%S\n", in1); wcstombs(str, in1, sizeof(str)); //printf("%s\n", str); if (str) { //printf("lenght of string input: %d\n", strlen(str)); ins[i].type = TYPE_STRING; ins[i].n_in_rows = 1; ins[i].n_in_cols = strlen(str); size = (ins[i].n_in_rows) * (ins[i].n_in_cols); ins[i].in_data_real = malloc(sizeof(char) * (size + 1)); c = (char *)ins[i].in_data_real; int ci; strcpy(c, str); ins[i].n_in_cols = strlen(c); //printf("in scilab strin is: %s\n", c); } } else if (scilab_getType(env, in[i]) == 18) //Checking for Struct input { ins[i].type = TYPE_STRUCT; wchar_t** keys = NULL; scilabVar struct_out; int dims = 0; // call getfields only when struct is not empty else getfields will crash if(scilab_isEmpty(env, in[i]) != 1){ dims = scilab_getFields(env, in[i], &keys); // Retrieving Struct Keys } ins[i].n_in_struct_len = dims; //std::cout<