/*--------------------------------------------------------------------------*/ int sci_getio(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out) { if (nin != 0) { Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), fname, 0); return 1; } if (nout != 1) { Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), fname, 1); return 1; } //get diarys count int diary = 0; int* diarys = getDiaryIDs(&diary); //don't care about ids delete[] diarys; //create data values array std::vector<double> val = {5, 5, (double)diary, 6}; //create output matrix scilabVar var = scilab_createDoubleMatrix2d(env, 1, 4, 0); //fill it scilab_setDoubleArray(env, var, val.data()); //return it to scilab out[0] = var; return 0; }
/* ==================================================================== */ int sci_foo6(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt* opt, int nout, scilabVar* out) { int i = 0; int row1 = 0; int col1 = 0; int size1 = 0; double* in1 = NULL; double* out1 = NULL; int row2 = 0; int col2 = 0; int size2 = 0; int* in2 = NULL; int* out2 = NULL; /* --> [a,b] = foo(d[i,j], b[i,j]) /* check that we have only 2 input arguments */ /* check that we have only 1 output argument */ if (nin > 2) { Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), fname, 2); return 1; } if (scilab_isDouble(env, in[0]) == 0 || scilab_isMatrix2d(env, in[0]) == 0 || scilab_isComplex(env, in[0]) == 1) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), fname, 1); return 1; } if (scilab_isBoolean(env, in[1]) == 0 || scilab_isMatrix2d(env, in[1]) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: A boolean matrix expected.\n"), fname, 2); return 1; } size1 = scilab_getDim2d(env, in[0], &row1, &col1); size2 = scilab_getDim2d(env, in[1], &row2, &col2); if (row1 != row2 || col1 != col2) { Scierror(999, _("%s: Wrong size for input arguments: Same size expected.\n"), fname); return 1; } scilab_getDoubleArray(env, in[0], &in1); scilab_getBooleanArray(env, in[1], &in2); out[0] = scilab_createDoubleMatrix2d(env, row1, col1, 0); out[1] = scilab_createBooleanMatrix2d(env, row2, col2); scilab_getDoubleArray(env, out[0], &out1); scilab_getBooleanArray(env, out[1], &out2); for (i = 0; i < size1; ++i) { //For each element of the matrix, multiply by 2 out1[i] = in1[i] * 2; } for (i = 0; i < size2; ++i) { //For each element of the matrix, invert the value out2[i] = in2[i] == TRUE ? FALSE : TRUE; } return 0; }
int sci_call(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out) { std::vector<Parameter> params(30); std::vector<int> output_order(nout); wchar_t* interf = NULL; if (nin < 1) { Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), fname, 1); return 1; } //1st is the interface name if (scilab_isString(env, in[0]) == 0 || scilab_isScalar(env, in[0]) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1); return 1; } scilab_getString(env, in[0], &interf); ConfigVariable::EntryPointStr* func = ConfigVariable::getEntryPoint(interf); if (func == NULL) { Scierror(999, _("%s: unable to find entry point %ls.\n"), fname, interf); return 1; } int pos = 1; bool hasOutputs = true; //inputs while (1) { //check "out" to break loop if (isOut(env, in[pos])) { hasOutputs = true; break; } if (pos > nin) { break; } int type = 0; if (nin < pos + 2) { Scierror(77, _("%s: Wrong number of input argument(s).\n"), fname); return 1; } type = scilab_getType(env, in[pos]); if (type != sci_matrix && type != sci_strings) { Scierror(77, _("%s: Wrong type for input argument #%d: A real matrix or a string expected.\n"), fname, pos + 1); return 1; } //data //position if (scilab_isDouble(env, in[pos + 1]) == 0 || scilab_isScalar(env, in[pos + 1]) == 0) { Scierror(77, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), fname, pos + 2); return 1; } double param_pos = 0; scilab_getDouble(env, in[pos + 1], ¶m_pos); //type if (scilab_isString(env, in[pos + 2]) == 0 || scilab_isScalar(env, in[pos + 2]) == 0) { Scierror(77, _("%s: Wrong type for input argument #%d : string expected.\n"), fname, pos + 3); return 1; } void* data = NULL; int row = 0; int col = 0; wchar_t* param_type = NULL; scilab_getString(env, in[pos + 2], ¶m_type); if (param_type[0] == L'c' || type == sci_strings) { if (param_type[0] != L'c' || type != sci_strings) { Scierror(77, _("%s: Wrong type for input argument #%d : string expected.\n"), fname, pos + 1); return 1; } } bool alloc = false; switch (param_type[0]) { case L'c': { wchar_t* strs = NULL; scilab_getString(env, in[pos], &strs); char* c = wide_string_to_UTF8(strs); data = c; alloc = true; break; } case L'd': { double* dbls = NULL; scilab_getDoubleArray(env, in[pos], &dbls); data = dbls; break; } case L'r': { double* dbls = NULL; int size = scilab_getSize(env, in[pos]); scilab_getDoubleArray(env, in[pos], &dbls); float* f = (float*)malloc(size * sizeof(float)); for (int i = 0; i < size; ++i) { f[i] = (float)dbls[i]; } data = f; alloc = true; break; } case L'i': { double* dbls = NULL; int size = scilab_getSize(env, in[pos]); scilab_getDoubleArray(env, in[pos], &dbls); int* ints = (int*)malloc(size * sizeof(int)); for (int i = 0; i < size; ++i) { ints[i] = (int)dbls[i]; } data = ints; alloc = true; break; } default: { Scierror(77, _("%s: Wrong value for input argument #%d: '%s', '%s', '%s' or '%s' expected.\n"), fname, pos + 3, "d", "r", "i", "c"); return 1; } } scilab_getDim2d(env, in[pos], &row, &col); Parameter& p = params[(int)param_pos - 1]; p.alloc = alloc; p.data = data; p.row = row; p.col = col; p.type = param_type[0]; pos += 3; } int output_pos = 0; //outputs if (hasOutputs) { ++pos; //avoid "out" while (1) { //check if is 3 or 1 arg ... if (scilab_isDouble(env, in[pos]) == 0) { Scierror(77, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), fname, pos + 1); return 1; } if (scilab_isScalar(env, in[pos])) { double dorder = 0; scilab_getDouble(env, in[pos], &dorder); int order = (int)dorder; if (params[order - 1].data == nullptr) { Scierror(77, _("%s: Wrong value for input argument #%d.\n"), fname, pos + 1); return 1; } pos += 1; output_order[output_pos] = order - 1; } else { //dims double* dims = 0; scilab_getDoubleArray(env, in[pos], &dims); int size = (int)dims[0] * (int)dims[1]; //pos if (scilab_isDouble(env, in[pos + 1]) == 0 || scilab_isScalar(env, in[pos + 1]) == 0) { Scierror(77, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), fname, pos + 2); return 1; } double param_pos = 0; scilab_getDouble(env, in[pos + 1], ¶m_pos); //type if (scilab_isString(env, in[pos + 2]) == 0 || scilab_isScalar(env, in[pos + 2]) == 0) { Scierror(77, _("%s: Wrong type for input argument #%d : string expected.\n"), fname, pos + 3); return 1; } wchar_t* param_type = NULL; scilab_getString(env, in[pos + 2], ¶m_type); void* data = NULL; switch (param_type[0]) { case L'c': { data = malloc((size + 1) * sizeof(char)); break; } case L'd': { data = malloc(size * sizeof(double)); break; } case L'r': { data = malloc(size * sizeof(float)); break; } case L'i': { data = malloc(size * sizeof(int)); break; } } Parameter& p = params[(int)param_pos - 1]; p.row = (int)dims[0]; p.col = (int)dims[1]; p.alloc = true; p.type = param_type[0]; p.data = data; pos += 3; output_order[output_pos] = (int)param_pos - 1; } ++output_pos; if (pos + 1 > nin) { break; } } } //the unbelievable call ! ((fct)func->functionPtr)(params[0].data, params[1].data, params[2].data, params[3].data, params[4].data, params[5].data, params[6].data, params[7].data, params[8].data, params[9].data, params[10].data, params[11].data, params[12].data, params[13].data, params[14].data, params[15].data, params[16].data, params[17].data, params[18].data, params[19].data, params[20].data, params[21].data, params[22].data, params[23].data, params[24].data, params[25].data, params[26].data, params[27].data, params[28].data, params[29].data); //create output variables for (int i = 0; i < nout; ++i) { Parameter& p = params[output_order[i]]; switch (p.type) { case L'c': { wchar_t* w = to_wide_string((char*)p.data); scilabVar var = scilab_createString(env, w); out[i] = var; FREE(w); break; } case L'd': { scilabVar var = scilab_createDoubleMatrix2d(env, p.row, p.col, 0); scilab_setDoubleArray(env, var, (double*)p.data); out[i] = var; break; } case L'r': { double* d = NULL; scilabVar var = scilab_createDoubleMatrix2d(env, p.row, p.col, 0); scilab_getDoubleArray(env, var, &d); int size = p.row * p.col; for (int j = 0; j < size; ++j) { d[j] = (double)((float*)p.data)[j]; } out[i] = var; break; } case L'i': { double* d = NULL; scilabVar var = scilab_createDoubleMatrix2d(env, p.row, p.col, 0); scilab_getDoubleArray(env, var, &d); int size = p.row * p.col; for (int j = 0; j < size; ++j) { d[j] = (double)((int*)p.data)[j]; } out[i] = var; break; } } } return STATUS_OK; }