X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fsubrip.cc;h=a707d1f9fd13641301a3782f0ee84b272ea5a8c9;hb=d2ca18d5b2a3351745cc8be2840308808e5a2d36;hp=8a7423698d850268c7b927970831ab0a63c3029a;hpb=f90d74f7a0382f0dc63eef81bd553d7a7b38edb2;p=dcpomatic.git diff --git a/src/lib/subrip.cc b/src/lib/subrip.cc index 8a7423698..a707d1f9f 100644 --- a/src/lib/subrip.cc +++ b/src/lib/subrip.cc @@ -21,22 +21,51 @@ #include "cross.h" #include "exceptions.h" #include "subrip_content.h" -#include -#include +#include "data.h" +#include +#include +#include +#include +#include #include "i18n.h" using std::vector; +using std::cout; +using std::string; using boost::shared_ptr; +using boost::scoped_array; SubRip::SubRip (shared_ptr content) { - FILE* f = fopen_boost (content->path (0), "r"); - if (!f) { - throw OpenFileError (content->path (0)); - } + Data in (content->path (0)); + + UErrorCode status = U_ZERO_ERROR; + UCharsetDetector* detector = ucsdet_open (&status); + ucsdet_setText (detector, reinterpret_cast (in.data().get()), in.size(), &status); + + UCharsetMatch const * match = ucsdet_detect (detector, &status); + char const * in_charset = ucsdet_getName (match, &status); + + UConverter* to_utf16 = ucnv_open (in_charset, &status); + /* This is a guess; I think we should be able to encode any input in 4 times its input size */ + scoped_array utf16 (new uint16_t[in.size() * 2]); + int const utf16_len = ucnv_toUChars ( + to_utf16, reinterpret_cast(utf16.get()), in.size() * 2, + reinterpret_cast (in.data().get()), in.size(), + &status + ); + + UConverter* to_utf8 = ucnv_open ("UTF-8", &status); + /* Another guess */ + scoped_array utf8 (new char[utf16_len * 2]); + ucnv_fromUChars (to_utf8, utf8.get(), utf16_len * 2, reinterpret_cast(utf16.get()), utf16_len, &status); + + ucsdet_close (detector); + ucnv_close (to_utf16); + ucnv_close (to_utf8); - sub::SubripReader reader (f); + sub::SubripReader reader (utf8.get()); _subtitles = sub::collect > (reader.subtitles ()); } @@ -47,5 +76,5 @@ SubRip::length () const return ContentTime (); } - return ContentTime::from_seconds (_subtitles.back().to.metric().get().all_as_seconds ()); + return ContentTime::from_seconds (_subtitles.back().to.all_as_seconds ()); }