Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-load subtitles/audio: Improve multivolume rar file handling #1918

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/DSUtil/PathUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "PathUtils.h"
#include <memory>
#include "text.h"
#include <regex>

namespace PathUtils
{
Expand Down Expand Up @@ -266,4 +267,27 @@ namespace PathUtils
{
return (fn.Find(_T(":")) > 0) && !IsURL(fn) || (fn.Find(_T("\\\\")) == 0);
}

CString BaseName(const CString& fn)
{
return fn.Mid(std::max(fn.ReverseFind('\\'), fn.ReverseFind('/')) + 1);
}

CString Path(const CString& fn)
{
return fn.Left(std::max(fn.ReverseFind('\\'), fn.ReverseFind('/')) + 1);
}

CString StripExtensionAndMultiVolumeRarSuffix(const CString& fn)
{
// C:\some\dir\base.mkv ==> C:\some\dir\base
// C:\some\dir\base.partNN.rar ==> C:\some\dir\base
std::wregex re(_T("(\\.part\\d+\\.rar|\\.[^.]+)$"));
std::wcmatch mc;
if (std::regex_search(fn.GetString(), mc, re)) {
return fn.Left((int)mc.position());
} else {
return fn;
}
}
}
3 changes: 3 additions & 0 deletions src/DSUtil/PathUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ namespace PathUtils

bool IsURL(CString& fn);
bool IsFullFilePath(CString& fn);
CString BaseName(const CString& fn);
CString Path(const CString& fn);
CString StripExtensionAndMultiVolumeRarSuffix(const CString& fn);
}
12 changes: 3 additions & 9 deletions src/Subtitles/SubtitleHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,8 @@ void Subtitle::GetSubFileNames(CString fn, const CAtlArray<CString>& paths, CAtl

ExtendMaxPathLengthIfNeeded(fn, MAX_PATH);

int l = fn.ReverseFind('\\') + 1;
int l2 = fn.ReverseFind('.');
if (l2 < l) { // no extension, read to the end
l2 = fn.GetLength();
}

CString orgpath = fn.Left(l);
CString title = fn.Mid(l, l2 - l);
CString orgpath = PathUtils::Path(fn);
CString title = PathUtils::StripExtensionAndMultiVolumeRarSuffix(PathUtils::BaseName(fn));
int titleLength = title.GetLength();

WIN32_FIND_DATA wfd;
Expand All @@ -92,7 +86,7 @@ void Subtitle::GetSubFileNames(CString fn, const CAtlArray<CString>& paths, CAtl
CString path = paths[k];
path.Replace('/', '\\');

l = path.GetLength();
int l = path.GetLength();
if (l > 0 && path[l - 1] != '\\') {
path += _T('\\');
}
Expand Down
16 changes: 4 additions & 12 deletions src/mpc-hc/Playlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,12 @@ void CPlaylistItem::AutoLoadFiles()
CString ext = fn.Mid(i + 1).MakeLower();

if (!mf.FindExt(ext, true)) {
CString path = fn;
path.Replace('/', '\\');
path = path.Left(path.ReverseFind('\\') + 1);
CString path = PathUtils::Path(fn);
CString base = PathUtils::StripExtensionAndMultiVolumeRarSuffix(fn);

WIN32_FIND_DATA fd;
ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
HANDLE hFind = FindFirstFile(fn.Left(i) + _T("*.*"), &fd);
HANDLE hFind = FindFirstFile(base + _T("*.*"), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Expand Down Expand Up @@ -199,14 +198,7 @@ void CPlaylistItem::AutoLoadFiles()
}
} while (pos != -1);

CString dir = fn;
dir.Replace('\\', '/');
int l = dir.ReverseFind('/') + 1;
int l2 = dir.ReverseFind('.');
if (l2 < l) { // no extension, read to the end
l2 = fn.GetLength();
}
CString title = dir.Mid(l, l2 - l);
CString title = PathUtils::StripExtensionAndMultiVolumeRarSuffix(PathUtils::BaseName(fn));
paths.Add(title);

CAtlArray<Subtitle::SubFile> ret;
Expand Down