aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Project.cpp
blob: f0709406c212cf8de09f073d794e3b55332101ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "Project.hpp"

#include <json/reader.h>
#include <json/value.h>
#include <json/writer.h>
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <utility>

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();
	proj.mRootPathString = proj.mRootPath.string();

	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 silently 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.mRootPathString = path.string();
	proj.mName = std::move(name);
	return proj;
}

const fs::path& Project::GetPath() const {
	return mRootPath;
}

const std::string& Project::GetPathString() const {
	return mRootPathString;
}

const std::string& Project::GetName() const {
	return mName;
}

void Project::SetName(std::string name) {
	mName = std::move(name);
}

Json::Value Project::Serialize() {
	Json::Value root(Json::objectValue);

	root["Name"] = mName;

	return root;
}

void Project::WriteToDisk() {
	auto root = Serialize();
	std::ofstream ofs(mRootPath / "cplt_project.json");
	ofs << root;
}