Use uchardet to guess encoding of subtitle files and reject non-UTF-8.
[dcpomatic.git] / src / lib / subrip.cc
index 8a7423698d850268c7b927970831ab0a63c3029a..d4adee428456823d3c62d94d596388204e6f6f16 100644 (file)
 #include "cross.h"
 #include "exceptions.h"
 #include "subrip_content.h"
-#include <libsub/subrip_reader.h>
-#include <libsub/collect.h>
+#include <sub/subrip_reader.h>
+#include <sub/collect.h>
+#include <uchardet/uchardet.h>
+#include <iostream>
 
 #include "i18n.h"
 
 using std::vector;
+using std::cout;
+using std::string;
 using boost::shared_ptr;
 
 SubRip::SubRip (shared_ptr<const SubRipContent> content)
@@ -36,6 +40,25 @@ SubRip::SubRip (shared_ptr<const SubRipContent> content)
                throw OpenFileError (content->path (0));
        }
 
+       /* Guess the encoding */
+       uchardet_t det = uchardet_new ();
+       char buffer[1024];
+       while (!feof (f)) {
+               int const n = fread (buffer, 1, sizeof (buffer), f);
+               if (uchardet_handle_data (det, buffer, n)) {
+                       break;
+               }
+       }
+
+       uchardet_data_end (det);
+       string charset = uchardet_get_charset (det);
+       uchardet_delete (det);
+
+       if (charset != "UTF-8") {
+               throw TextEncodingError (_("unrecognised character set; please use files encoded in UTF-8"));
+       }
+
+       rewind (f);
        sub::SubripReader reader (f);
        _subtitles = sub::collect<vector<sub::Subtitle> > (reader.subtitles ());
 }
@@ -47,5 +70,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 ());
 }