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;
}
Beispiel #2
0
int sci_struct_test(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out)
{
    int i = 0;
    //input
    scilabVar in1 = NULL;
    int size1 = 0;
    wchar_t** fields = NULL;
    scilabVar in2 = NULL;
    int size2 = 0;
    //output
    scilabVar out1 = NULL;

    //goal is to take string vector and list from intput to
    //create a struct with fields names from string and
    //fields data from list.

    if (nin != 2)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2);
        return STATUS_ERROR;
    }

    if (nout != 1)
    {
        Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), fname, 1);
        return STATUS_ERROR;
    }

    //in1
    in1 = in[0];
    if (scilab_isString(env, in1) == 0 || scilab_isVector(env, in1) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string vector expected.\n"), fname, 1);
        return STATUS_ERROR;
    }

    size1 = scilab_getSize(env, in1);
    scilab_getStringArray(env, in1, &fields);

    //in2
    in2 = in[1];
    if (scilab_isList(env, in2) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A list expected.\n"), fname, 2);
        return STATUS_ERROR;
    }

    size2 = scilab_getSize(env, in2);

    if (size1 != size2)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: arg1 and arg2 must have same size.\n"), fname);
        return STATUS_ERROR;
    }

    out1 = scilab_createStruct(env);

    for (i = 0; i < size1; ++i)
    {
        scilab_addField(env, out1, fields[i]);
        scilab_setStructMatrix2dData(env, out1, fields[i], 0, 0, scilab_getListItem(env, in2, i));
    }

    out[0] = out1;
    return STATUS_OK;
}