Commit 0e7cbf3c authored by Legodev's avatar Legodev

ADDED: search for configfile in all subfolders instead of the default path only

parent adbd01d4
......@@ -32,37 +32,21 @@
#include "fileio/fileio.hpp"
fileio::fileio(EXT_FUNCTIONS &extFunctions) {
extFunctions.insert(
std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_READ_FILE),
std::make_tuple(
boost::bind(&fileio::readFile, this, _1, _2),
SYNC_MAGIC)));
extFunctions.insert(
std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_WRITE_FILE),
std::make_tuple(
boost::bind(&fileio::writeFile, this, _1, _2),
SYNC_MAGIC)));
extFunctions.insert(
std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_APPEND_FILE),
std::make_tuple(
boost::bind(&fileio::appendFile, this, _1, _2),
SYNC_MAGIC)));
extFunctions.insert(
std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_PLUGINSYSTEM_GETINITORDER),
std::make_tuple(
boost::bind(&fileio::GetInitOrder, this, _1, _2),
SYNC_MAGIC)));
extFunctions.insert(
std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_PLUGINSYSTEM_GETCFGFILE),
std::make_tuple(
boost::bind(&fileio::GetCfgFile, this, _1, _2),
SYNC_MAGIC)));
boost::property_tree::ptree configtree;
boost::property_tree::json_parser::read_json(CONFIG_FILE_NAME, configtree);
for (auto& item : configtree.get_child("fileio.readonce")) {
extFunctions.insert(std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_READ_FILE), std::make_tuple(boost::bind(&fileio::readFile, this, _1, _2),
SYNC_MAGIC)));
extFunctions.insert(std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_WRITE_FILE), std::make_tuple(boost::bind(&fileio::writeFile, this, _1, _2),
SYNC_MAGIC)));
extFunctions.insert(std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_APPEND_FILE), std::make_tuple(boost::bind(&fileio::appendFile, this, _1, _2),
SYNC_MAGIC)));
extFunctions.insert(std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_PLUGINSYSTEM_GETINITORDER), std::make_tuple(boost::bind(&fileio::GetInitOrder, this, _1, _2),
SYNC_MAGIC)));
extFunctions.insert(std::make_pair(std::string(PROTOCOL_IOCALL_FUNCTION_PLUGINSYSTEM_GETCFGFILE), std::make_tuple(boost::bind(&fileio::GetCfgFile, this, _1, _2),
SYNC_MAGIC)));
boost::property_tree::ptree configtree;
boost::property_tree::json_parser::read_json(CONFIG_FILE_NAME, configtree);
for (auto& item : configtree.get_child("fileio.readonce")) {
readlist.insert(std::make_pair(item.second.get_value<std::string>(), 1));
}
......@@ -81,51 +65,30 @@ fileio::~fileio() {
return;
}
#if defined(__linux__)
boost::filesystem::path fileio::ToLowerIfNeeded(boost::filesystem::path Path) {
boost::filesystem::path loweredPath = boost::algorithm::to_lower_copy(Path.string());
boost::filesystem::path fileio::GetConfigPath(std::string filename, bool useAsStartPath) {
const std::string toFind = boost::algorithm::to_lower_copy(filename);
if (boost::filesystem::exists(loweredPath)) {
return loweredPath;
}
return Path;
}
#endif
boost::filesystem::path fileio::GetConfigPath() {
boost::filesystem::path configPath = "@DesolationServer";
configPath /= "Config";
return configPath;
#if defined(__linux__)
configPath = ToLowerIfNeeded(configPath);
#endif
if (boost::filesystem::is_directory(configPath)) {
return configPath;
}
configPath = "Config";
#if defined(__linux__)
configPath = ToLowerIfNeeded(configPath);
#endif
auto begin = boost::filesystem::recursive_directory_iterator(this->configPath);
const auto end = boost::filesystem::recursive_directory_iterator();
if (boost::filesystem::is_directory(configPath)) {
return configPath;
for (; begin != end; ++begin) {
const std::string path = boost::algorithm::to_lower_copy(begin->path().string());
if (path.find(toFind) != std::string::npos) {
if (!boost::filesystem::is_directory(path)) {
if (useAsStartPath) {
// speedup future search by starting on the last searched directory
this->configPath = begin->path().parent_path();
}
return begin->path();
}
}
}
throw std::runtime_error("could not find the config path try to lower all names and check the dll location");
throw std::runtime_error("could not find the configs path, i started searching on path: " + this->configPath.string());
}
std::string fileio::GetInitOrder(std::string &extFunction, ext_arguments &extArguments) {
boost::filesystem::path configPath = GetConfigPath();
configPath /= "PluginList.cfg";
#if defined(__linux__)
configPath = ToLowerIfNeeded(configPath);
#endif
boost::filesystem::path configPath = GetConfigPath("PluginList.cfg", true);
if (boost::filesystem::exists(configPath)) {
int charpos, linenum;
......@@ -135,7 +98,9 @@ std::string fileio::GetInitOrder(std::string &extFunction, ext_arguments &extArg
linenum = 0;
while (std::getline(infile, line)) {
std::istringstream iss(line);
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
//std::istringstream iss(line);
int i = 0;
while ((i = line.find("\"", i)) != std::string::npos) {
......@@ -181,22 +146,15 @@ std::string fileio::GetInitOrder(std::string &extFunction, ext_arguments &extArg
}
std::string fileio::GetCfgFile(std::string &extFunction, ext_arguments &extArguments) {
boost::filesystem::path configPath = GetConfigPath();
std::string returnString = "[";
int filenum = 0;
int itemnum = 0;
for (std::string& filename : extArguments.get_simplelist<std::string>("configfiles")) {
boost::regex dirupexpression("\\.\\.");
filename = boost::regex_replace(filename, dirupexpression, "", boost::match_default | boost::format_all);
boost::filesystem::path filepath = configPath / (filename + ".cfg");
#if defined(__linux__)
filepath = ToLowerIfNeeded(filepath);
#endif
boost::filesystem::path filepath = GetConfigPath(filename + ".cfg");
if (filenum != 0) {
returnString += ",";
......@@ -209,8 +167,8 @@ std::string fileio::GetCfgFile(std::string &extFunction, ext_arguments &extArgum
itemnum = 0;
BOOST_FOREACH(boost::property_tree::ptree::value_type &val, configtree) {
// val.first is the name of the child.
// val.second is the child tree.
// val.first is the name of the child.
// val.second is the child tree.
std::string key = val.first;
std::string value = val.second.get_value<std::string>();
int i = 0;
......@@ -267,7 +225,7 @@ std::string fileio::readFile(std::string &extFunction, ext_arguments &extArgumen
linenum = 0;
while (std::getline(infile, line)) {
std::istringstream iss(line);
//std::istringstream iss(line);
// add second " to get the string after call compile to concatinate with the remaining parts
charpos = 0;
......@@ -287,8 +245,8 @@ std::string fileio::readFile(std::string &extFunction, ext_arguments &extArgumen
returnString += "]";
} else {
throw std::runtime_error("cannot read file: " + filename);
}
throw std::runtime_error("cannot read file: " + filename);
}
return "[\"" + std::string(PROTOCOL_MESSAGE_TYPE_MESSAGE) + "\"," + filename + "]";
}
......
......@@ -40,6 +40,8 @@ private:
typedef std::map<std::string, int> FILE_IO_MAP;
FILE_IO_MAP readlist;
FILE_IO_MAP writelist;
boost::filesystem::path configPath = ".";
std::string readFile(std::string &extFunction, ext_arguments &extArguments);
std::string writeFile(std::string &extFunction, ext_arguments &extArguments);
......@@ -47,10 +49,7 @@ private:
std::string GetInitOrder(std::string &extFunction, ext_arguments &extArguments);
std::string GetCfgFile(std::string &extFunction, ext_arguments &extArguments);
boost::filesystem::path GetConfigPath();
#if defined(__linux__)
boost::filesystem::path ToLowerIfNeeded(boost::filesystem::path configPath);
#endif
boost::filesystem::path GetConfigPath(std::string filename, bool useAsStartPath = false);
};
......
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