Browse Source

added to class Config: save filename of extracted config file;
added function to create a absolute filename relative to the config's path

Johannes Ruehle 11 years ago
parent
commit
e3d99880ef
2 changed files with 36 additions and 1 deletions
  1. 21 1
      core/basics/Config.cpp
  2. 15 0
      core/basics/Config.h

+ 21 - 1
core/basics/Config.cpp

@@ -7,6 +7,7 @@
 #include "core/basics/Config.h"
 #include "core/basics/ossettings.h"
 #include "core/basics/StringTools.h"
+#include "core/basics/FileName.h"
 
 using namespace NICE;
   
@@ -26,6 +27,7 @@ Config::Config ( const std::string & configfn )
     read(configfn);
   }
   ioUntilEndOfFile = true;
+  m_sConfigFilename = configfn;
 }
 
 Config::Config ( int argc, 
@@ -43,6 +45,7 @@ Config::Config ( int argc,
 Config::Config ( const Config & conf ) : Persistent()
 {
   ioUntilEndOfFile = true;
+  m_sConfigFilename = conf.m_sConfigFilename;
 	confB.copyFrom ( conf.confB );
 	confD.copyFrom ( conf.confD );
 	confI.copyFrom ( conf.confI );
@@ -308,7 +311,7 @@ bool Config::keyExists ( const std::string & block, const std::string & key ) co
     return ( confS.keyExists ( block, key ) || 
              confI.keyExists ( block, key ) ||
 	     confB.keyExists ( block, key ) ||
-	     confD.keyExists ( block, key ) );
+             confD.keyExists ( block, key ) );
 }
 
 void Config::addHelp ( const std::string & block, const std::string & key,
@@ -568,3 +571,20 @@ void Config::getAllBlocks ( std::set< std::string > & list ) const
     confS.getAllBlocks ( list );
     confI.getAllBlocks ( list );
 }
+
+string Config::getAbsoluteFilenameRelativeToThisConfig(const string &p_Filename) const
+{
+    if( m_sConfigFilename.empty() )
+        return p_Filename;
+
+    NICE::FileName t_DatasetFilename( p_Filename );
+
+    if( !t_DatasetFilename.isRelative() )
+    {
+        return p_Filename;
+    }
+
+    NICE::FileName t_ConfigFilename( this->m_sConfigFilename );
+    return  t_ConfigFilename.extractPath().str() + p_Filename;
+}
+

+ 15 - 0
core/basics/Config.h

@@ -54,6 +54,8 @@ class Config : public NICE::Persistent
     /** read until end of file fore restore (default: yes)*/
     bool ioUntilEndOfFile;
 
+    /** stores filename the config was created from*/
+    std::string m_sConfigFilename;
   public:
 
       /** simplest constructor, create an empty config */
@@ -136,6 +138,19 @@ class Config : public NICE::Persistent
       */
       bool keyExists ( const std::string & block, const std::string & key ) const;
 
+      /** returns the filename the config was created by*/
+      std::string getFilename() const{ return m_sConfigFilename;}
+
+      /**
+       * @brief Returns the given filename as an absolute path relative to this config file' location
+       *
+       * If p_Filename is not a relative path, then the filepath is alread absolute and just return that!
+       *
+       * @param p_Filename filename being relative or absolute
+       * @return absolute filename
+       */
+      std::string getAbsoluteFilenameRelativeToThisConfig(const std::string &p_Filename) const;
+
       /** add a help text */
       void addHelp ( const std::string & block, const std::string & key,
 		     const std::string & helpText );