fileio : fix some memory leaks, bad assignement, ... 32/12832/2
Antoine ELIAS [Sun, 6 Oct 2013 12:30:29 +0000 (14:30 +0200)]
fix almost problem from coverity in fileio ( more than 120 )

Change-Id: Ic4429eb12bf6994bf514480030c40ac3a675cf90

43 files changed:
scilab/modules/fileio/sci_gateway/c/sci_basename.c
scilab/modules/fileio/sci_gateway/c/sci_chdir.c
scilab/modules/fileio/sci_gateway/c/sci_copyfile.c
scilab/modules/fileio/sci_gateway/c/sci_fileinfo.c
scilab/modules/fileio/sci_gateway/c/sci_fileparts.c
scilab/modules/fileio/sci_gateway/c/sci_findfiles.c
scilab/modules/fileio/sci_gateway/c/sci_fprintfMat.c
scilab/modules/fileio/sci_gateway/c/sci_fscanfMat.c
scilab/modules/fileio/sci_gateway/c/sci_fullpath.c
scilab/modules/fileio/sci_gateway/c/sci_getURL.c
scilab/modules/fileio/sci_gateway/c/sci_getrelativefilename.c
scilab/modules/fileio/sci_gateway/c/sci_isfile.c
scilab/modules/fileio/sci_gateway/c/sci_mgetl.c
scilab/modules/fileio/sci_gateway/c/sci_movefile.c
scilab/modules/fileio/sci_gateway/c/sci_mputl.c
scilab/modules/fileio/sci_gateway/c/sci_mscanf.c
scilab/modules/fileio/sci_gateway/c/sci_msscanf.c
scilab/modules/fileio/sci_gateway/c/sci_pathconvert.c
scilab/modules/fileio/sci_gateway/c/sci_pathsep.c
scilab/modules/fileio/sci_gateway/c/sci_splitURL.c
scilab/modules/fileio/sci_gateway/c/sci_tempname.c
scilab/modules/fileio/src/c/basename.c
scilab/modules/fileio/src/c/convert_tools.c
scilab/modules/fileio/src/c/convert_tools.h
scilab/modules/fileio/src/c/deleteafile.c
scilab/modules/fileio/src/c/dlManager.c
scilab/modules/fileio/src/c/do_xxscanf.c
scilab/modules/fileio/src/c/expandPathVariable.c
scilab/modules/fileio/src/c/fileinfo.c
scilab/modules/fileio/src/c/filesmanagement.c
scilab/modules/fileio/src/c/fscanfMat.c
scilab/modules/fileio/src/c/fullpath.c
scilab/modules/fileio/src/c/getFullFilename.c
scilab/modules/fileio/src/c/getlongpathname.c
scilab/modules/fileio/src/c/getrelativefilename.c
scilab/modules/fileio/src/c/mgeti.c
scilab/modules/fileio/src/c/mgetl.c
scilab/modules/fileio/src/c/mputi.c
scilab/modules/fileio/src/c/pathconvert.c
scilab/modules/fileio/src/c/readline.c
scilab/modules/fileio/src/c/removedir.c
scilab/modules/fileio/src/c/scanf_functions.c
scilab/modules/fileio/src/c/splitpath.c

index 0cb7c27..346d4dc 100644 (file)
@@ -25,9 +25,9 @@ int sci_basename(char *fname, unsigned long fname_len)
 
     int *piAddressVarOne = NULL;
     wchar_t **pStVarOne = NULL;
-    int *lenStVarOne = NULL;
-    int iType1                                 = 0;
-    int m1 = 0, n1 = 0;
+    int m1 = 0;
+    int n1 = 0;
+    int i = 0;
 
     wchar_t **pStResult = NULL;
 
@@ -38,10 +38,6 @@ int sci_basename(char *fname, unsigned long fname_len)
     if (Rhs > 2)
     {
         int *piAddressVarThree = NULL;
-        int *piData = NULL;
-        int iType3     = 0;
-        int m3 = 0, n3 = 0;
-
         sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
         if (sciErr.iErr)
         {
@@ -50,52 +46,24 @@ int sci_basename(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
-            return 0;
-        }
-
-        if (iType3 != sci_boolean)
+        if (isBooleanType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 3);
             return 0;
         }
 
-        sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarThree, &m3, &n3,  &piData);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
-            return 0;
-        }
-
-        sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);
-        if (sciErr.iErr)
+        if (getScalarBoolean(pvApiCtx, piAddressVarThree, &flagexpand))
         {
             printError(&sciErr, 0);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
             return 0;
         }
-
-        if ( (m3 != n3) && (n3 != 1) )
-        {
-            Scierror(999, _("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 3);
-            return 0;
-        }
-
-        flagexpand = piData[0];
     }
 
+    //do not care of 2nd param ? Oo
     if (Rhs > 1)
     {
         int *piAddressVarTwo = NULL;
-        int *piData = NULL;
-        int iType2     = 0;
-        int m2 = 0, n2 = 0;
-
         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
         if (sciErr.iErr)
         {
@@ -104,53 +72,15 @@ int sci_basename(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
-            return 0;
-        }
-
-        if (iType2 != sci_boolean)
+        if (isBooleanType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 2);
             return 0;
         }
-
-        sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
-            return 0;
-        }
-
-        if ( (m2 != n2) && (n2 != 1) )
-        {
-            Scierror(999, _("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 2);
-            return 0;
-        }
-
-        sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2,  &piData);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
-            return 0;
-        }
-
     }
 
-    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;
-    }
 
-    sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
+    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
     if (sciErr.iErr)
     {
         printError(&sciErr, 0);
@@ -158,154 +88,65 @@ int sci_basename(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    if (iType1 == sci_matrix)
+    if (isDoubleType(pvApiCtx, piAddressVarOne))
     {
-        sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
-        if (sciErr.iErr)
+        if (isEmptyMatrix(pvApiCtx, piAddressVarOne))
         {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-            return 0;
-        }
-
-        if ( (m1 == n1) && (m1 == 0) )
-        {
-            sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, NULL);
-            if (sciErr.iErr)
+            if (createEmptyMatrix(pvApiCtx, Rhs + 1))
             {
-                printError(&sciErr, 0);
                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
                 return 0;
             }
-
-            LhsVar(1) = Rhs + 1;
-            PutLhsVar();
         }
         else
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
-        }
-    }
-    else if (iType1 == sci_strings)
-    {
-        int i = 0;
-
-        sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-            return 0;
-        }
-
-        lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1));
-        if (lenStVarOne == NULL)
-        {
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
-
-        // get lenStVarOne value
-        sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL);
-        if (sciErr.iErr)
-        {
-            freeArrayOfWideString(pStVarOne, m1 * n1);
-            if (lenStVarOne)
-            {
-                FREE(lenStVarOne);
-                lenStVarOne = NULL;
-            }
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-            return 0;
-        }
-
-        pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1));
-        if (pStVarOne == NULL)
-        {
-            if (lenStVarOne)
-            {
-                FREE(lenStVarOne);
-                lenStVarOne = NULL;
-            }
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
-
-        for (i = 0; i < (m1 * n1); i++)
-        {
-            pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1));
-            if (pStVarOne[i] == NULL)
-            {
-                freeArrayOfWideString(pStVarOne, m1 * n1);
-                if (lenStVarOne)
-                {
-                    FREE(lenStVarOne);
-                    lenStVarOne = NULL;
-                }
-                Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                return 0;
-            }
-        }
-
-        // get pStVarOne
-        sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
-        if (sciErr.iErr)
-        {
-            freeArrayOfWideString(pStVarOne, m1 * n1);
-            if (lenStVarOne)
-            {
-                FREE(lenStVarOne);
-                lenStVarOne = NULL;
-            }
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-            return 0;
-        }
-
-        pStResult = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1));
-
-        if (pStResult == NULL)
-        {
-            if (lenStVarOne)
-            {
-                FREE(lenStVarOne);
-                lenStVarOne = NULL;
-            }
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
-
-        for (i = 0; i < m1 * n1; i++)
-        {
-            pStResult[i] = basenameW(pStVarOne[i], flagexpand);
-        }
-
-        sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, m1, n1, pStResult);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
 
         LhsVar(1) = Rhs + 1;
+        PutLhsVar();
+        return 0;
+    }
 
-        if (lenStVarOne)
-        {
-            FREE(lenStVarOne);
-            lenStVarOne = NULL;
-        }
-        freeArrayOfWideString(pStResult, m1 * n1);
-        freeArrayOfWideString(pStVarOne, m1 * n1);
+    if (isStringType(pvApiCtx, piAddressVarOne) == 0)
+    {
+        Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
+        return 0;
+    }
 
-        PutLhsVar();
+    if (getAllocatedMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, &pStVarOne))
+    {
+        freeAllocatedMatrixOfWideString(m1, n1, pStVarOne);
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
+        return 0;
+    }
+
+    pStResult = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1));
+    if (pStResult == NULL)
+    {
+        freeAllocatedMatrixOfWideString(m1, n1, pStVarOne);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
+    }
 
+    for (i = 0; i < m1 * n1; i++)
+    {
+        pStResult[i] = basenameW(pStVarOne[i], flagexpand);
     }
-    else
+
+    freeAllocatedMatrixOfWideString(m1, n1, pStVarOne);
+    sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, m1, n1, pStResult);
+    freeArrayOfWideString(pStResult, m1 * n1);
+    if (sciErr.iErr)
     {
-        Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
+        printError(&sciErr, 0);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
     }
+
+    LhsVar(1) = Rhs + 1;
+    PutLhsVar();
     return 0;
 }
 /*--------------------------------------------------------------------------*/
index 04417b8..662ef6f 100644 (file)
@@ -62,176 +62,120 @@ int sci_chdir(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        if (iType1 != sci_strings )
+        if (iType1 != sci_strings)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
             return 0;
         }
 
-        // get value of lenStVarOne
-        sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, NULL);
-        if (sciErr.iErr)
+        if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &pStVarOne))
         {
-            printError(&sciErr, 0);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
             return 0;
         }
+    }
 
-        if ( (m1 != n1) && (n1 != 1) )
-        {
-            Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
-            return 0;
-        }
+    expandedPath = expandPathVariableW(pStVarOne);
+    FREE(pStVarOne);
 
-        pStVarOne = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne + 1));
-        if (pStVarOne == NULL)
-        {
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
+    if (expandedPath == NULL)
+    {
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
+    }
 
-        sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
+    /* get value of PWD scilab variable (compatiblity scilab 4.x) */
+    if (wcscmp(expandedPath, L"PWD") == 0)
+    {
+        sciErr = getNamedVarType(pvApiCtx, "PWD", &iType1);
         if (sciErr.iErr)
         {
+            FREE(expandedPath);
             printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
+            Scierror(999, _("%s: Can not read named argument %s.\n"), fname, "PWD");
             return 0;
         }
-    }
-
-    expandedPath = expandPathVariableW(pStVarOne);
-    if (pStVarOne)
-    {
-        FREE(pStVarOne);
-        pStVarOne = NULL;
-    }
 
-    if (expandedPath)
-    {
-        /* get value of PWD scilab variable (compatiblity scilab 4.x) */
-        if (wcscmp(expandedPath, L"PWD") == 0)
+        if (iType1 == sci_strings)
         {
-            sciErr = getNamedVarType(pvApiCtx, "PWD", &iType1);
-            if (sciErr.iErr)
+            wchar_t *VARVALUE = NULL;
+            if (getAllocatedNamedSingleWideString(pvApiCtx, "PWD", &VARVALUE))
             {
-                printError(&sciErr, 0);
+                FREE(expandedPath);
                 Scierror(999, _("%s: Can not read named argument %s.\n"), fname, "PWD");
                 return 0;
             }
 
-            if (iType1 == sci_strings)
-            {
-                wchar_t *VARVALUE = NULL;
-                int VARVALUElen = 0;
-                int m = 0, n = 0;
-                sciErr = readNamedMatrixOfWideString(pvApiCtx, "PWD", &m, &n, &VARVALUElen, &VARVALUE);
-                if (sciErr.iErr)
-                {
-                    printError(&sciErr, 0);
-                    Scierror(999, _("%s: Can not read named argument %s.\n"), fname, "PWD");
-                    return 0;
-                }
-
-                if ( (m == 1) && (n == 1) )
-                {
-                    VARVALUE = (wchar_t*)MALLOC(sizeof(wchar_t) * (VARVALUElen + 1));
-                    if (VARVALUE)
-                    {
-                        readNamedMatrixOfWideString(pvApiCtx, "PWD", &m, &n, &VARVALUElen, &VARVALUE);
-                        FREE(expandedPath);
-                        expandedPath = VARVALUE;
-                    }
-                }
-            }
+            FREE(expandedPath);
+            expandedPath = VARVALUE;
         }
+    }
 
-        if (strcmp(fname, "chdir") == 0) /* chdir output boolean */
+    if (strcmp(fname, "chdir") == 0) /* chdir output boolean */
+    {
+        int iOutput = FALSE;
+        if (scichdirW(expandedPath))
         {
-            BOOL *bOutput = (BOOL*)MALLOC(sizeof(BOOL));
+            iOutput = FALSE;
+        }
+        else
+        {
+            iOutput = TRUE;
+        }
 
-            int ierr = scichdirW(expandedPath);
+        if (createScalarBoolean(pvApiCtx, Rhs + 1, iOutput))
+        {
+            FREE(expandedPath);
+            Scierror(999, _("%s: Memory allocation error.\n"), fname);
+            return 0;
+        }
 
-            if (ierr)
+        LhsVar(1) = Rhs + 1;
+        PutLhsVar();
+    }
+    else /* cd output string current path */
+    {
+        if ( isdirW(expandedPath) || (wcscmp(expandedPath, L"/") == 0) ||
+                (wcscmp(expandedPath, L"\\") == 0) )
+        {
+            int ierr = scichdirW(expandedPath);
+            wchar_t *currentDir = scigetcwdW(&ierr);
+            if ( (ierr == 0) && currentDir)
             {
-                bOutput[0] = FALSE;
+                sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, 1, 1, &currentDir);
             }
             else
             {
-                bOutput[0] = TRUE;
+                sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 0, 0, NULL);
             }
 
-            sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, 1, 1, bOutput);
+            FREE(expandedPath);
+            FREE(currentDir);
             if (sciErr.iErr)
             {
                 printError(&sciErr, 0);
-                FREE(bOutput);
                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
                 return 0;
             }
 
             LhsVar(1) = Rhs + 1;
-
-            if (bOutput)
-            {
-                FREE(bOutput);
-                bOutput = NULL;
-            }
-
             PutLhsVar();
         }
-        else /* cd output string current path */
+        else
         {
-            if ( isdirW(expandedPath) || (wcscmp(expandedPath, L"/") == 0) ||
-                    (wcscmp(expandedPath, L"\\") == 0) )
+            char *path = wide_string_to_UTF8(expandedPath);
+            FREE(expandedPath);
+            if (path)
             {
-                int ierr = scichdirW(expandedPath);
-                wchar_t *currentDir = scigetcwdW(&ierr);
-                if ( (ierr == 0) && currentDir)
-                {
-                    sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, 1, 1, &currentDir);
-                }
-                else
-                {
-                    sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 0, 0, NULL);
-                }
-
-                if (sciErr.iErr)
-                {
-                    printError(&sciErr, 0);
-                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                    return 0;
-                }
-
-                LhsVar(1) = Rhs + 1;
-                if (currentDir)
-                {
-                    FREE(currentDir);
-                    currentDir = NULL;
-                }
-                PutLhsVar();
+                Scierror(998, _("%s: Cannot go to directory %s\n"), fname, path);
+                FREE(path);
+                path = NULL;
             }
             else
             {
-                char *path = wide_string_to_UTF8(expandedPath);
-                if (path)
-                {
-                    Scierror(998, _("%s: Cannot go to directory %s\n"), fname, path);
-                    FREE(path);
-                    path = NULL;
-                }
-                else
-                {
-                    Scierror(998, _("%s: Cannot go to directory.\n"), fname);
-                }
+                Scierror(998, _("%s: Cannot go to directory.\n"), fname);
             }
         }
-
-        FREE(expandedPath);
-        expandedPath = NULL;
-    }
-    else
-    {
-        Scierror(999, _("%s: Memory allocation error.\n"), fname);
     }
 
     return 0;
index e7707b6..5830d0a 100644 (file)
@@ -57,18 +57,12 @@ int sci_copyfile(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    if (!isStringType(pvApiCtx, piAddressVarOne))
+    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;
     }
 
-    if (!isScalar(pvApiCtx, piAddressVarOne))
-    {
-        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
-        return 0;
-    }
-
     sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
     if (sciErr.iErr)
     {
@@ -77,18 +71,12 @@ int sci_copyfile(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    if (!isStringType(pvApiCtx, piAddressVarTwo))
+    if (isStringType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0)
     {
         Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
         return 0;
     }
 
-    if (!isScalar(pvApiCtx, piAddressVarTwo))
-    {
-        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
-        return 0;
-    }
-
     if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &pStVarOne))
     {
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
@@ -97,47 +85,37 @@ int sci_copyfile(char *fname, unsigned long fname_len)
 
     if (getAllocatedSingleWideString(pvApiCtx, piAddressVarTwo, &pStVarTwo))
     {
-        if (pStVarOne)
-        {
-            freeAllocatedSingleWideString(pStVarOne);
-            pStVarOne = NULL;
-        }
+        freeAllocatedSingleWideString(pStVarOne);
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
 
-    if (pStVarOne == NULL)
+    pStVarOneExpanded = expandPathVariableW(pStVarOne);
+    pStVarTwoExpanded = expandPathVariableW(pStVarTwo);
+
+    freeAllocatedSingleWideString(pStVarTwo);
+    freeAllocatedSingleWideString(pStVarOne);
+
+    if (pStVarOneExpanded == NULL)
     {
-        if (pStVarTwo)
+        if (pStVarTwoExpanded)
         {
-            freeAllocatedSingleWideString(pStVarTwo);
-            pStVarTwo = NULL;
+            FREE(pStVarTwoExpanded);
         }
-        Scierror(999, _("%s: Wrong value for input argument #%d: A string expected.\n"), fname, 1);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
 
-    if (pStVarTwo == NULL)
+    if (pStVarTwoExpanded == NULL)
     {
-        if (pStVarOne)
+        if (pStVarOneExpanded)
         {
-            freeAllocatedSingleWideString(pStVarOne);
-            pStVarOne = NULL;
+            FREE(pStVarOneExpanded);
         }
-        Scierror(999, _("%s: Wrong value for input argument #%d: A string expected.\n"), fname, 2);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
 
-    pStVarOneExpanded = expandPathVariableW(pStVarOne);
-    pStVarTwoExpanded = expandPathVariableW(pStVarTwo);
-
-    freeAllocatedSingleWideString(pStVarTwo);
-    pStVarTwo = NULL;
-
-    freeAllocatedSingleWideString(pStVarOne);
-    pStVarOne = NULL;
-
-
     if (isdirW(pStVarOneExpanded) || FileExistW(pStVarOneExpanded))
     {
         int ierrCopy = 0;
@@ -176,25 +154,12 @@ int sci_copyfile(char *fname, unsigned long fname_len)
                         ierrCopy = CopyFileFunction(destFullFilename, pStVarOneExpanded);
 
                         FREE(filename);
-                        filename = NULL;
-
                         FREE(destFullFilename);
-                        destFullFilename = NULL;
                     }
                     else
                     {
-                        if (pStVarOneExpanded)
-                        {
-                            FREE(pStVarOneExpanded);
-                            pStVarOneExpanded = NULL;
-                        }
-
-                        if (pStVarTwoExpanded)
-                        {
-                            FREE(pStVarTwoExpanded);
-                            pStVarTwo = NULL;
-                        }
-
+                        FREE(pStVarOneExpanded);
+                        FREE(pStVarTwoExpanded);
                         Scierror(999, _("%s: Memory allocation error.\n"), fname);
                         return 0;
                     }
@@ -207,34 +172,15 @@ int sci_copyfile(char *fname, unsigned long fname_len)
             }
             else
             {
-                if (pStVarOneExpanded)
-                {
-                    FREE(pStVarOneExpanded);
-                    pStVarOneExpanded = NULL;
-                }
-
-                if (pStVarTwoExpanded)
-                {
-                    FREE(pStVarTwoExpanded);
-                    pStVarTwoExpanded = NULL;
-                }
+                FREE(pStVarOneExpanded);
+                FREE(pStVarTwoExpanded);
                 Scierror(999, _("%s: Wrong value for input argument #%d: A valid filename or directory expected.\n"), fname, 1);
             }
         }
         else
         {
-            if (pStVarOneExpanded)
-            {
-                FREE(pStVarOneExpanded);
-                pStVarOneExpanded = NULL;
-            }
-
-            if (pStVarTwoExpanded)
-            {
-                FREE(pStVarTwoExpanded);
-                pStVarTwoExpanded = NULL;
-            }
-
+            FREE(pStVarOneExpanded);
+            FREE(pStVarTwoExpanded);
             Scierror(999, _("%s: Wrong value(s) for input argument(s).\n"), fname);
             return 0;
         }
@@ -246,17 +192,8 @@ int sci_copyfile(char *fname, unsigned long fname_len)
         Scierror(999, _("%s: Wrong value for input argument #%d: A valid filename or directory expected.\n"), fname, 1);
     }
 
-    if (pStVarOneExpanded)
-    {
-        FREE(pStVarOneExpanded);
-        pStVarOneExpanded = NULL;
-    }
-
-    if (pStVarTwoExpanded)
-    {
-        FREE(pStVarTwoExpanded);
-        pStVarTwoExpanded = NULL;
-    }
+    FREE(pStVarOneExpanded);
+    FREE(pStVarTwoExpanded);
 
     return 0;
 }
@@ -268,45 +205,35 @@ static wchar_t *getFilenameWithExtension(wchar_t * wcFullFilename)
 
     if (wcFullFilename)
     {
-        wchar_t *wcdrv = MALLOC(sizeof(wchar_t *) * ((int)wcslen(wcFullFilename) + 1));
-        wchar_t *wcdir = MALLOC(sizeof(wchar_t *) * ((int)wcslen(wcFullFilename) + 1));
-        wchar_t *wcname = MALLOC(sizeof(wchar_t *) * ((int)wcslen(wcFullFilename) + 1));
-        wchar_t *wcext = MALLOC(sizeof(wchar_t *) * ((int)wcslen(wcFullFilename) + 1));
-
-        wcfilename = MALLOC(sizeof(wchar_t *) * ((int)wcslen(wcFullFilename) + 1));
+        wchar_t *wcdrv = (wchar_t *)MALLOC(sizeof(wchar_t) * ((int)wcslen(wcFullFilename) + 1));
+        wchar_t *wcdir = (wchar_t *)MALLOC(sizeof(wchar_t) * ((int)wcslen(wcFullFilename) + 1));
+        wchar_t *wcname = (wchar_t *)MALLOC(sizeof(wchar_t) * ((int)wcslen(wcFullFilename) + 1));
+        wchar_t *wcext = (wchar_t *)MALLOC(sizeof(wchar_t) * ((int)wcslen(wcFullFilename) + 1));
 
-        splitpathW(wcFullFilename, FALSE, wcdrv, wcdir, wcname, wcext);
+        wcfilename = (wchar_t *)MALLOC(sizeof(wchar_t) * ((int)wcslen(wcFullFilename) + 1));
 
-        wcscpy(wcfilename, wcname);
-        wcscat(wcfilename, wcext);
-
-        if (wcdrv)
+        if (wcdrv == NULL || wcdir == NULL || wcname == NULL || wcext == NULL || wcfilename == NULL)
         {
             FREE(wcdrv);
-            wcdrv = NULL;
-        }
-
-        if (wcdir)
-        {
             FREE(wcdir);
-            wcdir = NULL;
-        }
-
-        if (wcname)
-        {
             FREE(wcname);
-            wcname = NULL;
-        }
-
-        if (wcext)
-        {
             FREE(wcext);
-            wcext = NULL;
+            FREE(wcfilename);
+            return NULL;
         }
+
+        splitpathW(wcFullFilename, FALSE, wcdrv, wcdir, wcname, wcext);
+
+        wcscpy(wcfilename, wcname);
+        wcscat(wcfilename, wcext);
+
+        FREE(wcdrv);
+        FREE(wcdir);
+        FREE(wcname);
+        FREE(wcext);
     }
     return wcfilename;
 }
-
 /*--------------------------------------------------------------------------*/
 static int returnCopyFileResultOnStack(int ierr, char *fname)
 {
index c3377bc..d44643b 100644 (file)
@@ -45,6 +45,7 @@ int sci_fileinfo(char *fname, unsigned long fname_len)
         {
             int result = 0;
             double *infos = fileinfo(InputString_Parameter[0], &result);
+            freeArrayOfString(InputString_Parameter, mn);
 
             if (infos)
             {
@@ -53,7 +54,6 @@ int sci_fileinfo(char *fname, unsigned long fname_len)
                 CreateVarFromPtr(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &m_out, &n_out, &infos);
                 LhsVar(1) = Rhs + 1;
                 FREE(infos);
-                infos = NULL;
             }
             else
             {
@@ -67,7 +67,6 @@ int sci_fileinfo(char *fname, unsigned long fname_len)
                 LhsVar(1) = Rhs + 1;
             }
 
-            freeArrayOfString(InputString_Parameter, mn);
 
             if (Lhs == 2)
             {
@@ -80,25 +79,23 @@ int sci_fileinfo(char *fname, unsigned long fname_len)
             }
 
             PutLhsVar();
+            return 0;
         }
         else
         {
-            if ( (m != 1) && (n == 1) )
+            if (m != 1 && n == 1) //column vector only
             {
                 int *results = (int*)MALLOC(sizeof(int) * mn);
                 double *infos = filesinfo(InputString_Parameter, mn, results);
+                freeArrayOfString(InputString_Parameter, mn);
                 if (infos)
                 {
                     int m_out = 0;
-                    int n_out = m;
+                    int n_out = 0;
                     double *transposeInfos = NULL;
 
-                    m_out = FILEINFO_ARRAY_SIZE;
-
-
                     transposeInfos = transposeMatrixDouble(FILEINFO_ARRAY_SIZE, m, infos);
                     FREE(infos);
-                    infos = NULL;
 
                     n_out = FILEINFO_ARRAY_SIZE;
                     m_out = m;
@@ -106,7 +103,6 @@ int sci_fileinfo(char *fname, unsigned long fname_len)
                     CreateVarFromPtr(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &m_out, &n_out, &transposeInfos);
                     LhsVar(1) = Rhs + 1;
                     FREE(transposeInfos);
-                    transposeInfos = NULL;
                 }
                 else
                 {
@@ -115,9 +111,17 @@ int sci_fileinfo(char *fname, unsigned long fname_len)
                     int l_out = 0;
 
                     /* returns [] */
-
+                    FREE(results);
                     CreateVar(Rhs + 1, MATRIX_OF_DOUBLE_DATATYPE, &m_out, &n_out, &l_out);
                     LhsVar(1) = Rhs + 1;
+                    if (Lhs == 2)
+                    {
+                        CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &m_out, &n_out, &l_out);
+                        LhsVar(2) = Rhs + 2;
+                    }
+
+                    PutLhsVar();
+                    return 0;
                 }
 
                 if (Lhs == 2)
@@ -135,26 +139,22 @@ int sci_fileinfo(char *fname, unsigned long fname_len)
                     LhsVar(2) = Rhs + 2;
                 }
 
-                freeArrayOfString(InputString_Parameter, mn);
-                if (results)
-                {
-                    FREE(results);
-                    results = NULL;
-                }
-
+                FREE(results);
                 PutLhsVar();
             }
             else
             {
-                freeArrayOfString(InputString_Parameter, mn);
                 Scierror(999, _("%s: Wrong size for input argument #%d: A m-by-1 array expected.\n"), fname, 1);
+                return 0;
             }
         }
     }
     else
     {
         Scierror(999, _("%s: Wrong type for input argument: A string expected.\n"), fname);
+        return 0;
     }
+
     return 0;
 }
 /*--------------------------------------------------------------------------*/
index 244d86e..918f215 100644 (file)
@@ -22,8 +22,8 @@
 #define FILEPARTS_FNAME_SELECTOR L"fname"
 #define FILEPARTS_EXTENSION_SELECTOR L"extension"
 
-static int allocStrings(void** _pwstDrv, void** _pwstDir, void** _pwstName, void** _pwstExt, void** _pwstPath, int _iNum, int _iSize);
-static void freeStrings(void** _pwstDrv, void** _pwstDir, void** _pwstName, void** _pwstExt, void** _pwstPath);
+static void allocStrings(wchar_t** _pwstDrv, wchar_t** _pwstDir, wchar_t** _pwstName, wchar_t** _pwstExt, wchar_t** _pwstPath, int _iNum);
+static void freeStrings(wchar_t** _pwstDrv, wchar_t** _pwstDir, wchar_t** _pwstName, wchar_t** _pwstExt, wchar_t** _pwstPath);
 
 /*--------------------------------------------------------------------------*/
 int sci_fileparts(char *fname, unsigned long fname_len)
@@ -32,14 +32,9 @@ int sci_fileparts(char *fname, unsigned long fname_len)
 
     int *piAddressVarOne = NULL;
     wchar_t **pStVarOne = NULL;
-    wchar_t *pStVarTwo = NULL;
     int *piAddressVarTwo = NULL;
+    wchar_t *pStVarTwo = NULL;
 
-    wchar_t **drv = NULL;
-    wchar_t **dir = NULL;
-    wchar_t **name = NULL;
-    wchar_t **ext = NULL;
-    wchar_t **path_out = NULL;
     wchar_t **output_value = NULL;
     int iRows = 0;
     int iCols = 0;
@@ -72,6 +67,7 @@ int sci_fileparts(char *fname, unsigned long fname_len)
 
     if (getAllocatedMatrixOfWideString(pvApiCtx, piAddressVarOne, &iRows, &iCols, &pStVarOne))
     {
+        freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
         Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"), fname, 1);
         return 0;
     }
@@ -86,29 +82,19 @@ int sci_fileparts(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        if (getAllocatedSingleWideString(pvApiCtx, piAddressVarTwo, &pStVarTwo))
+        if (!isStringType(pvApiCtx, piAddressVarTwo))
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
             return 0;
         }
-    }
 
-    if (allocStrings((void**)&drv, (void**)&dir, (void**)&name, (void**)&ext, (void**)&path_out, iRows * iCols, sizeof(wchar_t**)))
-    {
-        if (pStVarOne)
+        if (getAllocatedSingleWideString(pvApiCtx, piAddressVarTwo, &pStVarTwo))
         {
             freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
-            pStVarOne = NULL;
-        }
-
-        if (pStVarTwo)
-        {
             freeAllocatedSingleWideString(pStVarTwo);
-            pStVarTwo = NULL;
+            Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
+            return 0;
         }
-
-        Scierror(999, _("%s: Memory allocation error.\n"), fname);
-        return 0;
     }
 
     if (pStVarTwo)              /* Rhs == 2 */
@@ -118,173 +104,140 @@ int sci_fileparts(char *fname, unsigned long fname_len)
         {
             freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
             freeAllocatedSingleWideString(pStVarTwo);
-            freeStrings((void**)&drv, (void**)&dir, (void**)&name, (void**)&ext, (void**)&path_out);
             Scierror(999, _("%s: No more memory.\n"), fname);
             return 0;
         }
 
         for (i = 0; i < iRows * iCols; i++)
         {
+            wchar_t *drv = NULL;
+            wchar_t *dir = NULL;
+            wchar_t *name = NULL;
+            wchar_t *ext = NULL;
+
             int iLen        = (int)wcslen(pStVarOne[i]) + 1;
             output_value[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * iLen);
 
-            if (allocStrings((void**)&drv[i], (void**)&dir[i], (void**)&name[i], (void**)&ext[i], (void**)&path_out[i], iLen, sizeof(wchar_t*))
-                    || output_value[i] == NULL)
+            if (output_value[i] == NULL)
             {
-                if (pStVarOne[i])
-                {
-                    freeArrayOfWideString(pStVarOne, i);
-                    pStVarOne[i] = NULL;
-                }
-                if (pStVarTwo)
-                {
-                    freeAllocatedSingleWideString(pStVarTwo);
-                    pStVarTwo = NULL;
-                }
-
-                if (output_value[i])
-                {
-                    freeArrayOfWideString(output_value, i);
-                    output_value[i] = NULL;
-                }
-
+                freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
+                freeAllocatedSingleWideString(pStVarTwo);
+                freeArrayOfWideString(output_value, i);
                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
                 return 0;
             }
 
-            splitpathW(pStVarOne[i], FALSE, drv[i], dir[i], name[i], ext[i]);
+            allocStrings(&drv, &dir, &name, &ext, NULL, iLen);
+            splitpathW(pStVarOne[i], FALSE, drv, dir, name, ext);
 
             if (wcscmp(pStVarTwo, FILEPARTS_PATH_SELECTOR) == 0)
             {
-                output_value = path_out;
-                wcscpy(output_value[i], drv[i]);
-                wcscat(output_value[i], dir[i]);
+                wcscpy(output_value[i], drv);
+                wcscat(output_value[i], dir);
             }
             else if (wcscmp(pStVarTwo, FILEPARTS_FNAME_SELECTOR) == 0)
             {
-                output_value[i] = name[i];
+                output_value[i] = name;
             }
             else if (wcscmp(pStVarTwo, FILEPARTS_EXTENSION_SELECTOR) == 0)
             {
-                output_value[i] = ext[i];
+                output_value[i] = ext;
             }
             else
             {
                 int j = 0;
-                if (pStVarTwo[i])
-                {
-                    freeAllocatedSingleWideString(pStVarTwo);
-                    pStVarTwo = NULL;
-                }
-
-                for (j = 0 ; j <= i ; j++)
-                {
-                    freeStrings((void**)&drv[j], (void**)&dir[j], (void**)&name[j], (void**)&ext[j], (void**)&path_out[j]);
-                }
-
-                freeStrings((void**)&drv, (void**)&dir, (void**)&name, (void**)&ext, (void**)&path_out);
-
+                freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
+                freeAllocatedSingleWideString(pStVarTwo);
+                freeArrayOfWideString(output_value, i);
+                freeStrings(&drv, &dir, &name, &ext, NULL);
                 Scierror(999, _("%s: Wrong value for input argument #%d.\n"), fname, 2);
                 return 0;
             }
 
         }
+
         sciErr = createMatrixOfWideString(pvApiCtx, iRhs + 1, iRows, iCols, output_value);
+        freeArrayOfWideString(output_value, iRows * iCols);
+        freeAllocatedSingleWideString(pStVarTwo);
         if (sciErr.iErr)
         {
             printError(&sciErr, 0);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
-        AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
 
-        if (pStVarTwo)
-        {
-            freeAllocatedSingleWideString(pStVarTwo);
-            pStVarTwo = NULL;
-        }
+        AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
     }
     else
     {
+        wchar_t **path_out = (wchar_t**)MALLOC(sizeof(wchar_t*) * iRows * iCols);
+        wchar_t **name = (wchar_t**)MALLOC(sizeof(wchar_t*) * iRows * iCols);
+        wchar_t **ext = (wchar_t**)MALLOC(sizeof(wchar_t*) * iRows * iCols);
         for (i = 0; i < iRows * iCols; i++)
         {
+            wchar_t *drv = NULL;
+            wchar_t *dir = NULL;
 
-            int iLen    = (int)wcslen(pStVarOne[i]) + 1;
-            if (allocStrings((void**)&drv[i], (void**)&dir[i], (void**)&name[i], (void**)&ext[i], (void**)&path_out[i], iLen, sizeof(wchar_t*)))
-            {
-                Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                return 0;
-            }
+            int iLen = (int)wcslen(pStVarOne[i]) + 1;
+            allocStrings(&drv, &dir, &name[i], &ext[i], &path_out[i], iLen);
+            splitpathW(pStVarOne[i], FALSE, drv, dir, name[i], ext[i]);
+            wcscpy(path_out[i], drv);
+            wcscat(path_out[i], dir);
 
-            splitpathW(pStVarOne[i], FALSE, drv[i], dir[i], name[i], ext[i]);
-            wcscpy(path_out[i], drv[i]);
-            wcscat(path_out[i], dir[i]);
+            freeStrings(&drv, &dir, NULL, NULL, NULL);
         }
 
         sciErr = createMatrixOfWideString(pvApiCtx, iRhs + 1, iRows, iCols, path_out);
         if (sciErr.iErr)
         {
+            freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
             printError(&sciErr, 0);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
 
+        freeArrayOfWideString(path_out, iRows * iCols);
         AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
 
         sciErr = createMatrixOfWideString(pvApiCtx, iRhs + 2, iRows, iCols, name);
         if (sciErr.iErr)
         {
+            freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
             printError(&sciErr, 0);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
 
+        freeArrayOfWideString(name, iRows * iCols);
         AssignOutputVariable(pvApiCtx, 2) = iRhs + 2;
 
         sciErr = createMatrixOfWideString(pvApiCtx, iRhs + 3, iRows, iCols, ext);
         if (sciErr.iErr)
         {
+            freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
             printError(&sciErr, 0);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
 
-
+        freeArrayOfWideString(ext, iRows * iCols);
         AssignOutputVariable(pvApiCtx, 3) = iRhs + 3;
     }
 
-    if (pStVarOne)
-    {
-        freeAllocatedMatrixOfWideString(iRows, iCols, pStVarOne);
-        pStVarOne = NULL;
-    }
     ReturnArguments(pvApiCtx);
-
-    for (i = 0 ; i < iRows * iCols ; i++)
-    {
-        freeStrings((void**)&drv[i], (void**)&dir[i], (void**)&name[i], (void**)&ext[i], (void**)&path_out[i]);
-    }
-
-    freeStrings((void**)&drv, (void**)&dir, (void**)&name, (void**)&ext, (void**)&path_out);
-
     return 0;
 }
 
-static void* allocVariable(int _iSize, int _iNum)
-{
-    return MALLOC(_iSize * _iNum);
-}
-
-static void freeVariable(void** _pwstVar)
+static void freeVariable(wchar_t** _pwstVar)
 {
-    if (*_pwstVar)
+    if (_pwstVar && *_pwstVar)
     {
         FREE(*_pwstVar);
         *_pwstVar = NULL;
     }
 }
 
-static void freeStrings(void** _pwstDrv, void** _pwstDir, void** _pwstName, void** _pwstExt, void** _pwstPath)
+static void freeStrings(wchar_t** _pwstDrv, wchar_t** _pwstDir, wchar_t** _pwstName, wchar_t** _pwstExt, wchar_t** _pwstPath)
 {
     freeVariable(_pwstDrv);
     freeVariable(_pwstDir);
@@ -293,20 +246,20 @@ static void freeStrings(void** _pwstDrv, void** _pwstDir, void** _pwstName, void
     freeVariable(_pwstPath);
 }
 
-static int allocStrings(void** _pwstDrv, void** _pwstDir, void** _pwstName, void** _pwstExt, void** _pwstPath, int _iNum, int _iSize)
+static void allocString(wchar_t** _pwst, int _iNum)
 {
-    *_pwstDrv   = allocVariable(_iSize, _iNum);
-    *_pwstDir   = allocVariable(_iSize, _iNum);
-    *_pwstName  = allocVariable(_iSize, _iNum);
-    *_pwstExt   = allocVariable(_iSize, _iNum);
-    *_pwstPath  = allocVariable(_iSize, _iNum);
-
-    if (*_pwstDrv == NULL || *_pwstDir == NULL || *_pwstName == NULL || *_pwstExt == NULL || *_pwstPath == NULL)
+    if (_pwst)
     {
-        freeStrings((void**)_pwstDrv, (void**)_pwstDir, (void**)_pwstName, (void**)_pwstExt, (void**)_pwstPath);
-        return 1;
+        *_pwst = (wchar_t*)MALLOC(sizeof(wchar_t) * _iNum);
     }
+}
 
-    return 0;
+static void allocStrings(wchar_t** _pwstDrv, wchar_t** _pwstDir, wchar_t** _pwstName, wchar_t** _pwstExt, wchar_t** _pwstPath, int _iNum)
+{
+    allocString(_pwstDrv, _iNum);
+    allocString(_pwstDir, _iNum);
+    allocString(_pwstName, _iNum);
+    allocString(_pwstExt, _iNum);
+    allocString(_pwstPath, _iNum);
 }
 /*--------------------------------------------------------------------------*/
index 4373578..45b3b32 100644 (file)
@@ -103,22 +103,11 @@ int sci_findfiles(char *fname, unsigned long fname_len)
     }
 
     pathextented = expandPathVariable(path);
-    if (path)
-    {
-        FREE(path);
-        path = NULL;
-    }
     FilesList = findfiles(pathextented, filespec, &sizeListReturned, FALSE);
-    if (pathextented)
-    {
-        FREE(pathextented);
-        pathextented = NULL;
-    }
-    if (filespec)
-    {
-        FREE(filespec);
-        filespec = NULL;
-    }
+
+    FREE(path);
+    FREE(pathextented);
+    FREE(filespec);
 
     if (FilesList)
     {
index 200e79e..69fd02b 100644 (file)
@@ -23,6 +23,8 @@
 #include "strdup_windows.h"
 #endif
 #include "fprintfMat.h"
+
+static void freeVar(char** filename, char** expandedFilename, char*** textAdded, int m4n4, char** Format, char** separator);
 /*--------------------------------------------------------------------------*/
 int sci_fprintfMat(char *fname, unsigned long fname_len)
 {
@@ -47,15 +49,12 @@ int sci_fprintfMat(char *fname, unsigned long fname_len)
     int i = 0;
 
     Nbvars = 0;
-    CheckRhs(1, 5);
+    CheckRhs(2, 5);
     CheckLhs(1, 1);
 
     if (Rhs >= 3)
     {
         int *piAddressVarThree = NULL;
-        int iType3     = 0;
-        int m3 = 0, n3 = 0;
-
         sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
         if (sciErr.iErr)
         {
@@ -64,30 +63,19 @@ int sci_fprintfMat(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
-            return 0;
-        }
-
-        if (iType3 != sci_strings)
+        if (isStringType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3);
             return 0;
         }
 
-        sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);
-
-        if ( (m3 != n3) && (n3 != 1) )
-        {
-            Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
-            return 0;
-        }
-
         if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &Format))
         {
+            if (Format)
+            {
+                freeAllocatedSingleString(Format);
+            }
+
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
@@ -100,156 +88,29 @@ int sci_fprintfMat(char *fname, unsigned long fname_len)
     if ( Rhs >= 4 )
     {
         int *piAddressVarFour = NULL;
-        int *lengthStrings = NULL;
-        int iType4     = 0;
-        int m4 = 0, n4 = 0;
+        int m4 = 0;
+        int n4 = 0;
 
         sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour);
         if (sciErr.iErr)
         {
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
-            return 0;
-        }
-
-        sciErr = getVarType(pvApiCtx, piAddressVarFour, &iType4);
-        if (sciErr.iErr)
-        {
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
-            return 0;
-        }
-
-        if (iType4 != sci_strings)
-        {
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 4);
-            return 0;
-        }
-
-        sciErr = getVarDimension(pvApiCtx, piAddressVarFour, &m4, &n4);
-        if (sciErr.iErr)
-        {
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
-            return 0;
-        }
-
-        if  (! ((m4 == 1) || (n4 == 1)))
-        {
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            Scierror(999, _("%s: Wrong size for input argument #%d.\n"), fname, 4);
-            return 0;
-        }
-
-        lengthStrings = (int*)MALLOC(sizeof(int) * (m4 * n4));
-        if (lengthStrings == NULL)
-        {
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
-
-        // get lengthStrings value
-        sciErr = getMatrixOfString(pvApiCtx, piAddressVarFour, &m4, &n4, lengthStrings, NULL);
-        if (sciErr.iErr)
-        {
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            if (lengthStrings)
-            {
-                FREE(lengthStrings);
-                lengthStrings = NULL;
-            }
+            freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
             printError(&sciErr, 0);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
             return 0;
         }
 
-        textAdded = (char**)MALLOC(sizeof(char*) * (m4 * n4));
-        if (textAdded == NULL)
+        if (isStringType(pvApiCtx, piAddressVarFour) == FALSE ||
+                (isScalar(pvApiCtx, piAddressVarFour) == FALSE && isVector(pvApiCtx, piAddressVarFour) == FALSE))
         {
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            if (lengthStrings)
-            {
-                FREE(lengthStrings);
-                lengthStrings = NULL;
-            }
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
+            freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
+            Scierror(999, _("%s: Wrong size for input argument #%d: A 1-by-n or m-by-1 array expected.\n"), fname, 1);
             return 0;
         }
 
-        for (i = 0; i < (m4 * n4); i++)
-        {
-            textAdded[i] = (char*)MALLOC(sizeof(char) * (lengthStrings[i] + 1));
-            if (textAdded[i] == NULL)
-            {
-                freeArrayOfString(textAdded, m4 * n4);
-                if (Format)
-                {
-                    FREE(Format);
-                    Format = NULL;
-                }
-                if (lengthStrings)
-                {
-                    FREE(lengthStrings);
-                    lengthStrings = NULL;
-                }
-                Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                return 0;
-            }
-        }
-
-        // get textAdded
-        sciErr = getMatrixOfString(pvApiCtx, piAddressVarFour, &m4, &n4, lengthStrings, textAdded);
-        if (lengthStrings)
+        if (getAllocatedMatrixOfString(pvApiCtx, piAddressVarFour, &m4, &n4, &textAdded))
         {
-            FREE(lengthStrings);
-            lengthStrings = NULL;
-        }
-        if (sciErr.iErr)
-        {
-            freeArrayOfString(textAdded, m4 * n4);
-            if (Format)
-            {
-                FREE(Format);
-                Format = NULL;
-            }
-            printError(&sciErr, 0);
+            freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
             return 0;
         }
@@ -257,50 +118,29 @@ int sci_fprintfMat(char *fname, unsigned long fname_len)
         m4n4 = m4 * n4;
     }
 
-    if (Rhs > 4)
+    if (Rhs >= 5)
     {
         int *piAddressVarFive = NULL;
-        int iType5     = 0;
-        int m5 = 0, n5 = 0;
-
         sciErr = getVarAddressFromPosition(pvApiCtx, 5, &piAddressVarFive);
         if (sciErr.iErr)
         {
+            freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
             printError(&sciErr, 0);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 5);
             return 0;
         }
 
-        sciErr = getVarType(pvApiCtx, piAddressVarFive, &iType5);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 5);
-            return 0;
-        }
-
-        if (iType5 != sci_strings)
-        {
-            Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 5);
-            return 0;
-        }
-
-        sciErr = getVarDimension(pvApiCtx, piAddressVarFive, &m5, &n5);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 5);
-            return 0;
-        }
-
-        if ( (m5 != n5) && (n5 != 1) )
+        if (isStringType(pvApiCtx, piAddressVarFive) == 0 ||
+                isScalar(pvApiCtx, piAddressVarFive) == 0)
         {
-            Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 5);
+            freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
+            Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
             return 0;
         }
 
         if (getAllocatedSingleString(pvApiCtx, piAddressVarFive, &separator))
         {
+            freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
@@ -313,104 +153,23 @@ int sci_fprintfMat(char *fname, unsigned long fname_len)
     sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
     if (sciErr.iErr)
     {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        printError(&sciErr, 0);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
-        return 0;
-    }
-
-    sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
-    if (sciErr.iErr)
-    {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
         printError(&sciErr, 0);
         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
         return 0;
     }
 
-    if (iType2 != sci_matrix)
+    if (isDoubleType(pvApiCtx, piAddressVarTwo) == 0 || isVarComplex(pvApiCtx, piAddressVarTwo))
     {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
         Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of floating point numbers expected.\n"), fname, 2);
         return 0;
     }
 
-    if (isVarComplex(pvApiCtx, piAddressVarTwo))
-    {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        Scierror(999, _("%s: Wrong type for input argument #%d: Real values expected.\n"), fname, 2);
-        return 0;
-    }
-
     sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &dValues);
     if (sciErr.iErr)
     {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
         printError(&sciErr, 0);
         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
         return 0;
@@ -419,141 +178,34 @@ int sci_fprintfMat(char *fname, unsigned long fname_len)
     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
     if (sciErr.iErr)
     {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
         printError(&sciErr, 0);
         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
         return 0;
     }
 
-    sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
-    if (sciErr.iErr)
+    if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0)
     {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        printError(&sciErr, 0);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-        return 0;
-    }
-
-    if (iType1 != sci_strings)
-    {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
-        return 0;
-    }
-
-    sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
-
-    if ( (m1 != n1) && (n1 != 1) )
-    {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
         Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
         return 0;
     }
 
     if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &filename))
     {
-        if (textAdded)
-        {
-            freeArrayOfString(textAdded, m4n4);
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
 
     expandedFilename = expandPathVariable(filename);
     ierr = fprintfMat(expandedFilename, Format, separator, dValues, m2, n2, textAdded, m4n4);
-    if (expandedFilename)
-    {
-        FREE(expandedFilename);
-        expandedFilename = NULL;
-    }
-    if (textAdded)
-    {
-        freeArrayOfString(textAdded, m4n4);
-    }
-    if (Format)
-    {
-        FREE(Format);
-        Format = NULL;
-    }
-    if (separator)
-    {
-        FREE(separator);
-        separator = NULL;
-    }
 
     switch (ierr)
     {
         case FPRINTFMAT_NO_ERROR:
         {
             LhsVar(1) = 0;
-            if (filename)
-            {
-                FREE(filename);
-                filename = NULL;
-            }
             PutLhsVar();
         }
         break;
@@ -575,12 +227,40 @@ int sci_fprintfMat(char *fname, unsigned long fname_len)
         break;
     }
 
-    if (filename)
-    {
-        FREE(filename);
-        filename = NULL;
-    }
+    freeVar(&filename, &expandedFilename, &textAdded, m4n4, &Format, &separator);
     return 0;
 }
 /*--------------------------------------------------------------------------*/
+static void freeVar(char** filename, char** expandedFilename, char*** textAdded, int m4n4, char** Format, char** separator)
+{
+    if (filename && *filename)
+    {
+        FREE(*filename);
+        *filename = NULL;
+    }
+
+    if (expandedFilename && *expandedFilename)
+    {
+        FREE(*expandedFilename);
+        *expandedFilename = NULL;
+    }
+
+    if (textAdded && *textAdded)
+    {
+        freeArrayOfString(*textAdded, m4n4);
+        *textAdded = NULL;
+    }
+
+    if (Format && *Format)
+    {
+        FREE(*Format);
+        *Format = NULL;
+    }
+
+    if (separator && *separator)
+    {
+        FREE(*separator);
+        *separator = NULL;
+    }
+}
 
index 832983b..1008e0d 100644 (file)
@@ -24,6 +24,9 @@
 #include "strdup_windows.h"
 #endif
 #include "fscanfMat.h"
+
+/*--------------------------------------------------------------------------*/
+static void freeVar(char** filename, char** expandedFilename, char** Format, char** separator);
 /*--------------------------------------------------------------------------*/
 int sci_fscanfMat(char *fname, unsigned long fname_len)
 {
@@ -58,34 +61,12 @@ int sci_fscanfMat(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
-            return 0;
-        }
-
-        if (iType3 != sci_strings)
+        if (isStringType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 3);
             return 0;
         }
 
-        sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
-            return 0;
-        }
-
-        if ( (m3 != n3) && (n3 != 1) )
-        {
-            Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 3);
-            return 0;
-        }
-
         if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &separator))
         {
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
@@ -107,11 +88,7 @@ int sci_fscanfMat(char *fname, unsigned long fname_len)
         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
         if (sciErr.iErr)
         {
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
+            freeVar(&filename, &expandedFilename, &Format, &separator);
             printError(&sciErr, 0);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
             return 0;
@@ -120,58 +97,22 @@ int sci_fscanfMat(char *fname, unsigned long fname_len)
         sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
         if (sciErr.iErr)
         {
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
+            freeVar(&filename, &expandedFilename, &Format, &separator);
             printError(&sciErr, 0);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
             return 0;
         }
 
-        if (iType2 != sci_strings)
+        if (isStringType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0)
         {
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
+            freeVar(&filename, &expandedFilename, &Format, &separator);
             Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
             return 0;
         }
 
-        sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2);
-        if (sciErr.iErr)
-        {
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
-            return 0;
-        }
-
-        if ( (m2 != n2) && (n2 != 1) )
-        {
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
-            Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
-            return 0;
-        }
-
         if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &Format))
         {
-            if (separator)
-            {
-                FREE(separator);
-                separator = NULL;
-            }
+            freeVar(&filename, &expandedFilename, &Format, &separator);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
@@ -184,101 +125,22 @@ int sci_fscanfMat(char *fname, unsigned long fname_len)
     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
     if (sciErr.iErr)
     {
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        printError(&sciErr, 0);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-        return 0;
-    }
-
-    sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
-    if (sciErr.iErr)
-    {
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &Format, &separator);
         printError(&sciErr, 0);
         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
         return 0;
     }
 
-    if (iType1 != sci_strings)
+    if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0)
     {
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &Format, &separator);
         Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
         return 0;
     }
 
-    sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
-    if (sciErr.iErr)
-    {
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        printError(&sciErr, 0);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-        return 0;
-    }
-
-    if ( (m1 != n1) && (n1 != 1) )
-    {
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
-        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
-        return 0;
-    }
-
     if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &filename))
     {
-        if (separator)
-        {
-            FREE(separator);
-            separator = NULL;
-        }
-        if (Format)
-        {
-            FREE(Format);
-            Format = NULL;
-        }
+        freeVar(&filename, &expandedFilename, &Format, &separator);
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
@@ -307,134 +169,133 @@ int sci_fscanfMat(char *fname, unsigned long fname_len)
         results = fscanfMat(expandedFilename, Format, separator);
     }
 
-    if (expandedFilename)
-    {
-        FREE(expandedFilename);
-        expandedFilename = NULL;
-    }
-    if (Format)
-    {
-        FREE(Format);
-        Format = NULL;
-    }
-    if (separator)
+    if (results == NULL)
     {
-        FREE(separator);
-        separator = NULL;
+        freeVar(&filename, &expandedFilename, &Format, &separator);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
     }
 
-    if (results)
+    freeVar(NULL, &expandedFilename, &Format, &separator);
+    switch (results->err)
     {
-        switch (results->err)
+        case FSCANFMAT_NO_ERROR:
         {
-            case FSCANFMAT_NO_ERROR:
+            if ( (results->values) && (results->m > 0) && (results->n > 0))
+            {
+                sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, results->m, results->n, results->values);
+                if (sciErr.iErr)
+                {
+                    FREE(filename);
+                    freeFscanfMatResult(results);
+                    printError(&sciErr, 0);
+                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                    return 0;
+                }
+            }
+            else
+            {
+                if (createEmptyMatrix(pvApiCtx, Rhs + 1))
+                {
+                    FREE(filename);
+                    freeFscanfMatResult(results);
+                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                    return 0;
+                }
+            }
+
+            LhsVar(1) = Rhs + 1;
+
+            if (Lhs == 2)
             {
-                if ( (results->values) && (results->m > 0) && (results->n > 0))
+                if (results->text)
                 {
-                    sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, results->m, results->n, results->values);
+                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, results->sizeText, 1, results->text);
                     if (sciErr.iErr)
                     {
+                        FREE(filename);
                         freeFscanfMatResult(results);
-                        results = NULL;
                         printError(&sciErr, 0);
+                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
                         return 0;
                     }
-                    else
-                    {
-                        LhsVar(1) = Rhs + 1;
-                    }
                 }
                 else
                 {
-                    if (createEmptyMatrix(pvApiCtx, Rhs + 1) == 0)
-                    {
-                        LhsVar(1) = Rhs + 1;
-                    }
-                }
-
-                if (Lhs == 2)
-                {
-                    if (results->text)
+                    if (createSingleString(pvApiCtx, Rhs + 2, ""))
                     {
-                        sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, results->sizeText, 1, results->text);
-
+                        FREE(filename);
                         freeFscanfMatResult(results);
-                        results = NULL;
-
-                        if (sciErr.iErr)
-                        {
-                            printError(&sciErr, 0);
-                            return 0;
-                        }
-                        else
-                        {
-                            LhsVar(2) = Rhs + 2;
-                        }
-                    }
-                    else
-                    {
-                        char *emptryStr = strdup("");
-                        if (emptryStr)
-                        {
-                            createSingleString(pvApiCtx, Rhs + 2, emptryStr);
-                            FREE(emptryStr);
-                            emptryStr = NULL;
-                        }
-                        LhsVar(2) = Rhs + 2;
+                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                        return 0;
                     }
                 }
 
-                freeFscanfMatResult(results);
-                results = NULL;
-
-                if (filename)
-                {
-                    FREE(filename);
-                    filename = NULL;
-                }
-
-                PutLhsVar();
-            }
-            break;
-            case FSCANFMAT_MOPEN_ERROR:
-            {
-                Scierror(999, _("%s: can not open file %s.\n"), fname, filename);
-            }
-            break;
-            case FSCANFMAT_READLINES_ERROR:
-            {
-                Scierror(999, _("%s: can not read file %s.\n"), fname, filename);
-            }
-            break;
-            case FSCANFMAT_FORMAT_ERROR:
-            {
-                Scierror(999, _("%s: Invalid format.\n"), fname);
-            }
-            break;
-            case FSCANFMAT_MEMORY_ALLOCATION:
-            {
-                Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            }
-            break;
-            default:
-            case FSCANFMAT_ERROR:
-            {
-                Scierror(999, _("%s: error.\n"), fname);
+                LhsVar(2) = Rhs + 2;
             }
-            break;
+
+            freeFscanfMatResult(results);
+            FREE(filename);
+            PutLhsVar();
+            return 0;
+        }
+        break;
+        case FSCANFMAT_MOPEN_ERROR:
+        {
+            Scierror(999, _("%s: can not open file %s.\n"), fname, filename);
         }
+        break;
+        case FSCANFMAT_READLINES_ERROR:
+        {
+            Scierror(999, _("%s: can not read file %s.\n"), fname, filename);
+        }
+        break;
+        case FSCANFMAT_FORMAT_ERROR:
+        {
+            Scierror(999, _("%s: Invalid format.\n"), fname);
+        }
+        break;
+        case FSCANFMAT_MEMORY_ALLOCATION:
+        {
+            Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        }
+        break;
+        default:
+        case FSCANFMAT_ERROR:
+        {
+            Scierror(999, _("%s: error.\n"), fname);
+        }
+        break;
     }
-    else
+
+    FREE(filename);
+    freeFscanfMatResult(results);
+    return 0;
+}
+/*--------------------------------------------------------------------------*/
+static void freeVar(char** filename, char** expandedFilename, char** Format, char** separator)
+{
+    if (filename && *filename)
     {
-        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        FREE(*filename);
+        *filename = NULL;
     }
 
-    if (filename)
+    if (expandedFilename && *expandedFilename)
     {
-        FREE(filename);
-        filename = NULL;
+        FREE(*expandedFilename);
+        *expandedFilename = NULL;
     }
 
-    return 0;
-}
-/*--------------------------------------------------------------------------*/
+    if (Format && *Format)
+    {
+        FREE(*Format);
+        *Format = NULL;
+    }
+
+    if (separator && *separator)
+    {
+        FREE(*separator);
+        *separator = NULL;
+    }
+}
\ No newline at end of file
index 1892c9b..15dbaa6 100644 (file)
@@ -65,6 +65,7 @@ int sci_fullpath(char *fname, unsigned long fname_len)
 
     if (getAllocatedMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, &pStVarOne) != 0)
     {
+        freeAllocatedMatrixOfString(mOne, nOne, pStVarOne);
         Scierror(999, _("%s: No more memory.\n"), fname);
         return 0;
     }
index 4a25b53..9491aa4 100644 (file)
@@ -18,7 +18,7 @@
 #include "dlManager.h"
 #include "localization.h"
 /* ==================================================================== */
-static void freeAllocatedStrings(char *url, char *dest, char *username, char *password, char* content);
+static void freeAllocatedStrings(char** url, char** dest, char** username, char** password, char** content, char** filePath);
 /* ==================================================================== */
 int sci_getURL(char *fname, int fname_len)
 {
@@ -55,20 +55,20 @@ int sci_getURL(char *fname, int fname_len)
     if (ret)
     {
         Scierror(999, _("%s: Wrong type for argument %d: A string expected.\n"), fname, 1);
-        freeAllocatedStrings(url, dest, username, password, content);
+        freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
         return 0;
     }
 
     if (iRhs > 1)
     {
-        /* Specify the destination */
+        //Specify the destination
         int *piAddressVarTwo = NULL;
 
         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
         if (sciErr.iErr)
         {
             printError(&sciErr, 0);
-            freeAllocatedStrings(url, dest, username, password, content);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
             return 0;
         }
 
@@ -76,21 +76,21 @@ int sci_getURL(char *fname, int fname_len)
         if (ret)
         {
             Scierror(999, _("%s: Wrong type for argument %d: A string expected.\n"), fname, 2);
-            freeAllocatedStrings(url, dest, username, password, content);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
             return 0;
         }
 
     }
     if (iRhs > 2)
     {
-        /* Specify the username */
+        //Specify the username
         int *piAddressVarThree = NULL;
 
         sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
         if (sciErr.iErr)
         {
             printError(&sciErr, 0);
-            freeAllocatedStrings(url, dest, username, password, content);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
             return 0;
         }
 
@@ -98,7 +98,7 @@ int sci_getURL(char *fname, int fname_len)
         if (ret)
         {
             Scierror(999, _("%s: Wrong type for argument %d: A string expected.\n"), fname, 3);
-            freeAllocatedStrings(url, dest, username, password, content);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
             return 0;
         }
 
@@ -106,14 +106,14 @@ int sci_getURL(char *fname, int fname_len)
 
     if (iRhs > 3)
     {
-        /* Specify the password */
+        //Specify the password
         int *piAddressVarFour = NULL;
 
         sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour);
         if (sciErr.iErr)
         {
             printError(&sciErr, 0);
-            freeAllocatedStrings(url, dest, username, password, content);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
             return 0;
         }
 
@@ -121,95 +121,110 @@ int sci_getURL(char *fname, int fname_len)
         if (ret)
         {
             Scierror(999, _("%s: Wrong type for argument %d: A string expected.\n"), fname, 4);
-            freeAllocatedStrings(url, dest, username, password, content);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
             return 0;
         }
 
     }
 
-    /* Set to NULL if 0 length strings */
+    //Set to NULL if 0 length strings
     if (url != NULL && strlen(url) == 0)
     {
+        FREE(url);
         url = NULL;
     }
 
     if (dest != NULL && strlen(dest) == 0)
     {
+        FREE(url);
         dest = NULL;
     }
 
     if (username != NULL && strlen(username) == 0)
     {
+        FREE(url);
         username = NULL;
     }
 
     if (password != NULL && strlen(password) == 0)
     {
+        FREE(url);
         password = NULL;
     }
 
     // call function
     filePath = downloadFile(url, dest, username, password, &content);
-    if (filePath != NULL)
+    if (filePath)
     {
-        //create new variable
+        //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"));
-            freeAllocatedStrings(url, dest, username, password, content);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
             return 0;
         }
     }
-    // call function
-    /* Return the second argument which is the content */
-    if (content != NULL && nbOutputArgument(pvApiCtx) == 2)
+
+    //Return the second argument which is the content
+    if (content && nbOutputArgument(pvApiCtx) == 2)
     {
         //create new variable with the content
         int res = createSingleString(pvApiCtx, iRhs + 2, content);
-        FREE(content);
-        content = NULL;
         if (res)
         {
             Scierror(999, _("%s: Could not create the output argument.\n"));
-            freeAllocatedStrings(url, dest, username, password, content);
+            freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
             return 0;
         }
     }
 
+    freeAllocatedStrings(&url, &dest, &username, &password, &content, &filePath);
+
     AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
     AssignOutputVariable(pvApiCtx, 2) = iRhs + 2;
     ReturnArguments(pvApiCtx);
     return 0;
 }
 /* ==================================================================== */
-static void freeAllocatedStrings(char *url, char *dest, char *username, char *password, char* content)
+static void freeAllocatedStrings(char** url, char** dest, char** username, char** password, char** content, char** filePath)
 {
-    if (url != NULL)
+    if (url && *url)
+    {
+        freeAllocatedSingleString(*url);
+        *url = NULL;
+    }
+
+    if (dest && *dest)
     {
-        freeAllocatedSingleString(url);
+        freeAllocatedSingleString(*dest);
+        *dest = NULL;
     }
 
-    if (dest != NULL)
+    if (username && *username)
     {
-        freeAllocatedSingleString(dest);
+        freeAllocatedSingleString(*username);
+        *username = NULL;
     }
 
-    if (username != NULL)
+    if (password && *password)
     {
-        freeAllocatedSingleString(username);
+        freeAllocatedSingleString(*password);
+        *password = NULL;
     }
 
-    if (password != NULL)
+    if (content && *content)
     {
-        freeAllocatedSingleString(password);
+        FREE(*content);
+        *content = NULL;
     }
 
-    if (content != NULL)
+    if (filePath && *filePath)
     {
-        freeAllocatedSingleString(content);
+        FREE(*filePath);
+        *filePath = NULL;
     }
 }
 
index 86d2c89..c32cb6b 100644 (file)
@@ -59,6 +59,7 @@ int sci_getrelativefilename(char *fname, unsigned long l)
     }
     if (getAllocatedMatrixOfString(pvApiCtx, piAddr1, &iRows1, &iCols1, &Input_StringMatrix_1))
     {
+        freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
         Scierror(999, _("%s: Wrong type for input argument #%d: A matrix of strings expected.\n"), fname, 1);
         return 0;
     }
@@ -67,6 +68,7 @@ int sci_getrelativefilename(char *fname, unsigned long l)
     sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);
     if (sciErr.iErr)
     {
+        freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
         printError(&sciErr, 0);
         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
         return 0;
@@ -74,11 +76,14 @@ int sci_getrelativefilename(char *fname, unsigned long l)
     // Check type
     if (isStringType(pvApiCtx, piAddr2) == 0)
     {
+        freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
         Scierror(999, _("%s: Wrong type for input argument #%d: A matrix of strings expected.\n"), fname, 2);
         return 0;
     }
     if (getAllocatedMatrixOfString(pvApiCtx, piAddr2, &iRows2, &iCols2, &Input_StringMatrix_2))
     {
+        freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
+        freeAllocatedMatrixOfString(iRows2, iCols2, Input_StringMatrix_2);
         Scierror(999, _("%s: Wrong type for input argument #%d: A matrix of strings expected.\n"), fname, 2);
         return 0;
     }
@@ -102,42 +107,43 @@ int sci_getrelativefilename(char *fname, unsigned long l)
 
     for ( x = 0; x < iRows1 * iCols1; x++)
     {
-        OutputStrings[x] = (char*)MALLOC(PATH_MAX * sizeof(char*));
-        if (OutputStrings[x] == NULL)
-        {
-            freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
-            freeAllocatedMatrixOfString(iRows2, iCols2, Input_StringMatrix_2);
-            freeAllocatedMatrixOfString(iRows1, iCols1, OutputStrings);
-            Scierror(112, _("%s: No more memory.\n"), fname);
-            return 0;
-        }
         // make sure the names are not too long
         if ( strlen(Input_StringMatrix_1[x]) > PATH_MAX )
         {
+            freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
+            freeAllocatedMatrixOfString(iRows2, iCols2, Input_StringMatrix_2);
+            freeArrayOfString(OutputStrings, iRows1 * iCols1);
             Scierror(999, _("%s: Wrong size for input argument #%d: Must be less than %d characters.\n"), fname, 1, PATH_MAX);
+            return 0;
         }
+
         if ( strlen(Input_StringMatrix_2[x]) > PATH_MAX )
         {
+            freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
+            freeAllocatedMatrixOfString(iRows2, iCols2, Input_StringMatrix_2);
+            freeArrayOfString(OutputStrings, iRows1 * iCols1);
             Scierror(999, _("%s: Wrong size for input argument #%d: Must be less than %d characters.\n"), fname, 2, PATH_MAX);
+            return 0;
         }
+
         OutputStrings[x] = getrelativefilename(Input_StringMatrix_1[x], Input_StringMatrix_2[x]);
     }
 
+    freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
+    freeAllocatedMatrixOfString(iRows2, iCols2, Input_StringMatrix_2);
+
     sciErr = createMatrixOfString(pvApiCtx, iRhs + 1, iRows1, iCols1, (char**)OutputStrings);
     if (sciErr.iErr)
     {
+        freeArrayOfString(OutputStrings, iRows1 * iCols1);
         printError(&sciErr, 0);
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
 
-    AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
-
-    freeAllocatedMatrixOfString(iRows1, iCols1, OutputStrings);
-    freeAllocatedMatrixOfString(iRows1, iCols1, Input_StringMatrix_1);
-    freeAllocatedMatrixOfString(iRows2, iCols2, Input_StringMatrix_2);
+    freeArrayOfString(OutputStrings, iRows1 * iCols1);
 
+    AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
     ReturnArguments(pvApiCtx);
-
     return 0;
 }
index 960d460..e66556f 100644 (file)
@@ -26,8 +26,6 @@ int sci_isfile(char *fname, unsigned long fname_len)
     SciErr sciErr;
     int *piAddressVarOne = NULL;
     wchar_t **pStVarOne = NULL;
-    int iType = 0;
-    int *lenStVarOne = NULL;
     int m1 = 0, n1 = 0;
 
     BOOL *results = NULL;
@@ -45,100 +43,19 @@ int sci_isfile(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType);
-    if (sciErr.iErr)
-    {
-        printError(&sciErr, 0);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-        return 0;
-    }
-
-    if (iType != sci_strings)
-    {
-        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
-        return 0;
-    }
-
-    sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
-    if (sciErr.iErr)
+    if (getAllocatedMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, &pStVarOne))
     {
         printError(&sciErr, 0);
         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-        return 0;
-    }
-
-    lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1));
-    if (lenStVarOne == NULL)
-    {
-        Scierror(999, _("%s: Memory allocation error.\n"), fname);
-        return 0;
-    }
-
-    results = (BOOL*)MALLOC(sizeof(BOOL) * (m1 * n1));
-    if (results == NULL)
-    {
-        if (lenStVarOne)
-        {
-            FREE(lenStVarOne);
-            lenStVarOne = NULL;
-        }
-        Scierror(999, _("%s: Memory allocation error.\n"), fname);
-        return 0;
-    }
-
-    sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL);
-    if (sciErr.iErr)
-    {
-        printError(&sciErr, 0);
         FREE(results);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
         return 0;
     }
 
-    pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1));
-    if (pStVarOne == NULL)
-    {
-        if (lenStVarOne)
-        {
-            FREE(lenStVarOne);
-            lenStVarOne = NULL;
-        }
-        FREE(results);
-        Scierror(999, _("%s: Memory allocation error.\n"), fname);
-        return 0;
-    }
-
-    for (i = 0; i < m1 * n1; i++)
-    {
-        pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1));
-        if (pStVarOne[i] == NULL)
-        {
-            if (i == 0)
-            {
-                FREE(pStVarOne);
-            }
-            else
-            {
-                freeArrayOfWideString(pStVarOne, i - 1);
-            }
-            if (lenStVarOne)
-            {
-                FREE(lenStVarOne);
-                lenStVarOne = NULL;
-            }
-            FREE(results);
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
-    }
-
-    sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
+    sciErr = allocMatrixOfBoolean(pvApiCtx, Rhs + 1, m1, n1, &results);
     if (sciErr.iErr)
     {
         printError(&sciErr, 0);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-        freeArrayOfWideString(pStVarOne, m1 * n1);
-        FREE(results);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
 
@@ -157,30 +74,9 @@ int sci_isfile(char *fname, unsigned long fname_len)
         }
     }
 
-    if (lenStVarOne)
-    {
-        FREE(lenStVarOne);
-        lenStVarOne = NULL;
-    }
-    freeArrayOfWideString(pStVarOne, m1 * n1);
-
-    sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m1, n1, results);
-    if (sciErr.iErr)
-    {
-        printError(&sciErr, 0);
-        Scierror(999, _("%s: Memory allocation error.\n"), fname);
-        FREE(results);
-        return 0;
-    }
+    freeAllocatedMatrixOfWideString(m1, n1, pStVarOne);
 
     LhsVar(1) = Rhs + 1;
-
-    if (results)
-    {
-        FREE(results);
-        results = NULL;
-    }
-
     PutLhsVar();
     return 0;
 }
index 883473a..615aa6a 100644 (file)
@@ -29,11 +29,18 @@ int sci_mgetl(char *fname, unsigned long fname_len)
     int *piAddressVarOne = NULL;
     int numberOfLinesToRead = -1;
 
+    char **wcReadStrings = NULL;
+    int numberOfLinesRead = 0;
+    int fileDescriptor = 0;
+    int iErrorMgetl = 0;
+    BOOL bCloseFile = FALSE;
+
     Rhs = Max(0, Rhs);
 
     CheckRhs(1, 2);
     CheckLhs(1, 1);
 
+
     if (Rhs == 2)
     {
         int *piAddressVarTwo = NULL;
@@ -80,254 +87,205 @@ int sci_mgetl(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    if ( isStringType(pvApiCtx, piAddressVarOne) || isDoubleType(pvApiCtx, piAddressVarOne) )
+    if (isStringType(pvApiCtx, piAddressVarOne) == FALSE &&  isDoubleType(pvApiCtx, piAddressVarOne) == FALSE)
     {
-        char **wcReadedStrings = NULL;
-        int numberOfLinesReaded = 0;
-        int fileDescriptor = -1;
-        int iErrorMgetl = 0;
-        BOOL bCloseFile = FALSE;
+        Scierror(999, _("%s: Wrong type for input argument #%d: String or logical unit expected.\n"), fname, 1);
+        return 0;
+    }
 
-        if (!isScalar(pvApiCtx, piAddressVarOne))
-        {
-            Scierror(999, _("%s: Wrong size for input argument #%d: String or logical unit expected.\n"), fname, 1);
-            return 0;
-        }
+    if (!isScalar(pvApiCtx, piAddressVarOne))
+    {
+        Scierror(999, _("%s: Wrong size for input argument #%d: String or logical unit expected.\n"), fname, 1);
+        return 0;
+    }
 
-        if (isStringType(pvApiCtx, piAddressVarOne))
+    if (isStringType(pvApiCtx, piAddressVarOne))
+    {
+        char *fileName = NULL;
+        if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &fileName) == 0)
         {
-            char *fileName = NULL;
-            if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &fileName) == 0)
+            char *expandedFileName = expandPathVariable(fileName);
+            freeAllocatedSingleString(fileName);
+
+            if (IsAlreadyOpenedInScilab(expandedFileName))
+            {
+                int fd = GetIdFromFilename(expandedFileName);
+                fileDescriptor = fd;
+                FREE(expandedFileName);
+                bCloseFile = FALSE;
+            }
+            else
             {
-                char *expandedFileName = expandPathVariable(fileName);
-                freeAllocatedSingleString(fileName);
-                fileName = NULL;
+#define READ_ONLY_TEXT_MODE "rt"
+                int fd = 0;
+                int f_swap = 0;
+                double res = 0.0;
+                int ierr = 0;
+
+                C2F(mopen)(&fd, expandedFileName, READ_ONLY_TEXT_MODE, &f_swap, &res, &ierr);
+                bCloseFile = TRUE;
 
-                if (IsAlreadyOpenedInScilab(expandedFileName))
+                switch (ierr)
                 {
-                    int fd = GetIdFromFilename(expandedFileName);
-                    fileDescriptor = fd;
-                    if (expandedFileName)
+                    case MOPEN_NO_ERROR:
+                        fileDescriptor = fd;
+                        FREE(expandedFileName);
+                        break;
+                    case MOPEN_NO_MORE_LOGICAL_UNIT:
                     {
+                        Scierror(66, _("%s: Too many files opened!\n"), fname);
                         FREE(expandedFileName);
-                        expandedFileName = NULL;
+                        return 0;
                     }
-                    bCloseFile = FALSE;
-                }
-                else
-                {
-#define READ_ONLY_TEXT_MODE "rt"
-                    int fd = 0;
-                    int f_swap = 0;
-                    double res = 0.0;
-                    int ierr = 0;
-
-                    C2F(mopen)(&fd, expandedFileName, READ_ONLY_TEXT_MODE, &f_swap, &res, &ierr);
-                    bCloseFile = TRUE;
-
-                    switch (ierr)
+                    break;
+                    case MOPEN_CAN_NOT_OPEN_FILE:
                     {
-                        case MOPEN_NO_ERROR:
-                            fileDescriptor = fd;
-                            if (expandedFileName)
-                            {
-                                FREE(expandedFileName);
-                                expandedFileName = NULL;
-                            }
-                            break;
-                        case MOPEN_NO_MORE_LOGICAL_UNIT:
-                        {
-                            Scierror(66, _("%s: Too many files opened!\n"), fname);
-                            if (expandedFileName)
-                            {
-                                FREE(expandedFileName);
-                                expandedFileName = NULL;
-                            }
-                            return 0;
-                        }
-                        break;
-                        case MOPEN_CAN_NOT_OPEN_FILE:
-                        {
-                            Scierror(999, _("%s: Cannot open file %s.\n"), fname, expandedFileName);
-                            if (expandedFileName)
-                            {
-                                FREE(expandedFileName);
-                                expandedFileName = NULL;
-                            }
-                            return 0;
-                        }
-                        break;
-                        case MOPEN_NO_MORE_MEMORY:
-                        {
-                            if (expandedFileName)
-                            {
-                                FREE(expandedFileName);
-                                expandedFileName = NULL;
-                            }
-                            Scierror(999, _("%s: No more memory.\n"), fname);
-                            return 0;
-                        }
-                        break;
-                        case MOPEN_INVALID_FILENAME:
-                        {
-                            Scierror(999, _("%s: invalid filename %s.\n"), fname, expandedFileName);
-                            if (expandedFileName)
-                            {
-                                FREE(expandedFileName);
-                                expandedFileName = NULL;
-                            }
-                            return 0;
-                        }
-                        break;
-                        case MOPEN_INVALID_STATUS:
-                        default:
-                        {
-                            if (expandedFileName)
-                            {
-                                FREE(expandedFileName);
-                                expandedFileName = NULL;
-                            }
-                            Scierror(999, _("%s: invalid status.\n"), fname);
-                            return 0;
-                        }
-                        break;
+                        Scierror(999, _("%s: Cannot open file %s.\n"), fname, expandedFileName);
+                        FREE(expandedFileName);
+                        return 0;
+                    }
+                    break;
+                    case MOPEN_NO_MORE_MEMORY:
+                    {
+                        FREE(expandedFileName);
+                        Scierror(999, _("%s: No more memory.\n"), fname);
+                        return 0;
+                    }
+                    break;
+                    case MOPEN_INVALID_FILENAME:
+                    {
+                        Scierror(999, _("%s: invalid filename %s.\n"), fname, expandedFileName);
+                        FREE(expandedFileName);
+                        return 0;
+                    }
+                    break;
+                    case MOPEN_INVALID_STATUS:
+                    default:
+                    {
+                        FREE(expandedFileName);
+                        Scierror(999, _("%s: invalid status.\n"), fname);
+                        return 0;
                     }
+                    break;
                 }
             }
-            else
-            {
-                Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                return 0;
-            }
         }
-        else /* double */
+        else
         {
-            double dValue = 0.;
-
-            if ( !getScalarDouble(pvApiCtx, piAddressVarOne, &dValue) )
-            {
-                FILE *fd = NULL;
-                fileDescriptor = (int)dValue;
-                if ((fileDescriptor == STDIN_ID) || (fileDescriptor == STDOUT_ID))
-                {
-                    SciError(244);
-                    return 0;
-                }
-
-                fd = GetFileOpenedInScilab(fileDescriptor);
-                if (fd == NULL)
-                {
-                    Scierror(245, _("%s: No input file associated to logical unit %d.\n"), fname, fileDescriptor);
-                    return 0;
-                }
-            }
-            else
-            {
-                Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                return 0;
-            }
+            Scierror(999, _("%s: Memory allocation error.\n"), fname);
+            return 0;
         }
+    }
+    else /* double */
+    {
+        double dValue = 0.;
+        FILE *fd = NULL;
 
-        wcReadedStrings = mgetl(fileDescriptor, numberOfLinesToRead, &numberOfLinesReaded, &iErrorMgetl);
+        if (getScalarDouble(pvApiCtx, piAddressVarOne, &dValue) )
+        {
+            Scierror(999, _("%s: Memory allocation error.\n"), fname);
+            return 0;
+        }
 
-        if (bCloseFile)
+        fileDescriptor = (int)dValue;
+        if ((fileDescriptor == STDIN_ID) || (fileDescriptor == STDOUT_ID))
         {
-            double dErrClose = 0.;
-            C2F(mclose)(&fileDescriptor, &dErrClose);
+            SciError(244);
+            return 0;
         }
 
-        switch (iErrorMgetl)
+        fd = GetFileOpenedInScilab(fileDescriptor);
+        if (fd == NULL)
         {
-            case MGETL_NO_ERROR:
-            {
-                if (numberOfLinesReaded == 0)
-                {
-                    if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
-                    {
-                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                        return 0;
-                    }
-                }
-                else
-                {
-                    int m = numberOfLinesReaded;
-                    int n = 1;
+            Scierror(245, _("%s: No input file associated to logical unit %d.\n"), fname, fileDescriptor);
+            return 0;
+        }
+    }
 
-                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m, n, wcReadedStrings);
-                    if (sciErr.iErr)
-                    {
-                        printError(&sciErr, 0);
-                        Scierror(17, _("%s: Memory allocation error.\n"), fname);
-                        return 0;
-                    }
-                }
+    wcReadStrings = mgetl(fileDescriptor, numberOfLinesToRead, &numberOfLinesRead, &iErrorMgetl);
 
-                freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
-                wcReadedStrings = NULL;
-            }
-            break;
+    if (bCloseFile)
+    {
+        double dErrClose = 0.;
+        C2F(mclose)(&fileDescriptor, &dErrClose);
+    }
 
-            case MGETL_EOF:
+    switch (iErrorMgetl)
+    {
+        case MGETL_NO_ERROR:
+        {
+            if (numberOfLinesRead == 0)
             {
-                if (numberOfLinesReaded == 0)
+                if (createEmptyMatrix(pvApiCtx, Rhs + 1))
                 {
-                    if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
-                    {
-                        Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                        return 0;
-                    }
+                    freeArrayOfString(wcReadStrings, numberOfLinesRead);
+                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                    return 0;
                 }
-                else
+            }
+            else
+            {
+                sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, numberOfLinesRead, 1, wcReadStrings);
+                if (sciErr.iErr)
                 {
-                    int m = numberOfLinesReaded;
-                    int n = 1;
-
-                    sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m, n, wcReadedStrings);
-                    if (sciErr.iErr)
-                    {
-                        printError(&sciErr, 0);
-                        Scierror(17, _("%s: Memory allocation error.\n"), fname);
-                        return 0;
-                    }
-                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
-                    wcReadedStrings = NULL;
+                    freeArrayOfString(wcReadStrings, numberOfLinesRead);
+                    printError(&sciErr, 0);
+                    Scierror(17, _("%s: Memory allocation error.\n"), fname);
+                    return 0;
                 }
             }
-            break;
 
-            case MGETL_MEMORY_ALLOCATION_ERROR:
+            freeArrayOfString(wcReadStrings, numberOfLinesRead);
+        }
+        break;
+
+        case MGETL_EOF:
+        {
+            if (numberOfLinesRead == 0)
             {
-                if (wcReadedStrings)
+                if (createEmptyMatrix(pvApiCtx, Rhs + 1))
                 {
-                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
-                    wcReadedStrings = NULL;
+                    freeArrayOfString(wcReadStrings, numberOfLinesRead);
+                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
+                    return 0;
                 }
-                Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                return 0;
             }
-            break;
-
-            case MGETL_ERROR:
+            else
             {
-                if (wcReadedStrings)
+                sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, numberOfLinesRead, 1, wcReadStrings);
+                if (sciErr.iErr)
                 {
-                    freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
-                    wcReadedStrings = NULL;
+                    freeArrayOfString(wcReadStrings, numberOfLinesRead);
+                    printError(&sciErr, 0);
+                    Scierror(17, _("%s: Memory allocation error.\n"), fname);
+                    return 0;
                 }
-                Scierror(999, _("%s: error.\n"), fname);
-                return 0;
             }
-            break;
+
+            freeArrayOfString(wcReadStrings, numberOfLinesRead);
         }
+        break;
 
-        LhsVar(1) = Rhs + 1;
-        PutLhsVar();
-    }
-    else
-    {
-        Scierror(999, _("%s: Wrong type for input argument #%d: String or logical unit expected.\n"), fname, 1);
+        case MGETL_MEMORY_ALLOCATION_ERROR:
+        {
+            freeArrayOfString(wcReadStrings, numberOfLinesRead);
+            Scierror(999, _("%s: Memory allocation error.\n"), fname);
+            return 0;
+        }
+        break;
+
+        case MGETL_ERROR:
+        {
+            freeArrayOfString(wcReadStrings, numberOfLinesRead);
+            Scierror(999, _("%s: error.\n"), fname);
+            return 0;
+        }
+        break;
     }
 
+    LhsVar(1) = Rhs + 1;
+    PutLhsVar();
+
     return 0;
 }
 /*--------------------------------------------------------------------------*/
index 80a4673..ab2e0e7 100644 (file)
@@ -57,19 +57,12 @@ int sci_movefile(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    if (!isStringType(pvApiCtx, piAddressVarOne))
+    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;
     }
 
-    if (!isScalar(pvApiCtx, piAddressVarOne))
-    {
-        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 1);
-        return 0;
-    }
-
-
     sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
     if (sciErr.iErr)
     {
@@ -78,28 +71,12 @@ int sci_movefile(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    if (!isStringType(pvApiCtx, piAddressVarTwo))
+    if (isStringType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0)
     {
-        if (pStVarOne)
-        {
-            FREE(pStVarOne);
-            pStVarOne = NULL;
-        }
         Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
         return 0;
     }
 
-    if (!isScalar(pvApiCtx, piAddressVarTwo))
-    {
-        if (pStVarOne)
-        {
-            FREE(pStVarOne);
-            pStVarOne = NULL;
-        }
-        Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 2);
-        return 0;
-    }
-
     if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &pStVarOne))
     {
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
@@ -108,11 +85,8 @@ int sci_movefile(char *fname, unsigned long fname_len)
 
     if (getAllocatedSingleWideString(pvApiCtx, piAddressVarTwo, &pStVarTwo))
     {
-        if (pStVarOne)
-        {
-            freeAllocatedSingleWideString(pStVarOne);
-            pStVarOne = NULL;
-        }
+        freeAllocatedSingleWideString(pStVarOne);
+        freeAllocatedSingleWideString(pStVarTwo);
         Scierror(999, _("%s: Memory allocation error.\n"), fname);
         return 0;
     }
@@ -120,11 +94,8 @@ int sci_movefile(char *fname, unsigned long fname_len)
     pStVarOneExpanded = expandPathVariableW(pStVarOne);
     pStVarTwoExpanded = expandPathVariableW(pStVarTwo);
 
-    freeAllocatedSingleWideString(pStVarTwo);
-    pStVarTwo = NULL;
-
     freeAllocatedSingleWideString(pStVarOne);
-    pStVarOne = NULL;
+    freeAllocatedSingleWideString(pStVarTwo);
 
     if (isdirW(pStVarOneExpanded) || FileExistW(pStVarOneExpanded))
     {
@@ -241,48 +212,33 @@ static wchar_t *getFilenameWithExtensionForMove(wchar_t * wcFullFilename)
 
         wcfilename = (wchar_t *) MALLOC(sizeof(wchar_t) * ((int)wcslen(wcFullFilename) + 1));
 
-        splitpathW(wcFullFilename, FALSE, wcdrv, wcdir, wcname, wcext);
-
-        wcscpy(wcfilename, wcname);
-        wcscat(wcfilename, wcext);
-
-        if (wcdrv)
+        if (wcdrv == NULL || wcdir == NULL || wcname == NULL || wcext == NULL || wcfilename == NULL)
         {
             FREE(wcdrv);
-            wcdrv = NULL;
-        }
-        if (wcdir)
-        {
             FREE(wcdir);
-            wcdir = NULL;
-        }
-        if (wcname)
-        {
             FREE(wcname);
-            wcname = NULL;
-        }
-        if (wcext)
-        {
             FREE(wcext);
-            wcext = NULL;
+            FREE(wcfilename);
+            return NULL;
         }
+
+        splitpathW(wcFullFilename, FALSE, wcdrv, wcdir, wcname, wcext);
+
+        wcscpy(wcfilename, wcname);
+        wcscat(wcfilename, wcext);
+
+        FREE(wcdrv);
+        FREE(wcdir);
+        FREE(wcname);
+        FREE(wcext);
     }
     return wcfilename;
 }
-
 /*--------------------------------------------------------------------------*/
 static int returnMoveFileResultOnStack(int ierr, char *fname)
 {
     double dError = 0.;
-    wchar_t **sciError = NULL;
-    int m_out = 1, n_out = 1;
-
-    sciError = (wchar_t **) MALLOC(sizeof(wchar_t *) * 1);
-    if (sciError == NULL)
-    {
-        Scierror(999, _("%s: Memory allocation error.\n"), fname);
-        return 0;
-    }
+    wchar_t *sciError = NULL;
 
 #ifdef _MSC_VER
     if (ierr)
@@ -301,25 +257,25 @@ static int returnMoveFileResultOnStack(int ierr, char *fname)
         //dError = (double) dw;
         dError = (double)0;
 
-        sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * ((int)wcslen(buffer) + 1));
-        if (sciError[0] == NULL)
+        sciError = (wchar_t *) MALLOC(sizeof(wchar_t) * ((int)wcslen(buffer) + 1));
+        if (sciError == NULL)
         {
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
 
-        wcscpy(sciError[0], buffer);
+        wcscpy(sciError, buffer);
     }
     else
     {
         dError = 1.;
-        sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * 1);
-        if (sciError[0] == NULL)
+        sciError = (wchar_t *) MALLOC(sizeof(wchar_t) * 1);
+        if (sciError == NULL)
         {
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
-        wcscpy(sciError[0], L"");
+        wcscpy(sciError, L"");
     }
 #else
     if (ierr)
@@ -328,8 +284,8 @@ static int returnMoveFileResultOnStack(int ierr, char *fname)
         //dError = (double) ierr;
         dError = (double)0.;
 
-        sciError[0] = to_wide_string(strerror(errno));
-        if (sciError[0] == NULL)
+        sciError = to_wide_string(strerror(errno));
+        if (sciError == NULL)
         {
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
@@ -338,27 +294,26 @@ static int returnMoveFileResultOnStack(int ierr, char *fname)
     else
     {
         dError = 1.;
-        sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * 1);
-        if (sciError[0] == NULL)
+        sciError = (wchar_t *) MALLOC(sizeof(wchar_t) * 1);
+        if (sciError == NULL)
         {
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            freeArrayOfWideString(sciError, 1);
             return 0;
         }
-        wcscpy(sciError[0], L"");
+        wcscpy(sciError, L"");
     }
 #endif
 
-    createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, &dError);
+    createScalarDouble(pvApiCtx, Rhs + 1, dError);
     LhsVar(1) = Rhs + 1;
 
     if (Lhs == 2)
     {
-        createMatrixOfWideString(pvApiCtx, Rhs + 2, m_out, n_out, sciError);
+        createSingleWideString(pvApiCtx, Rhs + 2, sciError);
         LhsVar(2) = Rhs + 2;
     }
 
-    freeArrayOfWideString(sciError, 1);
+    FREE(sciError);
 
     PutLhsVar();
     return 0;
index 92775eb..a115481 100644 (file)
@@ -29,13 +29,13 @@ int sci_mputl(char *fname, unsigned long fname_len)
     int *piAddressVarOne = NULL;
     int *piAddressVarTwo = NULL;
 
-    char **pStVarOne                   = NULL;
-    int *lenStVarOne                   = NULL;
+    char **pStVarOne = NULL;
+    int *lenStVarOne = NULL;
     int mOne = 0, nOne = 0;
     int mnOne = 0;
 
     char *filename = NULL;
-    int fileDescriptor = -1;
+    int fileDescriptor = 0;
     BOOL bCloseFile = FALSE;
 
     int i = 0;
@@ -61,7 +61,7 @@ int sci_mputl(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    if ( isDoubleType(pvApiCtx, piAddressVarTwo) )
+    if (isDoubleType(pvApiCtx, piAddressVarTwo))
     {
         double dValue = 0.;
 
@@ -71,17 +71,15 @@ int sci_mputl(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        if ( getScalarDouble(pvApiCtx, piAddressVarTwo, &dValue) == 0)
-        {
-            fileDescriptor = (int)dValue;
-        }
-        else
+        if (getScalarDouble(pvApiCtx, piAddressVarTwo, &dValue))
         {
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
             return 0;
         }
+
+        fileDescriptor = (int)dValue;
     }
-    else if ( isStringType(pvApiCtx, piAddressVarTwo) )
+    else if (isStringType(pvApiCtx, piAddressVarTwo))
     {
         if (!isScalar(pvApiCtx, piAddressVarTwo))
         {
@@ -98,11 +96,7 @@ int sci_mputl(char *fname, unsigned long fname_len)
             char *expandedFileName = expandPathVariable(filename);
 
             C2F(mopen)(&fileDescriptor, expandedFileName, WRITE_ONLY_TEXT_MODE, &f_swap, &res, &ierr);
-            if (expandedFileName)
-            {
-                FREE(expandedFileName);
-                expandedFileName = NULL;
-            }
+            FREE(expandedFileName);
 
             switch (ierr)
             {
@@ -164,6 +158,7 @@ int sci_mputl(char *fname, unsigned long fname_len)
     else
     {
         Scierror(999, _("%s: Wrong type for input argument #%d: a String or Integer expected.\n"), fname, 2);
+        return 0;
     }
 
     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
@@ -180,74 +175,19 @@ int sci_mputl(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, NULL, NULL);
-    if (sciErr.iErr)
-    {
-        printError(&sciErr, 0);
-        return 0;
-    }
-
-    if ( !((mOne == 1) || (nOne == 1)) )
+    if (isScalar(pvApiCtx, piAddressVarOne) == FALSE && isVector(pvApiCtx, piAddressVarOne) == FALSE)
     {
         Scierror(999, _("%s: Wrong size for input argument #%d: A 1-by-n or m-by-1 array expected.\n"), fname, 1);
         return 0;
     }
 
-    mnOne = mOne * nOne;
-
-    lenStVarOne = (int*)MALLOC(sizeof(int) * mnOne);
-    if (lenStVarOne == NULL)
-    {
-        Scierror(999, _("%s: No more memory.\n"), fname);
-        return 0;
-    }
-
-    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, NULL);
-    if (sciErr.iErr)
+    if (getAllocatedMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, &pStVarOne))
     {
-        printError(&sciErr, 0);
         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
         return 0;
     }
 
-    pStVarOne = (char**) MALLOC(sizeof(char*) * mnOne);
-    if (pStVarOne == NULL)
-    {
-        FREE(lenStVarOne);
-        lenStVarOne = NULL;
-        Scierror(999, _("%s: No more memory.\n"), fname);
-        return 0;
-    }
-
-    for (i = 0; i < mnOne; i++)
-    {
-        pStVarOne[i] = (char*)MALLOC(sizeof(char) * (lenStVarOne[i] + 1));
-        if (pStVarOne[i] == NULL)
-        {
-            freeArrayOfString(pStVarOne, i);
-            if (lenStVarOne)
-            {
-                FREE(lenStVarOne);
-                lenStVarOne = NULL;
-            }
-            Scierror(999, _("%s: No more memory.\n"), fname);
-            return 0;
-        }
-    }
-
-    sciErr = getMatrixOfString(pvApiCtx, piAddressVarOne, &mOne, &nOne, lenStVarOne, pStVarOne);
-    if (lenStVarOne)
-    {
-        FREE(lenStVarOne);
-        lenStVarOne = NULL;
-    }
-    if (sciErr.iErr)
-    {
-        freeArrayOfString(pStVarOne, mnOne);
-        printError(&sciErr, 0);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-        return 0;
-    }
+    mnOne = mOne * nOne;
 
     mputlErr = mputl(fileDescriptor, pStVarOne, mnOne);
     freeArrayOfString(pStVarOne, mnOne);
index be8aa0e..8a8382e 100644 (file)
@@ -33,7 +33,8 @@ int sci_scanf(char *fname, unsigned long fname_len)
     int args = 0, retval = 0, retval_s = 0, lline = 0, iflag = 0, err = 0, n_count = 0;
     entry *data = NULL;
     rec_entry buf[MAXSCAN];
-    sfdir type[MAXSCAN], type_s[MAXSCAN];
+    sfdir type[MAXSCAN] = {SF_C};
+    sfdir type_s[MAXSCAN] = {SF_C};
 
     Nbvars = 0;
     CheckRhs(1, 2);
index a2653af..0f32f43 100644 (file)
@@ -31,7 +31,7 @@ int sci_sscanf(char *fname, unsigned long fname_len)
 
     entry *data = NULL;
     rec_entry buf[MAXSCAN];
-    sfdir type[MAXSCAN], type_s[MAXSCAN];
+    sfdir type[MAXSCAN] = {SF_C}, type_s[MAXSCAN] = {SF_C};
     char *str = NULL;
 
     Nbvars = 0;
@@ -84,7 +84,17 @@ int sci_sscanf(char *fname, unsigned long fname_len)
             break;
         }
         skip = *istk(ild1 + k) - 1;
-        SciStrtoStr(istk(il1 + skip), &n_count, istk(ild1 + k), &str);
+
+        if (SciStrtoStr(istk(il1 + skip), &n_count, istk(ild1 + k), &str) == MEM_LACK)
+        {
+            return 0;
+        }
+
+        if (str == NULL)
+        {
+            return 0;
+        }
+
         k += n_count;
 
         args = Rhs;             /* args set to Rhs on entry */
index 4e21d6d..97fdde7 100644 (file)
@@ -23,8 +23,6 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
     SciErr sciErr;
     int *piAddressVarOne = NULL;
     wchar_t **pStVarOne = NULL;
-    int iType1 = 0;
-    int *lenStVarOne = NULL;
     int m1 = 0, n1 = 0;
 
     wchar_t **results = NULL;
@@ -32,7 +30,6 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
 
     BOOL flagtrail = TRUE;
     BOOL flagexpand = TRUE;
-
     PathConvertType PType = AUTO_STYLE;
 
     /* Check Input & Output parameters */
@@ -43,9 +40,6 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
     {
         int *piAddressVarFour = NULL;
         wchar_t *pStVarFour = NULL;
-        int iType4 = 0;
-        int lenStVarFour = 0;
-        int m4 = 0, n4 = 0;
 
         sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour);
         if (sciErr.iErr)
@@ -55,53 +49,14 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        sciErr = getVarType(pvApiCtx, piAddressVarFour, &iType4);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
-            return 0;
-        }
-
-        if (iType4 != sci_strings)
+        if (isStringType(pvApiCtx, piAddressVarFour) == 0 || isScalar(pvApiCtx, piAddressVarFour) == 0)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 4);
             return 0;
         }
 
-        sciErr = getVarDimension(pvApiCtx, piAddressVarFour, &m4, &n4);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
-            return 0;
-        }
-
-        if ((m4 != n4) && (n4 != 1))
-        {
-            Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), fname, 4);
-            return 0;
-        }
-
-        sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarFour, &m4, &n4, &lenStVarFour, NULL);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
-            return 0;
-        }
-
-        pStVarFour = (wchar_t *) MALLOC(sizeof(wchar_t) * (lenStVarFour + 1));
-        if (pStVarFour == NULL)
-        {
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
-
-        sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarFour, &m4, &n4, &lenStVarFour, &pStVarFour);
-        if (sciErr.iErr)
+        if (getAllocatedSingleWideString(pvApiCtx, piAddressVarFour, &pStVarFour))
         {
-            printError(&sciErr, 0);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4);
             return 0;
         }
@@ -116,19 +71,12 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
             {
                 PType = UNIX_STYLE;
             }
-            if (pStVarFour)
-            {
-                FREE(pStVarFour);
-                pStVarFour = NULL;
-            }
+
+            freeAllocatedSingleWideString(pStVarFour);
         }
         else
         {
-            if (pStVarFour)
-            {
-                FREE(pStVarFour);
-                pStVarFour = NULL;
-            }
+            freeAllocatedSingleWideString(pStVarFour);
             Scierror(999, _("%s: Wrong value for input argument #%d: 'w' or 'u' string expected.\n"), fname, 4);
             return 0;
         }
@@ -137,10 +85,6 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
     if (Rhs > 2)
     {
         int *piAddressVarThree = NULL;
-        int *piData = NULL;
-        int iType3 = 0;
-        int m3 = 0, n3 = 0;
-
         sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
         if (sciErr.iErr)
         {
@@ -149,52 +93,22 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
-            return 0;
-        }
-
-        if (iType3 != sci_boolean)
+        if (isBooleanType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 3);
             return 0;
         }
 
-        sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
-            return 0;
-        }
-
-        if ((m3 != n3) && (n3 != 1))
+        if (getScalarBoolean(pvApiCtx, piAddressVarThree, &flagexpand))
         {
             Scierror(999, _("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 3);
             return 0;
         }
-
-        sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarThree, &m3, &n3, &piData);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
-            return 0;
-        }
-
-        flagexpand = piData[0];
     }
 
     if (Rhs > 1)
     {
         int *piAddressVarTwo = NULL;
-        int *piData = NULL;
-        int iType2 = 0;
-        int m2 = 0, n2 = 0;
-
         sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
         if (sciErr.iErr)
         {
@@ -203,43 +117,17 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
             return 0;
         }
 
-        sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
-            return 0;
-        }
-
-        if (iType2 != sci_boolean)
+        if (isBooleanType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0)
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 2);
             return 0;
         }
 
-        sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
-            return 0;
-        }
-
-        if ((m2 != n2) && (n2 != 1))
+        if (getScalarBoolean(pvApiCtx, piAddressVarTwo, &flagtrail))
         {
             Scierror(999, _("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 2);
             return 0;
         }
-
-        sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2, &piData);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
-            return 0;
-        }
-
-        flagtrail = piData[0];
     }
 
     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
@@ -250,114 +138,41 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
         return 0;
     }
 
-    sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
-    if (sciErr.iErr)
-    {
-        printError(&sciErr, 0);
-        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-        return 0;
-    }
-
-    if (iType1 == sci_matrix)
+    if (isDoubleType(pvApiCtx, piAddressVarOne))
     {
-        sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-            return 0;
-        }
-
-        if ((m1 == n1) && (m1 == 0))
+        if (isEmptyMatrix(pvApiCtx, piAddressVarOne))
         {
-            sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, NULL);
-            if (sciErr.iErr)
+            if (createEmptyMatrix(pvApiCtx, Rhs + 1))
             {
-                printError(&sciErr, 0);
                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
                 return 0;
             }
 
             LhsVar(1) = Rhs + 1;
             PutLhsVar();
+            return 0;
         }
         else
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1);
+            return 0;
         }
     }
-    else if (iType1 == sci_strings)
+    else if (isStringType(pvApiCtx, piAddressVarOne))
     {
-        sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
-        if (sciErr.iErr)
+
+        if (getAllocatedMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, &pStVarOne))
         {
             printError(&sciErr, 0);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
             return 0;
         }
 
-        lenStVarOne = (int *)MALLOC(sizeof(int) * (m1 * n1));
-        if (lenStVarOne == NULL)
-        {
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
-
         results = (wchar_t **) MALLOC(sizeof(wchar_t *) * (m1 * n1));
-
         if (results == NULL)
         {
-            FREE(lenStVarOne);
-            lenStVarOne = NULL;
-            freeArrayOfWideString(pStVarOne, m1 * n1);
-            Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            return 0;
-        }
-
-        sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
-            FREE(results);
-            return 0;
-        }
-
-        pStVarOne = (wchar_t **) MALLOC(sizeof(wchar_t *) * (m1 * n1));
-        if (pStVarOne == NULL)
-        {
-            if (lenStVarOne)
-            {
-                FREE(lenStVarOne);
-                lenStVarOne = NULL;
-            }
+            freeAllocatedMatrixOfWideString(m1, n1, pStVarOne);
             Scierror(999, _("%s: Memory allocation error.\n"), fname);
-            FREE(results);
-            return 0;
-        }
-
-        for (i = 0; i < m1 * n1; i++)
-        {
-            pStVarOne[i] = (wchar_t *) MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1));
-            if (pStVarOne[i] == NULL)
-            {
-                if (lenStVarOne)
-                {
-                    FREE(lenStVarOne);
-                    lenStVarOne = NULL;
-                }
-                freeArrayOfWideString(pStVarOne, m1 * n1);
-                Scierror(999, _("%s: Memory allocation error.\n"), fname);
-                FREE(results);
-                return 0;
-            }
-        }
-
-        sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne);
-        if (sciErr.iErr)
-        {
-            printError(&sciErr, 0);
-            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
             return 0;
         }
 
@@ -366,23 +181,18 @@ int sci_pathconvert(char *fname, unsigned long fname_len)
             results[i] = pathconvertW(pStVarOne[i], flagtrail, flagexpand, PType);
         }
 
+        freeAllocatedMatrixOfWideString(m1, n1, pStVarOne);
+
         sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, m1, n1, results);
+        freeArrayOfWideString(results, m1 * n1);
         if (sciErr.iErr)
         {
             printError(&sciErr, 0);
-            freeArrayOfWideString(results, m1 * n1);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
             return 0;
         }
 
         LhsVar(1) = Rhs + 1;
-        if (lenStVarOne)
-        {
-            FREE(lenStVarOne);
-            lenStVarOne = NULL;
-        }
-        freeArrayOfWideString(results, m1 * n1);
-        freeArrayOfWideString(pStVarOne, m1 * n1);
         PutLhsVar();
     }
     else
index f1eae47..38397ce 100644 (file)
@@ -15,6 +15,7 @@
 #include "stack-c.h"
 #include "MALLOC.h"
 #include "Scierror.h"
+#include "localization.h"
 /*--------------------------------------------------------------------------*/
 int sci_pathsep(char *fname, unsigned long fname_len)
 {
@@ -25,21 +26,19 @@ int sci_pathsep(char *fname, unsigned long fname_len)
     CheckLhs(1, 1);
 
     separator = (char*)MALLOC(sizeof(char) * (strlen(PATH_SEPARATOR) + 1));
-    if (separator)
+    if (separator == NULL)
     {
-        strcpy(separator, PATH_SEPARATOR);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
     }
 
+    strcpy(separator, PATH_SEPARATOR);
     n1 = 1;
     m1 = (int)strlen(separator);
     CreateVarFromPtr(Rhs + 1, STRING_DATATYPE, &m1, &n1, &separator);
-    LhsVar(1) = Rhs + 1;
+    FREE(separator);
 
-    if (separator)
-    {
-        FREE(separator);
-        separator = NULL;
-    }
+    LhsVar(1) = Rhs + 1;
     PutLhsVar();
 
     return 0;
index be60202..870546d 100644 (file)
@@ -46,14 +46,15 @@ int sci_splitURL(char *fname, int fname_len)
         return 0;
     }
 
-    ret = getAllocatedSingleString(pvApiCtx, piAddressVarOne, &url);
-    if (ret)
+    if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &url))
     {
+        freeAllocatedSingleString(url);
         Scierror(999, _("%s: Wrong type for argument %d: A string expected.\n"), fname, 1);
         return 0;
     }
 
     c = xmlParseURI(url);
+    freeAllocatedSingleString(url);
     if (c == NULL)
     {
         Scierror(999, "Could not parse the URL.\n");
@@ -61,21 +62,17 @@ int sci_splitURL(char *fname, int fname_len)
     }
 
     // protocol (http, ftp...)
-    sciErr = createMatrixOfString(pvApiCtx, iRhs + 1, 1, 1, &c->scheme);
-    if (sciErr.iErr)
+    if (createSingleString(pvApiCtx, iRhs + 1, c->scheme))
     {
-        printError(&sciErr, 0);
-        return 0;
+        goto err;
     }
 
     AssignOutputVariable(pvApiCtx, 1) = iRhs + 1;
 
     // server
-    sciErr = createMatrixOfString(pvApiCtx, iRhs + 2, 1, 1, &c->server);
-    if (sciErr.iErr)
+    if (createSingleString(pvApiCtx, iRhs + 2, c->server))
     {
-        printError(&sciErr, 0);
-        return 0;
+        goto err;
     }
 
     AssignOutputVariable(pvApiCtx, 2) = iRhs + 2;
@@ -83,17 +80,16 @@ int sci_splitURL(char *fname, int fname_len)
     // path
     if (c->path != NULL)
     {
-        sciErr = createMatrixOfString(pvApiCtx, iRhs + 3, 1, 1, &c->path);
+        ret = createSingleString(pvApiCtx, iRhs + 3, c->path);
     }
     else
     {
-        sciErr = createMatrixOfString(pvApiCtx, iRhs + 3, 1, 1, &emptyString);
+        ret = createSingleString(pvApiCtx, iRhs + 3, emptyString);
     }
 
-    if (sciErr.iErr)
+    if (ret)
     {
-        printError(&sciErr, 0);
-        return 0;
+        goto err;
     }
 
     AssignOutputVariable(pvApiCtx, 3) = iRhs + 3;
@@ -101,17 +97,16 @@ int sci_splitURL(char *fname, int fname_len)
     // query
     if (c->query != NULL)
     {
-        sciErr = createMatrixOfString(pvApiCtx, iRhs + 4, 1, 1, &c->query);
+        ret = createSingleString(pvApiCtx, iRhs + 4, c->query);
     }
     else
     {
-        sciErr = createMatrixOfString(pvApiCtx, iRhs + 4, 1, 1, &emptyString);
+        ret = createSingleString(pvApiCtx, iRhs + 4, emptyString);
     }
 
-    if (sciErr.iErr)
+    if (ret)
     {
-        printError(&sciErr, 0);
-        return 0;
+        goto err;
     }
 
     AssignOutputVariable(pvApiCtx, 4) = iRhs + 4;
@@ -119,35 +114,24 @@ int sci_splitURL(char *fname, int fname_len)
     // user
     if (c->user != NULL)
     {
-        sciErr = createMatrixOfString(pvApiCtx, iRhs + 5, 1, 1, &c->user);
+        ret = createSingleString(pvApiCtx, iRhs + 5, c->user);
     }
     else
     {
-        sciErr = createMatrixOfString(pvApiCtx, iRhs + 5, 1, 1, &emptyString);
+        ret = createSingleString(pvApiCtx, iRhs + 5, emptyString);
     }
 
-    if (sciErr.iErr)
+    if (ret)
     {
-        printError(&sciErr, 0);
-        return 0;
+        goto err;
     }
 
     AssignOutputVariable(pvApiCtx, 5) = iRhs + 5;
 
     // port
-    if (c->port != NULL)
+    if (createScalarInteger32(pvApiCtx, iRhs + 6, c->port))
     {
-        sciErr = createMatrixOfInteger32(pvApiCtx, iRhs + 6, 1, 1, &c->port);
-    }
-    else
-    {
-        sciErr = createMatrixOfInteger32(pvApiCtx, iRhs + 6, 1, 1, &zero);
-    }
-
-    if (sciErr.iErr)
-    {
-        printError(&sciErr, 0);
-        return 0;
+        goto err;
     }
 
     AssignOutputVariable(pvApiCtx, 6) = iRhs + 6;
@@ -155,22 +139,28 @@ int sci_splitURL(char *fname, int fname_len)
     // Fragment
     if (c->fragment != NULL)
     {
-        sciErr = createMatrixOfString(pvApiCtx, iRhs + 7, 1, 1, &c->fragment);
+        ret = createSingleString(pvApiCtx, iRhs + 7, c->fragment);
     }
     else
     {
-        sciErr = createMatrixOfString(pvApiCtx, iRhs + 7, 1, 1, &emptyString);
+        ret = createSingleString(pvApiCtx, iRhs + 7, emptyString);
     }
 
-    if (sciErr.iErr)
+    if (ret)
     {
-        printError(&sciErr, 0);
-        return 0;
+        goto err;
     }
 
+    xmlFreeURI(c);
     AssignOutputVariable(pvApiCtx, 7) = iRhs + 7;
     ReturnArguments(pvApiCtx);
     return 0;
+
+err:
+    xmlFreeURI(c);
+    printError(&sciErr, 0);
+    return 0;
+
 }
 
 /*
index ab21dbf..ba4b7b0 100644 (file)
@@ -34,8 +34,7 @@ int sci_tempname(char *fname, unsigned long fname_len)
         wcprefix = (wchar_t *)MALLOC(sizeof(wchar_t) * (wcslen(DEFAULT_PREFIX) + 1));
         wcscpy(wcprefix, DEFAULT_PREFIX);
     }
-
-    if (Rhs == 1)
+    else if (Rhs == 1)
     {
         int *piAddressVarOne = NULL;
 
@@ -43,7 +42,6 @@ int sci_tempname(char *fname, unsigned long fname_len)
         if (sciErr.iErr)
         {
             printError(&sciErr, 0);
-            FREE(wcprefix);
             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
             return 0;
         }
@@ -56,8 +54,13 @@ int sci_tempname(char *fname, unsigned long fname_len)
 
         if (isStringType(pvApiCtx, piAddressVarOne))
         {
-            if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &wcprefix) != 0)
+            if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &wcprefix))
             {
+                if (wcprefix)
+                {
+                    freeAllocatedSingleWideString(wcprefix);
+                }
+
                 Scierror(999, _("%s: Memory allocation error.\n"), fname);
                 return 0;
             }
@@ -66,8 +69,6 @@ int sci_tempname(char *fname, unsigned long fname_len)
             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;
             }
@@ -76,34 +77,29 @@ int sci_tempname(char *fname, unsigned long fname_len)
         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 (wcTempFilename == NULL)
     {
-        if (createSingleWideString(pvApiCtx, Rhs + 1, wcTempFilename) == 0)
-        {
-            FREE(wcTempFilename);
-            wcTempFilename = NULL;
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
+    }
 
-            LhsVar(1) = Rhs + 1;
-            PutLhsVar();
-            return 0;
-        }
+    if (createSingleWideString(pvApiCtx, Rhs + 1, wcTempFilename))
+    {
+        FREE(wcTempFilename);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
     }
 
     FREE(wcTempFilename);
-    wcTempFilename = NULL;
-    Scierror(999, _("%s: Memory allocation error.\n"), fname);
+    LhsVar(1) = Rhs + 1;
+    PutLhsVar();
     return 0;
 }
 /*--------------------------------------------------------------------------*/
index 03ea4c9..9da28d8 100644 (file)
@@ -24,10 +24,10 @@ wchar_t *basenameW(wchar_t *wcfullfilename, BOOL bExpand)
         wchar_t *expandedPath = expandPathVariableW(wcfullfilename);
         if (expandedPath)
         {
-            wchar_t *wcdrv = MALLOC(sizeof(wchar_t*) * ((int)wcslen(expandedPath) + 1));
-            wchar_t* wcdir = MALLOC(sizeof(wchar_t*) * ((int)wcslen(expandedPath) + 1));
-            wchar_t* wcname = MALLOC(sizeof(wchar_t*) * ((int)wcslen(expandedPath) + 1));
-            wchar_t* wcext = MALLOC(sizeof(wchar_t*) * ((int)wcslen(expandedPath) + 1));
+            wchar_t *wcdrv = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(expandedPath) + 1));
+            wchar_t* wcdir = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(expandedPath) + 1));
+            wchar_t* wcname = (wchar_t*) MALLOC(sizeof(wchar_t) * ((int)wcslen(expandedPath) + 1));
+            wchar_t* wcext = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(expandedPath) + 1));
 
             splitpathW(expandedPath, bExpand, wcdrv, wcdir, wcname, wcext);
 
index b893744..f083026 100644 (file)
@@ -46,7 +46,7 @@ char swap_char(char _val)
     return _val;
 }
 /*--------------------------------------------------------------------------*/
-void writeInt(int _val, FILE* _pF, int _iEndian)
+int writeInt(int _val, FILE* _pF, int _iEndian)
 {
     //reverse _val bytes and write
     int newVal = _val;
@@ -55,10 +55,10 @@ void writeInt(int _val, FILE* _pF, int _iEndian)
         newVal = swap_int(_val);
     }
 
-    fwrite(&newVal, sizeof(unsigned char), sizeof(int), _pF);
+    return fwrite(&newVal, sizeof(int), 1, _pF) != 1 ? -1 : 0;
 }
 /*--------------------------------------------------------------------------*/
-void writeLongLong(long long _val, FILE* _pF, int _iEndian)
+int writeLongLong(long long _val, FILE* _pF, int _iEndian)
 {
     //reverse _val bytes and write
     long long newVal = _val;
@@ -67,10 +67,10 @@ void writeLongLong(long long _val, FILE* _pF, int _iEndian)
         newVal = swap_long_long(_val);
     }
 
-    fwrite(&newVal, sizeof(unsigned char), sizeof(long long), _pF);
+    return fwrite(&newVal, sizeof(long long), 1, _pF) != 1 ? -1 : 0;
 }
 /*--------------------------------------------------------------------------*/
-void writeShort(short _val, FILE* _pF, int _iEndian)
+int writeShort(short _val, FILE* _pF, int _iEndian)
 {
     //reverse _val bytes and write
     short newVal = _val;
@@ -79,62 +79,67 @@ void writeShort(short _val, FILE* _pF, int _iEndian)
         newVal = swap_short(_val);
     }
 
-    fwrite(&newVal, sizeof(unsigned char), 2, _pF);
+    return fwrite(&newVal, sizeof(short), 1, _pF) != 1 ? -1 : 0;
 }
 /*--------------------------------------------------------------------------*/
-void writeChar(short _val, FILE* _pF, int _iEndian)
+int writeChar(char _val, FILE* _pF, int _iEndian)
 {
-    //reverse _val bytes and write
-    fwrite(&_val, sizeof(unsigned char), 1, _pF);
+    return fwrite(&_val, sizeof(char), 1, _pF) != 1 ? -1 : 0;
 }
 /*--------------------------------------------------------------------------*/
-int readInt(FILE* _pF, int _iEndian)
+int readInt(FILE* _pF, int _iEndian, unsigned int* val)
 {
-    int val = 0;
-    fread(&val, sizeof(int), 1, _pF);
-    if (_iEndian == BIG_ENDIAN)
+    if (fread(val, sizeof(int), 1, _pF) != 1)
     {
-        return swap_int(val);
+        return -1;
     }
-    else
+
+    if (_iEndian == BIG_ENDIAN)
     {
-        return val;
+        *val = swap_int(*val);
     }
+
+    return 0;
 }
 /*--------------------------------------------------------------------------*/
-long long readLongLong(FILE* _pF, int _iEndian)
+int readLongLong(FILE* _pF, int _iEndian, unsigned long long* val)
 {
-    long long val = 0;
-    fread(&val, sizeof(long long), 1, _pF);
-    if (_iEndian == BIG_ENDIAN)
+    if (fread(val, sizeof(long long), 1, _pF) != 1)
     {
-        return swap_long_long(val);
+        return -1;
     }
-    else
+
+    if (_iEndian == BIG_ENDIAN)
     {
-        return val;
+        *val = swap_long_long(*val);
     }
+
+    return 0;
 }
 /*--------------------------------------------------------------------------*/
-short readShort(FILE* _pF, int _iEndian)
+int readShort(FILE* _pF, int _iEndian, unsigned short* val)
 {
-    short val = 0;
-    fread(&val, 2, 1, _pF);
-    if (_iEndian == BIG_ENDIAN)
+    if (fread(val, sizeof(short), 1, _pF) != 1)
     {
-        return swap_short(val);
+        return -1;
     }
-    else
+
+    if (_iEndian == BIG_ENDIAN)
     {
-        return val;
+        *val = swap_short(*val);
     }
+
+    return 0;
 }
 /*--------------------------------------------------------------------------*/
-char readChar(FILE* _pF, int _iEndian)
+int readChar(FILE* _pF, int _iEndian, unsigned char* val)
 {
-    char val = 0;
-    fread(&val, 1, 1, _pF);
-    return val;
+    if (fread(val, sizeof(char), 1, _pF) != 1)
+    {
+        return -1;
+    }
+
+    return 0;
 }
 /*--------------------------------------------------------------------------*/
 int checkType(char _type)
index 98ce6b7..9463676 100644 (file)
@@ -31,15 +31,15 @@ int swap_int(int _val);
 char swap_char(char _val);
 long long swap_long_long(long long _val);
 
-void writeInt(int _val, FILE* _pF, int _iEndian);
-void writeLongLong(long long _val, FILE* _pF, int _iEndian);
-void writeShort(short _val, FILE* _pF, int _iEndian);
-void writeChar(short _val, FILE* _pF, int _iEndian);
-
-int readInt(FILE* _pF, int _iEndian);
-long long readLongLong(FILE* _pF, int _iEndian);
-short readShort(FILE* _pF, int _iEndian);
-char readChar(FILE* _pF, int _iEndian);
+int writeInt(int _val, FILE* _pF, int _iEndian);
+int writeLongLong(long long _val, FILE* _pF, int _iEndian);
+int writeShort(short _val, FILE* _pF, int _iEndian);
+int writeChar(char _val, FILE* _pF, int _iEndian);
+
+int readInt(FILE* _pF, int _iEndian, unsigned int* val);
+int readLongLong(FILE* _pF, int _iEndian, unsigned long long* val);
+int readShort(FILE* _pF, int _iEndian, unsigned short* val);
+int readChar(FILE* _pF, int _iEndian, unsigned char* val);
 
 int checkEndian(char _endian);
 int checkType(char _type);
index 6aef5f7..a65cf81 100644 (file)
 /*--------------------------------------------------------------------------*/
 BOOL deleteafile(char *filename)
 {
-    BOOL bOK = FALSE;
 #ifndef _MSC_VER
     {
         FILE *f = fopen(filename, "r") ;
         if (! f)
         {
-            return bOK;
+            return FALSE;
         }
         fclose(f) ;
-        chmod(filename, S_IWRITE) ;
+
+        if (chmod(filename, S_IWRITE))
+        {
+            return FALSE;
+        }
+
         if (remove(filename) == 0)
         {
-            bOK = TRUE;
+            return TRUE;
         }
     }
 #else
     {
+        BOOL bOK = FALSE;
         if (filename)
         {
             wchar_t *wcfilename = to_wide_string(filename);
@@ -48,12 +53,12 @@ BOOL deleteafile(char *filename)
             {
                 bOK = deleteafileW(wcfilename);
                 FREE(wcfilename);
-                wcfilename = NULL;
             }
         }
+
+        return bOK;
     }
 #endif
-    return bOK;
 }
 /*--------------------------------------------------------------------------*/
 BOOL deleteafileW(wchar_t *filenameW)
index e290a71..20b9fd3 100644 (file)
 #ifndef _MSC_VER
 #define _GNU_SOURCE             /* basename crashes this extension otherwise */
 #endif
+#include "MALLOC.h"
 #include <curl/curl.h>
 #include <libxml/uri.h>
 #include <string.h>
 #include "dlManager.h"
 #include "Scierror.h"
 #include "SCIHOME.h"
-#include "getos.h"
 #include "PATH_MAX.h"
-#include "MALLOC.h"
 #include "isdir.h"
 #include "charEncoding.h"
 #include "localization.h"
@@ -36,13 +35,13 @@ static char errorBuffer[CURL_ERROR_SIZE];
 /* ==================================================================== */
 static int getProxyValues(char **proxyHost, long *proxyPort, char **proxyUserPwd);
 /* ==================================================================== */
-struct inputString
+typedef struct __INPUTSTRING__
 {
     char *ptr;
     size_t len;
-};
+} inputString;
 /* ==================================================================== */
-static void init_string(struct inputString *s)
+static void init_string(inputString *s)
 {
     s->len = 0;
     s->ptr = (char*)CALLOC(s->len + 1, sizeof(char));
@@ -51,10 +50,18 @@ static void init_string(struct inputString *s)
         Scierror(999, "Internal error: calloc() failed.\n");
         return;
     }
-    s->ptr[0] = '\0';
 }
 /* ==================================================================== */
-static size_t writefunc(void *ptr, size_t size, size_t nmemb, struct inputString *s)
+static void free_string(inputString *s)
+{
+    if (s->len && s->ptr)
+    {
+        FREE(s->ptr);
+        s->ptr = NULL;
+    }
+}
+/* ==================================================================== */
+static size_t writefunc(void *ptr, size_t size, size_t nmemb, inputString *s)
 {
     size_t new_len = s->len + size * nmemb;
 
@@ -89,13 +96,14 @@ static char *getFileNameFromURL(char *url)
     }
     else
     {
-        char bname[PATH_MAX];
+        char bname[PATH_MAX] = {0};
 
         if (c->path == NULL)
         {
             Scierror(43, "Internal error: c->path is null ?!\n");
         }
-        strcpy(bname, basename(c->path));
+
+        strncpy(bname, basename(c->path), sizeof(bname));
         filename = (char *)MALLOC((strlen(bname) + 1) * sizeof(char));
         strcpy(filename, bname);
     }
@@ -105,19 +113,27 @@ static char *getFileNameFromURL(char *url)
 /* ==================================================================== */
 int getProxyValues(char **proxyHost, long *proxyPort, char **proxyUserPwd)
 {
-    FILE * pFile;
-    long lSize;
-    char * buffer;
-    size_t result;
+    FILE* pFile = NULL;
+    long lSize = 0;
+    char* buffer = NULL;
+    size_t result = 0;
+
+    char* configPtr = NULL;
 
-    char *configPtr;
+    char* host = NULL;
+    char* user = NULL;
+    char* password = NULL;
+    char* userpwd = NULL;
+    long port = 0;
+    int useproxy = 0;
 
-    char *host, *user, *password, *userpwd;
-    long port;
-    int useproxy;
+    char *tp = NULL;
+    char *field = NULL;
+    char *value = NULL;
+    char *eqptr = NULL;
 
-    char *tp, *field, *value, *eqptr;
-    int eqpos = 0 , tplen;
+    int eqpos = 0;
+    int tplen = 0;
 
     //construct ATOMS config file path
     configPtr = (char *)MALLOC(PATH_MAX * sizeof(char));
@@ -163,7 +179,7 @@ int getProxyValues(char **proxyHost, long *proxyPort, char **proxyUserPwd)
     buffer = (char*)MALLOC((lSize + 1) * sizeof(char));
     if (buffer == NULL)
     {
-        FREE(pFile);
+        fclose(pFile);
         return 0;
     }
     buffer[lSize] = '\0';
@@ -176,6 +192,9 @@ int getProxyValues(char **proxyHost, long *proxyPort, char **proxyUserPwd)
         return 0;
     }
 
+    //add null terminated
+    buffer[result] = '\0';
+
     host = user = password = userpwd = NULL;
     useproxy = 0;
 
@@ -302,12 +321,11 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
     char *filename = NULL;
 
     curl = curl_easy_init();
-
     if (curl)
     {
         FILE *file;
 
-        struct inputString buffer;
+        inputString buffer;
 
         init_string(&buffer);
 
@@ -433,7 +451,6 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
         } /* end of the set of the proxy */
 
         res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
-
         if (res != CURLE_OK)
         {
             Scierror(999, _("Failed to set write function [%s]\n"), errorBuffer);
@@ -444,6 +461,7 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
         res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
         if (res != CURLE_OK)
         {
+            free_string(&buffer);
             Scierror(999, _("Failed to set write data [%s]\n"), errorBuffer);
             return NULL;
         }
@@ -452,14 +470,15 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
         res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
         if (res != CURLE_OK)
         {
+            free_string(&buffer);
             Scierror(999, _("Failed to set 'Follow Location' [%s]\n"), errorBuffer);
             return NULL;
         }
 
         res = curl_easy_perform(curl);
-
         if (res != 0)
         {
+            free_string(&buffer);
             Scierror(999, _("Transfer did not complete successfully: %s\n"), errorBuffer);
             fclose(file);
             return NULL;
@@ -475,7 +494,6 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
         curl_easy_cleanup(curl);
 
         fclose(file);
-
         return filename;
     }
     else
@@ -483,7 +501,6 @@ char *downloadFile(char *url, char *dest, char *username, char *password, char *
         Scierror(999, "Failed opening the curl handle.\n");
         return NULL;
     }
-    return NULL;
 }
 /* ==================================================================== */
 static char *Curl_basename(char *path)
index 7272aa5..969be74 100644 (file)
@@ -57,7 +57,7 @@ int do_xxscanf(char *fname, FILE * fp, char *format, int *nargs, char *strv, int
     int ignore_flag = 0;
     int str_width_flag = 0;
     int num_conversion = -1;
-    void *ptrtab[MAXSCAN];
+    void *ptrtab[MAXSCAN] = {NULL};
     char sformat[MAX_STR];
     char backupcurrrentchar;
     char directive;
@@ -293,6 +293,7 @@ int do_xxscanf(char *fname, FILE * fp, char *format, int *nargs, char *strv, int
 
                 case 'n':
                     n_directive_count++;
+                    //pass to next statement
 
                 case 'i':
                 case 'd':
index 72868a4..fc95565 100644 (file)
@@ -147,6 +147,7 @@ wchar_t *getVariableValueDefinedInScilab(wchar_t *wcVarName)
             SciErr sciErr = getNamedVarType(pvApiCtx, varname, &iType);
             if (sciErr.iErr)
             {
+                FREE(varname);
                 return NULL;
             }
 
index af8bcd1..918cd7e 100644 (file)
@@ -82,6 +82,11 @@ double * filesinfo(char **filenames, int dim_filenames, int *ierrs)
         int i = 0;
         int j = 0;
         FILES_INFO_ARRAY = (double*)MALLOC(sizeof(double ) * (dim_filenames * FILEINFO_ARRAY_SIZE));
+        if (FILES_INFO_ARRAY == NULL)
+        {
+            return NULL;
+        }
+
         for (i = 0; i < dim_filenames; i++)
         {
             int k = 0;
index 3934ac8..8218dc4 100644 (file)
@@ -287,10 +287,10 @@ BOOL IsAlreadyOpenedInScilab(char *filename)
 {
     if (ScilabFileList)
     {
-        char fullpath[PATH_MAX * 4];
+        char fullpath[PATH_MAX * 4] = {0};
         int i = 0;
 
-        if ((strcmp(filename, "") == 0) || (filename == NULL))
+        if (filename == NULL || strcmp(filename, "") == 0)
         {
             return FALSE;
         }
@@ -298,7 +298,7 @@ BOOL IsAlreadyOpenedInScilab(char *filename)
         if ( get_full_path( fullpath, filename, PATH_MAX * 4 ) == NULL )
         {
             /* if we are a problem */
-            strcpy(fullpath, filename);
+            strncpy(fullpath, filename, sizeof(fullpath));
         }
 
         for (i = 0; i < CurrentMaxFiles; i++)
@@ -319,12 +319,12 @@ int GetIdFromFilename(char *filename)
 {
     if (ScilabFileList)
     {
-        char fullpath[PATH_MAX * 4];
+        char fullpath[PATH_MAX * 4] = {0};
         int i = 0;
         if ( get_full_path( fullpath, filename, PATH_MAX * 4 ) == NULL )
         {
             /* if we are a problem */
-            strcpy(fullpath, filename);
+            strncpy(fullpath, filename, sizeof(fullpath));
         }
 
         for (i = 0; i < CurrentMaxFiles; i++)
index f7c104c..d2c4cbe 100644 (file)
@@ -460,6 +460,7 @@ static char **splitLine(char *str, char *sep, int *toks, char meta)
 
                         if (retstr[curr_str] == NULL)
                         {
+                            freeArrayOfString(retstr, curr_str);
                             *toks = 0;
                             return NULL;
                         }
index 5053fb0..cb0ca97 100644 (file)
@@ -77,9 +77,9 @@ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes)
         char * _Path_start;
         char * _FullPath_start;
         char* pstWorkingPath = NULL;
-        
+
         //if argument is a relative path, add currentdir at start
-        if(_Path[0] != '/')
+        if (_Path[0] != '/')
         {
             int ierr = 0;
             char* pstCurrentPath = scigetcwd(&ierr);
@@ -91,12 +91,12 @@ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes)
         else
         {
             pstWorkingPath = strdup(_Path);
-        }        
-        
+        }
+
         _Path_tmp = (char *)MALLOC(sizeof(char) * (lenPath + 1));
         _Path_start = (char *)MALLOC(sizeof(char) * (lenPath + 1));
         _FullPath_start = (char *)MALLOC(sizeof(char) * (lenFullPath + 1));
-//First case(1): fullpath(TMPDIR+"/a/b/c"), second case(2): fullpath("a/b/c") or third case(3): fullpath("../a/b")
+        //First case(1): fullpath(TMPDIR+"/a/b/c"), second case(2): fullpath("a/b/c") or third case(3): fullpath("../a/b")
         strcpy(_Path_start, pstWorkingPath); // _Path_start=TMPDIR+"/a/b/c" (1) or _Path_start="a/b/c" (2) or _Path_start="../a/b/c" (3)
         strcpy(_FullPath_start, _FullPath); // _Fullpath_Start=TMPDIR+"/a" (1) or _FullPath_start=SCI+"/a" (2) or _FullPath_start=../SCI+"/a" (3)
         strtok(_Path_start, "/"); // _Path_start=/tmp  (1) or _Path_start="a" (2) or _Path_start="a/b/c" (3)
@@ -116,7 +116,7 @@ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes)
         {
             strcpy(_Path_tmp, pstWorkingPath); //_Path_tmp="a/b/c" (2) or _Path_tmp="../a/b/c" (3)
             strtok(_Path_tmp, "./"); // _Path_tmp becomes a (2) or ../a (3)
-            toadd=strsub(pstWorkingPath, _Path_tmp, ""); // to add = "/b/c"
+            toadd = strsub(pstWorkingPath, _Path_tmp, ""); // to add = "/b/c"
             strcat(_FullPath, toadd); //_FullPath=_Fullpath+toadd
             FREE(_Path_tmp);
             _Path_tmp = NULL;
@@ -125,10 +125,10 @@ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes)
             FREE(_FullPath_start);
             _FullPath_start = NULL;
         }
-   
+
         FREE(pstWorkingPath);
     }
-    
+
     lenFullPath = (int)strlen(_FullPath);
     addFileSep = ((lenFullPath > 1) && (!isDirSeparator(_FullPath[lenFullPath - 1])) && haveFileSep);
     if (addFileSep)
@@ -149,19 +149,16 @@ char *get_full_path(char *_FullPath, const char *_Path, size_t _SizeInBytes)
 /*--------------------------------------------------------------------------*/
 wchar_t *get_full_pathW(wchar_t * _wcFullPath, const wchar_t * _wcPath, size_t _SizeInBytes)
 {
-    wchar_t *wcResult = NULL;
-
 #if defined(_MSC_VER)
     if (_wcPath)
     {
-        wcResult = (wchar_t *) MALLOC(sizeof(wchar_t) * _SizeInBytes);
-        if (wcResult)
-        {
-            _wfullpath(wcResult, _wcPath, _SizeInBytes);
-            wcscpy(_wcFullPath, wcResult);
-        }
+        _wfullpath(_wcFullPath, _wcPath, _SizeInBytes);
+        return _wcFullPath;
     }
+    return NULL;
 #else
+    wchar_t *wcResult = NULL;
+
     if (_wcPath)
     {
         char *_Path = wide_string_to_UTF8(_wcPath);
@@ -192,8 +189,8 @@ wchar_t *get_full_pathW(wchar_t * _wcFullPath, const wchar_t * _wcPath, size_t _
             _Path = NULL;
         }
     }
-#endif
     return wcResult;
+#endif
 }
 
 /*--------------------------------------------------------------------------*/
index 2e148c0..84d2bf9 100644 (file)
@@ -124,24 +124,21 @@ wchar_t *getFullFilenameW(wchar_t* FilenameInput)
 char *getFullFilename(char* Filename)
 {
     char *pStFullFilename = NULL;
-
     if (Filename)
     {
         wchar_t *wcFilename = to_wide_string(Filename);
         if (wcFilename)
         {
-            wchar_t *pStwcFullFilename = NULL;
-            pStwcFullFilename = getFullFilenameW(wcFilename);
+            wchar_t *pStwcFullFilename = getFullFilenameW(wcFilename);
             FREE(wcFilename);
-            wcFilename = NULL;
             if (pStwcFullFilename)
             {
                 pStFullFilename = wide_string_to_UTF8(pStwcFullFilename);
                 FREE(pStwcFullFilename);
-                pStwcFullFilename = NULL;
             }
         }
     }
+
     return pStFullFilename;
 }
 /*--------------------------------------------------------------------------*/
index a201e0a..557407d 100644 (file)
@@ -32,6 +32,7 @@ char *getlongpathname(char *shortpathname, BOOL *convertok)
     if (wcshortpathname)
     {
         wchar_t *wcLongName = getlongpathnameW(wcshortpathname, convertok);
+        FREE(wcshortpathname);
         if (wcLongName)
         {
             LongName = wide_string_to_UTF8(wcLongName);
index 96ecddd..f81ffa1 100644 (file)
@@ -28,7 +28,6 @@ static char *normalizeFileSeparator(const char *path);
 /* ================================================================================== */
 char* getrelativefilename(char *currentDirectory, char *absoluteFilename)
 {
-    // declarations - put here so this should work in a C compiler
     int afMarker = 0, rfMarker = 0;
     int cdLen = 0, afLen = 0;
     int i = 0;
@@ -45,16 +44,8 @@ char* getrelativefilename(char *currentDirectory, char *absoluteFilename)
     {
         // fix bug 2181
         strcpy(relativeFilename, _absoluteFilename);
-        if (_currentDirectory)
-        {
-            FREE(_currentDirectory);
-            _currentDirectory = NULL;
-        }
-        if (_absoluteFilename)
-        {
-            FREE(_absoluteFilename);
-            _absoluteFilename = NULL;
-        }
+        FREE(_currentDirectory);
+        FREE(_absoluteFilename);
         return relativeFilename;
     }
 
@@ -63,16 +54,8 @@ char* getrelativefilename(char *currentDirectory, char *absoluteFilename)
     {
         // not on the same drive, so only absolute filename will do
         strcpy(relativeFilename, _absoluteFilename);
-        if (_currentDirectory)
-        {
-            FREE(_currentDirectory);
-            _currentDirectory = NULL;
-        }
-        if (_absoluteFilename)
-        {
-            FREE(_absoluteFilename);
-            _absoluteFilename = NULL;
-        }
+        FREE(_currentDirectory);
+        FREE(_absoluteFilename);
         return relativeFilename;
     }
 
@@ -105,16 +88,8 @@ char* getrelativefilename(char *currentDirectory, char *absoluteFilename)
         }
 
         strcpy(relativeFilename, &_absoluteFilename[i]);
-        if (_currentDirectory)
-        {
-            FREE(_currentDirectory);
-            _currentDirectory = NULL;
-        }
-        if (_absoluteFilename)
-        {
-            FREE(_absoluteFilename);
-            _absoluteFilename = NULL;
-        }
+        FREE(_currentDirectory);
+        FREE(_absoluteFilename);
         return relativeFilename;
     }
 
@@ -151,16 +126,8 @@ char* getrelativefilename(char *currentDirectory, char *absoluteFilename)
     // check that the result will not be too long
     if (levels * 3 + afLen - afMarker > PATH_MAX)
     {
-        if (_currentDirectory)
-        {
-            FREE(_currentDirectory);
-            _currentDirectory = NULL;
-        }
-        if (_absoluteFilename)
-        {
-            FREE(_absoluteFilename);
-            _absoluteFilename = NULL;
-        }
+        FREE(_currentDirectory);
+        FREE(_absoluteFilename);
         return NULL;
     }
 
@@ -176,17 +143,8 @@ char* getrelativefilename(char *currentDirectory, char *absoluteFilename)
     // copy the rest of the filename into the result string
     strcpy(&relativeFilename[rfMarker], &_absoluteFilename[afMarker]);
 
-    if (_currentDirectory)
-    {
-        FREE(_currentDirectory);
-        _currentDirectory = NULL;
-    }
-    if (_absoluteFilename)
-    {
-        FREE(_absoluteFilename);
-        _absoluteFilename = NULL;
-    }
-
+    FREE(_currentDirectory);
+    FREE(_absoluteFilename);
     return relativeFilename;
 }
 /* ================================================================================== */
index 99749f0..842f2a4 100644 (file)
@@ -118,8 +118,8 @@ void C2F(mgeti)(int* _pF, long long* _pVal, int* _iSize, char* _iOpt, int* _iErr
             for (i = 0 ; i < *_iSize ; i++)
             {
                 unsigned long long val;
-                val = readLongLong(fa, iEndian);
-                if (feof(fa))
+                *_iErr = readLongLong(fa, iEndian, &val);
+                if (*_iErr || feof(fa))
                 {
                     iCount = i;
                     break;
@@ -133,8 +133,8 @@ void C2F(mgeti)(int* _pF, long long* _pVal, int* _iSize, char* _iOpt, int* _iErr
             for (i = 0 ; i < *_iSize ; i++)
             {
                 unsigned int val;
-                val = readInt(fa, iEndian);
-                if (feof(fa))
+                *_iErr = readInt(fa, iEndian, &val);
+                if (*_iErr || feof(fa))
                 {
                     iCount = i;
                     break;
@@ -147,8 +147,8 @@ void C2F(mgeti)(int* _pF, long long* _pVal, int* _iSize, char* _iOpt, int* _iErr
             for (i = 0 ; i < *_iSize ; i++)
             {
                 unsigned short val;
-                val = readShort(fa, iEndian);
-                if (feof(fa))
+                *_iErr = readShort(fa, iEndian, &val);
+                if (*_iErr || feof(fa))
                 {
                     iCount = i;
                     break;
@@ -161,8 +161,8 @@ void C2F(mgeti)(int* _pF, long long* _pVal, int* _iSize, char* _iOpt, int* _iErr
             for (i = 0 ; i < *_iSize ; i++)
             {
                 unsigned char val;
-                val = readChar(fa, iEndian);
-                if (feof(fa))
+                *_iErr = readChar(fa, iEndian, &val);
+                if (*_iErr || feof(fa))
                 {
                     iCount = i;
                     break;
index b8fc507..a7792a7 100644 (file)
@@ -22,6 +22,7 @@
 #include "BOOL.h"
 #include "strsubst.h"
 #include "charEncoding.h"
+#include "freeArrayOfString.h"
 /*--------------------------------------------------------------------------*/
 #define LINE_MAX 4096
 #define CR '\r'
@@ -78,17 +79,14 @@ char **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
                     Line = tmpLine;
                 }
             }
+
             while ( Line != NULL )
             {
                 nbLines++;
                 strLines = (char **)REALLOC(strLines, nbLines * sizeof(char *));
                 if (strLines == NULL)
                 {
-                    if (Line)
-                    {
-                        FREE(Line);
-                        Line = NULL;
-                    }
+                    FREE(Line);
                     *nbLinesOut = 0;
                     *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
                     return NULL;
@@ -96,28 +94,19 @@ char **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
 
                 strLines[nbLines - 1] = convertAnsiToUtf(removeEOL(Line));
 
-                if (Line)
-                {
-                    FREE(Line);
-                    Line = NULL;
-                }
+                FREE(Line);
 
                 if (strLines[nbLines - 1] == NULL)
                 {
                     *nbLinesOut = 0;
                     *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
+                    freeArrayOfString(strLines, nbLines);
                     return NULL;
                 }
 
                 Line = getNextLine(fa);
             }
 
-            if (Line)
-            {
-                FREE(Line);
-                Line = NULL;
-            }
-
             *nbLinesOut = nbLines;
             *ierr = MGETL_NO_ERROR;
         }
index beb2162..9ffd8ee 100644 (file)
@@ -113,7 +113,7 @@ void C2F(mputi)(int* _pF, long long* _pVal, int* _iSize, char* _iOpt, int* _iErr
             {
                 unsigned long long val;
                 val = *(_pVal + i);
-                writeLongLong(val, fa, iEndian);
+                *_iErr = writeLongLong(val, fa, iEndian);
             }
             break;
         case TYPE_INT :
@@ -121,7 +121,7 @@ void C2F(mputi)(int* _pF, long long* _pVal, int* _iSize, char* _iOpt, int* _iErr
             {
                 unsigned int val;
                 val = *(RES_ui + i);
-                writeInt(val, fa, iEndian);
+                *_iErr = writeInt(val, fa, iEndian);
             }
             break;
         case TYPE_SHORT :
@@ -129,7 +129,7 @@ void C2F(mputi)(int* _pF, long long* _pVal, int* _iSize, char* _iOpt, int* _iErr
             {
                 unsigned short val;
                 val = *(RES_us + i);
-                writeShort(val, fa, iEndian);
+                *_iErr = writeShort(val, fa, iEndian);
             }
             break;
         case TYPE_CHAR:
@@ -137,7 +137,7 @@ void C2F(mputi)(int* _pF, long long* _pVal, int* _iSize, char* _iOpt, int* _iErr
             {
                 unsigned char val;
                 val = *(RES_uc + i);
-                writeChar(val, fa, iEndian);
+                *_iErr = writeChar(val, fa, iEndian);
             }
             break;
     }
index 72b7508..577beb2 100644 (file)
@@ -61,14 +61,10 @@ wchar_t *pathconvertW(wchar_t* wcpath, BOOL flagtrail, BOOL flagexpand, PathConv
             convertedPath = windowstocygwinpath(expandedPath, &bConvCyg);
         }
 
+        FREE(expandedPath);
+
         if (convertedPath)
         {
-            if (expandedPath)
-            {
-                FREE(expandedPath);
-                expandedPath = NULL;
-            }
-
             if (flagtrail)
             {
                 int currentLen = (int) wcslen(convertedPath);
@@ -129,8 +125,9 @@ char *pathconvert(char* path, BOOL flagtrail, BOOL flagexpand, PathConvertType P
             {
                 convertedPath = wide_string_to_UTF8(wcconvertedPath);
                 FREE(wcconvertedPath);
-                wcconvertedPath = NULL;
             }
+
+            FREE(wcpath);
         }
     }
     return convertedPath;
@@ -212,10 +209,10 @@ static wchar_t *windowstocygwinpath(wchar_t *windowspath, BOOL *bConverted)
 
     if (windowspath)
     {
-        wchar_t *wcdrv = MALLOC(sizeof(wchar_t*) * ((int)wcslen(windowspath) + 1));
-        wchar_t* wcdir = MALLOC(sizeof(wchar_t*) * ((int)wcslen(windowspath) + 1));
-        wchar_t* wcname = MALLOC(sizeof(wchar_t*) * ((int)wcslen(windowspath) + 1));
-        wchar_t* wcext = MALLOC(sizeof(wchar_t*) * ((int)wcslen(windowspath) + 1));
+        wchar_t *wcdrv = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(windowspath) + 1));
+        wchar_t* wcdir = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(windowspath) + 1));
+        wchar_t* wcname = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(windowspath) + 1));
+        wchar_t* wcext = (wchar_t*)MALLOC(sizeof(wchar_t) * ((int)wcslen(windowspath) + 1));
 
         splitpathW(windowspath, FALSE, wcdrv, wcdir, wcname, wcext);
         if (wcscmp(wcdrv, L"") != 0)
index 52c1933..9548c28 100644 (file)
@@ -38,7 +38,7 @@ int LineRead(int fd, char buf[], int n, int *cnt, int *nr)
     {
         case MGETL_NO_ERROR:
         {
-            if ((lines[0]) && (lines) && (nbLinesReaded == 1))
+            if (lines && lines[0] && nbLinesReaded == 1)
             {
                 /* current limitation (bsiz) of line readed by scilab */
                 if ((int)strlen(lines[0]) < bsiz)
index 89d1482..d63fa72 100644 (file)
@@ -257,6 +257,7 @@ static int DeleteDirectory(char *refcstrRootDirectory)
         sciprint(_("Warning: Error while opening %s: %s\n"), refcstrRootDirectory, strerror(errno));
         return -1;
     }
+
     while ((ent = readdir(dir)) != NULL)
     {
         char *filename = NULL;
@@ -264,17 +265,14 @@ static int DeleteDirectory(char *refcstrRootDirectory)
         {
             continue ;
         }
+
         filename = MALLOC(sizeof(char) * (strlen(refcstrRootDirectory) + 1 + strlen(ent->d_name) + 1 + 1)) ;
         sprintf(filename, "%s/%s", refcstrRootDirectory, ent->d_name);
         if (isdir(filename))
         {
             /* Delete recursively */
             DeleteDirectory(filename);
-            if (filename)
-            {
-                FREE(filename);
-                filename = NULL;
-            }
+            FREE(filename);
         }
         else
         {
@@ -283,11 +281,8 @@ static int DeleteDirectory(char *refcstrRootDirectory)
             {
                 sciprint(_("Warning: Could not remove file %s: %s\n"), filename, strerror(errno));
             }
-            if (filename)
-            {
-                FREE(filename);
-                filename = NULL;
-            }
+
+            FREE(filename);
         }
     }
     if (rmdir(refcstrRootDirectory) != 0)
@@ -297,8 +292,7 @@ static int DeleteDirectory(char *refcstrRootDirectory)
 
     if (dir)
     {
-        FREE(dir);
-        dir = NULL;
+        closedir(dir);
     }
 
     return 0;
index 8424e83..0cf789d 100644 (file)
@@ -190,7 +190,7 @@ Complete:
             }
             else if ( (cur_type == SF_C) || (cur_type == SF_S) )
             {
-                if ( (temp = (char **) MALLOC(nrow * ncol * sizeof(char **))) == NULL)
+                if ( (temp = (char **) MALLOC(nrow * ncol * sizeof(char *))) == NULL)
                 {
                     return MEM_LACK;
                 }
index da621af..2ea9003 100644 (file)
@@ -163,7 +163,7 @@ void splitpathW(const wchar_t* path, BOOL bExpand, wchar_t* drv, wchar_t* dir, w
     }
 
     /* swap name & extension if no name */
-    if ( (name[0] == 0) && (wcslen(ext) > 0) )
+    if (name && name[0] == 0 && wcslen(ext) > 0)
     {
         wcscpy(name, ext);
         wcscpy(ext, L"");
@@ -172,75 +172,64 @@ void splitpathW(const wchar_t* path, BOOL bExpand, wchar_t* drv, wchar_t* dir, w
 /*--------------------------------------------------------------------------*/
 void splitpath(const char* path, BOOL bExpand, char* drv, char* dir, char* name, char* ext)
 {
-    wchar_t *wcpath = to_wide_string((char*)path);
-    wchar_t *wcdrv = (wchar_t*)MALLOC(sizeof(wchar_t) * (PATH_MAX + 1));
-    wchar_t *wcdir = (wchar_t*)MALLOC(sizeof(wchar_t) * (PATH_MAX + 1));
-    wchar_t *wcname = (wchar_t*)MALLOC(sizeof(wchar_t) * (PATH_MAX + 1));
-    wchar_t *wcext = (wchar_t*)MALLOC(sizeof(wchar_t) * (PATH_MAX + 1));
+    wchar_t *wcpath = NULL;
+    wchar_t *wcdrv = NULL;
+    wchar_t *wcdir = NULL;
+    wchar_t *wcname = NULL;
+    wchar_t *wcext = NULL;
 
     char *buffer = NULL;
 
-    if (drv)
-    {
-        strcpy(drv, "");
-    }
-    if (dir)
-    {
-        strcpy(dir, "");
-    }
-    if (name)
-    {
-        strcpy(name, "");
-    }
-    if (ext)
+    if (drv == NULL || dir == NULL || name == NULL || ext == NULL)
     {
-        strcpy(ext, "");
+        return;
     }
 
+    wcpath = to_wide_string((char*)path);
+    wcdrv = (wchar_t*)MALLOC(sizeof(wchar_t) * (PATH_MAX + 1));
+    wcdir = (wchar_t*)MALLOC(sizeof(wchar_t) * (PATH_MAX + 1));
+    wcname = (wchar_t*)MALLOC(sizeof(wchar_t) * (PATH_MAX + 1));
+    wcext = (wchar_t*)MALLOC(sizeof(wchar_t) * (PATH_MAX + 1));
+
+    strcpy(drv, "");
+    strcpy(dir, "");
+    strcpy(name, "");
+    strcpy(ext, "");
+
     splitpathW(wcpath, bExpand, wcdrv, wcdir, wcname, wcext);
 
+    FREE(wcpath);
+
     buffer = wide_string_to_UTF8(wcdrv);
     if (buffer)
     {
         strcpy(drv, buffer);
         FREE(buffer);
-        buffer = NULL;
     }
-    FREE(wcpath);
-    wcpath = NULL;
-
+    FREE(wcdrv);
 
     buffer = wide_string_to_UTF8(wcdir);
     if (buffer)
     {
         strcpy(dir, buffer);
         FREE(buffer);
-        buffer = NULL;
     }
     FREE(wcdir);
-    wcdir = NULL;
 
     buffer = wide_string_to_UTF8(wcname);
     if (buffer)
     {
         strcpy(name, buffer);
         FREE(buffer);
-        buffer = NULL;
     }
     FREE(wcname);
-    wcname = NULL;
 
     buffer = wide_string_to_UTF8(wcext);
     if (buffer)
     {
         strcpy(ext, buffer);
         FREE(buffer);
-        buffer = NULL;
     }
     FREE(wcext);
-    wcext = NULL;
-
-    FREE(wcdrv);
-    wcdrv = NULL;
 }
 /*--------------------------------------------------------------------------*/