-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentHandler.cpp
47 lines (39 loc) · 1.69 KB
/
ContentHandler.cpp
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
#include "ContentHandler.h"
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
void ContentHandler::readContentDirectory(const std::filesystem::path& contentDir) {
for (const auto& entry : std::filesystem::directory_iterator(contentDir)) {
if (entry.is_regular_file() && entry.path().filename() == "content.json") {
const std::filesystem::path path = entry.path();
std::ifstream fileStream(path);
if (!fileStream.is_open()) {
std::cerr << "Failed to open file: " << path << std::endl;
continue;
}
try {
nlohmann::json jsonObject = nlohmann::json::parse(fileStream);
MusicBox::ContentItem obj = jsonObject.template get<MusicBox::ContentItem>();
obj.id = path.parent_path().filename();
library.push_back(obj);
} catch (const nlohmann::json::parse_error& e) {
std::cerr << "Error parsing JSON file: " << path << ". Error: " << e.what() << std::endl;
} catch (const nlohmann::json::exception& e) {
std::cerr << "Unexpected JSON error: " << e.what() << std::endl;
}
} else if (entry.is_directory()) {
readContentDirectory(entry.path());
}
}
}
std::vector<MusicBox::ContentItem> ContentHandler::getContentLibrary() {
return library;
}
std::optional<MusicBox::ContentItem> ContentHandler::getContentItem(const std::string contentId) {
for(const MusicBox::ContentItem& item : library) {
if(item.id == contentId) {
return item;
}
}
return std::nullopt;
}