0029152: Coding Rules - eliminate GCC compiler warnings -Wmisleading-indentation...
[occt.git] / src / OSD / OSD_Directory.cxx
index de8467b..52999e0 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#ifndef _WIN32
-
 #include <OSD_Directory.hxx>
+
 #include <OSD_Path.hxx>
 #include <OSD_Protection.hxx>
 #include <OSD_WhoAmI.hxx>
-
-#include <errno.h>
-#include <stdio.h>
-#include <sys/stat.h>
-#include <unistd.h>
-const OSD_WhoAmI Iam = OSD_WDirectory;
-
-
-OSD_Directory::OSD_Directory():OSD_FileNode() {
-}
-
-OSD_Directory::OSD_Directory(const OSD_Path& Name):OSD_FileNode(Name){
-}
-
-// Create physically a directory
-
-void OSD_Directory::Build(const OSD_Protection& Protect){
-int status;
-Standard_Integer internal_prot;
-TCollection_AsciiString aBuffer;
-
- internal_prot = Protect.Internal();
- myPath.SystemName(aBuffer);
- umask ( 0 );
- status = mkdir (aBuffer.ToCString(), (mode_t)internal_prot);
- if (status == -1 && errno == ENOENT)
- {
-   OSD_Path aSupPath = myPath;
-   aSupPath.UpTrek();
-   aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
-   OSD_Directory aSupDir (aSupPath);
-   aSupDir.Build (Protect);
-   if (aSupDir.Failed())
-   {
-     myError = aSupDir.myError;
-     return;
-   }
-   status = mkdir (aBuffer.ToCString(), (mode_t)internal_prot);
- }
- if (status == -1 && errno != EEXIST) {
-   Standard_Character err_message[2048];
-   Sprintf (err_message, "OSD_Directory::Build Directory \"%.2000s\"",
-           aBuffer.ToCString());
-   myError.SetValue (errno, Iam, err_message);
- }
-}
-
-OSD_Directory OSD_Directory::BuildTemporary(){
-OSD_Directory           aDirectoryToReturn;
-char                    name[] = "/tmp/CSFXXXXXX";
-
- // Create a temporary directory with 0700 permissions.
- if (NULL == mkdtemp( name ))
-   return aDirectoryToReturn; // Can't create a directory
-  
- unlink(name);//Destroys link but directory still exists while
-              //current process lives.
-
- TCollection_AsciiString aString (name);
- aDirectoryToReturn.SetPath ( aString );
- return aDirectoryToReturn;
-
-}
-
-#else
-
-//------------------------------------------------------------------------
-//-------------------  WNT Sources of OSD_Diretory -----------------------
-//------------------------------------------------------------------------
-
-#include <OSD_Directory.hxx>
-#include <OSD_Protection.hxx>
 #include <Standard_ProgramError.hxx>
 #include <TCollection_ExtendedString.hxx>
 #include <NCollection_String.hxx>
 
-#include <OSD_WNT_1.hxx>
-
-#include <stdio.h>
+#ifdef _WIN32
+  #include <OSD_WNT_1.hxx>
+  #include <stdio.h>
 
-#ifndef _INC_TCHAR
-include <tchar.h>
-#endif  // _INC_TCHAR
+  #ifndef _INC_TCHAR
+    #include <tchar.h>
+  #endif
 
-#ifdef _UNICODE
-# define tctmpnam _wtmpnam
+  void _osd_wnt_set_error (OSD_Error&, OSD_WhoAmI, ... );
 #else
-# define tctmpnam tmpnam
-#endif  // _UNICODE
-
-void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
-
-OSD_Directory :: OSD_Directory () {
-
-}  // end constructor ( 1 )
+  #include <errno.h>
+  #include <stdio.h>
+  #include <sys/stat.h>
+  #include <unistd.h>
 
-OSD_Directory :: OSD_Directory ( const OSD_Path& Name ) :
-                 OSD_FileNode ( Name ) {
-
-}  // end constructor ( 2 )
-
-void OSD_Directory :: Build (const OSD_Protection& Protect) {
-
- TCollection_AsciiString dirName;
+  const OSD_WhoAmI Iam = OSD_WDirectory;
+#endif
 
- myPath.SystemName ( dirName );
+// =======================================================================
+// function : OSD_Directory
+// purpose  :
+// =======================================================================
+OSD_Directory::OSD_Directory()
+{
+  //
+}
 
- if (  dirName.IsEmpty ()  )
+// =======================================================================
+// function : OSD_Directory
+// purpose  :
+// =======================================================================
+OSD_Directory::OSD_Directory (const OSD_Path& theName)
+: OSD_FileNode (theName)
+{
+  //
+}
 
-  throw Standard_ProgramError ( "OSD_Directory :: Build (): incorrect call - no directory name");
+// =======================================================================
+// function : Build
+// purpose  :
+// =======================================================================
+void OSD_Directory::Build (const OSD_Protection& theProtect)
+{
+#ifdef _WIN32
+  TCollection_AsciiString aDirName;
+  myPath.SystemName (aDirName);
+  if (aDirName.IsEmpty())
+  {
+    throw Standard_ProgramError ( "OSD_Directory::Build(): incorrect call - no directory name");
+  }
 
   Standard_Boolean isOK = Exists();
-  if (! isOK)
+  if (!isOK)
   {
     // myError will be set to fail by Exists() if intermediate dirs do not exist
     myError.Reset();
 
     // create directory if it does not exist;
-    TCollection_ExtendedString dirNameW(dirName);
-    if (CreateDirectoryW (dirNameW.ToWideString(), NULL))
+    TCollection_ExtendedString aDirNameW (aDirName);
+    if (CreateDirectoryW (aDirNameW.ToWideString(), NULL))
     {
       isOK = Standard_True;
     }
@@ -148,43 +91,87 @@ void OSD_Directory :: Build (const OSD_Protection& Protect) {
       aSupPath.UpTrek();
       aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
       OSD_Directory aSupDir (aSupPath);
-      aSupDir.Build (Protect);
+      aSupDir.Build (theProtect);
       if (aSupDir.Failed())
       {
         myError = aSupDir.myError;
         return;
       }
-      isOK = (CreateDirectoryW (dirNameW.ToWideString(), NULL) != 0);
+      isOK = (CreateDirectoryW (aDirNameW.ToWideString(), NULL) != 0);
     }
   }
 
   if (isOK)
   {
 #ifndef OCCT_UWP
-    SetProtection(Protect);
+    SetProtection (theProtect);
 #else
-    (void)Protect;
+    (void)theProtect;
 #endif
   }
   else
   {
-    _osd_wnt_set_error ( myError, OSD_WDirectory );
+    _osd_wnt_set_error (myError, OSD_WDirectory);
   }
-}  // end OSD_Directory :: Build
-
-OSD_Directory OSD_Directory :: BuildTemporary () {
-
- OSD_Directory           retVal;
- OSD_Protection          prt;
-
- wchar_t* aName = _wtmpnam(NULL);
- OSD_Path dirPath (TCollection_AsciiString (aName != NULL ? aName : L""));
-
- retVal.SetPath ( dirPath );
- retVal.Build ( prt );                            
+#else
+  TCollection_AsciiString aBuffer;
+  mode_t anInternalProt = (mode_t )theProtect.Internal();
+  myPath.SystemName (aBuffer);
+  umask (0);
+  int aStatus = mkdir (aBuffer.ToCString(), anInternalProt);
+  if (aStatus == -1 && errno == ENOENT)
+  {
+    OSD_Path aSupPath = myPath;
+    aSupPath.UpTrek();
+    aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
+    OSD_Directory aSupDir (aSupPath);
+    aSupDir.Build (theProtect);
+    if (aSupDir.Failed())
+    {
+      myError = aSupDir.myError;
+      return;
+    }
+    aStatus = mkdir (aBuffer.ToCString(), anInternalProt);
+  }
+  if (aStatus == -1 && errno != EEXIST)
+  {
+    char anErrMsg[2048];
+    Sprintf (anErrMsg, "OSD_Directory::Build Directory \"%.2000s\"", aBuffer.ToCString());
+    myError.SetValue (errno, Iam, anErrMsg);
+  }
+#endif
+}
 
- return retVal;
+// =======================================================================
+// function : BuildTemporary
+// purpose  :
+// =======================================================================
+OSD_Directory OSD_Directory::BuildTemporary()
+{
+#ifdef _WIN32
+  wchar_t* aTmpNameW = _wtmpnam (NULL);
+  if (aTmpNameW == NULL)
+  {
+    return OSD_Directory();
+  }
 
-}  // end OSD_Directory :: BuildTemporary
+  TCollection_AsciiString aTmpName (aTmpNameW);
+  OSD_Path aDirPath (aTmpName);
+  OSD_Directory aDir;
+  aDir.SetPath (aDirPath);
+  aDir.Build (OSD_Protection());
+  return aDir;
+#else
+  // create a temporary directory with 0700 permissions
+  char aTmpName[] = "/tmp/CSFXXXXXX";
+  if (NULL == mkdtemp (aTmpName))
+  {
+    return OSD_Directory(); // can't create a directory
+  }
 
+  unlink (aTmpName); // destroys link but directory still exists while current process lives
+  OSD_Directory aDir;
+  aDir.SetPath (TCollection_AsciiString (aTmpName));
+  return aDir;
 #endif
+}