]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkUtilities.cxx
are now generated in user's .bbtk dir
[bbtk.git] / kernel / src / bbtkUtilities.cxx
index 778b45e215e705a18e2926d9f7bfee6ed1e962e0..21fa9479f3ceb892e86e48b51a177caa9a57d885 100644 (file)
@@ -2,8 +2,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkUtilities.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/12/12 10:20:48 $
-  Version:   $Revision: 1.10 $
+  Date:      $Date: 2009/01/27 14:22:57 $
+  Version:   $Revision: 1.11 $
 =========================================================================*/
 
 /* ---------------------------------------------------------------------
@@ -300,6 +300,22 @@ namespace bbtk
     }
     return libname;
   }
+  // =======================================================================
+
+  // =======================================================================
+  /// Returns the user settings dir, e.g. /home/username/.bbtk
+  std::string Utilities::GetUserSettingsDir()
+  {
+#if defined(__GNUC__)
+    std::string str_home(getenv("HOME"));
+#elif defined(_WIN32)
+    std::string str_home(getenv("USERPROFILE"));
+#endif
+    std::string fullname = str_home + "/.bbtk";
+    MakeValidFileName(fullname);
+    return fullname;
+  }
+  
 
   // =======================================================================
   /// Builds the complete path to the file 'name' located 
@@ -312,116 +328,128 @@ namespace bbtk
     std::string str_home(getenv("USERPROFILE"));
 #endif
     std::string fullname = str_home + "/.bbtk/" + name;
-    Utilities::replace( fullname, 
-                       INVALID_FILE_SEPARATOR , 
-                       VALID_FILE_SEPARATOR);
+    MakeValidFileName(fullname);
     return fullname;
   }
+  // =======================================================================
   
 
+  // =======================================================================
+  void Utilities::CreateDirectoryIfNeeded( std::string const &dirName)
+  {
+    if (FileExists(dirName)) return;
+    std::string cmd("mkdir \"");
+    cmd += dirName;
+    cmd += "\"";
+    system(cmd.c_str());
+  }  
+  // =======================================================================
+  
+  
+  //========================================================================
+  bool Utilities::IsAtRoot(std::string cwd)
+  {
+    if ( cwd == "/"              // hope it gets /     (for Linux)
+        || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows)
+      return (true);
+    else
+      return(false);
+  }
+  // ======================================================================
 
-    //========================================================================
-    
-    bool Utilities::IsAtRoot(std::string cwd)
-    {
-      if ( cwd == "/"              // hope it gets /     (for Linux)
-          || (cwd.size() <= 3 && cwd[1] == ':') ) // hope it gets C: D: (for Windows)
-       return (true);
-      else
-       return(false);
-    }
-    
-    // ======================================================================
+  // ======================================================================
+  bool Utilities::IsDirectory(std::string const &dirName)
+  {
+    struct stat fs;
     
-    bool Utilities::IsDirectory(std::string const &dirName)
-    {
-      struct stat fs;
-      
-      if ( stat(dirName.c_str(), &fs) == 0 )
-       {
+    if ( stat(dirName.c_str(), &fs) == 0 )
+      {
 #if _WIN32
-         return ((fs.st_mode & _S_IFDIR) != 0);
+       return ((fs.st_mode & _S_IFDIR) != 0);
 #else
-         return S_ISDIR(fs.st_mode);
+       return S_ISDIR(fs.st_mode);
 #endif
-       }
-      else
-       {
-         return false;
-       }
-    }
-    
-    // ===================================================================================
-    
-    void Utilities::SplitAroundFirstDot( const std::string& in,
-                                            std::string& left,
-                                            std::string& right)
-    {
-      std::string delimiter = ".";
-      std::string::size_type pos = in.find_first_of(delimiter);
-      if (std::string::npos != pos) 
-       {
-         left = in.substr(0,pos);
-         right = in.substr(pos+1,in.size());
-         
-       }
-      else
-       {
-         left ="";
-         right = "";
-         bbtkGlobalError("Token '"<<in<<"' : expected 'a.b' format but no dot found");
-       }
-    }
-    //=======================================================================
-    void Utilities::SplitString ( const std::string& str, 
-                                    const std::string& delimiters, 
-                                    std::vector<std::string>& tokens)
-    {
-      // Skip delimiters at beginning.
-      std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
-      // Find first delimiter.
-      std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
-      
-      while (std::string::npos != pos || std::string::npos != lastPos)
-       {
-         // Found a token, add it to the vector.
-         tokens.push_back(str.substr(lastPos, pos - lastPos));
-         // Skip delimiters.  Note the "not_of"
-         lastPos = str.find_first_not_of(delimiters, pos);
-         // Find next delimiter
-         pos = str.find_first_of(delimiters, lastPos);
-       }
-      
-    }
-    //=======================================================================
-    
+      }
+    else
+      {
+       return false;
+      }
+  }
+  // =======================================================================
     
-    // ===================================================================================
+  // =======================================================================
+  void Utilities::SplitAroundFirstDot( const std::string& in,
+                                      std::string& left,
+                                      std::string& right)
+  {
+    std::string delimiter = ".";
+    std::string::size_type pos = in.find_first_of(delimiter);
+    if (std::string::npos != pos) 
+      {
+       left = in.substr(0,pos);
+       right = in.substr(pos+1,in.size());
+       
+      }
+    else
+      {
+       left ="";
+       right = "";
+       bbtkGlobalError("Token '"<<in<<"' : expected 'a.b' format but no dot found");
+      }
+  }
+  //=======================================================================
+
+  //=======================================================================
+  void Utilities::SplitString ( const std::string& str, 
+                               const std::string& delimiters, 
+                               std::vector<std::string>& tokens)
+  {
+    // Skip delimiters at beginning.
+    std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
+    // Find first delimiter.
+    std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
     
-    std::string Utilities::get_file_name(const std::string& s) 
-    { 
-      std::string::size_type slash_position = s.find_last_of("/\\");
-      if (slash_position != std::string::npos) 
-       {
-         return  s.substr(slash_position+1,std::string::npos);   
-       }
-      else 
-       {
-         return s;
-       }
-    }
+    while (std::string::npos != pos || std::string::npos != lastPos)
+      {
+       // Found a token, add it to the vector.
+       tokens.push_back(str.substr(lastPos, pos - lastPos));
+       // Skip delimiters.  Note the "not_of"
+       lastPos = str.find_first_not_of(delimiters, pos);
+       // Find next delimiter
+       pos = str.find_first_of(delimiters, lastPos);
+      }
     
-    // ===================================================================================
-    /**
-     * \brief   Explore a directory with possibility of recursion
-     *          return number of files read
-     * @param  dirpath   directory to explore
-     * @param  recursive whether we want recursion or not
-     */
-    int Utilities::Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames)
-    {
-      int numberOfFiles = 0;
-      std::string fileName;
+  }
+  //=======================================================================
+  
+  
+  // ======================================================================
+  std::string Utilities::get_file_name(const std::string& s) 
+  { 
+    std::string::size_type slash_position = s.find_last_of("/\\");
+    if (slash_position != std::string::npos) 
+      {
+       return  s.substr(slash_position+1,std::string::npos);   
+      }
+    else 
+      {
+       return s;
+      }
+  }
+  //=======================================================================
+
+  // ========================================================================
+  /**
+   * \brief   Explore a directory with possibility of recursion
+   *          return number of files read
+   * @param  dirpath   directory to explore
+   * @param  recursive whether we want recursion or not
+   */
+  int Utilities::Explore(std::string const &dirpath, bool recursive, std::vector<std::string> &Filenames)
+  {
+    int numberOfFiles = 0;
+    std::string fileName;
       
       std::string dirName = dirpath;
       
@@ -518,7 +546,8 @@ namespace bbtk
   return numberOfFiles;
 
 }
-
+  //=======================================================================
 
     //=======================================================================
     // Replaces substrings "\\n" by a real carriage return "\n"
@@ -537,30 +566,180 @@ namespace bbtk
     //=======================================================================
 
 
+   //=======================================================================
 
-bool Utilities::loosematch(std::string stdLine,std::string stdOptions) 
-{
-       bool result=false;
-         std::vector<std::string> tokens;
-         SplitString ( stdOptions,"|", tokens);
-         int i,size=tokens.size();  
-         for (i=0; i<size; i++)
-         {               
+  bool Utilities::loosematch(std::string stdLine,std::string stdOptions) 
+  {
+    bool result=false;
+    std::vector<std::string> tokens;
+    SplitString ( stdOptions,"|", tokens);
+    int i,size=tokens.size();  
+    for (i=0; i<size; i++)
+      {                  
 #ifdef WIN32
-                 if ( strcmpi(stdLine.c_str(),tokens[i].c_str())==0) 
-                 
-                         result=true; 
-                 }               
+       if ( strcmpi(stdLine.c_str(),tokens[i].c_str())==0) 
+         { 
+           result=true; 
+         }               
 #else
-                 if ( strcasecmp(stdLine.c_str(),tokens[i].c_str())==0) 
-                 
-                         result=true; 
-                 }               
+       if ( strcasecmp(stdLine.c_str(),tokens[i].c_str())==0) 
+         { 
+           result=true; 
+         }               
 #endif
-
+       
+      }
+    return result;
+  }
+  //=========================================================================  
+  
+  //=========================================================================  
+  // From http://www.fltk.org/newsgroups.php?gfltk.general+v:22083
+  //
+  int get_app_path (char *pname, size_t pathsize)
+  {
+#ifdef LINUX
+    /* Oddly, the readlink(2) man page says no NULL is appended. */
+    /* So you have to do it yourself, based on the return value: */
+    pathsize --; /* Preserve a space to add the trailing NULL */
+    long result = readlink("/proc/self/exe", pname, pathsize);
+    if (result > 0)
+      {
+       pname[result] = 0; /* add the #@!%ing NULL */
+       
+       if ((access(pname, 0) == 0))
+         return 0; /* file exists, return OK */
+       /*else name doesn't seem to exist, return FAIL (falls
+         through) */
+      }
+#endif /* LINUX */
+    
+#ifdef WIN32
+    long result = GetModuleFileName(NULL, pname, pathsize);
+    if (result > 0)
+      {
+       /* fix up the dir slashes... */
+       int len = strlen(pname);
+       int idx;
+       for (idx = 0; idx < len; idx++)
+         {
+           if (pname[idx] == '\\') pname[idx] = '/';
          }
-         return result;
-}
+       
+       for (idx = len-1; idx >=0 ; idx--)
+         {
+           if (pname[idx] == '/')
+             { 
+               pname[idx+1] = '\0';
+               idx = -1;
+             }
+         }
+       
+       if ((access(pname, 0) == 0))
+         return 0; /* file exists, return OK */
+       /*else name doesn't seem to exist, return FAIL (falls
+         through) */
+      }
+#endif /* WIN32 */
+    
+#ifdef SOLARIS
+    char *p = getexecname();
+    if (p)
+      {
+       /* According to the Sun manpages, getexecname will
+          "normally" return an */
+       /* absolute path - BUT might not... AND that IF it is not,
+          pre-pending */
+       /* getcwd() will "usually" be the correct thing... Urgh!
+        */
+       
+       /* check pathname is absolute (begins with a / ???) */
+       if (p[0] == '/') /* assume this means we have an
+                           absolute path */
+         {
+           strncpy(pname, p, pathsize);
+           if ((access(pname, 0) == 0))
+             return 0; /* file exists, return OK */
+         }
+       else /* if not, prepend getcwd() then check if file
+               exists */
+         {
+           getcwd(pname, pathsize);
+           long result = strlen(pname);
+           strncat(pname, "/", (pathsize - result));
+           result ++;
+           strncat(pname, p, (pathsize - result));
+           
+           if ((access(pname, 0) == 0))
+             return 0; /* file exists, return OK */
+           /*else name doesn't seem to exist, return FAIL
+             (falls through) */
+         }
+      }
+#endif /* SOLARIS */
+    
+#ifdef MACOSX /* assume this is OSX */
+    /*
+      from http://www.hmug.org/man/3/NSModule.html
+      
+      extern int _NSGetExecutablePath(char *buf, unsigned long
+      *bufsize);
+      
+      _NSGetExecutablePath  copies  the  path  of the executable
+      into the buffer and returns 0 if the path was successfully
+      copied  in the provided buffer. If the buffer is not large
+      enough, -1 is returned and the  expected  buffer  size  is
+      copied  in  *bufsize.  Note that _NSGetExecutablePath will
+      return "a path" to the executable not a "real path" to the
+      executable.  That  is  the path may be a symbolic link and
+      not the real file. And with  deep  directories  the  total
+      bufsize needed could be more than MAXPATHLEN.
+    */
+    int status = -1;
+    char *given_path = (char*)malloc(MAXPATHLEN * 2);
+    if (!given_path) return status;
+    
+    uint32_t npathsize = MAXPATHLEN * 2;
+    long result = _NSGetExecutablePath(given_path, &npathsize);
+    if (result == 0)
+      { /* OK, we got something - now try and resolve the real path...
+        */
+       if (realpath(given_path, pname) != NULL)
+         {
+           if ((access(pname, 0) == 0))
+             status = 0; /* file exists, return OK */
+         }
+      }
+    free (given_path);
+    return status;
+#endif /* MACOSX */
+    
+    return -1; /* Path Lookup Failed */
+  } 
+  //=========================================================================
+       
+
+       
+  //=========================================================================
+  std::string Utilities::GetExecutablePath()
+  {
+    char name[PATH_MAX];
+    int err = get_app_path(name, PATH_MAX);
+    if (err) 
+      {
+       bbtkGlobalError("Could not determine current executable path ?");  
+      }
+    
+    // remove the exe name
+    char *slash;               
+    slash = strrchr(name, VALID_FILE_SEPARATOR_CHAR);
+    if (slash)
+      {
+       *slash = 0;
+      }
+    return name;
+  }
+  //=========================================================================