Read settings and check validity
This commit is contained in:
parent
e5006d4f2f
commit
b400cd8d73
@ -70,8 +70,7 @@ void ProjectHandler::save(Context *ctx, std::string filename)
|
|||||||
root.put("project.params.freq", int(ctx->target_freq / 1e6));
|
root.put("project.params.freq", int(ctx->target_freq / 1e6));
|
||||||
root.put("project.params.seed", ctx->rngstate);
|
root.put("project.params.seed", ctx->rngstate);
|
||||||
saveArch(ctx, root, proj.parent_path().string());
|
saveArch(ctx, root, proj.parent_path().string());
|
||||||
for(auto const &item : ctx->settings)
|
for (auto const &item : ctx->settings) {
|
||||||
{
|
|
||||||
std::string path = "project.settings.";
|
std::string path = "project.settings.";
|
||||||
path += item.first.c_str(ctx);
|
path += item.first.c_str(ctx);
|
||||||
std::replace(path.begin(), path.end(), '/', '.');
|
std::replace(path.begin(), path.end(), '/', '.');
|
||||||
@ -83,6 +82,19 @@ void ProjectHandler::save(Context *ctx, std::string filename)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addSettings(Context *ctx, std::string path, pt::ptree sub)
|
||||||
|
{
|
||||||
|
for (pt::ptree::value_type &v : sub) {
|
||||||
|
const std::string &key = v.first;
|
||||||
|
const boost::property_tree::ptree &subtree = v.second;
|
||||||
|
if (subtree.empty()) {
|
||||||
|
ctx->settings.emplace(ctx->id(path + key), subtree.get_value<std::string>().c_str());
|
||||||
|
} else {
|
||||||
|
addSettings(ctx, path + key + "/", subtree);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<Context> ProjectHandler::load(std::string filename)
|
std::unique_ptr<Context> ProjectHandler::load(std::string filename)
|
||||||
{
|
{
|
||||||
std::unique_ptr<Context> ctx;
|
std::unique_ptr<Context> ctx;
|
||||||
@ -118,6 +130,10 @@ std::unique_ptr<Context> ProjectHandler::load(std::string filename)
|
|||||||
if (params.count("seed"))
|
if (params.count("seed"))
|
||||||
ctx->rngseed(params.get<uint64_t>("seed"));
|
ctx->rngseed(params.get<uint64_t>("seed"));
|
||||||
}
|
}
|
||||||
|
if (project.count("settings")) {
|
||||||
|
addSettings(ctx.get(), "", project.get_child("settings"));
|
||||||
|
}
|
||||||
|
|
||||||
loadArch(ctx.get(), root, proj.parent_path().string());
|
loadArch(ctx.get(), root, proj.parent_path().string());
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
log_error("Error loading project file.\n");
|
log_error("Error loading project file.\n");
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#define SETTINGS_H
|
#define SETTINGS_H
|
||||||
|
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
|
#include "log.h"
|
||||||
#include "nextpnr.h"
|
#include "nextpnr.h"
|
||||||
|
|
||||||
NEXTPNR_NAMESPACE_BEGIN
|
NEXTPNR_NAMESPACE_BEGIN
|
||||||
@ -31,10 +32,15 @@ class Settings
|
|||||||
|
|
||||||
template <typename T> T get(const char *name, T defaultValue)
|
template <typename T> T get(const char *name, T defaultValue)
|
||||||
{
|
{
|
||||||
IdString id = ctx->id(name);
|
try {
|
||||||
if (ctx->settings.find(id) != ctx->settings.end())
|
IdString id = ctx->id(name);
|
||||||
return boost::lexical_cast<T>(ctx->settings[id]);
|
if (ctx->settings.find(id) != ctx->settings.end()) {
|
||||||
ctx->settings.emplace(id, std::to_string(defaultValue));
|
return boost::lexical_cast<T>(ctx->settings[id]);
|
||||||
|
}
|
||||||
|
ctx->settings.emplace(id, std::to_string(defaultValue));
|
||||||
|
} catch (boost::bad_lexical_cast &) {
|
||||||
|
log_error("Problem reading setting %s, using default value\n", name);
|
||||||
|
}
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user