Back to index

salome-kernel  6.5.0
Classes | Enumerations | Functions
Kernel_Utils Namespace Reference

Classes

protocol  BASICS_EXPORT

Enumerations

enum  GUIDtype { DefUserID = 1, ObjectdID }
 GUID type. More...

Functions

std::string GetBaseName (const std::string &file_path)
std::string GetDirName (const std::string &file_path)
std::string GetTmpDirByEnv (const std::string &tmp_path_env)
std::string GetTmpDirByPath (const std::string &tmp_path)
std::string GetTmpDir ()
std::string GetTmpFileName ()
bool IsExists (const std::string &thePath)
bool IsWritable (const std::string &thePath)
std::string GetDirByPath (const std::string &thePath)
bool IsEmptyDir (const std::string &thePath)
std::string GetHostname ()
std::string GetGUID (GUIDtype)
 Get predefined GUID.
void print_traceback ()

Enumeration Type Documentation

GUID type.

Enumerator:
DefUserID 

Default user attribute ID.

ObjectdID 

Global usage object identifier ID.

Definition at line 55 of file Basics_Utils.hxx.

                {
    DefUserID = 1,  
    ObjectdID       
  };

Function Documentation

BASICS_EXPORT std::string Kernel_Utils::GetBaseName ( const std::string &  file_path)

Definition at line 45 of file Basics_DirUtils.cxx.

  {
    int pos = file_path.rfind( _separator_ );
    if ( pos >= 0 )
      return pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
    return file_path;
  }

Here is the caller graph for this function:

BASICS_EXPORT std::string Kernel_Utils::GetDirByPath ( const std::string &  thePath)

Definition at line 199 of file Basics_DirUtils.cxx.

  {
    if (thePath.empty())
      return "";
    std::string path = thePath;
    std::string::size_type length = path.length();

    //detect all separators in Unix format
    for ( unsigned int i = 0; i < length; i++ )
    {
      if( path[i] == '/' )
        path[i] = '|';
    }

    //detect all separators in Windows format
    for ( unsigned int i = 0; i < length; i++ )
    {
      if( path[i] == '\\' )
        path[i] = '|';
    }


    std::string::size_type pos = path.rfind('|');
    if ( pos == std::string::npos )
    {
#ifdef WIN32
      //check for disk letter ( C: )
      if ( path.length() == 2 && path[1] == ':' )
        path += _separator_;
#else
      //not valid path
      return "";
#endif
    }
    else
    {
      //remove right subdirectory or filename from path
      path = path.substr( 0, pos );
    }

    length = path.length();
    for ( unsigned int i = 0; i < length; i++ )
    {
      if( path[i] == '|' )
        path[i] = _separator_;
    }
    return path;
  }
BASICS_EXPORT std::string Kernel_Utils::GetDirName ( const std::string &  file_path)

Definition at line 53 of file Basics_DirUtils.cxx.

  {
    int pos = file_path.rfind( _separator_ );
    if ( pos >= 0 )
      return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : "";
    return std::string(".");
  }
BASICS_EXPORT std::string Kernel_Utils::GetGUID ( GUIDtype  type)

Get predefined GUID.

Definition at line 94 of file Basics_Utils.cxx.

  {
    std::string guid;

    switch ( type ) {
    case DefUserID:
      guid = "FFFFFFFF-D9CD-11d6-945D-1050DA506788"; break;
    case ObjectdID:
      guid = "C08F3C95-F112-4023-8776-78F1427D0B6D"; break;
    }

    return guid;
  }

Definition at line 38 of file Basics_Utils.cxx.

  {
    int ls = 100, r = 1;
    char *s;
    
    while (ls < 10000 && r)
      {
        ls *= 2;
        s = new char[ls];
        r = gethostname(s, ls-1);
        switch (r) 
          {
          case 0:
            break;
          default:
#ifdef EINVAL
          case EINVAL:
#endif
#ifdef ENAMETOOLONG
          case ENAMETOOLONG:
#endif
#ifdef WIN32
          case WSAEFAULT:  
#endif
            delete [] s;
            continue;
          }
        
      }
    
    if (r != 0)
      {
        s = new char[50];
        strcpy(s, "localhost");
      }
    
    // remove all after '.'
    char *aDot = (strchr(s,'.'));
    if (aDot) aDot[0] = '\0';
    
    std::string p = s;
    delete [] s;
    return p;
  }

Here is the caller graph for this function:

Definition at line 126 of file Basics_DirUtils.cxx.

  {
    return GetTmpDirByPath( "" );
  }

Here is the call graph for this function:

Here is the caller graph for this function:

BASICS_EXPORT std::string Kernel_Utils::GetTmpDirByEnv ( const std::string &  tmp_path_env)

Definition at line 61 of file Basics_DirUtils.cxx.

  {
    char* val = getenv( tmp_path_env.c_str() );
    std::string dir = val ? val : "";
    return GetTmpDirByPath( dir );
  }

Here is the call graph for this function:

BASICS_EXPORT std::string Kernel_Utils::GetTmpDirByPath ( const std::string &  tmp_path)

Definition at line 68 of file Basics_DirUtils.cxx.

  {
    std::string aTmpDir = tmp_path;
    if ( aTmpDir == "" )
      {
#ifdef WIN32
        char *Tmp_dir = getenv("TEMP");
        if( Tmp_dir == NULL )
          {
            Tmp_dir = getenv("TMP");
            if (Tmp_dir == NULL)
              aTmpDir = std::string("C:\\");
            else 
              aTmpDir = std::string(Tmp_dir);
          }
        else
          aTmpDir = std::string(Tmp_dir);
#else
        aTmpDir = std::string("/tmp/");
#endif
      }
    
    if(aTmpDir[aTmpDir.size()-1] != _separator_)
      aTmpDir+=_separator_;
    
    srand((unsigned int)time(NULL));
    int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
    char buffer[127];
    sprintf(buffer, "%d", aRND);
    std::string aSubDir(buffer);
    if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
    
    aTmpDir += aSubDir; //Get RND sub directory
    
    std::string aDir = aTmpDir;
    
    if(IsExists(aDir)) {
      for(aRND = 0; IsExists(aDir); aRND++) {
        sprintf(buffer, "%d", aRND);
        aDir = aTmpDir+buffer;  //Build a unique directory name
      }
    }
    
    if(aDir[aDir.size()-1] != _separator_) aDir += _separator_;
    
#ifdef WIN32
    CreateDirectory(aDir.c_str(), NULL);
#else
    mkdir(aDir.c_str(), 0x1ff); 
#endif
    
    return aDir;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 135 of file Basics_DirUtils.cxx.

  {
    std::string tmpDir = GetTmpDir();
    std::string aFilePath = "";
    if(IsExists(tmpDir)) {
      srand((unsigned int)time(NULL));
      int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
      char buffer[127];
      sprintf(buffer, "%d", aRND);
      std::string aSubDir(buffer);
      if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
      
      aFilePath = tmpDir;
      for(aRND = 0; IsExists(aFilePath); aRND++) {
        sprintf(buffer, "%d", aRND);
        aFilePath = tmpDir+buffer;  //Build a unique file name
      }
    }
    return aFilePath;
  }

Here is the call graph for this function:

BASICS_EXPORT bool Kernel_Utils::IsEmptyDir ( const std::string &  thePath)

Definition at line 253 of file Basics_DirUtils.cxx.

  {
    if ( thePath.empty() || !IsExists(thePath))
      return false;

    bool result = false;

#ifdef WIN32
    WIN32_FIND_DATA aFileData;
    HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData );
    if ( hFile == INVALID_HANDLE_VALUE )
    {
      //empty dir
      result = true;
    }
    else
    {
      //close serching. path is not empty
      FindClose( hFile );
    }
#else
    DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(thePath.c_str())) == NULL)
    {
      //Could not open directory
      return false;
    }
    else
    {
      result = true; //empty if no file found
      while ((dirp = readdir(dp)) != NULL && result )
        {
          std::string file_name(dirp->d_name);
          result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false
        }
        closedir(dp);
    }
#endif
    return result;
  }

Here is the call graph for this function:

BASICS_EXPORT bool Kernel_Utils::IsExists ( const std::string &  thePath)

Definition at line 160 of file Basics_DirUtils.cxx.

  {
#ifdef WIN32 
    if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
      if (  GetLastError () == ERROR_FILE_NOT_FOUND  ) {
        return false;
      }
    }
#else 
    int status = access ( thePath.c_str() , F_OK ); 
    if (status != 0) return false;
#endif
    return true;
  }

Here is the caller graph for this function:

BASICS_EXPORT bool Kernel_Utils::IsWritable ( const std::string &  thePath)

Definition at line 179 of file Basics_DirUtils.cxx.

  {
#ifdef WIN32 
    if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
      if (  GetLastError () == FILE_ATTRIBUTE_READONLY ) {
        return false;
      }
    }
#else 
    int status = access(thePath.c_str(),W_OK); 
    if (status != 0) return false;
#endif
    return true;
  }

Definition at line 109 of file Basics_Utils.cxx.

  {
    void *array[50];
    size_t size;
    char **strings;
    size_t i;

    size = backtrace (array, 40);
    strings = backtrace_symbols (array, size);

    for (i = 0; i < size; i++)
      {
        std::cerr << strings[i] << std::endl;
      }

    free (strings);
  }