Remove a possibly dodgy use of fstream.
authorCarl Hetherington <cth@carlh.net>
Mon, 25 Nov 2013 11:23:07 +0000 (11:23 +0000)
committerCarl Hetherington <cth@carlh.net>
Mon, 25 Nov 2013 11:23:07 +0000 (11:23 +0000)
src/subtitle_asset.cc
src/subtitle_asset.h
src/util.cc
src/util.h
test/subs_in_out.cc

index d48d4ac054d653129973b56b7fb32b41c7e46aff..97c489788f77a79c99fac3fa5ee731a3048ef268 100644 (file)
@@ -302,12 +302,14 @@ struct SubtitleSorter {
 void
 SubtitleAsset::write_xml () const
 {
-       ofstream s (path().string().c_str());
-       write_xml (s);
+       FILE* f = fopen_boost (path (), "r");
+       Glib::ustring const s = xml_as_string ();
+       fwrite (s.c_str(), 1, s.length(), f);
+       fclose (f);
 }
 
-void
-SubtitleAsset::write_xml (ostream& s) const
+Glib::ustring
+SubtitleAsset::xml_as_string () const
 {
        xmlpp::Document doc;
        xmlpp::Element* root = doc.create_root_node ("DCSubtitle");
@@ -415,6 +417,6 @@ SubtitleAsset::write_xml (ostream& s) const
                text->add_child_text ((*i)->text());
        }
 
-       doc.write_to_stream_formatted (s, "UTF-8");
+       return doc.write_to_string_formatted ("UTF-8");
 }
 
index 6d6186a030e1809454141f49d290f55bac797a2c..7a00e0369eba87549de42fc4a75b3cebd2b5f939 100644 (file)
@@ -159,7 +159,7 @@ public:
 
        void read_xml (std::string);
        void write_xml () const;
-       void write_xml (std::ostream &) const;
+       Glib::ustring xml_as_string () const;
 
 private:
        std::string font_id_to_name (std::string id) const;
index 5907851156c9ff7603e6d5ba96a2de8a99551aa5..8624ae5184e3273acbd9194fcf9bc785815d7a97 100644 (file)
@@ -343,3 +343,20 @@ libdcp::ptime_to_string (boost::posix_time::ptime t)
        struct tm t_tm = boost::posix_time::to_tm (t);
        return tm_to_string (&t_tm);
 }
+
+
+/* Apparently there is no way to create an ofstream using a UTF-8
+   filename under Windows.  We are hence reduced to using fopen
+   with this wrapper.
+*/
+FILE *
+libdcp::fopen_boost (boost::filesystem::path p, string t)
+{
+#ifdef LIBDCP_WINDOWS
+        wstring w (t.begin(), t.end());
+       /* c_str() here should give a UTF-16 string */
+        return _wfopen (p.c_str(), w.c_str ());
+#else
+        return fopen (p.c_str(), t.c_str ());
+#endif
+}
index 2f661d772b3457514c80da6416aaf69ca43d18da..2a6aae1b004de20443ab631e34ad8c4fa2c415ad 100644 (file)
@@ -84,6 +84,7 @@ extern int base64_decode (std::string const & in, unsigned char* out, int out_le
 extern std::string tm_to_string (struct tm *);
 extern std::string utc_offset_to_string (int);
 extern std::string ptime_to_string (boost::posix_time::ptime);
+extern FILE * fopen_boost (boost::filesystem::path, std::string);
        
 }
 
index 209602e54a3e82b8feb7cc82c493ccf5025939f8..f0a7e96f1039d7c6ba7817203edccc008ef961d9 100644 (file)
@@ -12,6 +12,6 @@ int main (int argc, char* argv[])
        
        libdcp::SubtitleAsset s ("foo", "bar", "baz");
        s.read_xml (argv[1]);
-       s.write_xml (cout);
+       cout << s.xml_as_string ();
        return 0;
 }