-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.cpp
More file actions
51 lines (41 loc) · 1.11 KB
/
FileManager.cpp
File metadata and controls
51 lines (41 loc) · 1.11 KB
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
#include "FileManager.h"
#include "Project.h"
#include <fstream>
#include <iostream>
#include <filesystem>
#include "json.hpp"
using json = nlohmann::json;
namespace fs = std::filesystem;
std::string FileManager::getDataDirectory() {
std::string dir = "projects_data";
if (!fs::exists(dir)) {
fs::create_directory(dir);
}
return dir;
}
bool FileManager::saveProjects(const std::vector<Project>& projects) {
json j;
for (const auto& project : projects) {
j.push_back(project.toJson());
}
std::ofstream file(getDataDirectory() + "/projects.json");
if (!file.is_open()) return false;
file << j.dump(4);
file.close();
return true;
}
std::vector<Project> FileManager::loadProjects() {
std::vector<Project> projects;
std::ifstream file(getDataDirectory() + "/projects.json");
if (!file.is_open()) return projects;
try {
json j;
file >> j;
for (const auto& item : j) {
projects.emplace_back(Project::fromJson(item));
}
} catch (...) {
// Handle parse errors
}
return projects;
}