summaryrefslogtreecommitdiff
path: root/core/src/Model/Project.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-03-29 17:55:02 -0700
committerrtk0c <[email protected]>2021-03-29 17:55:02 -0700
commit70cc233165b5efa3a3888af34f7afce095fe6947 (patch)
treeb15e8f2e3816acc204846188e78514f2ba6ad816 /core/src/Model/Project.cpp
parent6032ae108064650324b2c45352e1baa5b36961cc (diff)
More work on project tab
Diffstat (limited to 'core/src/Model/Project.cpp')
-rw-r--r--core/src/Model/Project.cpp50
1 files changed, 49 insertions, 1 deletions
diff --git a/core/src/Model/Project.cpp b/core/src/Model/Project.cpp
index f301bb8..c54a02c 100644
--- a/core/src/Model/Project.cpp
+++ b/core/src/Model/Project.cpp
@@ -1,8 +1,56 @@
#include "Project.hpp"
+#include <json/reader.h>
+#include <json/value.h>
+#include <json/writer.h>
+#include <filesystem>
+#include <fstream>
+#include <stdexcept>
#include <utility>
-const std::filesystem::path& Project::GetPath() const {
+namespace fs = std::filesystem;
+
+Project Project::Load(const fs::path& path) {
+ // TODO better diagnostic
+ const char* kInvalidFormatErr = "Failed to load project: invalid format.";
+
+ std::ifstream ifs(path);
+ if (!ifs) {
+ std::string message;
+ message += "Failed to load project file at '";
+ message += path.string();
+ message += "'.";
+ throw std::runtime_error(message);
+ }
+
+ Project proj;
+ proj.mRootPath = path.parent_path();
+
+ Json::Value root;
+ ifs >> root;
+
+ const auto& croot = root; // Use const reference so that accessors default to returning a null if not found, instead of siliently creating new elements
+ if (!croot.isObject()) {
+ throw std::runtime_error(kInvalidFormatErr);
+ }
+
+ if (auto& name = croot["Name"]; name.isString()) {
+ proj.mName = name.asString();
+ } else {
+ throw std::runtime_error(kInvalidFormatErr);
+ }
+
+ return proj;
+}
+
+Project Project::Create(std::string name, const fs::path& path) {
+ Project proj;
+ proj.mRootPath = path;
+ proj.mName = std::move(name);
+ return proj;
+}
+
+const fs::path& Project::GetPath() const {
return mRootPath;
}