0027675: Foundation Classes - handle Unicode path to CSF_UnitsLexicon and CSF_UnitsDe...
authorkgv <kgv@opencascade.com>
Wed, 13 Jul 2016 19:47:16 +0000 (22:47 +0300)
committerbugmaster <bugmaster@opencascade.com>
Thu, 21 Jul 2016 08:07:03 +0000 (11:07 +0300)
Units package now uses Unicode-aware functions OSD_OpenStream
and OSD_FileStatCTime (introduced for fetching file timestamp).

src/OSD/OSD_OpenFile.cxx
src/OSD/OSD_OpenFile.hxx
src/Units/Units.cxx
src/Units/Units_Lexicon.cxx
src/Units/Units_UnitsDictionary.cxx
src/Units/Units_UnitsLexicon.cxx

index 0be7e84be4c8347e2d698ea22801fa5af82dde85..f99c9dcf2a7b7dcc15d88ce93a0bd487a23b3f39 100644 (file)
@@ -19,6 +19,9 @@
 #include <TCollection_ExtendedString.hxx>
 #include <NCollection_UtfString.hxx>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
 // ==============================================
 // function : OSD_OpenFile
 // purpose : Opens file
@@ -160,3 +163,28 @@ void OSD_OpenStream (std::ifstream&                    theStream,
   theStream.open (aString.ToCString(), theMode);
 #endif
 }
+
+// ==============================================
+// function : OSD_FileStatCTime
+// purpose :
+// ==============================================
+Standard_Time OSD_FileStatCTime (const char* theName)
+{
+  Standard_Time aTime = 0;
+#if defined(_WIN32)
+  // file name is treated as UTF-8 string and converted to UTF-16 one
+  const TCollection_ExtendedString aFileNameW (theName, Standard_True);
+  struct __stat64 aStat;
+  if (_wstat64 ((const wchar_t* )aFileNameW.ToExtString(), &aStat) == 0)
+  {
+    aTime = (Standard_Time )aStat.st_ctime;
+  }
+#else
+  struct stat aStat;
+  if (stat (theName, &aStat) == 0)
+  {
+    aTime = (Standard_Time )aStat.st_ctime;
+  }
+#endif
+  return aTime;
+}
index ca8bf7bf56b7b9ff808023eca0e80356ef41b392..2944c36225775fdb6b7af1e90a8ac301335c4cff 100644 (file)
@@ -80,6 +80,11 @@ __Standard_API void OSD_OpenFileBuf (std::filebuf& theBuff,
 __Standard_API FILE* OSD_OpenFile (const TCollection_ExtendedString& theName,
                                    const char* theMode);
 
+//! Function retrieves file timestamp.
+//! @param theName name of file encoded in UTF-8
+//! @return stat.st_ctime value
+__Standard_API Standard_Time OSD_FileStatCTime (const char* theName);
+
 extern "C" {
 #endif // __cplusplus
 
index 826867ec081813e704f8393d04ff482cbd3c0241..58ca07a43988ce2f7338156b8bd4705c63faa830 100644 (file)
@@ -22,8 +22,6 @@
 //             Convertir correctement les unites translatees
 
 #include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 
 #include <Units.hxx>
 #include <Units_Measurement.hxx>
index 1aecc6ff37234b424c601c37a77d67226385c304..156598dc712dfecb5877627120172b7ece9e79ab 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
+#include <Units_Lexicon.hxx>
 
 #include <OSD.hxx>
+#include <OSD_OpenFile.hxx>
 #include <Standard_Type.hxx>
 #include <TCollection_AsciiString.hxx>
 #include <TCollection_HAsciiString.hxx>
-#include <Units_Lexicon.hxx>
 #include <Units_Token.hxx>
 
-#include <sys/stat.h>
-#include <sys/types.h>
 IMPLEMENT_STANDARD_RTTIEXT(Units_Lexicon,MMgt_TShared)
 
 #ifdef _MSC_VER
@@ -56,7 +55,8 @@ static inline bool strrightadjust (char *str)
 
 void Units_Lexicon::Creates(const Standard_CString afilename)
 {
-  ifstream file(afilename, ios::in);
+  std::ifstream file;
+  OSD_OpenStream (file, afilename, std::ios::in);
   if(!file) {
 #ifdef OCCT_DEBUG
     cout<<"unable to open "<<afilename<<" for input"<<endl;
@@ -66,9 +66,7 @@ void Units_Lexicon::Creates(const Standard_CString afilename)
 
   thefilename = new TCollection_HAsciiString(afilename);
   thesequenceoftokens = new Units_TokensSequence();
-
-  struct stat buf;
-  if(!stat(afilename,&buf)) thetime = buf.st_ctime;
+  thetime = OSD_FileStatCTime (afilename);
 
   // read file line-by-line; each line has fixed format:
   // first 30 symbols for prefix or symbol (e.g. "k" for kilo)
@@ -122,15 +120,10 @@ void Units_Lexicon::Creates(const Standard_CString afilename)
 
 Standard_Boolean Units_Lexicon::UpToDate() const
 {
-  struct stat buf;
-  TCollection_AsciiString string = FileName();
-
-  if(!stat(string.ToCString(),&buf)) {
-    if(thetime >= (Standard_Time)buf.st_ctime)
-      return Standard_True;
-  }
-
-  return Standard_False;
+  TCollection_AsciiString aPath = FileName();
+  Standard_Time aTime = OSD_FileStatCTime (aPath.ToCString());
+  return aTime != 0
+      && aTime <= thetime;
 }
 
 
index 17d3f23d024a239d511e111a1452a00a4c4acb7c..a9d974f46da97c2c7a1cfb5072c8be9db2f40614 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
+#include <Units_UnitsDictionary.hxx>
 
 #include <OSD.hxx>
+#include <OSD_OpenFile.hxx>
 #include <Standard_Stream.hxx>
 #include <Standard_Type.hxx>
 #include <TCollection_AsciiString.hxx>
 #include <Units_Token.hxx>
 #include <Units_TokensSequence.hxx>
 #include <Units_Unit.hxx>
-#include <Units_UnitsDictionary.hxx>
 #include <Units_UnitSentence.hxx>
 #include <Units_UnitsLexicon.hxx>
 #include <Units_UnitsSequence.hxx>
 
 #include <stdio.h>
-#include <sys/stat.h>
+
 IMPLEMENT_STANDARD_RTTIEXT(Units_UnitsDictionary,MMgt_TShared)
 
 //=======================================================================
@@ -69,8 +70,9 @@ void Units_UnitsDictionary::Creates(const Standard_CString afilename)
   Handle(Units_Unit) unit;
   Handle(Units_ShiftedUnit) shiftedunit;
   Handle(Units_Quantity) quantity;
-  
-  ifstream file(afilename, ios::in);
+
+  std::ifstream file;
+  OSD_OpenStream (file, afilename, std::ios::in);
   if(!file) {
 #ifdef OCCT_DEBUG
     cout<<"unable to open "<<afilename<<" for input"<<endl;
@@ -79,9 +81,7 @@ void Units_UnitsDictionary::Creates(const Standard_CString afilename)
   }
   
   thefilename = new TCollection_HAsciiString(afilename);
-
-  struct stat buf;
-  if(!stat(afilename,&buf)) thetime = buf.st_ctime;
+  thetime = OSD_FileStatCTime (afilename);
 
   thequantitiessequence = new Units_QuantitiesSequence();
   
@@ -314,16 +314,11 @@ void Units_UnitsDictionary::Creates(const Standard_CString afilename)
 
 Standard_Boolean Units_UnitsDictionary::UpToDate() const
 {
-  struct stat buf;
-  TCollection_AsciiString string = thefilename->String();
-  if(!stat(string.ToCString(),&buf)) {
-    if(thetime == (Standard_Time)buf.st_ctime) return Standard_True;
-  }
-
-  return Standard_False;
+  Standard_Time aTime = OSD_FileStatCTime (thefilename->String().ToCString());
+  return aTime != 0
+      && aTime == thetime;
 }
 
-
 //=======================================================================
 //function : ActiveUnit
 //purpose  : 
index 8c12e7881dab6ee289dc6228b7d48bfb6c1b651e..50cb2e4862725e55e0885df87f23e8495e839b14 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
+#include <Units_UnitsLexicon.hxx>
 
+#include <OSD_OpenFile.hxx>
 #include <Standard_Type.hxx>
 #include <TCollection_AsciiString.hxx>
 #include <TCollection_HAsciiString.hxx>
 #include <Units.hxx>
 #include <Units_Token.hxx>
 #include <Units_UnitsDictionary.hxx>
-#include <Units_UnitsLexicon.hxx>
 
-#include <sys/stat.h>
-#include <sys/types.h>
 IMPLEMENT_STANDARD_RTTIEXT(Units_UnitsLexicon,Units_Lexicon)
 
 //=======================================================================
@@ -44,11 +43,13 @@ void Units_UnitsLexicon::Creates(const Standard_CString afilename1,
                                 const Standard_Boolean amode)
 {
   Handle(Units_UnitsDictionary) unitsdictionary;
-  struct stat buf;
 
   thefilename = new TCollection_HAsciiString(afilename2);
-
-  if(!stat(afilename2,&buf)) thetime = buf.st_ctime;
+  Standard_Time aTime2 = OSD_FileStatCTime (afilename2);
+  if (aTime2 != 0)
+  {
+    thetime = aTime2;
+  }
 
   Units_Lexicon::Creates(afilename1);
 
@@ -63,18 +64,15 @@ void Units_UnitsLexicon::Creates(const Standard_CString afilename1,
 
 Standard_Boolean Units_UnitsLexicon::UpToDate() const
 {
-  struct stat buf;
-  TCollection_AsciiString string = FileName2();
-
-  if(Units_Lexicon::UpToDate())
-    {
-      if(!stat(string.ToCString(),&buf))
-       {
-         if(thetime >= (Standard_Time)buf.st_ctime) return Standard_True;
-       }
-    }
-
-  return Standard_False;
+  TCollection_AsciiString aPath = FileName2();
+  if (!Units_Lexicon::UpToDate())
+  {
+    return Standard_False;
+  }
+
+  Standard_Time aTime = OSD_FileStatCTime (aPath.ToCString());
+  return aTime != 0
+      && aTime <= thetime;
 }
 
 //=======================================================================