2021-03-16 14:56:46 +00:00
|
|
|
#include "fs.hpp"
|
2021-09-11 14:48:13 +01:00
|
|
|
|
2021-03-16 14:56:46 +00:00
|
|
|
#include <borealis.hpp>
|
|
|
|
#include <filesystem>
|
2021-09-11 14:48:13 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "constants.hpp"
|
2021-03-16 14:56:46 +00:00
|
|
|
|
|
|
|
namespace i18n = brls::i18n;
|
|
|
|
using namespace i18n::literals;
|
|
|
|
|
2022-08-09 15:43:18 +01:00
|
|
|
namespace fs {
|
2021-09-11 14:48:13 +01:00
|
|
|
std::vector<std::string> splitString(const std::string& s, char delimiter)
|
|
|
|
{
|
2021-05-22 17:10:34 +01:00
|
|
|
std::vector<std::string> tokens;
|
|
|
|
std::string token;
|
|
|
|
std::istringstream tokenStream(s);
|
|
|
|
while (std::getline(tokenStream, token, delimiter)) {
|
|
|
|
tokens.push_back(token);
|
|
|
|
}
|
|
|
|
return tokens;
|
|
|
|
}
|
2021-03-16 14:56:46 +00:00
|
|
|
|
2021-09-21 14:07:47 +01:00
|
|
|
bool removeDir(const std::string& path)
|
2021-09-11 14:48:13 +01:00
|
|
|
{
|
2021-03-16 14:56:46 +00:00
|
|
|
Result ret = 0;
|
2021-09-11 14:48:13 +01:00
|
|
|
FsFileSystem* fs = fsdevGetDeviceFileSystem("sdmc");
|
2021-09-21 14:07:47 +01:00
|
|
|
if (R_FAILED(ret = fsFsDeleteDirectoryRecursively(fs, path.c_str())))
|
|
|
|
return false;
|
|
|
|
return true;
|
2021-03-16 14:56:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
nlohmann::ordered_json parseJsonFile(const std::string& path)
|
|
|
|
{
|
2021-03-16 14:56:46 +00:00
|
|
|
std::ifstream file(path);
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
std::string fileContent((std::istreambuf_iterator<char>(file)),
|
|
|
|
(std::istreambuf_iterator<char>()));
|
2021-03-16 14:56:46 +00:00
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
if (nlohmann::ordered_json::accept(fileContent))
|
|
|
|
return nlohmann::ordered_json::parse(fileContent);
|
|
|
|
else
|
|
|
|
return nlohmann::ordered_json::object();
|
2021-03-16 14:56:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
void writeJsonToFile(nlohmann::json& data, const std::string& path)
|
|
|
|
{
|
2021-03-16 14:56:46 +00:00
|
|
|
std::ofstream out(path);
|
|
|
|
out << data.dump(4);
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
bool copyFile(const std::string& from, const std::string& to)
|
|
|
|
{
|
2021-03-16 14:56:46 +00:00
|
|
|
std::ifstream src(from, std::ios::binary);
|
|
|
|
std::ofstream dst(to, std::ios::binary);
|
|
|
|
|
|
|
|
if (src.good() && dst.good()) {
|
|
|
|
dst << src.rdbuf();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
void createTree(std::string path)
|
|
|
|
{
|
2021-05-22 17:10:34 +01:00
|
|
|
std::string delimiter = "/";
|
|
|
|
size_t pos = 0;
|
|
|
|
std::string token;
|
|
|
|
std::string directories("");
|
|
|
|
while ((pos = path.find(delimiter)) != std::string::npos) {
|
|
|
|
token = path.substr(0, pos);
|
|
|
|
directories += token + "/";
|
|
|
|
std::filesystem::create_directory(directories);
|
|
|
|
path.erase(0, pos + delimiter.length());
|
|
|
|
}
|
2021-03-16 14:56:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
std::string copyFiles(const std::string& path)
|
|
|
|
{
|
2021-03-16 14:56:46 +00:00
|
|
|
std::string error = "";
|
2021-05-22 17:10:34 +01:00
|
|
|
if (std::filesystem::exists(path)) {
|
|
|
|
std::string str;
|
|
|
|
std::ifstream in(path);
|
2021-09-11 14:48:13 +01:00
|
|
|
if (in) {
|
|
|
|
while (std::getline(in, str)) {
|
|
|
|
if (str.size() > 0) {
|
2021-05-22 17:10:34 +01:00
|
|
|
auto toMove = splitString(str, '|');
|
2021-09-11 14:48:13 +01:00
|
|
|
if (std::filesystem::exists(toMove[0]) && toMove.size() > 1) {
|
2021-05-22 17:10:34 +01:00
|
|
|
copyFile(toMove[0], toMove[1]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error += toMove[0] + "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 14:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-11 14:48:13 +01:00
|
|
|
if (error == "") {
|
2021-03-16 14:56:46 +00:00
|
|
|
error = "menus/common/all_done"_i18n;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error = "menus/tools/batch_copy_not_found"_i18n + error;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
std::set<std::string> readLineByLine(const std::string& path)
|
|
|
|
{
|
2022-09-07 19:41:16 +01:00
|
|
|
std::set<std::string> res;
|
|
|
|
std::ifstream lines(path);
|
|
|
|
std::string line;
|
|
|
|
if (lines) {
|
|
|
|
while (std::getline(lines, line)) {
|
|
|
|
if (line.size() > 0) {
|
|
|
|
if (line.back() == '\r')
|
|
|
|
line.pop_back();
|
|
|
|
res.insert(line);
|
|
|
|
}
|
2021-03-16 14:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-07 19:41:16 +01:00
|
|
|
return res;
|
2021-03-16 14:56:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
Result getFreeStorageSD(s64& free)
|
|
|
|
{
|
2021-06-03 17:47:35 +01:00
|
|
|
return nsGetFreeSpaceSize(NcmStorageId_SdCard, &free);
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
} // namespace fs
|