logic: paths in ProjectSettings relative to file not working directory

This change checks all paths in the ProjectSettings file for relativity and makes them relative to the location of the
ProjectSettings file if necessary. The paths are also correctly saved relative to the file.
This commit is contained in:
Eberhard Graether
2015-07-29 22:20:26 +02:00
parent 336df76912
commit 5f25311389
31 changed files with 407 additions and 148 deletions
+16
View File
@@ -21,6 +21,22 @@ std::string ApplicationSettings::getStartupProjectFilePath() const
return getValue<std::string>("StartupProject", "");
}
std::vector<FilePath> ApplicationSettings::getHeaderSearchPaths() const
{
return getPathValues("source/HeaderSearchPaths/HeaderSearchPath");
}
std::vector<FilePath> ApplicationSettings::getFrameworkSearchPaths() const
{
return getPathValues("source/FrameworkSearchPaths/FrameworkSearchPath");
}
std::vector<std::string> ApplicationSettings::getCompilerFlags() const
{
std::vector<std::string> defaultValues;
return getValues("source/CompilerFlags/CompilerFlag", defaultValues);
}
std::string ApplicationSettings::getFontName() const
{
return getValue<std::string>("application/font_name", "Source Code Pro");
+7 -2
View File
@@ -3,10 +3,10 @@
#include <memory>
#include "settings/CommonSettings.h"
#include "settings/Settings.h"
class ApplicationSettings
: public CommonSettings
: public Settings
{
public:
static std::shared_ptr<ApplicationSettings> getInstance();
@@ -14,6 +14,11 @@ public:
std::string getStartupProjectFilePath() const;
// source
std::vector<FilePath> getHeaderSearchPaths() const;
std::vector<FilePath> getFrameworkSearchPaths() const;
std::vector<std::string> getCompilerFlags() const;
// application
std::string getFontName() const;
void setFontName(const std::string& fontName);
-27
View File
@@ -1,27 +0,0 @@
#include "settings/CommonSettings.h"
CommonSettings::~CommonSettings()
{
}
std::vector<std::string> CommonSettings::getHeaderSearchPaths() const
{
std::vector<std::string> defaultValues;
return getValues("source/HeaderSearchPaths/HeaderSearchPath", defaultValues);
}
std::vector<std::string> CommonSettings::getFrameworkSearchPaths() const
{
std::vector<std::string> defaultValues;
return getValues("source/FrameworkSearchPaths/FrameworkSearchPath", defaultValues);
}
std::vector<std::string> CommonSettings::getCompilerFlags() const
{
std::vector<std::string> defaultValues;
return getValues("source/CompilerFlags/CompilerFlag", defaultValues);
}
CommonSettings::CommonSettings()
{
}
-21
View File
@@ -1,21 +0,0 @@
#ifndef COMMON_SETTINGS_H
#define COMMON_SETTINGS_H
#include "settings/Settings.h"
class CommonSettings
: public Settings
{
public:
virtual ~CommonSettings();
// source
std::vector<std::string> getHeaderSearchPaths() const;
std::vector<std::string> getFrameworkSearchPaths() const;
std::vector<std::string> getCompilerFlags() const;
protected:
CommonSettings();
};
#endif // COMMON_SETTINGS_H
+39 -5
View File
@@ -20,15 +20,49 @@ ProjectSettings::~ProjectSettings()
{
}
std::vector<std::string> ProjectSettings::getSourcePaths() const
void ProjectSettings::save(const FilePath& filePath)
{
std::vector<std::string> defaultValues;
return getValues("source/SourcePaths/SourcePath", defaultValues);
moveRelativePathValues("source/SourcePaths/SourcePath", filePath);
moveRelativePathValues("source/HeaderSearchPaths/HeaderSearchPath", filePath);
moveRelativePathValues("source/FrameworkSearchPaths/FrameworkSearchPath", filePath);
Settings::save(filePath);
}
bool ProjectSettings::setSourcePaths(const std::vector<std::string>& sourcePaths)
std::vector<FilePath> ProjectSettings::getSourcePaths() const
{
return setValues("source/SourcePaths/SourcePath", sourcePaths);
return getRelativePathValues("source/SourcePaths/SourcePath");
}
bool ProjectSettings::setSourcePaths(const std::vector<FilePath>& sourcePaths)
{
return setPathValues("source/SourcePaths/SourcePath", sourcePaths);
}
std::vector<FilePath> ProjectSettings::getHeaderSearchPaths() const
{
return getRelativePathValues("source/HeaderSearchPaths/HeaderSearchPath");
}
bool ProjectSettings::setHeaderSearchPaths(const std::vector<FilePath>& headerSearchPaths)
{
return setPathValues("source/HeaderSearchPaths/HeaderSearchPath", headerSearchPaths);
}
std::vector<FilePath> ProjectSettings::getFrameworkSearchPaths() const
{
return getRelativePathValues("source/FrameworkSearchPaths/FrameworkSearchPath");
}
bool ProjectSettings::setFrameworkSearchPaths(const std::vector<FilePath>& frameworkSearchPaths)
{
return setPathValues("source/FrameworkSearchPaths/FrameworkSearchPath", frameworkSearchPaths);
}
std::vector<std::string> ProjectSettings::getCompilerFlags() const
{
std::vector<std::string> defaultValues;
return getValues("source/CompilerFlags/CompilerFlag", defaultValues);
}
std::vector<std::string> ProjectSettings::getHeaderExtensions() const
+14 -4
View File
@@ -4,18 +4,28 @@
#include <memory>
#include <vector>
#include "settings/CommonSettings.h"
#include "settings/Settings.h"
class ProjectSettings
: public CommonSettings
: public Settings
{
public:
static std::shared_ptr<ProjectSettings> getInstance();
~ProjectSettings();
virtual void save(const FilePath& filePath);
// source
std::vector<std::string> getSourcePaths() const;
bool setSourcePaths(const std::vector<std::string>& sourcePaths);
std::vector<FilePath> getSourcePaths() const;
bool setSourcePaths(const std::vector<FilePath>& sourcePaths);
std::vector<FilePath> getHeaderSearchPaths() const;
bool setHeaderSearchPaths(const std::vector<FilePath>& headerSearchPaths);
std::vector<FilePath> getFrameworkSearchPaths() const;
bool setFrameworkSearchPaths(const std::vector<FilePath>& frameworkSearchPaths);
std::vector<std::string> getCompilerFlags() const;
// extensions
std::vector<std::string> getHeaderExtensions() const;
+85 -8
View File
@@ -9,11 +9,11 @@ Settings::~Settings()
{
}
bool Settings::load(const std::string& filePath)
bool Settings::load(const FilePath& filePath)
{
if (FileSystem::exists(filePath))
if (filePath.exists())
{
m_config = ConfigManager::createAndLoad(TextAccess::createFromFile(filePath));
m_config = ConfigManager::createAndLoad(TextAccess::createFromFile(filePath.str()));
m_filePath = filePath;
return true;
}
@@ -27,9 +27,9 @@ bool Settings::load(const std::string& filePath)
void Settings::save()
{
if (m_config.get() && m_filePath.size())
if (m_config.get() && m_filePath.exists())
{
m_config->save(m_filePath);
m_config->save(m_filePath.str());
}
else
{
@@ -37,11 +37,13 @@ void Settings::save()
}
}
void Settings::save(const std::string& filePath)
void Settings::save(const FilePath& filePath)
{
setFilePath(filePath);
if (m_config)
{
m_config->save(filePath);
m_config->save(filePath.str());
}
else
{
@@ -52,10 +54,85 @@ void Settings::save(const std::string& filePath)
void Settings::clear()
{
m_config = ConfigManager::createEmpty();
m_filePath.clear();
m_filePath = FilePath();
}
Settings::Settings()
{
clear();
}
const FilePath& Settings::getFilePath() const
{
return m_filePath;
}
void Settings::setFilePath(const FilePath& filePath)
{
m_filePath = filePath;
}
std::vector<FilePath> Settings::getPathValues(const std::string& key) const
{
std::vector<std::string> values;
values = getValues(key, values);
std::vector<FilePath> paths;
for (const std::string& path : values)
{
paths.push_back(FilePath(path));
}
return paths;
}
std::vector<FilePath> Settings::getRelativePathValues(const std::string& key) const
{
std::vector<std::string> values;
values = getValues(key, values);
std::vector<FilePath> paths;
for (const std::string& path : values)
{
FilePath filePath(path);
if (!filePath.isAbsolute())
{
filePath = m_filePath.parentDirectory().concat(filePath);
}
paths.push_back(filePath.canonical());
}
return paths;
}
bool Settings::setPathValues(const std::string& key, const std::vector<FilePath>& paths)
{
std::vector<std::string> values;
for (const FilePath& path : paths)
{
values.push_back(path.str());
}
return setValues(key, values);
}
bool Settings::moveRelativePathValues(const std::string& key, const FilePath& filePath)
{
std::vector<std::string> values;
values = getValues(key, values);
FilePath oldPath = m_filePath.absolute();
FilePath newPath = filePath.absolute();
for (size_t i = 0; i < values.size(); i++)
{
FilePath path(values[i]);
if (!path.isAbsolute())
{
path = oldPath.parentDirectory().concat(path);
path = path.canonical().relativeTo(newPath);
values[i] = path.str();
}
}
return setValues(key, values);
}
+13 -3
View File
@@ -6,34 +6,44 @@
#include <vector>
#include "utility/ConfigManager.h"
#include "utility/file/FilePath.h"
class Settings
{
public:
virtual ~Settings();
bool load(const std::string& filePath);
bool load(const FilePath& filePath);
void save();
void save(const std::string& filePath);
virtual void save(const FilePath& filePath);
void clear();
protected:
Settings();
const FilePath& getFilePath() const;
void setFilePath(const FilePath& filePath);
template<typename T>
T getValue(const std::string& key, T defaultValue) const;
template<typename T>
std::vector<T> getValues(const std::string& key, std::vector<T> defaultValues) const;
std::vector<FilePath> getPathValues(const std::string& key) const;
std::vector<FilePath> getRelativePathValues(const std::string& key) const;
template<typename T>
bool setValue(const std::string& key, T value);
template<typename T>
bool setValues(const std::string& key, std::vector<T> values);
bool setPathValues(const std::string& key, const std::vector<FilePath>& paths);
bool moveRelativePathValues(const std::string& key, const FilePath& filePath);
private:
std::string m_filePath;
FilePath m_filePath;
std::shared_ptr<ConfigManager> m_config;
};