Skip to content

Commit

Permalink
load/save improvements in settings, fix microsoft#735
Browse files Browse the repository at this point in the history
  • Loading branch information
sytelus committed Jan 20, 2018
1 parent 963e30e commit 065ad2b
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions AirLib/include/common/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace msr { namespace airlib {

class Settings {
private:
std::string file_;
std::string full_filepath_;
nlohmann::json doc_;
bool load_success_ = false;

Expand All @@ -39,7 +39,7 @@ class Settings {
return instance;
}

std::string getFileName() { return file_; }
std::string getFullFilePath() { return full_filepath_; }

static std::string getUserDirectoryFullPath(std::string fileName)
{
Expand All @@ -55,7 +55,7 @@ class Settings {

static Settings& loadJSonString(const std::string& json_str)
{
singleton().file_ = "(loaded from string)";
singleton().full_filepath_ = "";
singleton().load_success_ = false;

if (json_str.length() > 0) {
Expand All @@ -76,16 +76,15 @@ class Settings {
return ss.str();
}

static Settings& loadJSonFile(std::string fileName)
static Settings& loadJSonFile(std::string full_filepath)
{
std::lock_guard<std::mutex> guard(getFileAccessMutex());
std::string path = getUserDirectoryFullPath(fileName);
singleton().file_ = fileName;
singleton().full_filepath_ = full_filepath;

singleton().load_success_ = false;

std::ifstream s;
common_utils::FileSystem::openTextFile(path, s);
common_utils::FileSystem::openTextFile(full_filepath, s);
if (!s.fail()) {
s >> singleton().doc_;
singleton().load_success_ = true;
Expand All @@ -101,15 +100,15 @@ class Settings {

bool hasFileName()
{
return !getFileName().empty();
return !getFullFilePath().empty();
}

void saveJSonFile(std::string fileName)
void saveJSonFile(std::string full_filepath)
{
std::lock_guard<std::mutex> guard(getFileAccessMutex());
std::string path = getUserDirectoryFullPath(fileName);
singleton().full_filepath_ = full_filepath;
std::ofstream s;
common_utils::FileSystem::createTextFile(path, s);
common_utils::FileSystem::createTextFile(full_filepath, s);
s << std::setw(2) << doc_ << std::endl;
}

Expand Down

0 comments on commit 065ad2b

Please sign in to comment.