diff options
Diffstat (limited to 'core/src/Model/Project.cpp')
-rw-r--r-- | core/src/Model/Project.cpp | 66 |
1 files changed, 50 insertions, 16 deletions
diff --git a/core/src/Model/Project.cpp b/core/src/Model/Project.cpp index f070940..cdb88c6 100644 --- a/core/src/Model/Project.cpp +++ b/core/src/Model/Project.cpp @@ -10,40 +10,62 @@ namespace fs = std::filesystem; -Project Project::Load(const fs::path& path) { +template <class T> +void ReadItemList(ItemList<T>& list, const fs::path& filePath) { + std::ifstream ifs(filePath); + if (ifs) { + Json::Value root; + ifs >> root; + + list = ItemList<T>(root); + } +} + +Project Project::Load(const fs::path& projectFilePath) { // TODO better diagnostic const char* kInvalidFormatErr = "Failed to load project: invalid format."; - std::ifstream ifs(path); + std::ifstream ifs(projectFilePath); if (!ifs) { std::string message; message += "Failed to load project file at '"; - message += path.string(); + message += projectFilePath.string(); message += "'."; throw std::runtime_error(message); } Project proj; - proj.mRootPath = path.parent_path(); + proj.mRootPath = projectFilePath.parent_path(); proj.mRootPathString = proj.mRootPath.string(); - Json::Value root; - ifs >> root; + { + 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); - } + 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); + if (auto& name = croot["Name"]; name.isString()) { + proj.mName = name.asString(); + } else { + throw std::runtime_error(kInvalidFormatErr); + } } + auto itemsDir = proj.mRootPath / "items"; + ReadItemList(proj.Products, itemsDir / "products.json"); + ReadItemList(proj.Factories, itemsDir / "factories.json"); + ReadItemList(proj.Customers, itemsDir / "customers.json"); + return proj; } +Project Project::LoadDir(const std::filesystem::path& projectPath) { + return Load(projectPath / "cplt_project.json"); +} + Project Project::Create(std::string name, const fs::path& path) { Project proj; proj.mRootPath = path; @@ -76,8 +98,20 @@ Json::Value Project::Serialize() { return root; } +template <class T> +static void WriteItemList(ItemList<T>& list, const fs::path& filePath) { + std::ofstream ofs(filePath); + ofs << list.Serialize(); +} + void Project::WriteToDisk() { - auto root = Serialize(); std::ofstream ofs(mRootPath / "cplt_project.json"); - ofs << root; + ofs << this->Serialize(); + + auto itemsDir = mRootPath / "items"; + fs::create_directories(itemsDir); + + WriteItemList(Products, itemsDir / "products.json"); + WriteItemList(Factories, itemsDir / "factories.json"); + WriteItemList(Customers, itemsDir / "customers.json"); } |