tempname function added (it will be used to fix bug 9614) 87/4687/3
Allan CORNET [Tue, 9 Aug 2011 13:04:45 +0000 (15:04 +0200)]
Change-Id: I58f003ef656ebf49a2f80891849b56bc3ce30903

16 files changed:
SEP/SEP_068_tempname.odt [new file with mode: 0644]
scilab/CHANGES_5.4.X
scilab/modules/fileio/LibScilab_Import.def
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/tempname.xml [new file with mode: 0644]
scilab/modules/fileio/includes/createtempfilename.h [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_tempname.c [new file with mode: 0644]
scilab/modules/fileio/sci_gateway/fileio_gateway.xml
scilab/modules/fileio/src/c/createtempfilename.c [new file with mode: 0644]
scilab/modules/fileio/tests/unit_tests/tempname.dia.ref [new file with mode: 0644]
scilab/modules/fileio/tests/unit_tests/tempname.tst [new file with mode: 0644]

diff --git a/SEP/SEP_068_tempname.odt b/SEP/SEP_068_tempname.odt
new file mode 100644 (file)
index 0000000..715a23d
Binary files /dev/null and b/SEP/SEP_068_tempname.odt differ
index 7c10dd4..097b2d7 100644 (file)
@@ -81,6 +81,8 @@ File input/output:
 
 * mgetl, exec, fscanfMat functions manage files encoded as UTF-8 with BOM.
 
+* tempname function added (SEP 68).
+
 
 New functions
 =============
index 653fe24..a596ba5 100644 (file)
@@ -28,3 +28,4 @@ getScilabMode
 overload_
 xscion_
 getwsmat_
+getTMPDIRW
\ No newline at end of file
index 99b3b3c..8f47f79 100644 (file)
@@ -54,7 +54,8 @@ src/c/copyfile_others.c \
 src/c/getFullFilename.c \
 src/c/mputl.c \
 src/c/fscanfMat.c \
-src/c/fprintfMat.c
+src/c/fprintfMat.c \
+src/c/createtempfilename.c
 
 
 GATEWAY_SOURCES = \
@@ -102,7 +103,8 @@ sci_gateway/c/sci_movefile.c \
 sci_gateway/c/sci_basename.c \
 sci_gateway/c/sci_pathconvert.c \
 sci_gateway/c/sci_isfile.c \
-sci_gateway/c/sci_mputl.c
+sci_gateway/c/sci_mputl.c \
+sci_gateway/c/sci_tempname.c
 
 
 libscifileio_la_CPPFLAGS=      -I$(srcdir)/includes/ \
index 9a66c59..3f3bfa3 100644 (file)
@@ -153,7 +153,8 @@ am__objects_1 = libscifileio_algo_la-FileExist.lo \
        libscifileio_algo_la-getFullFilename.lo \
        libscifileio_algo_la-mputl.lo \
        libscifileio_algo_la-fscanfMat.lo \
-       libscifileio_algo_la-fprintfMat.lo
+       libscifileio_algo_la-fprintfMat.lo \
+       libscifileio_algo_la-createtempfilename.lo
 am_libscifileio_algo_la_OBJECTS = $(am__objects_1)
 libscifileio_algo_la_OBJECTS = $(am_libscifileio_algo_la_OBJECTS)
 libscifileio_algo_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
@@ -197,7 +198,8 @@ am__objects_2 = libscifileio_la-gw_fileio.lo \
        libscifileio_la-sci_movefile.lo \
        libscifileio_la-sci_basename.lo \
        libscifileio_la-sci_pathconvert.lo \
-       libscifileio_la-sci_isfile.lo libscifileio_la-sci_mputl.lo
+       libscifileio_la-sci_isfile.lo libscifileio_la-sci_mputl.lo \
+       libscifileio_la-sci_tempname.lo
 am_libscifileio_la_OBJECTS = $(am__objects_2)
 libscifileio_la_OBJECTS = $(am_libscifileio_la_OBJECTS)
 libscifileio_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
@@ -523,7 +525,8 @@ src/c/copyfile_others.c \
 src/c/getFullFilename.c \
 src/c/mputl.c \
 src/c/fscanfMat.c \
-src/c/fprintfMat.c
+src/c/fprintfMat.c \
+src/c/createtempfilename.c
 
 GATEWAY_SOURCES = \
 sci_gateway/c/gw_fileio.c \
@@ -570,7 +573,8 @@ sci_gateway/c/sci_movefile.c \
 sci_gateway/c/sci_basename.c \
 sci_gateway/c/sci_pathconvert.c \
 sci_gateway/c/sci_isfile.c \
-sci_gateway/c/sci_mputl.c
+sci_gateway/c/sci_mputl.c \
+sci_gateway/c/sci_tempname.c
 
 libscifileio_la_CPPFLAGS = -I$(srcdir)/includes/ \
                                -I$(srcdir)/src/c/ \
@@ -783,6 +787,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_algo_la-cluni0.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_algo_la-copyfile_others.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_algo_la-createdirectory.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_algo_la-createtempfilename.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_algo_la-deleteafile.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_algo_la-delfile.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_algo_la-do_xxscanf.Plo@am__quote@
@@ -870,6 +875,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_la-sci_pathsep.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_la-sci_pwd.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_la-sci_removedir.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libscifileio_la-sci_tempname.Plo@am__quote@
 
 .c.o:
 @am__fastdepCC_TRUE@   $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
@@ -1249,6 +1255,13 @@ libscifileio_algo_la-fprintfMat.lo: src/c/fprintfMat.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libscifileio_algo_la_CFLAGS) $(CFLAGS) -c -o libscifileio_algo_la-fprintfMat.lo `test -f 'src/c/fprintfMat.c' || echo '$(srcdir)/'`src/c/fprintfMat.c
 
+libscifileio_algo_la-createtempfilename.lo: src/c/createtempfilename.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libscifileio_algo_la_CFLAGS) $(CFLAGS) -MT libscifileio_algo_la-createtempfilename.lo -MD -MP -MF $(DEPDIR)/libscifileio_algo_la-createtempfilename.Tpo -c -o libscifileio_algo_la-createtempfilename.lo `test -f 'src/c/createtempfilename.c' || echo '$(srcdir)/'`src/c/createtempfilename.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/libscifileio_algo_la-createtempfilename.Tpo $(DEPDIR)/libscifileio_algo_la-createtempfilename.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='src/c/createtempfilename.c' object='libscifileio_algo_la-createtempfilename.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libscifileio_algo_la_CFLAGS) $(CFLAGS) -c -o libscifileio_algo_la-createtempfilename.lo `test -f 'src/c/createtempfilename.c' || echo '$(srcdir)/'`src/c/createtempfilename.c
+
 libscifileio_la-gw_fileio.lo: sci_gateway/c/gw_fileio.c
 @am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(libscifileio_la_CFLAGS) $(CFLAGS) -MT libscifileio_la-gw_fileio.lo -MD -MP -MF $(DEPDIR)/libscifileio_la-gw_fileio.Tpo -c -o libscifileio_la-gw_fileio.lo `test -f 'sci_gateway/c/gw_fileio.c' || echo '$(srcdir)/'`sci_gateway/c/gw_fileio.c
 @am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/libscifileio_la-gw_fileio.Tpo $(DEPDIR)/libscifileio_la-gw_fileio.Plo
@@ -1564,6 +1577,13 @@ libscifileio_la-sci_mputl.lo: sci_gateway/c/sci_mputl.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(libscifileio_la_CFLAGS) $(CFLAGS) -c -o libscifileio_la-sci_mputl.lo `test -f 'sci_gateway/c/sci_mputl.c' || echo '$(srcdir)/'`sci_gateway/c/sci_mputl.c
 
+libscifileio_la-sci_tempname.lo: sci_gateway/c/sci_tempname.c
+@am__fastdepCC_TRUE@   $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(libscifileio_la_CFLAGS) $(CFLAGS) -MT libscifileio_la-sci_tempname.lo -MD -MP -MF $(DEPDIR)/libscifileio_la-sci_tempname.Tpo -c -o libscifileio_la-sci_tempname.lo `test -f 'sci_gateway/c/sci_tempname.c' || echo '$(srcdir)/'`sci_gateway/c/sci_tempname.c
+@am__fastdepCC_TRUE@   $(am__mv) $(DEPDIR)/libscifileio_la-sci_tempname.Tpo $(DEPDIR)/libscifileio_la-sci_tempname.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='sci_gateway/c/sci_tempname.c' object='libscifileio_la-sci_tempname.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscifileio_la_CPPFLAGS) $(CPPFLAGS) $(libscifileio_la_CFLAGS) $(CFLAGS) -c -o libscifileio_la-sci_tempname.lo `test -f 'sci_gateway/c/sci_tempname.c' || echo '$(srcdir)/'`sci_gateway/c/sci_tempname.c
+
 mostlyclean-libtool:
        -rm -f *.lo
 
index e90bf74..f2832aa 100644 (file)
@@ -252,11 +252,13 @@ lib /DEF:"$(ProjectDir)Console_Import.def" /SUBSYSTEM:WINDOWS /MACHINE:$(Platfor
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="sci_gateway\c\sci_tempname.c" />
     <ClCompile Include="src\c\addfile.c" />
     <ClCompile Include="src\c\basename.c" />
     <ClCompile Include="src\c\cluni0.c" />
     <ClCompile Include="src\c\copyfile_windows.c" />
     <ClCompile Include="src\c\createdirectory.c" />
+    <ClCompile Include="src\c\createtempfilename.c" />
     <ClCompile Include="src\c\deleteafile.c" />
     <ClCompile Include="src\c\delfile.c" />
     <ClCompile Include="src\c\DllmainFileio.c" />
@@ -352,6 +354,7 @@ lib /DEF:"$(ProjectDir)Console_Import.def" /SUBSYSTEM:WINDOWS /MACHINE:$(Platfor
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="includes\addfile.h" />
+    <ClInclude Include="includes\createtempfilename.h" />
     <ClInclude Include="includes\mgetl.h" />
     <ClInclude Include="src\c\basename.h" />
     <ClInclude Include="includes\cluni0.h" />
index 973c2d7..2c5ec86 100644 (file)
     <ClCompile Include="src\c\URIFileToFilename.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="src\c\createtempfilename.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="sci_gateway\c\sci_tempname.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="includes\addfile.h">
     <ClInclude Include="includes\mgetl.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="includes\createtempfilename.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="src\c\fileio.rc">
diff --git a/scilab/modules/fileio/help/en_US/tempname.xml b/scilab/modules/fileio/help/en_US/tempname.xml
new file mode 100644 (file)
index 0000000..2e63d49
--- /dev/null
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2011 - DIGITEO - Allan CORNET
+ * 
+ * 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-en.txt
+ *
+ -->
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" version="5.0-subset Scilab" xml:lang="en" xml:id="tempname">
+  <info>
+    <pubdate>$LastChangedDate: 2006-09-10 20:58:00 +0200 (dim., 10 sept. 2006)$</pubdate>
+  </info>
+  <refnamediv>
+    <refname>tempname</refname>
+    <refpurpose>Unique name for temporary file</refpurpose>
+  </refnamediv>
+  <refsynopsisdiv>
+    <title>Calling Sequence</title>
+    <synopsis>tmp_name = tempname([prefix])</synopsis>
+  </refsynopsisdiv>
+  <refsection>
+    <title>Arguments</title>
+    <variablelist>
+      <varlistentry>
+        <term>tmp_name</term>
+        <listitem>
+          <para>a string</para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+    <variablelist>
+      <varlistentry>
+        <term>prefix</term>
+        <listitem>
+          <para>a string: prefix of filename (On Windows, 3 characters maximum).</para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+  </refsection>
+  <refsection>
+    <title>Description</title>
+    <para>Creates a file with a unique filename in the TMPDIR directory.</para>
+    <para>tmp_nam = tempname() returns a unique string, tmp_nam, suitable for use as a temporary filename.</para>
+  </refsection>
+  <refsection>
+    <title>Examples</title>
+    <programlisting role="example"><![CDATA[
+      F1 = tempname()
+      isfile(F1)
+      F2 = tempname('TMP')
+      isfile(F2)
+ ]]></programlisting>
+  </refsection>
+  <refsection>
+    <title>Author</title>
+    <simplelist type="vert">
+      <member>A.C</member>
+    </simplelist>
+  </refsection>
+</refentry>
diff --git a/scilab/modules/fileio/includes/createtempfilename.h b/scilab/modules/fileio/includes/createtempfilename.h
new file mode 100644 (file)
index 0000000..62c1e17
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2011 - DIGITEO - Allan CORNET
+ * 
+ * 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-en.txt
+ *
+ */
+/*--------------------------------------------------------------------------*/
+#ifndef __CREATETEMPFILENAME_H__
+#define __CREATETEMPFILENAME_H__
+
+#include "charEncoding.h"
+#include "BOOL.h"
+
+/**
+* Create file with unique file name in TMPDIR
+* TMPDIR/PREFIX[XXXXXXX]
+* @param[in] prefix 
+* @param[in] bShortFormat 
+* @return a unique filename
+*/
+char *createtempfilename(const char *prefix, BOOL bShortFormat);
+
+wchar_t *createtempfilenameW(const wchar_t *wcprefix, BOOL bShortFormat);
+
+#endif /* __CREATETEMPFILENAME_H__ */
+/*--------------------------------------------------------------------------*/
index d4420d1..8730423 100644 (file)
@@ -61,6 +61,7 @@ int sci_basename(char *fname,unsigned long fname_len);
 int sci_pathconvert(char *fname,unsigned long fname_len);
 int sci_mgetl(char *fname,unsigned long fname_len);
 int sci_mputl(char *fname,unsigned long fname_len);
+int sci_tempname(char *fname,unsigned long fname_len);
 /*--------------------------------------------------------------------------*/ 
 #endif /* __GW_FILEIO_H__ */
 /*--------------------------------------------------------------------------*/
index 0e19376..c6e095b 100644 (file)
@@ -63,7 +63,8 @@ static gw_generic_table Tab[]={
     {sci_pathconvert,"pathconvert"},
     {sci_chdir,"cd"},
     {sci_mgetl,"mgetl"},
-    {sci_mputl,"mputl"}
+    {sci_mputl,"mputl"},
+    {sci_tempname,"tempname"}
 };
 /*--------------------------------------------------------------------------*/ 
 int gw_fileio(void)
diff --git a/scilab/modules/fileio/sci_gateway/c/sci_tempname.c b/scilab/modules/fileio/sci_gateway/c/sci_tempname.c
new file mode 100644 (file)
index 0000000..66da2ac
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2011 - DIGITEO - Allan CORNET
+ * 
+ * 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-en.txt
+ *
+ */
+/*--------------------------------------------------------------------------*/
+#include <string.h>
+#include "api_scilab.h"
+#include "stack-c.h"
+#include "createtempfilename.h"
+#include "gw_fileio.h"
+#include "Scierror.h"
+#include "localization.h"
+/*--------------------------------------------------------------------------*/
+#define DEFAULT_PREFIX L"SCI"
+/*--------------------------------------------------------------------------*/
+int sci_tempname(char *fname,unsigned long fname_len)
+{
+    SciErr sciErr;
+    wchar_t *wcprefix = NULL;
+    wchar_t *wcTempFilename = NULL;
+
+    Rhs = Max(Rhs, 0);
+    CheckRhs(0, 1);
+    CheckLhs(1, 1);
+
+    if (Rhs == 0)
+    {
+        wcprefix = (wchar_t *)MALLOC(sizeof(wchar_t) * (wcslen(DEFAULT_PREFIX) + 1));
+        wcscpy(wcprefix, DEFAULT_PREFIX);
+    }
+
+    if (Rhs == 1)
+    {
+        int *piAddressVarOne = NULL;
+        int m1 = 0, n1 = 0;
+
+        sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
+        if(sciErr.iErr)
+        {
+            printError(&sciErr, 0);
+            return 0;
+        }
+
+        if (!isScalar(pvApiCtx, piAddressVarOne))
+        {
+            Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 1);
+            return 0;
+        }
+
+        if (isStringType(pvApiCtx,piAddressVarOne))
+        {
+            if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &wcprefix) != 0)
+            {
+                Scierror(999,_("%s: Memory allocation error.\n"), fname);
+                return 0;
+            }
+
+#if _MSC_VER
+            if (wcslen(wcprefix) > 3)
+            {
+                FREE(wcprefix);
+                wcprefix = NULL;
+
+                Scierror(999,_("%s: Wrong size for input argument #%d: A string (3 characters max.) expected.\n"), fname, 1);
+                return 0;
+            }
+#endif
+        }
+        else
+        {
+            FREE(wcprefix);
+            wcprefix = NULL;
+
+            Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
+            return 0;
+        }
+    }
+
+    wcTempFilename = createtempfilenameW(wcprefix, TRUE);
+
+    FREE(wcprefix);
+    wcprefix = NULL;
+
+    if (wcTempFilename)
+    {
+        if (createSingleWideString(pvApiCtx, Rhs + 1, wcTempFilename) == 0)
+        {
+            FREE(wcTempFilename);
+            wcTempFilename = NULL;
+
+            LhsVar(1) = Rhs + 1;
+            PutLhsVar();
+            return 0;
+        }
+    }
+
+    FREE(wcTempFilename);
+    wcTempFilename = NULL;
+    Scierror(999,_("%s: Memory allocation error.\n"), fname);
+    return 0;
+}
+/*--------------------------------------------------------------------------*/
index 46069e7..39d5656 100644 (file)
@@ -78,4 +78,5 @@
 <PRIMITIVE gatewayId="34" primitiveId="43" primitiveName="cd" />
 <PRIMITIVE gatewayId="34" primitiveId="44" primitiveName="mgetl" />
 <PRIMITIVE gatewayId="34" primitiveId="45" primitiveName="mputl" />
+<PRIMITIVE gatewayId="34" primitiveId="46" primitiveName="tempname" />
 </GATEWAY>
diff --git a/scilab/modules/fileio/src/c/createtempfilename.c b/scilab/modules/fileio/src/c/createtempfilename.c
new file mode 100644 (file)
index 0000000..0e60e58
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2011 - DIGITEO - Allan CORNET
+ * 
+ * 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-en.txt
+ *
+ */
+/*--------------------------------------------------------------------------*/
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include "createtempfilename.h"
+#include "tmpdir.h"
+#include "splitpath.h"
+#include "machine.h"
+#include "PATH_MAX.h"
+#include "getshortpathname.h"
+#include "FileExist.h"
+/*--------------------------------------------------------------------------*/
+char *createtempfilename(const char *prefix, BOOL bShortFormat)
+{
+    char *tempfilename = NULL;
+#ifdef _MSC_VER
+    wchar_t *wcprefix = to_wide_string(prefix);
+    wchar_t *wcresult = createtempfilenameW(wcprefix, bShortFormat);
+
+    tempfilename = wide_string_to_UTF8(wcresult);
+
+    if (wcresult) {FREE(wcresult); wcresult = NULL;}
+    if (wcresult) {FREE(wcresult); wcresult = NULL;}
+#else
+     char *TmpDir = getTMPDIR();
+     if (TmpDir)
+     {
+        char TempFileName[PATH_MAX];
+        sprintf(TempFileName, "%s/%sXXXXXX",TmpDir, prefix);
+        int fd = mkstemp(TempFileName);
+        if (fd != -1) close(fd);
+        tempfilename = strdup(TempFileName);
+     }
+#endif
+    return tempfilename;
+}
+/*--------------------------------------------------------------------------*/
+wchar_t *createtempfilenameW(const wchar_t *wcprefix, BOOL bShortFormat)
+{
+    wchar_t *wcReturnedTempFilename = NULL;
+
+#ifdef _MSC_VER
+    wchar_t *wcTmpDir = getTMPDIRW();
+    if (wcTmpDir)
+    {
+        unsigned int uRetVal = 0;
+        wchar_t wcTempFileName[MAX_PATH];
+        uRetVal = GetTempFileNameW(wcTmpDir, wcprefix, 0, wcTempFileName);
+        if (uRetVal != 0)
+        {
+            int len = wcslen(wcTempFileName) + 1;
+            wchar_t* shortTempFilename = (wchar_t *)MALLOC(len * sizeof(wchar_t));
+            if (shortTempFilename)
+            {
+                if (bShortFormat)
+                {
+                    GetShortPathNameW(wcTempFileName, shortTempFilename, len);
+                }
+                wcReturnedTempFilename = shortTempFilename;
+            }
+        }
+    }
+#else
+    char *prefix = wide_string_to_UTF8(wcprefix);
+    char *result = createtempfilename(prefix, bShortFormat);
+
+    wcReturnedTempFilename = to_wide_string(result);
+
+    if (result) {FREE(result); result = NULL;}
+    if (prefix) {FREE(prefix); prefix = NULL;}
+    if (result) {FREE(result); result = NULL;}
+#endif
+    return wcReturnedTempFilename;
+}
+/*--------------------------------------------------------------------------*/
diff --git a/scilab/modules/fileio/tests/unit_tests/tempname.dia.ref b/scilab/modules/fileio/tests/unit_tests/tempname.dia.ref
new file mode 100644 (file)
index 0000000..185f1b8
--- /dev/null
@@ -0,0 +1,32 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2011 - DIGITEO - Allan CORNET
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- JVM NOT MANDATORY -->
+//
+// some unit tests about tempname
+//
+F = tempname();
+assert_checkequal(isfile(F), %t);
+F = tempname('PRE');
+assert_checkequal(isfile(F), %t);
+msgerror = "%s: Wrong type for input argument #%d: A string expected.\n";
+assert_checkerror ( "F = tempname(1)" , msgerror, 999, "tempname" , 1);
+msgerror = "%s: Wrong size for input argument #%d: A scalar expected.\n";
+assert_checkerror ( "F = tempname([''A'',''B''])" , msgerror, 999, "tempname" , 1);
+if getos() == "Windows" then
+  msgerror = "%s: Wrong size for input argument #%d: A string (3 characters max.) expected.\n";
+  assert_checkerror ( "F = tempname(''toolongasprefixonwindows'')" , msgerror, 999, "tempname" , 1);
+end
+F = tempname();
+[p, f, ext]=fileparts(F);
+assert_checkequal(part(f,1:3), "SCI");
+F = tempname("TMP");
+[p, f, ext] = fileparts(F);
+assert_checkequal(part(f,1:3), "TMP");
+if getos() == "Windows" then
+  assert_checkequal(ext, ".tmp");
+end
diff --git a/scilab/modules/fileio/tests/unit_tests/tempname.tst b/scilab/modules/fileio/tests/unit_tests/tempname.tst
new file mode 100644 (file)
index 0000000..b800a5e
--- /dev/null
@@ -0,0 +1,39 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2011 - DIGITEO - Allan CORNET
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- JVM NOT MANDATORY -->
+//
+// some unit tests about tempname
+//
+
+F = tempname();
+assert_checkequal(isfile(F), %t);
+
+F = tempname('PRE');
+assert_checkequal(isfile(F), %t);
+
+msgerror = "%s: Wrong type for input argument #%d: A string expected.\n";
+assert_checkerror ( "F = tempname(1)" , msgerror, 999, "tempname" , 1);
+
+msgerror = "%s: Wrong size for input argument #%d: A scalar expected.\n";
+assert_checkerror ( "F = tempname([''A'',''B''])" , msgerror, 999, "tempname" , 1);
+
+if getos() == "Windows" then
+  msgerror = "%s: Wrong size for input argument #%d: A string (3 characters max.) expected.\n";
+  assert_checkerror ( "F = tempname(''toolongasprefixonwindows'')" , msgerror, 999, "tempname" , 1);
+end
+
+F = tempname();
+[p, f, ext]=fileparts(F);
+assert_checkequal(part(f,1:3), "SCI");
+
+F = tempname("TMP");
+[p, f, ext] = fileparts(F);
+assert_checkequal(part(f,1:3), "TMP");
+if getos() == "Windows" then
+  assert_checkequal(ext, ".tmp");
+end