forked from mia/Aegisub
Use agi::scoped_holder to close files in mkvwrap
This commit is contained in:
parent
bbd4cbef78
commit
9a21c13cbe
1 changed files with 73 additions and 83 deletions
|
@ -44,6 +44,7 @@
|
|||
#include "MatroskaParser.h"
|
||||
|
||||
#include <libaegisub/fs.h>
|
||||
#include <libaegisub/scoped_ptr.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
|
@ -135,19 +136,17 @@ static void read_subtitles(agi::ProgressSink *ps, MatroskaFile *file, MkvStdIO *
|
|||
void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *target) {
|
||||
MkvStdIO input(filename);
|
||||
char err[2048];
|
||||
MatroskaFile *file = mkv_Open(&input, err, sizeof(err));
|
||||
agi::scoped_holder<MatroskaFile*, decltype(&mkv_Close)> file(mkv_Open(&input, err, sizeof(err)), mkv_Close);
|
||||
if (!file) throw MatroskaException(err);
|
||||
|
||||
try {
|
||||
// Get info
|
||||
unsigned tracks = mkv_GetNumTracks(file);
|
||||
std::vector<unsigned> tracksFound;
|
||||
std::vector<std::string> tracksNames;
|
||||
unsigned trackToRead;
|
||||
|
||||
// Find tracks
|
||||
for (unsigned track = 0; track < tracks; track++) {
|
||||
TrackInfo *trackInfo = mkv_GetTrackInfo(file, track);
|
||||
for (auto track : boost::irange(0u, tracks)) {
|
||||
auto trackInfo = mkv_GetTrackInfo(file, track);
|
||||
if (trackInfo->Type != 0x11) continue;
|
||||
|
||||
// Known subtitle format
|
||||
|
@ -166,6 +165,7 @@ void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *targe
|
|||
if (tracksFound.empty())
|
||||
throw MatroskaException("File has no recognised subtitle tracks.");
|
||||
|
||||
unsigned trackToRead;
|
||||
// Only one track found
|
||||
if (tracksFound.size() == 1)
|
||||
trackToRead = tracksFound[0];
|
||||
|
@ -180,7 +180,7 @@ void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *targe
|
|||
|
||||
// Picked track
|
||||
mkv_SetTrackMask(file, ~(1 << trackToRead));
|
||||
TrackInfo *trackInfo = mkv_GetTrackInfo(file, trackToRead);
|
||||
auto trackInfo = mkv_GetTrackInfo(file, trackToRead);
|
||||
std::string CodecID(trackInfo->CodecID);
|
||||
bool srt = CodecID == "S_TEXT/UTF8";
|
||||
bool ssa = CodecID == "S_TEXT/SSA";
|
||||
|
@ -204,25 +204,20 @@ void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *targe
|
|||
}
|
||||
|
||||
// Read timecode scale
|
||||
SegmentInfo *segInfo = mkv_GetFileInfo(file);
|
||||
auto segInfo = mkv_GetFileInfo(file);
|
||||
longlong timecodeScale = mkv_TruncFloat(trackInfo->TimecodeScale) * segInfo->TimecodeScale;
|
||||
|
||||
// Progress bar
|
||||
double totalTime = double(segInfo->Duration) / timecodeScale;
|
||||
auto totalTime = double(segInfo->Duration) / timecodeScale;
|
||||
DialogProgress progress(nullptr, _("Parsing Matroska"), _("Reading subtitles from Matroska file."));
|
||||
progress.Run([&](agi::ProgressSink *ps) { read_subtitles(ps, file, &input, srt, totalTime, &parser); });
|
||||
}
|
||||
catch (...) {
|
||||
mkv_Close(file);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
bool MatroskaWrapper::HasSubtitles(agi::fs::path const& filename) {
|
||||
char err[2048];
|
||||
try {
|
||||
MkvStdIO input(filename);
|
||||
auto file = mkv_Open(&input, err, sizeof(err));
|
||||
agi::scoped_holder<MatroskaFile*, decltype(&mkv_Close)> file(mkv_Open(&input, err, sizeof(err)), mkv_Close);
|
||||
if (!file) return false;
|
||||
|
||||
// Find tracks
|
||||
|
@ -232,20 +227,16 @@ bool MatroskaWrapper::HasSubtitles(agi::fs::path const& filename) {
|
|||
|
||||
if (trackInfo->Type == 0x11) {
|
||||
std::string CodecID(trackInfo->CodecID);
|
||||
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8") {
|
||||
mkv_Close(file);
|
||||
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8")
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mkv_Close(file);
|
||||
return false;
|
||||
}
|
||||
catch (...) {
|
||||
// We don't care about why we couldn't read subtitles here
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int StdIoRead(InputStream *_st, ulonglong pos, void *buffer, int count) {
|
||||
|
@ -274,9 +265,8 @@ longlong StdIoScan(InputStream *st, ulonglong start, unsigned signature) {
|
|||
return -1;
|
||||
|
||||
int c;
|
||||
unsigned cmp = 0;
|
||||
while ((c = getc(fp)) != EOF) {
|
||||
cmp = ((cmp << 8) | c) & 0xffffffff;
|
||||
unsigned cmp = ((cmp << 8) | c) & 0xffffffff;
|
||||
if (cmp == signature)
|
||||
return std_ftell(fp) - 4;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue