bool isOut(scilabEnv env, scilabVar var) { if (scilab_isString(env, var) && scilab_isScalar(env, var)) { wchar_t* strs = NULL; scilab_getString(env, var, &strs); if (wcscmp(strs, L"out") == 0 || wcscmp(strs, L"sort") == 0) { return true; } } return false; }
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; }
int sci_string_test(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out) { int i = 0; int inr1 = 0; int inc1 = 0; int size1 = 0; wchar_t** in1 = NULL; wchar_t* in2 = 0; int dim1 = 3; int dims1[] = {0, 0, 2}; wchar_t** out1 = NULL; wchar_t* out2; int len2 = 0; if (nin != 2) { Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2); return STATUS_ERROR; } if (nout != 2) { Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), fname, 3); return STATUS_ERROR; } //in[0] : matrix 2d of string if (scilab_isString(env, in[0]) == 0 || scilab_isMatrix2d(env, in[0]) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: A string matrix expected.\n"), fname, 1); return STATUS_ERROR; } size1 = scilab_getDim2d(env, in[0], &inr1, &inc1); scilab_getStringArray(env, in[0], &in1); //in[1] : string if (scilab_isString(env, in[1]) == 0 || scilab_isScalar(env, in[1]) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: A double expected.\n"), fname, 2); return STATUS_ERROR; } scilab_getString(env, in[1], &in2); //out1 : matrix 2d of string with same size of in[0] dims1[0] = inr1; dims1[1] = inc1; out[0] = scilab_createStringMatrix(env, dim1, dims1); scilab_getStringArray(env, out[0], &out1); for (i = 0; i < size1; ++i) { wchar_t temp[128]; wcscpy(temp, in1[i]); wcscat(temp, L".one"); out1[i] = os_wcsdup(temp); wcscpy(temp, in1[i]); wcscat(temp, L".two"); out1[i + size1] = os_wcsdup(temp); } //out2 : string out2 = os_wcsdup(in2); len2 = wcslen(out2); for (i = 0; i < len2; ++i) { if (out2[i] >= L'a' && out2[i] <= L'z') { out2[i] = ((out2[i] - 'a' + 26 - 1) % 26) + 'a'; } else if (out2[i] >= L'A' && out2[i] <= L'Z') { out2[i] = ((out2[i] - 'A' + 26 - 1) % 26) + 'A'; } else { //no change } } out[1] = scilab_createString(env, out2); FREE(out2); return STATUS_OK; }
int sci_light(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out) { int light = 0; if (nin > 1) { Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), fname, 0, 1); return 1; } if (nin + nopt == 0) { light = ConstructLight(fname, 0, -1, TRUE, NULL, NULL, NULL, NULL, NULL); } else { int type = -1; BOOL visible = 1; double* position = NULL; double* direction = NULL; double* ambient_color = NULL; double* diffuse_color = NULL; double* specular_color = NULL; int axes = 0; scilabVar var = NULL; if (nin > 0 && scilab_isHandle(env, in[0])) { long long axesHandle = 0; if (scilab_isScalar(env, in[0])) { if (scilab_getHandle(env, in[0], &axesHandle)) { Scierror(999, _("%s: Wrong type for input argument #%d: A graphic handle expected.\n"), fname, 1); return 1; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A graphic handle expected.\n"), fname, 1); return 1; } axes = getObjectFromHandle((long)axesHandle); } //optionals var = scilab_getOptional(env, opt, L"visible"); if (var && scilab_isString(env, var) && scilab_isScalar(env, var)) { wchar_t* wstr = NULL; if (scilab_getString(env, var, &wstr)) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 7); return 0; } if (wcsicmp(wstr, L"on") == 0) { visible = 1; } else if (wcsicmp(wstr, L"off") == 0) { visible = 0; } } var = scilab_getOptional(env, opt, L"type"); if (var && scilab_isString(env, var) && scilab_isScalar(env, var)) { wchar_t* wstr = NULL; if (scilab_getString(env, var, &wstr)) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 7); return 0; } if (wcsicmp(wstr, L"directional") == 0) { type = 0; } else if (wcsicmp(wstr, L"point") == 0) { type = 1; } } var = scilab_getOptional(env, opt, L"position"); if (var && scilab_isDouble(env, var) && scilab_getSize(env, var) == 3) { scilab_getDoubleArray(env, var, &position); } var = scilab_getOptional(env, opt, L"direction"); if (var && scilab_isDouble(env, var) && scilab_getSize(env, var) == 3) { scilab_getDoubleArray(env, var, &direction); } var = scilab_getOptional(env, opt, L"ambient_color"); if (var && scilab_isDouble(env, var) && scilab_getSize(env, var) == 3) { scilab_getDoubleArray(env, var, &ambient_color); } var = scilab_getOptional(env, opt, L"diffuse_color"); if (var && scilab_isDouble(env, var) && scilab_getSize(env, var) == 3) { scilab_getDoubleArray(env, var, &diffuse_color); } var = scilab_getOptional(env, opt, L"specular_color"); if (var && scilab_isDouble(env, var) && scilab_getSize(env, var) == 3) { scilab_getDoubleArray(env, var, &specular_color); } light = ConstructLight(fname, axes, type, visible, position, direction, ambient_color, diffuse_color, specular_color); } //error occurs in ConstructLight if (light == 0) { //error is manage in ConstructLight return 1; } out[0] = scilab_createHandle(env); if (out[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } scilab_setHandle(env, out[0], getHandle(light)); return 0; }