Implement 'setenv' for windows

Paths converter
Move 'split' from main to Utils
coverity_scan^2
Koncord 9 years ago
parent 72026955f5
commit bdfaea2d46

@ -80,7 +80,7 @@ void Script::LoadScripts(char *scripts, const char *base)
while (token)
{
char path[4096];
snprintf(path, sizeof(path), "%s/%s/%s", base, "scripts", token);
snprintf(path, sizeof(path), Utils::convertPath("%s/%s/%s").c_str(), base, "scripts", token);
Script::scripts.emplace_back(new Script(path));
token = strtok(nullptr, ",");
}
@ -104,6 +104,6 @@ void Script::UnloadScripts()
void Script::LoadScript(const char *script, const char *base)
{
char path[4096];
snprintf(path, sizeof(path), "%s/%s/%s", base, "scripts", script);
snprintf(path, sizeof(path), Utils::convertPath("%s/%s/%s").c_str(), base, "scripts", script);
Script::scripts.emplace_back(new Script(path));
}

@ -10,6 +10,59 @@
using namespace std;
#ifdef _WIN32
int setenv(const char *name, const char *value, int overwrite)
{
std::unique_ptr<char> tmp(new char[strlen(name) + strlen(value) + 1]);
sprintf(tmp.get(), "%s=%s", name, value);
printf("%s\n",tmp.get());
return putenv((const char*)tmp.get());
}
#endif
std::string Utils::convertPath(std::string str)
{
#if defined(_WIN32)
#define _SEP_ '\\'
#elif defined(__APPLE__)
#define _SEP_ ':'
#endif
#if defined(_WIN32) || defined(__APPLE__)
for(auto &ch : str)
if(ch == '/')
ch = _SEP_;
#endif //defined(_WIN32) || defined(__APPLE__)
return str;
#undef _SEP_
}
const vector<string> Utils::split(const string &str, int delimiter)
{
string buffer;
vector<string> result;
for (auto symb:str)
if (symb != delimiter)
buffer += symb;
else if (!buffer.empty())
{
result.push_back(move(buffer));
buffer.clear();
}
if (!buffer.empty())
result.push_back(move(buffer));
return result;
}
#undef _SEP_
void Utils::timestamp()
{
time_t ltime;

@ -7,7 +7,7 @@
#include <cstddef>
#include <string>
#include <vector>
#if (!defined(DEBUG_PRINTF) && defined(DEBUG))
#define DEBUG_PRINTF printf
@ -15,8 +15,16 @@
#define DEBUG_PRINTF(...)
#endif
#ifdef _WIN32
int setenv(const char *name, const char *value, int overwrite);
#endif
namespace Utils
{
const std::vector<std::string> split(const std::string &str, int delimiter);
std::string convertPath(std::string str);
template<size_t N>
constexpr unsigned int hash(const char(&str)[N], size_t I = N)
{

@ -63,44 +63,19 @@ std::string loadSettings (Settings::Manager & settings)
return settingspath;
}
const vector<string> split(const string &str, int delimiter)
{
string buffer;
vector<string> result;
for (auto symb:str)
if (symb != delimiter)
buffer += symb;
else if (!buffer.empty())
{
result.push_back(move(buffer));
buffer.clear();
}
if (!buffer.empty())
result.push_back(move(buffer));
return result;
}
int main(int argc, char *argv[])
{
Settings::Manager mgr;
loadSettings(mgr);
//string plugin_home = "/home/koncord/ClionProjects/tes3mp-server/files";
int players = mgr.getInt("players", "General");
int port = mgr.getInt("port", "General");
std::string plugin_home = mgr.getString("home", "Plugins");
string moddir = plugin_home + "/files";
vector<string> plugins (split(mgr.getString("plugins", "Plugins"), ','));
string plugin_home = mgr.getString("home", "Plugins");
string moddir = Utils::convertPath(plugin_home + "/files");
cout << plugins[0] << endl;
vector<string> plugins (Utils::split(mgr.getString("plugins", "Plugins"), ','));
printVersion("0.0.1b", 1);
@ -108,7 +83,7 @@ int main(int argc, char *argv[])
setenv("AMXFILE", moddir.c_str(), 1);
setenv("MOD_DIR", moddir.c_str(), 1); // hack for lua
setenv("LUA_PATH", (plugin_home + "/scripts/?.lua" + ";" + plugin_home + "/scripts/?.t").c_str(), 1);
setenv("LUA_PATH", Utils::convertPath(plugin_home + "/scripts/?.lua" + ";" + plugin_home + "/scripts/?.t").c_str(), 1);
for(auto plugin : plugins)
Script::LoadScript(plugin.c_str(), plugin_home.c_str());

Loading…
Cancel
Save