GUI module ported to api_scilab. test_run("gui")
[scilab.git] / scilab / modules / gui / sci_gateway / c / sci_ClipBoard.c
index 1a5af2e..6c48698 100644 (file)
@@ -3,11 +3,11 @@
 * Copyright (C) 2005 - INRIA - Allan CORNET
 * Copyright (C) 2008 - INRIA - Vincent COUVERT
 * Copyright (C) 2010 - DIGITEO - Vincent COUVERT
-* 
+*
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
-* are also available at    
+* are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */
@@ -16,7 +16,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include "gw_gui.h"
-#include "stack-c.h"
+#include "api_scilab.h"
 #include "Scierror.h"
 #include "MALLOC.h"
 #include "sciprint.h"
 #include "strdup_windows.h"
 #endif
 /*--------------------------------------------------------------------------*/
-int sci_ClipBoard(char *fname,unsigned long l)
+int sci_ClipBoard(char *fname, unsigned long l)
 {
-    static int l1 = 0,n1 = 0,m1 = 0;
-    char *param1 = NULL, *param2 = NULL;
+    SciErr sciErr;
+
+    int* piAddr1   = NULL;
+    int* piAddrl1   = NULL;
+    int* piAddrStr  = NULL;
+    double* pdbll1  = NULL;
+    int* pil1       = NULL;
+
+    static int n1 = 0, m1 = 0;
+    char* param1 = NULL;
+    char* param2 = NULL;
 
-    Rhs=Max(0,Rhs);
-    CheckRhs(0,2);
-    CheckLhs(0,1);
+    nbInputArgument(pvApiCtx) = Max(0, nbInputArgument(pvApiCtx));
+    CheckInputArgument(pvApiCtx, 0, 2);
+    CheckOutputArgument(pvApiCtx, 0, 1);
 
     if ( getScilabMode() != SCILAB_NWNI )
     {
         /*--------------------*/
         /* clipboard("paste") */
         /*--------------------*/
-        if (Rhs == 1)
+        if (nbInputArgument(pvApiCtx) == 1)
         {
-            if (GetType(1)==sci_strings)
+            if (checkInputArgumentType(pvApiCtx, 1, sci_strings))
             {
-                GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
-                param1=cstk(l1);
+                sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
+                if (sciErr.iErr)
+                {
+                    printError(&sciErr, 0);
+                    return 1;
+                }
 
-                if ( ( strcmp(param1,"paste") == 0 ) || ( strcmp(param1,"pastespecial") == 0 ) )
+                // Retrieve a matrix of double at position 1.
+                if (getAllocatedSingleString(pvApiCtx, piAddrl1, &param1))
+                {
+                    Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1);
+                    return 1;
+                }
+
+                if ( ( strcmp(param1, "paste") == 0 ) || ( strcmp(param1, "pastespecial") == 0 ) )
                 {
                     /* Use the Java clipboard (CallScilabBridge.java returns "" if clipbaord could not be read) */
                     char *output = getClipboardContents();
@@ -59,42 +79,61 @@ int sci_ClipBoard(char *fname,unsigned long l)
                     m1 = (int)strlen(output);
                     n1 = 1;
 
-                    CreateVarFromPtr(Rhs+ 1,STRING_DATATYPE, &m1, &n1, &output);
+                    if (createSingleString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, output))
+                    {
+                        freeAllocatedSingleString(param1);
+                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                        return 1;
+                    }
+
                     /* TO DO a delete [] and not a FREE */
                     FREE(output);
-                    LhsVar(1)=Rhs+1;
+                    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
 
-                    PutLhsVar();       
+                    freeAllocatedSingleString(param1);
+                    ReturnArguments(pvApiCtx);
                     return TRUE;
                 }
                 else
                 {
-                    Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "paste", "pastespecial");
+                    freeAllocatedSingleString(param1);
+                    Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "paste", "pastespecial");
                     return FALSE;
                 }
             }
             else
             {
-                Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
+                Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
                 return FALSE;
             }
-        } 
-
-        else if (Rhs == 2)
+        }
+        else if (nbInputArgument(pvApiCtx) == 2)
         {
-            if (GetType(1)==sci_strings)
+            if (checkInputArgumentType(pvApiCtx, 1, sci_strings))
             {
                 /* Get the first argument: should be "copy" or "do" */
-                GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
-                param1=cstk(l1);
+                sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1);
+                if (sciErr.iErr)
+                {
+                    printError(&sciErr, 0);
+                    return 1;
+                }
 
-                if (n1!=1)
+                // Retrieve a matrix of double at position 1.
+                if (getAllocatedSingleString(pvApiCtx, piAddrl1, &param1))
                 {
-                    Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "do","copy");
+                    Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1);
+                    return 1;
+                }
+
+                if (( strcmp(param1, "do") != 0 ) && ( strcmp(param1, "copy") != 0 ))
+                {
+                    freeAllocatedSingleString(param1);
+                    Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "do", "copy");
                     return FALSE;
                 }
 
-                if (GetType(2)==sci_strings)
+                if (checkInputArgumentType(pvApiCtx, 2, sci_strings))
                 {
 
                     /*-------------------------------------------*/
@@ -102,37 +141,61 @@ int sci_ClipBoard(char *fname,unsigned long l)
                     /*-------------------------------------------*/
 
                     /* @TODO : should be remplaced by an enum */
-                    if ( strcmp(param1,"do") == 0 )
+                    if ( strcmp(param1, "do") == 0 )
                     {
-                        GetRhsVar(2,STRING_DATATYPE,&m1,&n1,&l1);
-                        param2=cstk(l1);
+                        freeAllocatedSingleString(param1);
+
+                        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl1);
+                        if (sciErr.iErr)
+                        {
+                            printError(&sciErr, 0);
+                            return 1;
+                        }
 
-                        if ( strcmp(param2,"paste") == 0 )
+                        // Retrieve a matrix of double at position 2.
+                        if (getAllocatedSingleString(pvApiCtx, piAddrl1, &param2))
+                        {
+                            Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
+                            return 1;
+                        }
+
+                        if ( strcmp(param2, "paste") == 0 )
                         {
                             /* Call Java to do the job */
                             pasteClipboardIntoConsole();
                         }
-                        else if ( strcmp(param2,"copy") == 0 )
+                        else if ( strcmp(param2, "copy") == 0 )
                         {
                             /* Call Java to do the job */
                             copyConsoleSelection();
                         }
-                        else if ( strcmp(param2,"empty") == 0 )
+                        else if ( strcmp(param2, "empty") == 0 )
                         {
                             /* Call Java to do the job */
                             emptyClipboard();
                         }
                         else
                         {
-                            Scierror(999,_("%s: Wrong value for input argument #%d: '%s', '%s' or '%s' expected.\n"), fname, 2, "copy","paste","empty");
+                            freeAllocatedSingleString(param2);
+                            Scierror(999, _("%s: Wrong value for input argument #%d: '%s', '%s' or '%s' expected.\n"), fname, 2, "copy", "paste", "empty");
                             return FALSE;
                         }
 
-                        m1=0;
-                        n1=0;
-                        CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,  &m1, &n1, &l1);
-                        LhsVar(1)=Rhs+1;
-                        PutLhsVar();   
+                        m1 = 0;
+                        n1 = 0;
+
+                        freeAllocatedSingleString(param2);
+
+                        sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, &pdbll1);
+                        if (sciErr.iErr)
+                        {
+                            printError(&sciErr, 0);
+                            Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                            return 1;
+                        }
+
+                        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
+                        ReturnArguments(pvApiCtx);
                         return TRUE;
                     }
 
@@ -140,17 +203,31 @@ int sci_ClipBoard(char *fname,unsigned long l)
                     /* clipboard("copy", data) */
                     /*-------------------------*/
 
-                    else if ( strcmp(param1,"copy") == 0 )
+                    else if ( strcmp(param1, "copy") == 0 )
                     {
                         char *TextToPutInClipboard = NULL;
                         char **Str = NULL;
                         int m2 = 0, n2 = 0;
 
-                        GetRhsVar(2, MATRIX_OF_STRING_DATATYPE, &m2, &n2, &Str);
+                        freeAllocatedSingleString(param1);
+
+                        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrStr);
+                        if (sciErr.iErr)
+                        {
+                            printError(&sciErr, 0);
+                            return 1;
+                        }
+
+                        // Retrieve a matrix of string at position 2.
+                        if (getAllocatedMatrixOfString(pvApiCtx, piAddrStr, &m2, &n2, &Str))
+                        {
+                            Scierror(202, _("%s: Wrong type for argument #%d: String matrix expected.\n"), fname, 2);
+                            return 1;
+                        }
 
                         if (m2 * n2 == 1) /* Single line copy */
                         {
-                            TextToPutInClipboard=Str[0];
+                            TextToPutInClipboard = Str[0];
                             /* Call Java to do the job */
                             setClipboardContents(TextToPutInClipboard);
                         }
@@ -159,67 +236,78 @@ int sci_ClipBoard(char *fname,unsigned long l)
                             int i = 0, j = 0, l2 = 0;
                             char *TextToSendInClipboard = NULL;
                             int SizeofTextToSendInClipboard = 0;
-                            char **buffer = (char**)MALLOC( (m2 * n2)*sizeof(char *) );
+                            char **buffer = (char**)MALLOC( (m2 * n2) * sizeof(char *) );
                             if (buffer == NULL)
                             {
+
+                                freeAllocatedMatrixOfString(m2, n2, Str);
                                 Scierror(999, _("%s: No more memory.\n"), fname);
                                 return FALSE;
                             }
 
-                            for (i=0; i<m2; i++) 
+                            for (i = 0; i < m2; i++)
                             {
-                                for (j=0; j<n2; j++) 
+                                for (j = 0; j < n2; j++)
                                 {
-                                    SizeofTextToSendInClipboard = SizeofTextToSendInClipboard + (int)strlen(Str[j*m2+i]) + (int)strlen("\n") + (int)strlen(" ");
-                                    buffer[i*n2+j] = strdup(Str[j*m2+i]);
+                                    SizeofTextToSendInClipboard = SizeofTextToSendInClipboard + (int)strlen(Str[j * m2 + i]) + (int)strlen("\n") + (int)strlen(" ");
+                                    buffer[i * n2 + j] = strdup(Str[j * m2 + i]);
                                 }
                             }
 
-                            TextToSendInClipboard = (char*)MALLOC( (SizeofTextToSendInClipboard)*sizeof(char) );
+                            TextToSendInClipboard = (char*)MALLOC( (SizeofTextToSendInClipboard) * sizeof(char) );
                             if (TextToSendInClipboard == NULL)
                             {
+                                freeAllocatedMatrixOfString(m2, n2, Str);
                                 Scierror(999, _("%s: No more memory.\n"), fname);
                                 return FALSE;
                             }
+
                             strcpy(TextToSendInClipboard, "");
 
                             for (i = 0; i < m2; i++)
                             {
-                                for (j = 0; j < n2; j++) 
+                                for (j = 0; j < n2; j++)
                                 {
                                     strcat(TextToSendInClipboard, buffer[l2++]);
-                                    strcat(TextToSendInClipboard," ");
+                                    strcat(TextToSendInClipboard, " ");
+                                }
+                                if ( i != (m2 - 1) )
+                                {
+                                    strcat(TextToSendInClipboard, "\n");
                                 }
-                                if ( i != (m2 - 1) ) strcat(TextToSendInClipboard, "\n");
                             }
 
                             /* Call Java to do the job */
                             setClipboardContents(TextToSendInClipboard);
 
                             FREE(buffer);
-                            buffer=NULL;
+                            buffer = NULL;
 
                             FREE(TextToSendInClipboard);
-                            TextToSendInClipboard=NULL;
+                            TextToSendInClipboard = NULL;
                         }
 
-                        freeArrayOfString(Str,m2*n2);
+                        freeAllocatedMatrixOfString(m2, n2, Str);
                         m1 = 0;
                         n1 = 0;
-                        CreateVar(Rhs+1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1);
-                        LhsVar(1) = Rhs+1;
-                        PutLhsVar();           
+
+                        sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, &pdbll1);
+                        if (sciErr.iErr)
+                        {
+                            printError(&sciErr, 0);
+                            Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                            return 1;
+                        }
+
+                        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
+                        ReturnArguments(pvApiCtx);
                         return TRUE;
                     }
-                    else
-                    {
-                        Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 1, "do","copy");
-                        return FALSE;
-                    }
                 }
                 else
                 {
-                    Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
+                    freeAllocatedSingleString(param1);
+                    Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
                     return FALSE;
                 }
             }
@@ -228,31 +316,57 @@ int sci_ClipBoard(char *fname,unsigned long l)
             /* clipboard(fignum, {"EMF","DIB"}) */
             /*----------------------------------*/
 
-            else if (GetType(1)==sci_matrix)
+            else if (checkInputArgumentType(pvApiCtx, 1, sci_matrix))
             {
 
-                int num_win=-2;
+                int num_win = -2;
+
+                sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
+                if (sciErr.iErr)
+                {
+                    printError(&sciErr, 0);
+                    return 1;
+                }
+
+                // Retrieve a matrix of double at position 1.
+                sciErr = getMatrixOfDoubleAsInteger(pvApiCtx, piAddr1, &m1, &n1, &pil1);
+                if (sciErr.iErr)
+                {
+                    printError(&sciErr, 0);
+                    Scierror(202, _("%s: Wrong type for argument %d: A real expected.\n"), fname, 1);
+                    return 1;
+                }
 
-                GetRhsVar(1,MATRIX_OF_INTEGER_DATATYPE,&m1,&n1,&l1);
-                num_win=*istk(l1);
+                num_win = pil1[0];
 
-                if (m1*n1!=1)
+                if (m1*n1 != 1)
                 {
-                    Scierror(999,_("%s: Wrong size for input argument #%d: A real expected.\n"), fname, 1);
+                    Scierror(999, _("%s: Wrong size for input argument #%d: A real expected.\n"), fname, 1);
                     return FALSE;
                 }
 
-                if (GetType(2)==sci_strings)
+                if (checkInputArgumentType(pvApiCtx, 2, sci_strings))
                 {
-                    GetRhsVar(2,STRING_DATATYPE,&m1,&n1,&l1);
-                    param2=cstk(l1);
+                    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl1);
+                    if (sciErr.iErr)
+                    {
+                        printError(&sciErr, 0);
+                        return 1;
+                    }
+
+                    // Retrieve a matrix of double at position 2.
+                    if (getAllocatedSingleString(pvApiCtx, piAddrl1, &param2))
+                    {
+                        Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 2);
+                        return 1;
+                    }
 
-                    if ( ( strcmp(param2,"EMF") == 0 ) || ( strcmp(param2,"DIB") == 0 ) )
+                    if ( ( strcmp(param2, "EMF") == 0 ) || ( strcmp(param2, "DIB") == 0 ) )
                     {
-                        if (num_win>=0)
+                        if (num_win >= 0)
                         {
                             /* Call Java */
-                            if ( strcmp(param2,"EMF") == 0)
+                            if ( strcmp(param2, "EMF") == 0)
                             {
                                 /* @TODO create EMF */
                                 copyFigureToClipBoard(num_win);
@@ -263,43 +377,54 @@ int sci_ClipBoard(char *fname,unsigned long l)
                                 copyFigureToClipBoard(num_win);
                             }
 
-                            m1=0;
-                            n1=0;
-                            CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,  &m1, &n1, &l1);
-                            LhsVar(1)=Rhs+1;
-                            PutLhsVar();               
+                            m1 = 0;
+                            n1 = 0;
+
+                            freeAllocatedSingleString(param2);
+
+                            sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, &pdbll1);
+                            if (sciErr.iErr)
+                            {
+                                printError(&sciErr, 0);
+                                Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                                return 1;
+                            }
+                            AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
+                            ReturnArguments(pvApiCtx);
                             return TRUE;
                         }
                         else
                         {
-                            Scierror(999,_("%s: Wrong value for input argument #%d: Must be >= %d expected.\n"), fname, 1, 0);
+                            freeAllocatedSingleString(param2);
+                            Scierror(999, _("%s: Wrong value for input argument #%d: Must be >= %d expected.\n"), fname, 1, 0);
                             return FALSE;
                         }
 
                     }
                     else
                     {
-                        Scierror(999,_("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 2, "EMF","DIB");
+                        freeAllocatedSingleString(param2);
+                        Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 2, "EMF", "DIB");
                         return FALSE;
                     }
 
                 }
                 else
                 {
-                    Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
+                    Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
                     return FALSE;
                 }
             }
             else
             {
-                Scierror(999,_("%s: Wrong type for input argument #%d: A string or a real expected.\n"), fname, 1);
+                Scierror(999, _("%s: Wrong type for input argument #%d: A string or a real expected.\n"), fname, 1);
                 return FALSE;
             }
         }
     }
     else
     {
-        Scierror(999,_("%s: Function not available in NWNI mode.\n"), fname);
+        Scierror(999, _("%s: Function not available in NWNI mode.\n"), fname);
         return FALSE;
     }