Common/FileSystem: Add BuildRelativePath() function

This commit is contained in:
Connor McLaughlin
2021-04-17 03:47:40 +10:00
parent 1b16662f17
commit e1578be20f
5 changed files with 52 additions and 145 deletions

View File

@ -1,11 +1,12 @@
#include "assert.h"
#include "cd_image.h"
#include "cd_subchannel_replacement.h"
#include "error.h"
#include "file_system.h"
#include "log.h"
#include <algorithm>
#include <cerrno>
#include <fstream>
#include <sstream>
#include <map>
Log_SetChannel(CDImageMemory);
@ -48,13 +49,19 @@ CDImageM3u::~CDImageM3u() = default;
bool CDImageM3u::Open(const char* path, Common::Error* error)
{
std::ifstream ifs(path);
if (!ifs.is_open())
std::FILE* fp = FileSystem::OpenCFile(path, "rb");
if (!fp)
return false;
std::optional<std::string> cue_file(FileSystem::ReadFileToString(fp));
std::fclose(fp);
if (!cue_file.has_value() || cue_file->empty())
{
Log_ErrorPrintf("Failed to open %s", path);
error->SetMessage("Failed to read cue sheet");
return false;
}
std::istringstream ifs(cue_file.value());
m_filename = path;
std::vector<std::string> entries;
@ -79,14 +86,12 @@ bool CDImageM3u::Open(const char* path, Common::Error* error)
continue;
Entry entry;
entry.filename.assign(line.begin() + start_offset, line.begin() + end_offset + 1);
entry.title = FileSystem::GetFileTitleFromPath(entry.filename);
if (!FileSystem::IsAbsolutePath(entry.filename))
{
SmallString absolute_path;
FileSystem::BuildPathRelativeToFile(absolute_path, path, entry.filename.c_str());
entry.filename = absolute_path;
}
std::string entry_filename(line.begin() + start_offset, line.begin() + end_offset + 1);
entry.title = FileSystem::GetFileTitleFromPath(entry_filename);
if (!FileSystem::IsAbsolutePath(entry_filename))
entry.filename = FileSystem::BuildRelativePath(path, entry_filename);
else
entry.filename = std::move(entry_filename);
Log_DevPrintf("Read path from m3u: '%s'", entry.filename.c_str());
m_entries.push_back(std::move(entry));