Merge pull request #63 from IntelOrca/fix/reading-config

Fix reading / writing of config file
This commit is contained in:
Ted John 2018-01-27 16:44:18 +00:00 committed by GitHub
commit 77676e6d46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 15 deletions

3
.gitignore vendored
View File

@ -1,5 +1,8 @@
lib/ lib/
## Visual Studio Code
.vscode/
## Ignore Visual Studio temporary files, build results, and ## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons. ## files generated by popular Visual Studio add-ons.
## ##

View File

@ -78,7 +78,13 @@ namespace openloco::config
if (!fs::is_directory(dir)) if (!fs::is_directory(dir))
{ {
fs::create_directories(configPath.parent_path()); fs::create_directories(configPath.parent_path());
fs::permissions(dir, fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read | fs::perms::others_read); // clang-format off
fs::permissions(
dir,
fs::perms::owner_all |
fs::perms::group_read | fs::perms::group_exec |
fs::perms::others_read | fs::perms::others_exec);
// clang-format on
} }
#ifdef _OPENLOCO_USE_BOOST_FS_ #ifdef _OPENLOCO_USE_BOOST_FS_

View File

@ -125,16 +125,24 @@ namespace openloco::environment
*/ */
static fs::path find_similar_file(const fs::path& path) static fs::path find_similar_file(const fs::path& path)
{ {
auto expectedFilename = path.filename().generic_string(); try
auto directory = path.parent_path();
for (auto& item : fs::directory_iterator(directory))
{ {
auto& p = item.path(); auto expectedFilename = path.filename().generic_string();
if (utility::iequals(p.filename().generic_string(), expectedFilename)) auto directory = path.parent_path();
for (auto& item : fs::directory_iterator(directory))
{ {
return p; auto& p = item.path();
if (utility::iequals(p.filename().generic_string(), expectedFilename))
{
return p;
}
} }
} }
catch (const std::exception&)
{
// Ignore errors when searching, most common will be that the
// parent directory does not exist
}
return fs::path(); return fs::path();
} }
#endif // _WIN32 #endif // _WIN32

View File

@ -3,6 +3,7 @@
#include "../interop/interop.hpp" #include "../interop/interop.hpp"
#include "../openloco.h" #include "../openloco.h"
#include "platform.h" #include "platform.h"
#include <iostream>
#include <pwd.h> #include <pwd.h>
#include <time.h> #include <time.h>
@ -21,7 +22,8 @@ int main(int argc, const char** argv)
static std::string GetEnvironmentVariable(const std::string& name) static std::string GetEnvironmentVariable(const std::string& name)
{ {
return getenv(name.c_str()); auto result = getenv(name.c_str());
return result == nullptr ? std::string() : result;
} }
uint32_t openloco::platform::get_time() uint32_t openloco::platform::get_time()
@ -31,29 +33,44 @@ uint32_t openloco::platform::get_time()
return spec.tv_nsec / 1000000; return spec.tv_nsec / 1000000;
} }
fs::path openloco::platform::get_user_directory() static fs::path get_home_directory()
{ {
std::string path;
auto pw = getpwuid(getuid()); auto pw = getpwuid(getuid());
if (pw != nullptr) if (pw != nullptr)
{ {
path = pw->pw_dir; return pw->pw_dir;
} }
else else
{ {
path = GetEnvironmentVariable("HOME"); return GetEnvironmentVariable("HOME");
} }
}
fs::path openloco::platform::get_user_directory()
{
auto path = fs::path(GetEnvironmentVariable("XDG_CONFIG_HOME"));
if (path.empty()) if (path.empty())
{ {
path = "/"; path = get_home_directory();
if (path.empty())
{
path = "/";
}
else
{
path = path / fs::path(".config");
}
} }
return path; return path / fs::path("OpenLoco");
} }
#if !(defined(__APPLE__) && defined(__MACH__)) #if !(defined(__APPLE__) && defined(__MACH__))
std::string openloco::platform::prompt_directory(const std::string& title) std::string openloco::platform::prompt_directory(const std::string& title)
{ {
return "/"; std::string input;
std::cout << "Type your Locomotion path: ";
std::cin >> input;
return input;
} }
#endif #endif