CDImage: Work around cue parser requiring new line at end

This commit is contained in:
Connor McLaughlin 2020-08-05 03:14:06 +10:00
parent 466e4099d4
commit a7dbb9ab28
3 changed files with 41 additions and 4 deletions

View file

@ -46,15 +46,18 @@ CDImageCueSheet::~CDImageCueSheet()
bool CDImageCueSheet::OpenAndParse(const char* filename)
{
std::FILE* cue_fp = FileSystem::OpenCFile(filename, "rb");
if (!cue_fp)
std::optional<std::string> cuesheet_string = FileSystem::ReadFileToString(filename);
if (!cuesheet_string.has_value())
{
Log_ErrorPrintf("Failed to open cuesheet '%s': errno %d", filename, errno);
return false;
}
m_cd = cue_parse_file(cue_fp);
std::fclose(cue_fp);
// work around cuesheet parsing issue - ensure the last character is a newline.
if (!cuesheet_string->empty() && cuesheet_string->at(cuesheet_string->size() - 1) != '\n')
*cuesheet_string += '\n';
m_cd = cue_parse_string(cuesheet_string->c_str());
if (!m_cd)
{
Log_ErrorPrintf("Failed to parse cuesheet '%s'", filename);

View file

@ -452,6 +452,26 @@ std::optional<std::vector<u8>> ReadBinaryFile(const char* filename)
return res;
}
std::optional<std::string> ReadFileToString(const char* filename)
{
ManagedCFilePtr fp = OpenManagedCFile(filename, "rb");
if (!fp)
return std::nullopt;
std::fseek(fp.get(), 0, SEEK_END);
long size = std::ftell(fp.get());
std::fseek(fp.get(), 0, SEEK_SET);
if (size < 0)
return std::nullopt;
std::string res;
res.resize(static_cast<size_t>(size));
if (size > 0 && std::fread(res.data(), 1u, static_cast<size_t>(size), fp.get()) != static_cast<size_t>(size))
return std::nullopt;
return res;
}
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length)
{
ManagedCFilePtr fp = OpenManagedCFile(filename, "wb");
@ -464,6 +484,18 @@ bool WriteBinaryFile(const char* filename, const void* data, size_t data_length)
return true;
}
bool WriteFileToString(const char* filename, const std::string_view& sv)
{
ManagedCFilePtr fp = OpenManagedCFile(filename, "wb");
if (!fp)
return false;
if (sv.length() > 0 && std::fwrite(sv.data(), 1u, sv.length(), fp.get()) != sv.length())
return false;
return true;
}
void BuildOSPath(char* Destination, u32 cbDestination, const char* Path)
{
u32 i;

View file

@ -173,7 +173,9 @@ ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode);
std::FILE* OpenCFile(const char* filename, const char* mode);
std::optional<std::vector<u8>> ReadBinaryFile(const char* filename);
std::optional<std::string> ReadFileToString(const char* filename);
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length);
bool WriteFileToString(const char* filename, const std::string_view& sv);
// creates a directory in the local filesystem
// if the directory already exists, the return value will be true.