Sfoglia il codice sorgente

added FileName function to resolve relative pathes to 'realpath's

Johannes Ruehle 11 anni fa
parent
commit
3bc5bbe46d
2 ha cambiato i file con 24 aggiunte e 0 eliminazioni
  1. 15 0
      core/basics/FileName.cpp
  2. 9 0
      core/basics/FileName.h

+ 15 - 0
core/basics/FileName.cpp

@@ -3,6 +3,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include <sys/param.h> //for MAXPATHLEN - the maximal path length
+
 
 using namespace std;
 
@@ -143,4 +145,17 @@ bool FileName::isRelative() const {
     return  fileName.substr(0,1) != "/";
 }
 
+bool FileName::convertToRealPath()
+{
+    char actualpath [MAXPATHLEN];
+    char *pRet = realpath( fileName.c_str(), actualpath);
+
+    //TODO: how to treat the return value???
+//    if( pRet == NULL)
+//        return false;
+
+    this->fileName = string( actualpath);
+    return true;
+}
+
 } // namespace

+ 9 - 0
core/basics/FileName.h

@@ -150,6 +150,15 @@ public:
       @return true if the filename is realtive
   */
   bool isRelative() const;
+
+  /** Converts the path into a real path with all relative path elements being resolved.
+   * Example:
+   *    path before: "/home/user/experiment/../config/conf.conf
+   *    path after : "/home/user/config/conf.conf
+   * @return true if resolution was successfull
+   */
+  bool convertToRealPath();
+
 private:
   //! stores the file name
   std::string fileName;