Merge master; fix crash on new film.
[dcpomatic.git] / src / lib / util.cc
index 760b826c7649cc18cc20462bf07773c459412897..ec1fd47bd7f03bdca133d791b6201d824f252cab 100644 (file)
@@ -27,7 +27,7 @@
 #include <iostream>
 #include <fstream>
 #include <climits>
-#ifdef DVDOMATIC_POSIX
+#ifdef DCPOMATIC_POSIX
 #include <execinfo.h>
 #include <cxxabi.h>
 #endif
@@ -83,9 +83,10 @@ using std::pair;
 using boost::shared_ptr;
 using boost::thread;
 using boost::lexical_cast;
+using boost::optional;
 using libdcp::Size;
 
-thread::id ui_thread;
+boost::thread::id ui_thread;
 
 /** Convert some number of seconds to a string representation
  *  in hours, minutes and seconds.
@@ -105,9 +106,9 @@ seconds_to_hms (int s)
        stringstream hms;
        hms << h << N_(":");
        hms.width (2);
-       hms << setfill ('0') << m << N_(":");
+       hms << std::setfill ('0') << m << N_(":");
        hms.width (2);
-       hms << setfill ('0') << s;
+       hms << std::setfill ('0') << s;
 
        return hms.str ();
 }
@@ -148,7 +149,7 @@ seconds_to_approximate_hms (int s)
        return ap.str ();
 }
 
-#ifdef DVDOMATIC_POSIX
+#ifdef DCPOMATIC_POSIX
 /** @param l Mangled C++ identifier.
  *  @return Demangled version.
  */
@@ -203,7 +204,7 @@ stacktrace (ostream& out, int levels)
      
        if (strings) {
                for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
-                       out << N_("  ") << demangle (strings[i]) << endl;
+                       out << N_("  ") << demangle (strings[i]) << "\n";
                }
                
                free (strings);
@@ -247,11 +248,11 @@ seconds (struct timeval t)
        return t.tv_sec + (double (t.tv_usec) / 1e6);
 }
 
-/** Call the required functions to set up DVD-o-matic's static arrays, etc.
+/** Call the required functions to set up DCP-o-matic's static arrays, etc.
  *  Must be called from the UI thread, if there is one.
  */
 void
-dvdomatic_setup ()
+dcpomatic_setup ()
 {
        avfilter_register_all ();
        
@@ -264,7 +265,7 @@ dvdomatic_setup ()
        ui_thread = boost::this_thread::get_id ();
 }
 
-#ifdef DVDOMATIC_WINDOWS
+#ifdef DCPOMATIC_WINDOWS
 boost::filesystem::path
 mo_path ()
 {
@@ -279,9 +280,9 @@ mo_path ()
 #endif
 
 void
-dvdomatic_setup_i18n (string lang)
+dcpomatic_setup_i18n (string lang)
 {
-#ifdef DVDOMATIC_POSIX
+#ifdef DCPOMATIC_POSIX
        lang += ".UTF8";
 #endif
 
@@ -297,14 +298,15 @@ dvdomatic_setup_i18n (string lang)
        }
 
        setlocale (LC_ALL, "");
-       textdomain ("libdvdomatic");
+       textdomain ("libdcpomatic");
 
-#ifdef DVDOMATIC_WINDOWS
-       bindtextdomain ("libdvdomatic", mo_path().string().c_str());
+#ifdef DCPOMATIC_WINDOWS
+       bindtextdomain ("libdcpomatic", mo_path().string().c_str());
+       bind_textdomain_codeset ("libdcpomatic", "UTF8");
 #endif 
 
-#ifdef DVDOMATIC_POSIX
-       bindtextdomain ("libdvdomatic", POSIX_LOCALE_PREFIX);
+#ifdef DCPOMATIC_POSIX
+       bindtextdomain ("libdcpomatic", POSIX_LOCALE_PREFIX);
 #endif
 }
 
@@ -355,7 +357,7 @@ md5_digest (void const * data, int size)
        
        stringstream s;
        for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
-               s << hex << setfill('0') << setw(2) << ((int) digest[i]);
+               s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
        }
 
        return s.str ();
@@ -367,14 +369,14 @@ md5_digest (void const * data, int size)
 string
 md5_digest (boost::filesystem::path file)
 {
-       ifstream f (file.string().c_str(), ios::binary);
+       ifstream f (file.string().c_str(), std::ios::binary);
        if (!f.good ()) {
                throw OpenFileError (file.string());
        }
        
-       f.seekg (0, ios::end);
+       f.seekg (0, std::ios::end);
        int bytes = f.tellg ();
-       f.seekg (0, ios::beg);
+       f.seekg (0, std::ios::beg);
 
        int const buffer_size = 64 * 1024;
        char buffer[buffer_size];
@@ -393,7 +395,7 @@ md5_digest (boost::filesystem::path file)
 
        stringstream s;
        for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
-               s << hex << setfill('0') << setw(2) << ((int) digest[i]);
+               s << std::hex << std::setfill('0') << std::setw(2) << ((int) digest[i]);
        }
 
        return s.str ();
@@ -458,8 +460,8 @@ best_dcp_frame_rate (float source_fps)
        }
 
        /* Pick the best one, bailing early if we hit an exact match */
-       float error = numeric_limits<float>::max ();
-       boost::optional<FrameRateCandidate> best;
+       float error = std::numeric_limits<float>::max ();
+       optional<FrameRateCandidate> best;
        list<FrameRateCandidate>::iterator i = candidates.begin();
        while (i != candidates.end()) {
                
@@ -768,6 +770,21 @@ AudioBuffers::AudioBuffers (AudioBuffers const & other)
        }
 }
 
+/* XXX: it's a shame that this is a copy-and-paste of the above;
+   probably fixable with c++0x.
+*/
+AudioBuffers::AudioBuffers (boost::shared_ptr<const AudioBuffers> other)
+       : _channels (other->_channels)
+       , _frames (other->_frames)
+       , _allocated_frames (other->_frames)
+{
+       _data = new float*[_channels];
+       for (int i = 0; i < _channels; ++i) {
+               _data[i] = new float[_frames];
+               memcpy (_data[i], other->_data[i], _frames * sizeof (float));
+       }
+}
+
 /** AudioBuffers destructor */
 AudioBuffers::~AudioBuffers ()
 {
@@ -870,7 +887,7 @@ AudioBuffers::move (int from, int to, int frames)
 
 /** Add data from from `from', `from_channel' to our channel `to_channel' */
 void
-AudioBuffers::accumulate (shared_ptr<AudioBuffers> from, int from_channel, int to_channel)
+AudioBuffers::accumulate (shared_ptr<const AudioBuffers> from, int from_channel, int to_channel)
 {
        int const N = frames ();
        assert (from->frames() == N);
@@ -908,7 +925,7 @@ cpu_info ()
        pair<string, int> info;
        info.second = 0;
        
-#ifdef DVDOMATIC_POSIX
+#ifdef DCPOMATIC_POSIX
        ifstream f (N_("/proc/cpuinfo"));
        while (f.good ()) {
                string l;
@@ -971,7 +988,26 @@ FrameRateConversion::FrameRateConversion (float source, int dcp)
 
                if (change_speed) {
                        float const pc = dcp * 100 / (source * factor());
-                       description += String::compose (_("DCP will run at %1%% of the source speed."), pc);
+                       description += String::compose (_("DCP will run at %1%% of the source speed.\n"), pc);
                }
        }
 }
+
+LocaleGuard::LocaleGuard ()
+       : _old (0)
+{
+       char const * old = setlocale (LC_NUMERIC, 0);
+
+        if (old) {
+                _old = strdup (old);
+                if (strcmp (_old, "POSIX")) {
+                        setlocale (LC_NUMERIC, "POSIX");
+                }
+        }
+}
+
+LocaleGuard::~LocaleGuard ()
+{
+       setlocale (LC_NUMERIC, _old);
+       free (_old);
+}