<?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="int_getmatrixofintegerprecision_API" xml:lang="en">
    <refnamediv>
        <refname>Integer Precision (Scilab gateway)</refname>
        <refpurpose>
            How to get precision of an integer matrix.
        </refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>SciErr getMatrixOfIntegerPrecision(void* _pvCtx, int* _piAddress, int* _piPrecision)</synopsis>
        <synopsis>SciErr getNamedMatrixOfIntegerPrecision(void* _pvCtx, const char* _pstName, int* _piPrecision)</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>_piAddress</term>
                <listitem>
                    <para>
                        Address of the variable.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_pstName</term>
                <listitem>
                    <para>
                        Name of the variable for "named" functions.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piPrecision</term>
                <listitem>
                    <para>
                        Return precision of an integer variable. If value is greater then 10 then the variable is unsigned.
                    </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 get precision of an integer matrix.</para>
    </refsection>
    <refsection>
        <!--File_gateway: SCI/modules/api_scilab/tests/unit_tests/common_function_api.c-->
        <!--File_scilab: SCI/modules/api_scilab/tests/unit_tests/common_function.tst-->
        <!--Lib_name: common_function-->
        <!--Func_list: common_function-->
        <title>Gateway Source</title>
        <programlisting role="code_gateway">
            <![CDATA[
#include "api_scilab.h"
SciErr printf_info(int _iVar);

int common_function(char *fname,void* pvApiCtx)
{
	SciErr sciErr;
	int i;
	int *piAddr1    = NULL;
	int iBool       = 0;

	for(i = 0 ; i < nbInputArgument(pvApiCtx) ; i++)
	{
		sciErr = printf_info(i + 1);
		if(sciErr.iErr)
		{
			printError(&sciErr, 0);
			break;
		}
		sciprint("\n\n");
	}

	//1 for true, 0 for false
	iBool = sciErr.iErr == 0 ? 1 : 0;

	sciErr = createMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 1, &iBool);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	//assign allocated variables to Lhs position
	AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
	return 0;
}

SciErr printf_info(int _iVar)
{
	SciErr sciErr;
	int* piAddr     = NULL;
	int iType       = 0;

	sciErr = getVarAddressFromPosition(pvApiCtx, _iVar, &piAddr);
	if(sciErr.iErr)
	{
		return sciErr;
	}

	sciprint("Variable %d information:\n", _iVar);

	sciErr = getVarType(pvApiCtx, piAddr, &iType);
	if(sciErr.iErr)
	{
		return sciErr;
	}

	sciprint("\tType: ");
	if(iType == sci_ints)
	{
		char pstSigned[]    = "signed";
		char pstUnsigned[]  = "unsigned";
		char* pstSign       = pstSigned;
		int iPrec           = 0;

		sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddr, &iPrec);
		if(sciErr.iErr)
		{
			return sciErr;
		}

		if(iPrec > 10)
		{
			pstSign = pstUnsigned;
		}

		sciprint("%s integer %d bits\n", pstSign, (iPrec % 10) * 8);
    }
	else
	{
		sciprint("Not manage by this function\n");
		return sciErr;
    }

	return sciErr;
}
 ]]>
        </programlisting>
    </refsection>
    <refsection>
        <title>Scilab test script</title>
        <programlisting role="code_scilab"><![CDATA[
l1 = [1,2*%i,3;%i,2,3*%i];
l2 = ["may","the";"puffin","be";"with","you"];
l3 = int8([1,2,3]);
l4 = uint16([1000,2000,3000]);
l5 = list(l1,l2,l3);
l = list(l1,l2,l3,l4,l5);
common_function(l(1:$))
 ]]></programlisting>
    </refsection>
</refentry>
