WSL/SLF GitLab Repository

Commit 6bf3bbee authored by Mathias Bavay's avatar Mathias Bavay
Browse files

A new IOUtils::getCWD() method has been created, a new...

A new IOUtils::getCWD() method has been created, a new Config::getConfigRootDir() has been created (to help resolve relative paths) and is now used for the ADD and MULT filters as well as for the EXCLUDE feature of PSQLIO and IOHandler.
parent eefb432e
......@@ -25,13 +25,14 @@ namespace mio {
const std::string Config::defaultSection = "GENERAL";
//Constructors
Config::Config() : properties(), imported(), sourcename()
Config::Config() : properties(), imported(), sourcename(), configRootDir()
{
//nothing is even put in the property map, the user will have to fill it by himself
}
Config::Config(const std::string& i_filename) : properties(), imported(), sourcename(i_filename)
Config::Config(const std::string& i_filename) : properties(), imported(), sourcename(i_filename), configRootDir(IOUtils::getPath(i_filename, true))
{
addFile(i_filename);
}
......@@ -48,12 +49,14 @@ ConfigProxy Config::get(const std::string& key, const std::string& section, cons
//Populating the property map
void Config::addFile(const std::string& i_filename)
{
if (configRootDir.empty()) configRootDir = IOUtils::getPath(i_filename, true);
sourcename = i_filename;
parseFile(i_filename);
}
void Config::addCmdLine(const std::string& cmd_line)
{
if (configRootDir.empty()) configRootDir = IOUtils::getPath(IOUtils::getCWD(), true); //resolve symlinks, etc
sourcename = std::string("Command line");
parseCmdLine(cmd_line);
}
......@@ -253,6 +256,11 @@ std::string Config::getSourceName() const
return sourcename;
}
std::string Config::getConfigRootDir() const
{
return configRootDir;
}
size_t Config::findKeys(std::vector<std::string>& vecResult, const std::string& keymatch,
std::string section, const bool& anywhere) const
{
......
......@@ -116,10 +116,16 @@ class Config {
/**
* @brief Returns the filename that the Config object was constructed with.
* @return std::string The absolute filename of the key/value file.
* @return The absolute filename of the key/value file.
*/
std::string getSourceName() const;
/**
* @brief Returns the directory where the root configuration file is (needed to resolv relative paths).
* @return The absolute path to the root config file (resolved for symlinks, relative paths, etc).
*/
std::string getConfigRootDir() const;
/**
* @brief Return if a given key exists in a given section
* @param[in] key string representing the key to be searched
......@@ -302,6 +308,7 @@ class Config {
std::map<std::string, std::string> properties; //Save key value pairs
std::vector<std::string> imported; //list of files already imported (to avoid circular references)
std::string sourcename; //description of the data source for the key/value pair
std::string configRootDir; //directory of the root config file
}; //end class definition Config
class ConfigProxy {
......
......@@ -103,8 +103,8 @@ std::string removeExtension(const std::string& filename)
{
const size_t start_basename = filename.find_last_of("/\\"); //we will skip the path
const size_t startpos = filename.find_last_of('.');
if( startpos==std::string::npos ) return filename;
if( start_basename!=std::string::npos && startpos<start_basename ) return filename;
if ( startpos==std::string::npos ) return filename;
if ( start_basename!=std::string::npos && startpos<start_basename ) return filename;
return filename.substr(0, startpos);
}
......@@ -113,7 +113,7 @@ std::string getPath(const std::string& filename, const bool& resolve)
{
const std::string clean_filename = cleanPath(filename, resolve);
const size_t end_path = clean_filename.find_last_of("/");
if(end_path!=std::string::npos) {
if (end_path!=std::string::npos) {
return clean_filename.substr(0, end_path);
} else {
return cleanPath("./", resolve);
......@@ -123,7 +123,7 @@ std::string getPath(const std::string& filename, const bool& resolve)
std::string getFilename(const std::string& path)
{
const size_t start_basename = path.find_last_of("/\\");
if(start_basename!=std::string::npos)
if (start_basename!=std::string::npos)
return path.substr(start_basename+1, std::string::npos);
else
return path;
......@@ -132,7 +132,7 @@ std::string getFilename(const std::string& path)
bool validFileName(const std::string& filename)
{
const size_t startpos = filename.find_first_not_of(" \t\n"); // Find the first character position after excluding leading blank spaces
if((startpos!=0) || (filename==".") || (filename=="..")) {
if ((startpos!=0) || (filename==".") || (filename=="..")) {
return false;
}
......@@ -150,6 +150,15 @@ bool isAbsolutePath(const std::string& in_path)
}
#if defined _WIN32 || defined __MINGW32__
std::string getCWD()
{
char buffer[MAX_PATH+1];
const DWORD status = GetCurrentDirectoryA(sizeof(buffer), buffer);
if (status==0 || status>MAX_PATH) throw IOException("Can not get current working directory", AT);
return std::string cwd( buffer );
}
bool fileExists(const std::string& filename)
{
return ( GetFileAttributes( filename.c_str() ) != INVALID_FILE_ATTRIBUTES );
......@@ -188,6 +197,14 @@ void readDirectory(const std::string& path, std::list<std::string>& dirlist, con
FindClose(hFind);
}
#else
std::string getCWD()
{
char buffer[1024];
if ( getcwd(buffer,sizeof(buffer))==NULL ) throw IOException("Can not get current working directory", AT);
return std::string( buffer );
}
bool fileExists(const std::string& filename)
{
struct stat buffer ;
......
......@@ -76,6 +76,12 @@ namespace IOUtils {
*/
std::string removeExtension(const std::string& filename);
/**
* @brief returns the current working directory.
* @return path
*/
std::string getCWD();
/**
* @brief returns the path preceeding a given filename.
* @param filename filename to extract the path from
......
......@@ -333,13 +333,13 @@ void IOHandler::checkTimestamps(const std::vector<METEO_SET>& vecVecMeteo) const
void IOHandler::create_exclude_map()
{
string exclude_file;
cfg.getValue("EXCLUDE", "Input", exclude_file, IOUtils::nothrow);
cfg.getValue("EXCLUDE_FILE", "Input", exclude_file, IOUtils::nothrow);
excludes_ready = true;
if (exclude_file.empty()) return;
//if this is a relative path, prefix the path with the current path
const std::string prefix = ( IOUtils::isAbsolutePath(exclude_file) )? "" : IOUtils::getPath(cfg.getSourceName(), true)+"/";
const std::string prefix = ( IOUtils::isAbsolutePath(exclude_file) )? "" : cfg.getConfigRootDir()+"/";
const std::string path = IOUtils::getPath(prefix+exclude_file, true); //clean & resolve path
const std::string filename = path + "/" + IOUtils::getFilename(exclude_file);
......@@ -375,6 +375,9 @@ void IOHandler::create_exclude_map()
fin.close();
}
/**
* reset to nodata the parameters marked as EXCLUDE on a per station basis
*/
void IOHandler::exclude_params(std::vector<METEO_SET>& vecVecMeteo) const
{
if (excluded_params.empty()) return;
......
......@@ -85,7 +85,7 @@ void ProcAdd::parse_args(const std::vector<std::string>& vec_args)
//if this is a relative path, prefix the path with the current path
const std::string in_filename = vec_args[1];
const std::string prefix = ( IOUtils::isAbsolutePath(in_filename) )? "" : IOUtils::getPath(root_path, true)+"/";
const std::string prefix = ( IOUtils::isAbsolutePath(in_filename) )? "" : root_path+"/";
const std::string path = IOUtils::getPath(prefix+in_filename, true); //clean & resolve path
const std::string filename = path + "/" + IOUtils::getFilename(in_filename);
ProcessingBlock::readCorrections(getName(), filename, type, 0., vecOffsets);
......
......@@ -86,7 +86,7 @@ void ProcMult::parse_args(const std::vector<std::string>& vec_args)
//if this is a relative path, prefix the path with the current path
const std::string in_filename = vec_args[1];
const std::string prefix = ( IOUtils::isAbsolutePath(in_filename) )? "" : IOUtils::getPath(root_path, true)+"/";
const std::string prefix = ( IOUtils::isAbsolutePath(in_filename) )? "" : root_path+"/";
const std::string path = IOUtils::getPath(prefix+in_filename, true); //clean & resolve path
const std::string filename = path + "/" + IOUtils::getFilename(in_filename);
ProcessingBlock::readCorrections(getName(), filename, type, 1., vecFactors);
......
......@@ -24,7 +24,7 @@ namespace mio {
ProcessingStack::ProcessingStack(const Config& cfg, const std::string& parname) : filter_stack(), param_name(parname)
{
//this is required by filters that need to read some parameters in extra files
const string root_path = cfg.getSourceName();
const string root_path = cfg.getConfigRootDir();
vector<string> vecFilters;
cfg.getValues(parname+"::filter", "Filters", vecFilters);
......
......@@ -157,6 +157,11 @@ void PSQLIO::getParameters(const Config& cfg)
string exclude_file;
cfg.getValue("EXCLUDE", "Input", exclude_file, IOUtils::nothrow);
if (!exclude_file.empty() && IOUtils::fileExists(exclude_file)) {
//if this is a relative path, prefix the path with the current path
const std::string prefix = ( IOUtils::isAbsolutePath(exclude_file) )? "" : cfg.getConfigRootDir()+"/";
const std::string path = IOUtils::getPath(prefix+exclude_file, true); //clean & resolve path
const std::string filename = path + "/" + IOUtils::getFilename(exclude_file);
create_shadow_map(exclude_file);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment