<?xml version="1.0" encoding="utf-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2009  - DIGITEO - 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="handle_writing_API" xml:lang="en">
    <refnamediv>
        <refname>Handle writing (Scilab gateway)</refname>
        <refpurpose>
            How to write matrices of handle.
        </refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <para>Input argument profile:</para>
        <synopsis>SciErr createMatrixOfHandle(void* _pvCtx, int _iVar, int _iRows, int _iCols, const long long* _pllHandle)</synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>_pvCtx</term>
                <listitem>
                    <para>
                        Scilab environment pointer, pass in "pvApiCtx" provided by api_scilab.h.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_iVar</term>
                <listitem>
                    <para>
                        Position in the Scilab memory where you want to put the variable.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piRows</term>
                <listitem>
                    <para>
                        Return number of rows of the variable.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piCols</term>
                <listitem>
                    <para>
                        Return number of columns of the variable.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_pllHandle</term>
                <listitem>
                    <para>
                        Return address of data array (size: _iRows * _iCols).
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>SciErr</term>
                <listitem>
                    <para>
                        Error structure where is stored errors messages history and first error number.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsynopsisdiv>
        <refsection>
            Write directly in Scilab memory.
        </refsection>
        <title>Syntax</title>
        <para>Input argument profile:</para>
        <synopsis>SciErr allocMatrixOfHandle(void* _pvCtx, int _iVar, int _iRows, int _iCols, long long** _pllHandle)</synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>_pvCtx</term>
                <listitem>
                    <para>
                        Scilab environment pointer, pass in "pvApiCtx" provided by api_scilab.h.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_iVar</term>
                <listitem>
                    <para>
                        Position in the Scilab memory where you want to put the variable.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_iRows</term>
                <listitem>
                    <para>
                        Number of rows of the new variable.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_iCols</term>
                <listitem>
                    <para>
                        Numbers of columns of the new variable.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_pllHandle</term>
                <listitem>
                    <para>
                        Returns address of real data array (size: _iCols * _iRows).
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>SciErr</term>
                <listitem>
                    <para>
                        Error structure where is stored errors messages history and first error number.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>This help describes how to write matrix of handle.</para>
    </refsection>
    <refsection>
        <!--File_gateway: SCI/modules/api_scilab/tests/unit_tests/read_write_handle_api.c-->
        <!--File_scilab: SCI/modules/api_scilab/tests/unit_tests/read_write_handle_api.tst-->
        <!--Lib_name: read_write_handle-->
        <!--Func_list: read_write_handle-->
        <title>Gateway Source</title>
        <programlisting role="code_gateway">
            <![CDATA[
#include "api_scilab.h"
int read_write_handle(char *fname,void* pvApiCtx)
{
    SciErr sciErr;
    int i;
    //first variable info : real matrix of double
    int iRows       = 0;
    int iCols       = 0;
    int *piAddr     = NULL;
    long long* pllHandle   = NULL;

    //check input and output arguments
    CheckInputArgument(pvApiCtx, 1,1);
    CheckOutputArgument(pvApiCtx, 0,1);

    //get variable address of the first input argument
    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    //get size and data from Scilab memory
    sciErr = getMatrixOfHandle(pvApiCtx, piAddr, &iRows, &iCols, &pllHandle);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    sciErr = createMatrixOfHandle(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iRows, iCols, pllHandle);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
    return 0;
}
 ]]>
        </programlisting>
    </refsection>
    <refsection>
        <title>Scilab test script</title>
        <programlisting role="code_scilab">
            <![CDATA[
f1 = scf(1);
f2 = scf(2);
a_ref = [f1, f2];
b = read_write_handle(a_ref);

if or(b <> a_ref) then error("failed"), end
 ]]>
        </programlisting>
    </refsection>
    <title>History</title>
    <revhistory>
        <revision>
            <revnumber>5.5.0</revnumber>
            <revremark>This function added in Scilab 5.5.0</revremark>
        </revision>
    </revhistory>
</refentry>
