<?xml version="1.0" encoding="utf-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2015  - Scilab Enterprises - Antoine ELIAS
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
          xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
          xmlns:scilab="http://www.scilab.org" xml:id="api_struct" xml:lang="en">
    <refnamediv>
        <refname>API struct functions</refname>
        <para>This page lists all the functions to manipulate functions to manipulate Struct objects.</para>
        <para></para>
    </refnamediv>
    <refsynopsisdiv>
        <refsection id="api_struct_scilab_createStructMatrix">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_createStructMatrix</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env, int dim, const int* dims)
            </literal>
            <para>
                Create a struct matrix with <varname>dim</varname> dimensions (<varname>dims</varname>[0], <varname>dims</varname>[1], ..., <varname>dims</varname>[<varname>dim</varname>-1]).
            </para>
        </refsection>
        <refsection id="api_struct_scilab_createStructMatrix2d">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_createStructMatrix2d</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env, int row, int col)
            </literal>
            <para>
                Create an struct matrix with 2 dimensions (<varname>row</varname>, <varname>col</varname>).
            </para>
        </refsection>
        <refsection id="api_struct_scilab_createStruct">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_createStruct</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env)
            </literal>
            <para>Create a scalar struct.</para>
        </refsection>
        <refsection id="api_struct_scilab_addFields">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_addFields</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, int count, const wchar_t** fields)
            </literal>
            <para>
                Add <varname>count</varname> fields <varname>fields</varname> in struct <varname>var</varname>.
            </para>
        </refsection>
        <refsection id="api_struct_scilab_addField">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_addField</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, const wchar_t* field)
            </literal>
            <para>
                Add field <varname>field</varname> in struct <varname>var</varname>.
            </para>
        </refsection>
        <refsection id="api_struct_scilab_getFields">
            <literal>
                int <varname>scilab_getFields</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, wchar_t*** fields)
            </literal>
            <para>
                Get field names <varname>fields</varname> from struct <varname>var</varname> and returns count of fields.
            </para>
        </refsection>
        <refsection id="api_struct_scilab_getStructMatrixData">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_getStructMatrixData</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env, const wchar_t* field, const int* dims)
            </literal>
            <para>
                Get field value at position (<varname>index</varname>[0], <varname>index</varname>[1], ..., <varname>index</varname>[dims-1]) from <varname>var</varname>.
            </para>
        </refsection>
        <refsection id="api_struct_scilab_getStructMatrixData2d">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_getStructMatrixData2d</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env, const wchar_t* field, int row, int col)
            </literal>
            <para>
                Get field value at position (<varname>row</varname>, <varname>col</varname>) from <varname>var</varname>.
            </para>
        </refsection>
        <refsection id="api_struct_scilab_setStructMatrixData">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_setStructMatrixData</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, const wchar_t* field, const int* index,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                data)
            </literal>
            <para>
                Set field value at position (<varname>index</varname>[0], <varname>index</varname>[1], ..., <varname>index</varname>[dims-1]) in <varname>var</varname>.
            </para>
        </refsection>
        <refsection id="api_struct_scilab_setStructMatrixData2d">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_setStructMatrixData2d</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, const wchar_t* field, int row, int col,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                data)
            </literal>
            <para>
                Set field value at position (<varname>row</varname>, <varname>col</varname>) in <varname>var</varname>.
            </para>
        </refsection>
    </refsynopsisdiv>
    <refsection>
        <title>Examples</title>
        <programlisting role="code_gateway">
            <![CDATA[
#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[] = "struct_test";

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: Arguments #%d and #%d: Same sizes expected.\n"), fname, 1, 2);
        return STATUS_ERROR;
    }

    out1 = scilab_createStruct(env);

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

    out[0] = out1;
    return STATUS_OK;
}
            ]]>
        </programlisting>
    </refsection>
    <refsection>
        <title>Scilab test script</title>
        <programlisting role="code_scilab">
            <![CDATA[
mkdir(pathconvert(TMPDIR+"/api_c/"));
cd(pathconvert(TMPDIR+"/api_c/"));
copyfile(SCI+"/modules/api_scilab/tests/unit_tests/api_c/struct_test.c",pathconvert(TMPDIR+"/api_c/struct_test.c",%F));

ilib_build("libstruct",["struct_test","sci_struct_test", "csci6"],"struct_test.c",[],"","","");
exec("loader.sce");

fields = ["double","string","boolean","poly","list","cell"];
data = list(1, "2", %t, %s, list(1,2,3), {1,2;3,4});
t = struct_test(fields, data);

assert_checkequal(typeof(t), "st");
assert_checkequal(t.double, 1);
assert_checkequal(t.string, "2");
assert_checkequal(t.boolean, %t);
assert_checkequal(t.poly, %s);
assert_checkequal(t.list, list(1,2,3));
assert_checkequal(t.cell, {1,2;3,4});
            ]]>
        </programlisting>
    </refsection>
</refentry>
