cleaning ...
Allan CORNET [Fri, 21 Aug 2009 13:49:28 +0000 (15:49 +0200)]
scilab/modules/output_stream/src/cpp/Diary.cpp
scilab/modules/output_stream/src/cpp/getFullFilename.cpp

index 4ff9ac0..865429a 100644 (file)
@@ -12,6 +12,7 @@
 */
 /*--------------------------------------------------------------------------*/ 
 #include <fstream> 
+#include <iostream> 
 #include "Diary.hxx"
 #include "getFullFilename.hxx"
 #include "getDiaryDate.hxx"
@@ -23,7 +24,7 @@ extern "C"
 /*--------------------------------------------------------------------------*/ 
 Diary::Diary(std::wstring _wfilename,int _mode,int ID)
 {
-       int wofstream_mode = 0;
+       std::ios::openmode wofstream_mode = std::ios::trunc | std::ios::binary;
 
        suspendwrite = false;
 
@@ -34,69 +35,37 @@ Diary::Diary(std::wstring _wfilename,int _mode,int ID)
        if (_mode == 0)
        {
                wofstream_mode = std::ios::trunc | std::ios::binary ;
-               
        }
        else
        {
                wofstream_mode = std::ios::app | std::ios::binary ;
        }
+
 #ifdef _MSC_VER
        std::wofstream fileDiary(getFullFilename(_wfilename).c_str(),wofstream_mode);
-       if ( fileDiary.bad())
-       {
-               wfilename = std::wstring(L"");
-               fileAttribMode = -1;
-               setID(-1);
-       }
-       else
-       {
-               wfilename = getFullFilename(_wfilename);
-               fileAttribMode = wofstream_mode;
-               setID(ID);
-       }
-       fileDiary.close();
-
 #else
        std::wstring wstrfile = getFullFilename(_wfilename);
        wchar_t *wcfile = (wchar_t*)wstrfile.c_str();
        char *filename = wide_string_to_UTF8(wcfile);
 
-       if (_mode == 0)
+       std::ofstream fileDiary(filename, wofstream_mode);
+
+       if (filename) {FREE(filename); filename = NULL;}
+#endif
+
+       if ( fileDiary.bad())
        {
-               std::ofstream fileDiary(filename,  std::ios::trunc | std::ios::binary) ;
-               if ( fileDiary.bad())
-               {
                wfilename = std::wstring(L"");
                fileAttribMode = -1;
                setID(-1);
-               }
-               else
-               {
-               wfilename = getFullFilename(_wfilename);
-               fileAttribMode = wofstream_mode;
-               setID(ID);
-               }       
-       fileDiary.close();
        }
        else
        {
-               std::ofstream fileDiary(filename, std::ios::app | std::ios::binary );
-               if ( fileDiary.bad())
-               {
-               wfilename = std::wstring(L"");
-               fileAttribMode = -1;
-               setID(-1);
-               }
-               else
-               {
                wfilename = getFullFilename(_wfilename);
                fileAttribMode = wofstream_mode;
                setID(ID);
-               }
-       fileDiary.close();
        }
-#endif
-
+       fileDiary.close();
 }
 /*--------------------------------------------------------------------------*/ 
 Diary::~Diary()
@@ -115,18 +84,22 @@ void Diary::write(std::wstring _wstr, bool bInput)
 {
        if (!suspendwrite)
        {
+               std::ios::openmode wofstream_mode = std::ios::app | std::ios::binary;
 #ifdef _MSC_VER
-               std::wofstream fileDiary(wfilename.c_str(),std::ios::app | std::ios::binary );
+               std::wofstream fileDiary(wfilename.c_str(), wofstream_mode );
 #else
                wchar_t *wcfile = (wchar_t*)wfilename.c_str();
                char *filename = wide_string_to_UTF8(wcfile);
-               std::ofstream fileDiary(filename, std::ios::app | std::ios::binary);
+               std::ofstream fileDiary(filename, wofstream_mode);
+               if (filename) {FREE(filename); filename = NULL;}
 #endif
+
                if (fileDiary.good())
                {
                        char *line = NULL;
 
 #ifdef _MSC_VER
+                       /* carriage return for Windows */
                        _wstr = replace(_wstr, std::wstring(L"\n"), std::wstring(L"\r\n"));
                        _wstr = replace(_wstr, std::wstring(L"\r\r"), std::wstring(L"\r"));
 #endif
@@ -167,6 +140,7 @@ void Diary::write(std::wstring _wstr, bool bInput)
 
                        if (line) {FREE(line); line = NULL;}
                }
+               fileDiary.close();
        }
 }
 /*--------------------------------------------------------------------------*/ 
index 9f90a44..4eb76ef 100644 (file)
@@ -28,10 +28,10 @@ static void wcsplitpath(const wchar_t* path, wchar_t* drv, wchar_t* dir, wchar_t
 /*--------------------------------------------------------------------------*/ 
 std::wstring getFullFilename(std::wstring _wfilename)
 {
-       wchar_t wcdrive[1024];
-       wchar_t wcdirectory[1024];
-       wchar_t wcname[1024];
-       wchar_t wcext [1024];
+       wchar_t wcdrive[PATH_MAX];
+       wchar_t wcdirectory[PATH_MAX];
+       wchar_t wcname[PATH_MAX];
+       wchar_t wcext [PATH_MAX];
 
        std::wstring wfullfilename(L"");
        std::wstring tmpWstr;
@@ -48,21 +48,21 @@ std::wstring getFullFilename(std::wstring _wfilename)
        wfullfilename.append(tmpWstr.assign(wcdirectory));
        if (wfullfilename.compare(L"") == 0)
        {
+               /* to get current directory as wide characters */
 #if _MSC_VER
-               wchar_t wcCurrentDir[1024];
+               wchar_t wcCurrentDir[PATH_MAX];
                if ( _wgetcwd(wcCurrentDir, PATH_MAX) != NULL)
-#else
-               char CurrentDir[1024];
-               if (getcwd(CurrentDir,1024) != NULL)
-#endif
                {
-#if _MSC_VER           
                        wfullfilename = tmpWstr.assign(wcCurrentDir);
 #else
+               char CurrentDir[PATH_MAX];
+               if (getcwd(CurrentDir, PATH_MAX) != NULL)
+               {
                        wchar_t *wcCurrentDir = to_wide_string(CurrentDir);
                        wfullfilename = tmpWstr.assign(wcCurrentDir);
                        FREE(wcCurrentDir);
 #endif
+                       /* replaces separator */
                        size_t found = wfullfilename.rfind(L"\\");
                        while (found != std::wstring::npos)
                        {
@@ -75,9 +75,6 @@ std::wstring getFullFilename(std::wstring _wfilename)
                {
                        wfullfilename.assign(L"");
                }
-#ifndef _MSC_VER
-               //if (CurrentDir) {FREE(CurrentDir); CurrentDir = NULL;}
-#endif
        }
        wfullfilename.append(tmpWstr.assign(wcname));
        wfullfilename.append(tmpWstr.assign(wcext));