read function fixed 62/16762/3
Cedric Delamarre [Thu, 2 Jul 2015 12:50:51 +0000 (14:50 +0200)]
* test updated, the format cannot accept constant string
* function fortran pototype fixed about length of input string
* move out of loop some allocation/delete
* file opened must be closed when error occured

test_run io read

Change-Id: I9bed20a1f414fd9aaace80e8c719bb772bedb054

scilab/modules/io/sci_gateway/cpp/sci_read.cpp
scilab/modules/io/tests/unit_tests/read.dia.ref
scilab/modules/io/tests/unit_tests/read.tst

index 5f4539e..ea73aa5 100644 (file)
@@ -31,10 +31,10 @@ extern "C"
     extern int C2F(readdoublefile)(int* ID, double* dat, int* m, int* n, int* err);
     extern int C2F(readdoublefileform)(int* ID, char* form, double* dat, int* m, int* n, int* err, int);
     extern int C2F(readdoublelinefile)(int* ID, double* dat, int* n, int* err);
-    extern int C2F(readdoublelinefileform)(int* ID, char* form, double* dat, int* n, int* err);
+    extern int C2F(readdoublelinefileform)(int* ID, char* form, double* dat, int* n, int* err, int);
 
     extern int C2F(readintfileform)(int* ID, char* form, int* dat, int* m, int* n, int* err, int);
-    extern int C2F(readintlinefileform)(int* ID, char* form, int* dat, int* n, int* err);
+    extern int C2F(readintlinefileform)(int* ID, char* form, int* dat, int* n, int* err, int);
 
     extern int C2F(readstringfile)(int* ID, char* form, char* dat, int* siz, int* err, int);
     extern int C2F(readstring)(char* form, char* dat, int* siz, int* err, int);
@@ -44,6 +44,7 @@ extern "C"
 using namespace types;
 
 InternalType::ScilabType checkformatread(const char* format);
+void closeFile(types::InternalType* _pIT, int _iID);
 
 template<typename T>
 bool is_of_type(const std::string & Str)
@@ -54,7 +55,6 @@ bool is_of_type(const std::string & Str)
     return (iss >> tmp) && (iss.eof());
 }
 
-
 /*--------------------------------------------------------------------------*/
 Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
 {
@@ -87,6 +87,7 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
 
         if (iErr == 240)
         {
+            closeFile(in[0], iID);
             Scierror(999, _("File \"%s\" already exists or directory write access denied.\n"), pstFilename);
             FREE(pstFilename);
             return Function::Error;
@@ -94,6 +95,7 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
 
         if (iErr == 241)
         {
+            closeFile(in[0], iID);
             Scierror(999, _("File \"%s\" does not exist or read access denied.\n"), pstFilename);
             FREE(pstFilename);
             return Function::Error;
@@ -127,6 +129,7 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
         int iPos = iRhs - 1;
         if (in[iPos]->isString() == false)
         {
+            closeFile(in[0], iID);
             Scierror(999, _("%s: Wrong type for input argument #%d : A string expected.\n"), "read", iRhs);
             return Function::Error;
         }
@@ -134,6 +137,7 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
         String* pSFormat = in[iPos]->getAs<String>();
         if (pSFormat->isScalar() == false)
         {
+            closeFile(in[0], iID);
             Scierror(999, _("%s: Wrong type for input argument #%d : A string expected.\n"), "read", iRhs);
             return Function::Error;
         }
@@ -144,6 +148,7 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
         itTypeOfData = checkformatread(pstFormat);
         if (itTypeOfData == InternalType::ScilabNull)
         {
+            closeFile(in[0], iID);
             Scierror(999, _("Incorrect file or format.\n"));
             return Function::Error;
         }
@@ -153,6 +158,7 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
 
     if (in[1]->isDouble() == false)
     {
+        closeFile(in[0], iID);
         Scierror(999, _("%s: Wrong type for input argument #%d: A scalar integer value expected.\n"), "read", 2);
         return Function::Error;
     }
@@ -160,12 +166,14 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
     Double* pIn1 = in[1]->getAs<Double>();
     if (pIn1->isScalar() == false)
     {
+        closeFile(in[0], iID);
         Scierror(999, _("%s: Wrong size for input argument #%d: A scalar integer value expected.\n"), "read", 2);
         return Function::Error;
     }
 
     if (in[2]->isDouble() == false)
     {
+        closeFile(in[0], iID);
         Scierror(999, _("%s: Wrong type for input argument #%d: A scalar integer value expected.\n"), "read", 3);
         return Function::Error;
     }
@@ -173,12 +181,13 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
     Double* pIn2 = in[2]->getAs<Double>();
     if (pIn2->isScalar() == false)
     {
+        closeFile(in[0], iID);
         Scierror(999, _("%s: Wrong size for input argument #%d: A scalar integer value expected.\n"), "read", 3);
         return Function::Error;
     }
 
     int iRows = (int)pIn1->get(0);
-    int iCols = (int) pIn2->get(0);
+    int iCols = (int)pIn2->get(0);
 
     //test dims
     if ( (iCols <= 0) || (iRows == 0))
@@ -188,6 +197,7 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
             FREE(pstFormat);
         }
         out.push_back(Double::Empty());
+        closeFile(in[0], iID);
         return Function::OK;
     }
 
@@ -204,9 +214,9 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
 
                     if (pstFormat == NULL)
                     {
+                        double* pdData = new double[iCols];
                         while (error == 0)
                         {
-                            double* pdData = new double[iCols];
                             C2F(readdoublelinefile)(&iID, pdData, &iCols, &error);
                             if (error == 0)
                             {
@@ -217,15 +227,15 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                                 }
                                 ++iRows;
                             }
-                            delete[] pdData;
                         }
+                        delete[] pdData;
                     }
                     else
                     {
+                        double* pdData = new double[iCols];
                         while (error == 0)
                         {
-                            double* pdData = new double[iCols];
-                            C2F(readdoublelinefileform)(&iID, pstFormat, pdData, &iCols, &error);
+                            C2F(readdoublelinefileform)(&iID, pstFormat, pdData, &iCols, &error, (int)strlen(pstFormat));
                             if (error == 0)
                             {
                                 pD->resize(iRows, iCols);
@@ -235,8 +245,8 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                                 }
                                 ++iRows;
                             }
-                            delete[] pdData;
                         }
+                        delete[] pdData;
                     }
 
                     if (error != 2)
@@ -253,11 +263,10 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                 {
                     iRows = 1;
                     Int32* pI = new Int32(iRows, iCols);
-
+                    int* piData = new int[iCols];
                     while (error == 0)
                     {
-                        int* piData = new int[iCols];
-                        C2F(readintlinefileform)(&iID, pstFormat, piData, &iCols, &error);
+                        C2F(readintlinefileform)(&iID, pstFormat, piData, &iCols, &error, (int)strlen(pstFormat));
                         if (error == 0)
                         {
                             pI->resize(iRows, iCols);
@@ -267,9 +276,10 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                             }
                             ++iRows;
                         }
-                        delete[] piData;
                     }
 
+                    delete[] piData;
+
                     if (error != 2)
                     {
                         out.push_back(pI);
@@ -284,16 +294,18 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                 {
                     if (iCols != 1)
                     {
+                        closeFile(in[0], iID);
                         Scierror(999, _("%s: Wrong input argument %d.\n"), "read", 3);
                         return Function::Error;
                     }
                     else
                     {
+                        iRows = 1;
                         String* pS = new String(iRows, iCols);
+                        char pCt[4096];
 
                         while (error == 0)
                         {
-                            char pCt[4096];
                             int siz = 0;
                             C2F(readstringfile)(&iID, pstFormat, pCt, &siz, &error, (int)strlen(pstFormat));
                             pCt[siz] = '\0';
@@ -319,33 +331,13 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                 break;
                 default:
                 {
+                    closeFile(in[0], iID);
                     Scierror(999, _("%s: Wrong type for input argument #%d : A string expected.\n"), "read", 2);
-
-                    //close file
-                    if (in[0]->isString())
-                    {
-                        int piMode[2] = { 0, 0 };
-                        String* pSPath = in[0]->getAs<String>();
-                        char* pstFilename = wide_string_to_UTF8(pSPath->get(0));
-                        int  close = -iID;
-                        int iErr = C2F(clunit)(&close, pstFilename, piMode, (int)strlen(pstFilename));
-                        FREE(pstFilename);
-                    }
                     return Function::Error;
-                    break;
                 }
             }
 
-            //close file
-            if (in[0]->isString())
-            {
-                int piMode[2] = { 0, 0 };
-                String* pSPath = in[0]->getAs<String>();
-                char* pstFilename = wide_string_to_UTF8(pSPath->get(0));
-                int  close = -iID;
-                int iErr = C2F(clunit)(&close, pstFilename, piMode, (int)strlen(pstFilename));
-                FREE(pstFilename);
-            }
+            closeFile(in[0], iID);
         }
         else//read from the console
         {
@@ -357,10 +349,10 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                     Double* pD = new Double(iRows, iCols, false);
 
                     char pstString[4] = "(a)";
+                    char pCt[4096];
 
                     while (error != 2)
                     {
-                        char pCt[4096];
                         int siz = 0;
                         C2F(readstring)(pstString, pCt, &siz, &error, (int)strlen(pstString));
                         pCt[siz] = '\0';
@@ -435,10 +427,10 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                         bool bEndWrite = false;
                         iRows = 1;
                         String* pS = new String(iRows, iCols);
+                        char pCt[4096];
 
                         for (; bEndWrite == false; iRows++)
                         {
-                            char pCt[4096];
                             int siz = 0;
                             C2F(readstring)(pstFormat, pCt, &siz, &error, (int)strlen(pstFormat));
                             pCt[siz] = '\0';
@@ -522,9 +514,9 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                 case InternalType::ScilabString:
                 {
                     String* pS = new String(iRows, iCols);
+                    char pCt[4096];
                     for (int i = 0; i < iCols * iRows; ++i)
                     {
-                        char pCt[4096];
                         int siz = 0;
                         C2F(readstringfile)(&iID, pstFormat, pCt, &siz, &error, (int)strlen(pstFormat));
                         pCt[siz] = '\0';
@@ -542,32 +534,15 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                 }
                 break;
                 default:
+                {
                     Scierror(999, _("%s: Wrong type for input argument #%d : A string expected.\n"), "read", 2);
-
-                    //close file
-                    if (in[0]->isString())
-                    {
-                        int piMode[2] = { 0, 0 };
-                        String* pSPath = in[0]->getAs<String>();
-                        char* pstFilename = wide_string_to_UTF8(pSPath->get(0));
-                        int  close = -iID;
-                        int iErr = C2F(clunit)(&close, pstFilename, piMode, (int)strlen(pstFilename));
-                        FREE(pstFilename);
-                    }
+                    closeFile(in[0], iID);
                     return Function::Error;
-                    break;
+                }
             }
 
             //close file
-            if (in[0]->isString())
-            {
-                int piMode[2] = { 0, 0 };
-                String* pSPath = in[0]->getAs<String>();
-                char* pstFilename = wide_string_to_UTF8(pSPath->get(0));
-                int  close = -iID;
-                int iErr = C2F(clunit)(&close, pstFilename, piMode, (int)strlen(pstFilename));
-                FREE(pstFilename);
-            }
+            closeFile(in[0], iID);
 
             if (error == 2)
             {
@@ -584,10 +559,10 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                     Double* pD = new Double(iRows, iCols, false);
 
                     char pstString[4] = "(a)";
+                    char pCt[4096];
 
                     for (int i = 0; i < iRows && error == 0; ++i)
                     {
-                        char pCt[4096];
                         int siz = 0;
                         C2F(readstring)(pstString, pCt, &siz, &error, (int)strlen(pstString));
                         pCt[siz] = '\0';
@@ -644,10 +619,10 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
                     else
                     {
                         String* pS = new String(iRows, iCols);
+                        char pCt[4096];
 
                         for (int i = 0; i < (iRows); i++)
                         {
-                            char pCt[4096];
                             int siz = 0;
                             C2F(readstring)(pstFormat, pCt, &siz, &error, (int)strlen(pstFormat));
                             pCt[siz] = '\0';
@@ -672,10 +647,10 @@ Function::ReturnValue sci_read(typed_list &in, int _iRetCount, typed_list &out)
 
         if (error != 0)
         {
+            closeFile(in[0], iID);
             Scierror(999, _("Incorrect file or format.\n"));
             return Function::Error;
         }
-
     }
 
     return Function::OK;
@@ -746,3 +721,16 @@ InternalType::ScilabType checkformatread(const char* format)
 
     return previousType;
 }
+
+void closeFile(types::InternalType* _pIT, int _iID)
+{
+    if (_pIT->isString())
+    {
+        int piMode[2] = { 0, 0 };
+        String* pSPath = _pIT->getAs<String>();
+        char* pstFilename = wide_string_to_UTF8(pSPath->get(0));
+        int  close = -_iID;
+        int iErr = C2F(clunit)(&close, pstFilename, piMode, (int)strlen(pstFilename));
+        FREE(pstFilename);
+    }
+}
index 502048b..fe44a36 100644 (file)
@@ -22,7 +22,7 @@ assert_checkalmostequal(result, double(:,1:4),[],%eps);
 mdelete(path_double);
 double = rand(5,3);
 write(path_double,double,"("" | "",3(f10.3,"" | ""))");
-result = read(path_double,5,3,"("" | "",3(f10.3,"" | ""))");
+result = read(path_double,5,3,"(3x, 3(f10.3, 3x))");
 assert_checkalmostequal(result, round(double*1000)/1000);
 mdelete(path_double);
 test_size=rand(2,2);
@@ -40,11 +40,11 @@ assert_checkerror("read(path_size,3,2);", refMsg);
 assert_checkerror("read(path_size,2,3);", refMsg);
 assert_checkerror("read(path_size,3,3);", refMsg);
 mdelete(path_size);
-write(path_int,1:10,"(10(i))");
-result = read(path_int,1,10,"(10(i))");
+write(path_int,1:10,"(10(i3))");
+result = read(path_int,1,10,"(10(i3))");
 assert_checkequal(result, int32(1:10));
-assert_checkerror("read(path_int,10,1,""(10(i))"");",refMsg);
-result = read(path_int,-1,1,"(10(i))");
+assert_checkerror("read(path_int,10,1,""(10(i3))"");",refMsg);
+result = read(path_int,-1,1,"(10(i3))");
 assert_checkequal(result, int32(1));
 mdelete(path_int);
 text = "Scilab";
index 83cdbc3..e57ebed 100644 (file)
@@ -31,7 +31,7 @@ mdelete(path_double);
 double = rand(5,3);
 
 write(path_double,double,"("" | "",3(f10.3,"" | ""))");
-result = read(path_double,5,3,"("" | "",3(f10.3,"" | ""))");
+result = read(path_double,5,3,"(3x, 3(f10.3, 3x))");
 assert_checkalmostequal(result, round(double*1000)/1000);
 
 mdelete(path_double);
@@ -56,12 +56,12 @@ assert_checkerror("read(path_size,3,3);", refMsg);
 
 mdelete(path_size);
 
-write(path_int,1:10,"(10(i))");
-result = read(path_int,1,10,"(10(i))");
+write(path_int,1:10,"(10(i3))");
+result = read(path_int,1,10,"(10(i3))");
 assert_checkequal(result, int32(1:10));
-assert_checkerror("read(path_int,10,1,""(10(i))"");",refMsg);
+assert_checkerror("read(path_int,10,1,""(10(i3))"");",refMsg);
 
-result = read(path_int,-1,1,"(10(i))");
+result = read(path_int,-1,1,"(10(i3))");
 assert_checkequal(result, int32(1));
 
 mdelete(path_int);