Commit b41f38ec authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #12669 from mshabunin:add-paths-config

parents 682180e0 15632c63
...@@ -5,11 +5,17 @@ ...@@ -5,11 +5,17 @@
#ifndef OPENCV_CONFIGURATION_PRIVATE_HPP #ifndef OPENCV_CONFIGURATION_PRIVATE_HPP
#define OPENCV_CONFIGURATION_PRIVATE_HPP #define OPENCV_CONFIGURATION_PRIVATE_HPP
#include "opencv2/core/cvstd.hpp"
#include <vector>
#include <string>
namespace cv { namespace utils { namespace cv { namespace utils {
typedef std::vector<std::string> Paths;
CV_EXPORTS bool getConfigurationParameterBool(const char* name, bool defaultValue); CV_EXPORTS bool getConfigurationParameterBool(const char* name, bool defaultValue);
CV_EXPORTS size_t getConfigurationParameterSizeT(const char* name, size_t defaultValue); CV_EXPORTS size_t getConfigurationParameterSizeT(const char* name, size_t defaultValue);
CV_EXPORTS cv::String getConfigurationParameterString(const char* name, const char* defaultValue); CV_EXPORTS cv::String getConfigurationParameterString(const char* name, const char* defaultValue);
CV_EXPORTS Paths getConfigurationParameterPaths(const char* name, const Paths &defaultValue = Paths());
}} // namespace }} // namespace
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "precomp.hpp" #include "precomp.hpp"
#include <iostream> #include <iostream>
#include <ostream>
#include <opencv2/core/utils/configuration.private.hpp> #include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/utils/trace.private.hpp> #include <opencv2/core/utils/trace.private.hpp>
...@@ -1657,18 +1658,26 @@ static TLSData<ThreadID>& getThreadIDTLS() ...@@ -1657,18 +1658,26 @@ static TLSData<ThreadID>& getThreadIDTLS()
} // namespace } // namespace
int utils::getThreadID() { return getThreadIDTLS().get()->id; } int utils::getThreadID() { return getThreadIDTLS().get()->id; }
bool utils::getConfigurationParameterBool(const char* name, bool defaultValue)
class ParseError
{ {
#ifdef NO_GETENV std::string bad_value;
const char* envValue = NULL; public:
#else ParseError(const std::string bad_value_) :bad_value(bad_value_) {}
const char* envValue = getenv(name); std::string toString(const std::string &param) const
#endif
if (envValue == NULL)
{ {
return defaultValue; std::ostringstream out;
out << "Invalid value for parameter " << param << ": " << bad_value;
return out.str();
} }
cv::String value = envValue; };
template <typename T>
T parseOption(const std::string &);
template<>
inline bool parseOption(const std::string & value)
{
if (value == "1" || value == "True" || value == "true" || value == "TRUE") if (value == "1" || value == "True" || value == "true" || value == "TRUE")
{ {
return true; return true;
...@@ -1677,22 +1686,12 @@ bool utils::getConfigurationParameterBool(const char* name, bool defaultValue) ...@@ -1677,22 +1686,12 @@ bool utils::getConfigurationParameterBool(const char* name, bool defaultValue)
{ {
return false; return false;
} }
CV_Error(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str())); throw ParseError(value);
} }
template<>
size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultValue) inline size_t parseOption(const std::string &value)
{ {
#ifdef NO_GETENV
const char* envValue = NULL;
#else
const char* envValue = getenv(name);
#endif
if (envValue == NULL)
{
return defaultValue;
}
cv::String value = envValue;
size_t pos = 0; size_t pos = 0;
for (; pos < value.size(); pos++) for (; pos < value.size(); pos++)
{ {
...@@ -1708,22 +1707,80 @@ size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultVal ...@@ -1708,22 +1707,80 @@ size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultVal
return v * 1024 * 1024; return v * 1024 * 1024;
else if (suffixStr == "KB" || suffixStr == "Kb" || suffixStr == "kb") else if (suffixStr == "KB" || suffixStr == "Kb" || suffixStr == "kb")
return v * 1024; return v * 1024;
CV_Error(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str())); throw ParseError(value);
} }
cv::String utils::getConfigurationParameterString(const char* name, const char* defaultValue) template<>
inline cv::String parseOption(const std::string &value)
{
return value;
}
template<>
inline utils::Paths parseOption(const std::string &value)
{
utils::Paths result;
#ifdef _WIN32
const char sep = ';';
#else
const char sep = ':';
#endif
size_t start_pos = 0;
while (start_pos != std::string::npos)
{
const size_t pos = value.find(sep, start_pos);
const std::string one_piece(value, start_pos, pos == std::string::npos ? pos : pos - start_pos);
if (!one_piece.empty())
result.push_back(one_piece);
start_pos = pos == std::string::npos ? pos : pos + 1;
}
return result;
}
static inline const char * envRead(const char * name)
{ {
#ifdef NO_GETENV #ifdef NO_GETENV
const char* envValue = NULL; CV_UNUSED(name);
return NULL;
#else #else
const char* envValue = getenv(name); return getenv(name);
#endif #endif
if (envValue == NULL) }
template<typename T>
inline T read(const std::string & k, const T & defaultValue)
{
try
{ {
return defaultValue; const char * res = envRead(k.c_str());
if (res)
return parseOption<T>(std::string(res));
} }
cv::String value = envValue; catch (const ParseError &err)
return value; {
CV_Error(cv::Error::StsBadArg, err.toString(k));
}
return defaultValue;
}
bool utils::getConfigurationParameterBool(const char* name, bool defaultValue)
{
return read<bool>(name, defaultValue);
}
size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultValue)
{
return read<size_t>(name, defaultValue);
}
cv::String utils::getConfigurationParameterString(const char* name, const char* defaultValue)
{
return read<cv::String>(name, defaultValue);
}
utils::Paths utils::getConfigurationParameterPaths(const char* name, const utils::Paths &defaultValue)
{
return read<utils::Paths>(name, defaultValue);
} }
......
Markdown is supported
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