Stop using static initialisation so that dcpomatic::write() can be called more than...
[dcpomatic.git] / src / lib / string_text_file.cc
index d3c56832dfb85a40ccc979b6d85a7b1cdf13779d..76abe547f35045413e1a7b945f72a939d59ea72a 100644 (file)
 
 */
 
-#include "string_text_file.h"
+
 #include "cross.h"
 #include "exceptions.h"
+#include "string_text_file.h"
 #include "string_text_file_content.h"
-#include <sub/subrip_reader.h>
+#include <dcp/file.h>
+#include <sub/collect.h>
 #include <sub/ssa_reader.h>
 #include <sub/stl_binary_reader.h>
-#include <sub/collect.h>
+#include <sub/subrip_reader.h>
 #include <unicode/ucsdet.h>
 #include <unicode/ucnv.h>
 #include <iostream>
 
 #include "i18n.h"
 
-using std::vector;
+
 using std::cout;
+using std::shared_ptr;
 using std::string;
-using boost::shared_ptr;
+using std::vector;
 using boost::scoped_array;
 using boost::optional;
-using dcp::Data;
+using dcp::ArrayData;
 using namespace dcpomatic;
 
+
 StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
 {
        string ext = content->path(0).extension().string();
        transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
 
-       sub::Reader* reader = 0;
+       std::unique_ptr<sub::Reader> reader;
 
        if (ext == ".stl") {
-               FILE* f = fopen_boost (content->path(0), "rb");
+               dcp::File f(content->path(0), "rb");
                if (!f) {
-                       throw OpenFileError (content->path(0), errno, OpenFileError::READ);
+                       throw OpenFileError (f.path(), errno, OpenFileError::READ);
                }
                try {
-                       reader = new sub::STLBinaryReader (f);
+                       reader.reset(new sub::STLBinaryReader(f.get()));
                } catch (...) {
-                       fclose (f);
                        throw;
                }
-               fclose (f);
 
        } else {
                /* Text-based file; sort out its character encoding before we try to parse it */
 
-               Data in (content->path (0));
+               ArrayData in (content->path (0));
 
                UErrorCode status = U_ZERO_ERROR;
                UCharsetDetector* detector = ucsdet_open (&status);
-               ucsdet_setText (detector, reinterpret_cast<const char *> (in.data().get()), in.size(), &status);
+               ucsdet_setText (detector, reinterpret_cast<const char *>(in.data()), in.size(), &status);
 
                UCharsetMatch const * match = ucsdet_detect (detector, &status);
                char const * in_charset = ucsdet_getName (match, &status);
@@ -78,7 +80,7 @@ StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
                scoped_array<uint16_t> utf16 (new uint16_t[in.size() * 2]);
                int const utf16_len = ucnv_toUChars (
                                to_utf16, reinterpret_cast<UChar*>(utf16.get()), in.size() * 2,
-                               reinterpret_cast<const char *> (in.data().get()), in.size(),
+                               reinterpret_cast<const char *>(in.data()), in.size(),
                                &status
                                );
 
@@ -100,17 +102,15 @@ StringTextFile::StringTextFile (shared_ptr<const StringTextFileContent> content)
                ucnv_close (to_utf8);
 
                if (ext == ".srt") {
-                       reader = new sub::SubripReader (utf8.get());
+                       reader.reset(new sub::SubripReader(utf8.get()));
                } else if (ext == ".ssa" || ext == ".ass") {
-                       reader = new sub::SSAReader (utf8.get());
+                       reader.reset(new sub::SSAReader(utf8.get()));
                }
        }
 
        if (reader) {
-               _subtitles = sub::collect<vector<sub::Subtitle> > (reader->subtitles ());
+               _subtitles = sub::collect<vector<sub::Subtitle>>(reader->subtitles());
        }
-
-       delete reader;
 }
 
 /** @return time of first subtitle, if there is one */
@@ -118,7 +118,7 @@ optional<ContentTime>
 StringTextFile::first () const
 {
        if (_subtitles.empty()) {
-               return optional<ContentTime>();
+               return {};
        }
 
        return ContentTime::from_seconds(_subtitles[0].from.all_as_seconds());
@@ -128,7 +128,7 @@ ContentTime
 StringTextFile::length () const
 {
        if (_subtitles.empty ()) {
-               return ContentTime ();
+               return {};
        }
 
        return ContentTime::from_seconds (_subtitles.back().to.all_as_seconds ());