Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - Français


API string functions

This page lists all the functions to manipulate Scilab native objects containing strings

scilabVar scilab_createStringMatrix( scilabEnv env, int dim, const int* dims)

Create a string matrix with dim dimensions (dims[0], dims[1], ..., dims[dim-1]).

scilabVar scilab_createStringMatrix2d( scilabEnv env, int row, int col)

Create an string matrix with 2 dimensions (row, col).

scilabVar scilab_createString( scilabEnv env, const wchar_t* val)

Create a scalar string.

scilabStatus scilab_getStringArray( scilabEnv env, scilabVar var, wchar_t*** strs)

Get pointer on internal array of string of var.

scilabStatus scilab_getString( scilabEnv env, scilabVar var, wchar_t** str)

Get string value from an scalar string variable.

scilabStatus scilab_setStringArray( scilabEnv env, scilabVar var, const wchar_t* const* strs)

Set values of string variable var.

scilabStatus scilab_setString( scilabEnv env, scilabVar var, const wchar_t* str)

Set value of a scalar string variable var.

Examples

#include <ctype.h>
#include "api_scilab.h"
#include "Scierror.h"
#include "localization.h"
#include "sciprint.h"
#include "sci_malloc.h"
#include "os_string.h"

const char fname[] = "string_test";

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;
    int diff = L'a' - L'A';

    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_isMatrix(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;
}

Scilab test script

mkdir(pathconvert(TMPDIR+"/api_c/"));
cd(pathconvert(TMPDIR+"/api_c/"));
copyfile(SCI+"/modules/api_scilab/tests/unit_tests/api_c/string_test.c",pathconvert(TMPDIR+"/api_c/string_test.c",%F));

ilib_build("libstring",["string_test","sci_string_test", "csci6"],"string_test.c",[],"","","");
exec("loader.sce");

in1 = ["one.one", "one.two", "one.three", "one.four"; "two.one", "two.two", "two.three", "two.four"; "three.one", "three.two", "three.three", "three.four"];
in2 = "IBM™";

[out1, out2] = string_test(in1, in2);
ref(:,:,1) = in1 + ".one";
ref(:,:,2) = in1 + ".two";
assert_checkequal(out1, ref);
assert_checkequal(out2, "HAL™");
Report an issue
<< API common functions API Scilab API boolean functions >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:16:04 CEST 2024