postURL added 67/16867/2 atoms-improvements
Kartik Gupta [Wed, 22 Jul 2015 11:12:37 +0000 (16:12 +0530)]
Change-Id: If5542443b90721e268f3b6a01041f0ebbac11f1a

13 files changed:
scilab/modules/fileio/Makefile.am
scilab/modules/fileio/Makefile.in
scilab/modules/fileio/fileio.vcxproj
scilab/modules/fileio/fileio.vcxproj.filters
scilab/modules/fileio/help/en_US/postURL.xml [new file with mode: 0644]
scilab/modules/fileio/includes/gw_fileio.h
scilab/modules/fileio/sci_gateway/c/gw_fileio.c
scilab/modules/fileio/sci_gateway/c/sci_postURL.c [new file with mode: 0644]
scilab/modules/fileio/sci_gateway/fileio_gateway.xml
scilab/modules/fileio/src/c/dlManager.c
scilab/modules/fileio/src/c/dlManager.h
scilab/modules/fileio/tests/unit_tests/postURL.dia.ref [new file with mode: 0644]
scilab/modules/fileio/tests/unit_tests/postURL.tst [new file with mode: 0644]

index 7c2839c..2b4657c 100644 (file)
@@ -114,7 +114,8 @@ sci_gateway/c/sci_splitURL.c \
 sci_gateway/c/sci_archive_list.c \
 sci_gateway/c/sci_archive_extract.c \
 sci_gateway/c/sci_archive_compress.c \
-sci_gateway/c/sci_getURLheader.c
+sci_gateway/c/sci_getURLheader.c \
+sci_gateway/c/sci_postURL.c
 
 libscifileio_la_CPPFLAGS = -I$(srcdir)/includes/ \
                                -I$(srcdir)/src/c/ \
index 3efbdd5..e681546 100644 (file)
@@ -287,7 +287,8 @@ am__objects_2 = sci_gateway/c/libscifileio_la-gw_fileio.lo \
        sci_gateway/c/libscifileio_la-sci_archive_list.lo \
        sci_gateway/c/libscifileio_la-sci_archive_extract.lo \
        sci_gateway/c/libscifileio_la-sci_archive_compress.lo \
-       sci_gateway/c/libscifileio_la-sci_getURLheader.lo
+       sci_gateway/c/libscifileio_la-sci_getURLheader.lo \
+       sci_gateway/c/libscifileio_la-sci_postURL.lo
 am_libscifileio_la_OBJECTS = $(am__objects_2)
 libscifileio_la_OBJECTS = $(am_libscifileio_la_OBJECTS)
 @MAINTAINER_MODE_FALSE@am_libscifileio_la_rpath =
@@ -745,7 +746,8 @@ sci_gateway/c/sci_splitURL.c \
 sci_gateway/c/sci_archive_list.c \
 sci_gateway/c/sci_archive_extract.c \
 sci_gateway/c/sci_archive_compress.c \
-sci_gateway/c/sci_getURLheader.c
+sci_gateway/c/sci_getURLheader.c \
+sci_gateway/c/sci_postURL.c
 
 libscifileio_la_CPPFLAGS = -I$(srcdir)/includes/ \
                                -I$(srcdir)/src/c/ \
@@ -1234,6 +1236,9 @@ sci_gateway/c/libscifileio_la-sci_archive_compress.lo:  \
 sci_gateway/c/libscifileio_la-sci_getURLheader.lo:  \
        sci_gateway/c/$(am__dirstamp) \
        sci_gateway/c/$(DEPDIR)/$(am__dirstamp)
+sci_gateway/c/libscifileio_la-sci_postURL.lo:  \
+       sci_gateway/c/$(am__dirstamp) \
+       sci_gateway/c/$(DEPDIR)/$(am__dirstamp)
 
 libscifileio.la: $(libscifileio_la_OBJECTS) $(libscifileio_la_DEPENDENCIES) $(EXTRA_libscifileio_la_DEPENDENCIES) 
        $(AM_V_CCLD)$(LINK) $(am_libscifileio_la_rpath) $(libscifileio_la_OBJECTS) $(libscifileio_la_LIBADD) $(LIBS)
@@ -1296,6 +1301,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_newest.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_pathconvert.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_pathsep.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_postURL.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_pwd.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_removedir.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_splitURL.Plo@am__quote@
@@ -2145,6 +2151,13 @@ sci_gateway/c/libscifileio_la-sci_getURLheader.lo: sci_gateway/c/sci_getURLheade
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o sci_gateway/c/libscifileio_la-sci_getURLheader.lo `test -f 'sci_gateway/c/sci_getURLheader.c' || echo '$(srcdir)/'`sci_gateway/c/sci_getURLheader.c
 
+sci_gateway/c/libscifileio_la-sci_postURL.lo: sci_gateway/c/sci_postURL.c
+@am__fastdepCC_TRUE@   $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT sci_gateway/c/libscifileio_la-sci_postURL.lo -MD -MP -MF sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_postURL.Tpo -c -o sci_gateway/c/libscifileio_la-sci_postURL.lo `test -f 'sci_gateway/c/sci_postURL.c' || echo '$(srcdir)/'`sci_gateway/c/sci_postURL.c
+@am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_postURL.Tpo sci_gateway/c/$(DEPDIR)/libscifileio_la-sci_postURL.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      $(AM_V_CC)source='sci_gateway/c/sci_postURL.c' object='sci_gateway/c/libscifileio_la-sci_postURL.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o sci_gateway/c/libscifileio_la-sci_postURL.lo `test -f 'sci_gateway/c/sci_postURL.c' || echo '$(srcdir)/'`sci_gateway/c/sci_postURL.c
+
 mostlyclean-libtool:
        -rm -f *.lo
 
index 6c86a58..9a7365a 100644 (file)
@@ -213,6 +213,7 @@ lib /DEF:"$(ProjectDir)Preferences_Import.def" /SUBSYSTEM:WINDOWS /MACHINE:$(Pla
     <ClCompile Include="sci_gateway\c\sci_archive_list.c" />
     <ClCompile Include="sci_gateway\c\sci_getURL.c" />
     <ClCompile Include="sci_gateway\c\sci_getURLheader.c" />
+    <ClCompile Include="sci_gateway\c\sci_postURL.c" />
     <ClCompile Include="sci_gateway\c\sci_splitURL.c" />
     <ClCompile Include="sci_gateway\c\sci_tempname.c" />
     <ClCompile Include="src\c\addfile.c" />
index e9b3205..ca2f098 100644 (file)
     <ClCompile Include="src\c\scanf_functions.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="sci_gateway\c\sci_postURL.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="sci_gateway\c\sci_archive_compress.c">
       <Filter>Source Files</Filter>
     </ClCompile>
diff --git a/scilab/modules/fileio/help/en_US/postURL.xml b/scilab/modules/fileio/help/en_US/postURL.xml
new file mode 100644 (file)
index 0000000..86e94a7
--- /dev/null
@@ -0,0 +1,158 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) - S/E - Kartik Gupta
+ * 
+ * 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
+ * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+ *
+ -->
+<refentry xml:id="postURL" xml:lang="en"
+          xmlns="http://docbook.org/ns/docbook"
+          xmlns:xlink="http://www.w3.org/1999/xlink"
+          xmlns:svg="http://www.w3.org/2000/svg"
+          xmlns:ns3="http://www.w3.org/1999/xhtml"
+          xmlns:mml="http://www.w3.org/1998/Math/MathML"
+          xmlns:scilab="http://www.scilab.org"
+          xmlns:db="http://docbook.org/ns/docbook">
+    
+    <refnamediv>
+        <refname>postURL</refname>
+        
+        <refpurpose>Post to a URL and downloading the response (HTTP, HTTPS...) </refpurpose>
+    </refnamediv>
+    
+    <refsynopsisdiv>
+        <title>Calling Sequence</title>
+        
+        <synopsis>filename = postURL(URL,post_argument_names,post_argument_values);
+            [filename, [content]] = postURL(URL,post_argument_list,post_argument_values [, targetDir [, username [, password]]]]);
+            [filename, [content]] = postURL(URL,post_argument_list,post_argument_values [, targetFile [, username [, password]]]]);
+        </synopsis>
+    </refsynopsisdiv>
+    
+    <refsection>
+        <title>Arguments</title>
+        <variablelist>
+            <varlistentry>
+                <term>URL</term>
+                <listitem>
+                    <para>String: An URL. Supported and tested: HTTP, HTTPS, FTP (IPv4 and IPv6)</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>post_argument_list</term>
+                <listitem>
+                    <para>A list of strings containing the names of the post variables</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>post_argument_values</term>
+                <listitem>
+                    <para>A list of strings containing the values for the variables mentioned in the post_argument_list in the same order.</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>targetDir</term>
+                <listitem>
+                    <para>String optional parameter: In which directory the file will be stored (current directory if empty)</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>targetFile</term>
+                <listitem>
+                    <para>String optional parameter: Where (and under which name) the file will be stored (current directory if empty)</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>username</term>
+                <listitem>
+                    <para>String optional parameter: Username for the websites which require authentication</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>password</term>
+                <listitem>
+                    <para>String optional parameter: Password for the websites which require authentication</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>filename</term>
+                <listitem>
+                    <para>String: The path to the downloaded file</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>content</term>
+                <listitem>
+                    <para>String: The content to the downloaded file</para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
+    
+    <refsection>
+        <title>Description</title>
+        
+        <para>Posting to a URL and downloading the reponse</para>
+        
+        <para>
+            postURL posts to a URL and downloads the reponse. By default, postURL will use the same name as provided in the URL. However, under some circumstances (for example, <emphasis role="italic">http://www.scilab.org/</emphasis>), the filename is not provided by the URL itself, postURL will rename the file to <emphasis role="italic">index.html</emphasis>
+        </para>
+        
+        <para>content contains the content of a page pointed by an URL.</para>
+        
+        <para>
+            This function is based on the <ulink url="http://curl.haxx.se/libcurl/">curl library</ulink>.
+        </para>
+        <para>post_argument_list and the post_argument_values contains the post variables and their values to be posted on the page.</para>
+        
+        <para> Username and Password can be specified for the websites which required authentication. The authentication method used is the CURLAUTH_ANY provided by CURLOPT_HTTPAUTH parameter of the libcurl library.</para>
+        
+        <para>IPv6 (and obviously IPv4) are supported out-of-the box by postURL.</para>
+        
+        <para>Proxy configuration is enabled in this version, it can be modified in the Preferences under the rubric "Internet".</para>
+        
+        <para>
+            The <emphasis role="italic">CURLOPT_FOLLOWLOCATION</emphasis> curl option is activated to make sure the download follow the URL.
+        </para>
+        
+        <para>
+            <literal>postURL</literal> is based on libcurl. The version of this library in the official Scilab binary has been compiled with HTTP, HTTPS, FTP and FTPS support. If Scilab has been installed through a packaging system in a distribution, it is likely that <ulink url="http://curl.haxx.se/docs/features.html">other protocols</ulink> than HTTP, HTTPS and FTPS will work. However, they have not been tested enough.
+        </para>
+        
+    </refsection>
+    
+    <refsection>
+        <title>Examples</title>
+        
+        <programlisting role="example">
+            
+            filename = postURL("http://example.com",["var1","var2"],["test1","test2"]);
+        </programlisting>
+    </refsection>
+    <refsection role="see also">
+        <title>See Also</title>
+        <simplelist type="inline">
+            <member>
+                <link linkend="splitURL">splitURL</link>
+            </member>
+        </simplelist>
+    </refsection>
+    
+    <refsection>
+        <title>History</title>
+        <revhistory>
+            <revision>
+                <revnumber>5.6.0</revnumber>
+                <revremark>
+                    Function <literal>postURL()</literal> introduced
+                </revremark>
+            </revision>
+        </revhistory>
+    </refsection>
+    
+</refentry>
index 2139ee7..9c58e5f 100644 (file)
@@ -67,6 +67,7 @@ int sci_archive_list(char *fname, unsigned long fname_len);
 int sci_archive_extract(char *fname, unsigned long fname_len);
 int sci_archive_compress(char *fname, unsigned long fname_len);
 int sci_getURLheader(char *fname, unsigned long fname_len);
+int sci_postURL(char *fname, unsigned long fname_len);
 /*--------------------------------------------------------------------------*/
 #endif /* __GW_FILEIO_H__ */
 /*--------------------------------------------------------------------------*/
index 4838cb2..a6e5204 100644 (file)
@@ -71,7 +71,8 @@ static gw_generic_table Tab[] =
     {sci_archive_list, "archiveList"},
     {sci_archive_extract, "archiveExtract"},
     {sci_archive_compress, "archiveCompress"},
-    {sci_getURLheader, "getURLheader"}
+    {sci_getURLheader, "getURLheader"},
+    {sci_postURL, "postURL"}
 };
 /*--------------------------------------------------------------------------*/
 int gw_fileio(void)
diff --git a/scilab/modules/fileio/sci_gateway/c/sci_postURL.c b/scilab/modules/fileio/sci_gateway/c/sci_postURL.c
new file mode 100644 (file)
index 0000000..018197a
--- /dev/null
@@ -0,0 +1,269 @@
+/*
+* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+* Copyright (C) 2015 - Kartik Gupta
+*
+* 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
+* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+*
+*/
+/*--------------------------------------------------------------------------*/
+#include <stdlib.h>
+#include <string.h>
+#include "gw_fileio.h"
+#include "api_scilab.h"
+#include "MALLOC.h"
+#include "Scierror.h"
+#include "localization.h"
+#include "PATH_MAX.h"
+#include "freeArrayOfString.h"
+#include "expandPathVariable.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
+#include <unistd.h>
+#include "isdir.h"
+#include "createdirectory.h"
+#include "scicurdir.h"
+#include "archive_compress.h"
+#include "dlManager.h"
+/*--------------------------------------------------------------------------*/
+int sci_postURL(char *fname, unsigned long fname_len)
+{
+    SciErr sciErr;
+    int *piAddressVarOne = NULL;
+    char *pStVarOne = NULL;
+    char *pStVarOneExpanded = NULL;
+
+    int ret = 0;
+    int *piAddressVarTwo = NULL;
+    char **pStVarTwo = NULL;
+    int m1 = 0;
+    int n1 = 0;
+    int i = 0;
+
+    int *piAddressVarThree = NULL;
+    char **pStVarThree = NULL;
+    int m2 = 0;
+    int n2 = 0;
+
+    char *username = NULL;
+    char *password = NULL;
+    char *dest = NULL;
+    CheckRhs(3, 5);
+    CheckLhs(1, 2);
+    int result = 0;
+    int *error = 0;
+    int iRhs = nbInputArgument(pvApiCtx);
+    char *filePath = NULL;
+    char *content;
+    char *url;
+
+    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
+    if (sciErr.iErr)
+    {
+        printError(&sciErr, 0);
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
+        return 0;
+    }
+
+    if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0)
+    {
+        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
+        return 0;
+    }
+
+
+    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
+    if (sciErr.iErr)
+    {
+        printError(&sciErr, 0);
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
+        return 0;
+    }
+
+    if (isDoubleType(pvApiCtx, piAddressVarTwo))
+    {
+        if (isEmptyMatrix(pvApiCtx, piAddressVarTwo))
+        {
+            if (createEmptyMatrix(pvApiCtx, Rhs + 2))
+            {
+                Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                return 0;
+            }
+        }
+        else
+        {
+            Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
+            return 0;
+        }
+
+        LhsVar(1) = Rhs + 1;
+        PutLhsVar();
+        return 0;
+    }
+
+    if (isStringType(pvApiCtx, piAddressVarTwo) == 0)
+    {
+        Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 2);
+        return 0;
+    }
+
+    if (getAllocatedMatrixOfString(pvApiCtx, piAddressVarTwo, &m1, &n1, &pStVarTwo))
+    {
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
+        return 0;
+    }
+
+      sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
+    if (sciErr.iErr)
+    {
+        printError(&sciErr, 0);
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
+        return 0;
+    }
+
+    if (isDoubleType(pvApiCtx, piAddressVarThree))
+    {
+        if (isEmptyMatrix(pvApiCtx, piAddressVarThree))
+        {
+            if (createEmptyMatrix(pvApiCtx, Rhs + 3))
+            {
+                Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                return 0;
+            }
+        }
+        else
+        {
+            Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 3);
+            return 0;
+        }
+
+        LhsVar(1) = Rhs + 1;
+        PutLhsVar();
+        return 0;
+    }
+
+    if (isStringType(pvApiCtx, piAddressVarThree) == 0)
+    {
+        Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 3);
+        return 0;
+    }
+
+    if (getAllocatedMatrixOfString(pvApiCtx, piAddressVarThree, &m2, &n2, &pStVarThree))
+    {
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
+        return 0;
+    }
+
+    if(m1*n1 != m2*n2)
+    {
+           Scierror(999, _("%s: Sorry the number of post values are not equal to number of post arguments\n"), fname);
+    }
+
+    if (iRhs > 3)
+    {
+           int *piAddressVarFour = NULL;
+           sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour);
+           if (sciErr.iErr)
+           {
+                   Scierror(999, _("%s:Sorry memory allocation error \n"), fname);
+                   return 0;
+           }
+           ret = getAllocatedSingleString(pvApiCtx, piAddressVarFour, &dest);
+           if (ret)
+           {
+                   Scierror(999, _("%s:Sorry memory allocation error \n"), fname);
+                   return 0;
+           }
+
+    }
+   
+    if (iRhs > 4)
+    {
+           int *piAddressVarFive = NULL;
+           sciErr = getVarAddressFromPosition(pvApiCtx, 5, &piAddressVarFive);
+           if (sciErr.iErr)
+           {
+                   Scierror(999, _("%s:Sorry memory allocation error \n"), fname);
+                   return 0;
+           }
+           ret = getAllocatedSingleString(pvApiCtx, piAddressVarFive, &username);
+           if(ret)
+           {
+                   Scierror(999, _("%s:Sorry memory allocation error \n"), fname);
+                   return 0;
+           }
+    }
+
+    if (iRhs > 5)
+    {
+           int *piAddressVarSix = NULL;
+           sciErr = getVarAddressFromPosition(pvApiCtx, 6, &piAddressVarSix);
+           if (sciErr.iErr)
+           {
+                   Scierror(999, _("%s:Sorry memory allocation error \n"), fname);
+                   return 0;
+           }
+           ret = getAllocatedSingleString(pvApiCtx, piAddressVarSix, &password);
+           if(ret)
+           {
+                   Scierror(999, _("%s:Sorry memory allocation error \n"), fname);
+                   return 0;
+           }
+    }
+
+
+    if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &url))
+    {
+        if (pStVarOne)
+        {
+            freeAllocatedSingleWideString(pStVarOne);
+        }
+
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
+    }
+
+    pStVarTwo[m1*n1] = NULL;
+    pStVarThree[m1*n1] = NULL;
+    result = 1;
+    filePath = postFile(url, pStVarTwo,pStVarThree,dest, username, password, &content);
+    if (filePath)
+    {
+        //Return the second argument which si the file name
+        ret = createSingleString(pvApiCtx, iRhs + 1, filePath);
+        FREE(filePath);
+        filePath = NULL;
+        if (ret)
+        {
+            Scierror(999, _("%s: Could not create the output argument.\n"));
+            return 0;
+        }
+    }
+
+    if (content && nbOutputArgument(pvApiCtx) == 2)
+    {
+        //create new variable with the content
+        int res = createSingleString(pvApiCtx, iRhs + 2, content);
+        if (res)
+        {
+            Scierror(999, _("%s: Could not create the output argument.\n"));
+            return 0;
+        }
+    }
+
+    AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
+    AssignOutputVariable(pvApiCtx, 2) = iRhs + 2;
+    ReturnArguments(pvApiCtx);
+
+    freeAllocatedSingleString(pStVarOne);
+    freeAllocatedSingleString(pStVarTwo);
+    return 0;
+
+}
+/*--------------------------------------------------------------------------*/
+
+
index 2fbdcfe..6e810fd 100644 (file)
@@ -85,4 +85,5 @@
     <PRIMITIVE gatewayId="34" primitiveId="50" primitiveName="archiveExtract" />
     <PRIMITIVE gatewayId="34" primitiveId="51" primitiveName="archiveCompress" />
     <PRIMITIVE gatewayId="34" primitiveId="52" primitiveName="getURLheader" />
+    <PRIMITIVE gatewayId="34" primitiveId="53" primitiveName="postURL" />
 </GATEWAY>
index d539e21..e9f66dd 100644 (file)
@@ -450,6 +450,267 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
     return filename;
 }
 /* ==================================================================== */
+char *postFile(char *url, char **arguments, char **values, char *dest, char *username, char *password, char **content)
+{
+    CURL *curl;
+    CURLcode res;
+    char *filename = NULL;
+    FILE *file;
+    inputString buffer;
+    inputString header_buffer;
+    char *destdir = NULL;
+    char *destfile = NULL;
+
+    curl = curl_easy_init();
+    if (curl == NULL)
+    {
+        Scierror(999, "Failed opening the curl handle.\n");
+        return NULL;
+    }
+
+    init_string(&buffer);
+    res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
+    if (res != CURLE_OK)
+    {
+        Scierror(999, "Failed to set error buffer [%d]\n", res);
+        return NULL;
+    }
+
+    // Get destination directory and filename
+    if (dest != NULL)
+    {
+        // Destination is specified in argument
+        char* pathdrive = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+        char* pathdir = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+        char* pathfile = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+        char* pathext = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
+
+        splitpath(dest, TRUE, pathdrive, pathdir, pathfile, pathext);
+
+        if (!isdir(dest))
+        {
+            // Destination is a file
+            destdir = (char *)MALLOC((strlen(pathdrive) + strlen(pathdir) + 1) * sizeof(char));
+            strcpy(destdir, pathdrive);
+            strcat(destdir, pathdir);
+
+            // Get filename
+            destfile = (char *)MALLOC((strlen(pathfile) + strlen(pathext) + 1) * sizeof(char));
+            strcpy(destfile, pathfile);
+            strcat(destfile, pathext);
+        }
+        else
+        {
+            // Destination is a directory
+            destdir = (char *)MALLOC((strlen(pathdrive) + strlen(pathdir) + strlen(pathfile) + strlen(pathext) + strlen(DIR_SEPARATOR) + 1) * sizeof(char));
+            strcpy(destdir, pathdrive);
+            strcat(destdir, pathdir);
+            strcat(destdir, pathfile);
+            strcat(destdir, pathext);
+            strcat(destdir, DIR_SEPARATOR);
+
+            // Retrieve filename from URL
+            destfile = getFileNameFromURL(url);
+        }
+
+        FREE(pathdrive);
+        FREE(pathdir);
+        FREE(pathfile);
+        FREE(pathext);
+    }
+    else
+    {
+        // Destination is not specified in argument
+        // Destination directory is current dir
+        int err = 0;
+        char *currentdir;
+        currentdir = scigetcwd(&err);
+        if (!err)
+        {
+            destdir = (char *)MALLOC((strlen(currentdir) + strlen(DIR_SEPARATOR) + 1) * sizeof(char));
+            strcpy(destdir, currentdir);
+            strcat(destdir, DIR_SEPARATOR);
+            FREE(currentdir);
+        }
+        else
+        {
+            Scierror(999, _("Failed getting current dir, error code: %d\n"), err);
+            return NULL;
+        }
+
+        // Destination filename retrieved from URL
+        destfile = getFileNameFromURL(url);
+    }
+
+    if (destfile == NULL)
+    {
+        return NULL;
+    }
+
+    // Build file path
+    filename = (char *)MALLOC((strlen(destdir) + strlen(destfile) + 1) * sizeof(char));
+    strcpy(filename, destdir);
+    strcat(filename, destfile);
+    FREE(destdir);
+    FREE(destfile);
+    res = curl_easy_setopt(curl, CURLOPT_URL, url);
+    if (res != CURLE_OK)
+    {
+        Scierror(999, _("Failed to set URL [%s]\n"), errorBuffer);
+        FREE(filename);
+        return NULL;
+    }
+
+    //Set authentication variables
+    if (username != NULL)
+    {
+        char * userpass;
+        int uplen = (int)strlen(username);
+        if (password != NULL)
+        {
+            uplen = uplen + (int)strlen(password);
+        }
+
+        userpass = (char *)MALLOC((uplen + 2) * sizeof(char));
+
+        strcpy(userpass, username);
+        strcat(userpass, ":");
+        if (password != NULL)
+        {
+            strcat(userpass, password);
+        }
+
+        res = curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
+        if (res != CURLE_OK)
+        {
+            FREE(filename);
+            FREE(userpass);
+            Scierror(999, "Failed to set httpauth type to ANY [%s]\n", errorBuffer);
+            return NULL;
+        }
+
+        res = curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
+        if (res != CURLE_OK)
+        {
+            FREE(filename);
+            Scierror(999, _("Failed to set user:pwd [%s]\n"), errorBuffer);
+            return NULL;
+        }
+    } /* end authentication section */
+
+    {
+        //Set proxy variables
+        char *proxyHost = NULL;
+        char *proxyUserPwd = NULL;
+        long proxyPort = 1080;
+        int proxySet = 0;
+
+        proxySet = getProxyValues(&proxyHost, &proxyPort, &proxyUserPwd);
+
+        if (proxySet == 1)
+        {
+            res = curl_easy_setopt(curl, CURLOPT_PROXY, proxyHost);
+            if (res != CURLE_OK)
+            {
+                FREE(proxyHost);
+                FREE(proxyUserPwd);
+                FREE(filename);
+                Scierror(999, _("Failed to set proxy host [%s]\n"), errorBuffer);
+                return NULL;
+            }
+
+            res = curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxyPort);
+            if (res != CURLE_OK)
+            {
+                FREE(proxyHost);
+                FREE(proxyUserPwd);
+                FREE(filename);
+                Scierror(999, _("Failed to set proxy port [%s]\n"), errorBuffer);
+                return NULL;
+            }
+            if (proxyUserPwd != NULL)
+            {
+                res = curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxyUserPwd);
+                if (res != CURLE_OK)
+                {
+                    FREE(proxyHost);
+                    FREE(proxyUserPwd);
+                    FREE(filename);
+                    Scierror(999, _("Failed to set proxy user:password [%s]\n"), errorBuffer);
+                    return NULL;
+                }
+            }
+
+            FREE(proxyHost);
+            FREE(proxyUserPwd);
+        }
+    } /* end of the set of the proxy */
+    char post_string[1024];
+    int i = 0;
+    while(arguments[i]!=NULL)
+    {
+           strcat(post_string,arguments[i]);
+           strcat(post_string,"=");
+           strcat(post_string,values[i]);
+           strcat(post_string,"&");
+           i++;
+    }
+    post_string[strlen(post_string)-1]=0;
+    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_string);
+    res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
+    if (res != CURLE_OK)
+    {
+        FREE(filename);
+        Scierror(999, _("Failed to set write function [%s]\n"), errorBuffer);
+        return NULL;
+    }
+
+    //Get data to be written to the variable
+    res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
+    if (res != CURLE_OK)
+    {
+        FREE(filename);
+        free_string(&buffer);
+        Scierror(999, _("Failed to set write data [%s]\n"), errorBuffer);
+        return NULL;
+    }
+
+    // Follow redirects
+    res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+    if (res != CURLE_OK)
+    {
+        FREE(filename);
+        free_string(&buffer);
+        Scierror(999, _("Failed to set 'Follow Location' [%s]\n"), errorBuffer);
+        return NULL;
+    }
+    //    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+    res = curl_easy_perform(curl);
+    if (res != 0)
+    {
+        FREE(filename);
+        free_string(&buffer);
+        Scierror(999, _("Transfer did not complete successfully: %s\n"), errorBuffer);
+        return NULL;
+    }
+
+    wcfopen(file, (char*)filename, "wb");
+    if (file == NULL)
+    {
+        Scierror(999, _("Failed opening '%s' for writing.\n"), filename);
+        FREE(filename);
+        return NULL;
+    }
+    /* Write the file */
+    fwrite(buffer.ptr, sizeof(char), buffer.len, file);
+    *content = buffer.ptr;
+
+    /* always cleanup */
+    curl_easy_cleanup(curl);
+    fclose(file);
+    return filename;
+
+}
 static char *Curl_basename(char *path)
 {
     char *s1 = NULL;
index 8efcf93..37e1851 100644 (file)
@@ -25,5 +25,5 @@
  * @return the file pointer
 */
 char *downloadFile(char *url, char *dest, char *username, char *password, char **content, int header);
-
+char *postFile(char *url, char **arguments, char **values, char *dest, char *username, char *password, char **content);
 #endif /* __DLMANAGER_H__ */
diff --git a/scilab/modules/fileio/tests/unit_tests/postURL.dia.ref b/scilab/modules/fileio/tests/unit_tests/postURL.dia.ref
new file mode 100644 (file)
index 0000000..d894b0b
--- /dev/null
@@ -0,0 +1,26 @@
+// ============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2015 - Kartik Gupta
+//
+//  This file is distributed under the same license as the Scilab package.
+// ============================================================================
+// <-- CLI SHELL MODE -->
+// ============================================================================
+// Unitary tests for postURL function
+// ============================================================================
+filename = postURL("http://researchweb.iiit.ac.in/~kartik.gupta/index.php",["var1","var2"],["test1","test2"],"test.txt")
+ filename  =
+ test.txt   
+assert_checktrue(isfile("test.txt"));
+fd = mopen("test.txt");
+assert_checktrue(fd>0);
+line = mgetl(fd);
+words = strsplit(line," ");
+assert_checkequal(length(length(words)),2);
+assert_checkequal(words(1),"test1");
+assert_checkequal(words(2),"test2");
+deletefile("test.txt");
+assert_checkerror("postURL(''http://researchweb.iiit.ac.in/~kartik.gupta/index.php'',[''var1'',''var2''],[''test1'']);",[],999);
+assert_checkerror("postURL(''http://researchweb.iiit.ac.in/~kartik.gupta/index.php'',[''var1'',''var2''],''test1'');",[],999);
+assert_checkerror("postURL(''http://researchweb.iiit.ac.in/~kartik.gupta/index.php'',[''var2''],[''test1'',''test2'']);",[],999);
diff --git a/scilab/modules/fileio/tests/unit_tests/postURL.tst b/scilab/modules/fileio/tests/unit_tests/postURL.tst
new file mode 100644 (file)
index 0000000..281a9e6
--- /dev/null
@@ -0,0 +1,29 @@
+// ============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2015 - Kartik Gupta
+//
+//  This file is distributed under the same license as the Scilab package.
+// ============================================================================
+
+// <-- CLI SHELL MODE -->
+
+// ============================================================================
+// Unitary tests for postURL function
+// ============================================================================
+
+filename = postURL("http://researchweb.iiit.ac.in/~kartik.gupta/index.php",["var1","var2"],["test1","test2"],"test.txt")
+assert_checktrue(isfile("test.txt"));
+fd = mopen("test.txt");
+assert_checktrue(fd>0);
+line = mgetl(fd);
+words = strsplit(line," ");
+assert_checkequal(length(length(words)),2);
+assert_checkequal(words(1),"test1");
+assert_checkequal(words(2),"test2");
+deletefile("test.txt");
+
+assert_checkerror("postURL(''http://researchweb.iiit.ac.in/~kartik.gupta/index.php'',[''var1'',''var2''],[''test1'']);",[],999);
+
+assert_checkerror("postURL(''http://researchweb.iiit.ac.in/~kartik.gupta/index.php'',[''var1'',''var2''],''test1'');",[],999);
+
+assert_checkerror("postURL(''http://researchweb.iiit.ac.in/~kartik.gupta/index.php'',[''var2''],[''test1'',''test2'']);",[],999);