Merge master.
[dcpomatic.git] / src / lib / util.cc
index 1339be73d1044cf58192454984dc5a3de2a9f2c7..14dfd1fa558b6afa86c7dc03a2b05b139d9bce4d 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
     Copyright (C) 2000-2007 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
@@ -37,7 +37,6 @@
 #include <boost/algorithm/string.hpp>
 #include <boost/bind.hpp>
 #include <boost/lambda/lambda.hpp>
-#include <boost/lexical_cast.hpp>
 #include <boost/thread.hpp>
 #include <boost/filesystem.hpp>
 #ifdef DCPOMATIC_WINDOWS
@@ -53,6 +52,7 @@
 #include <dcp/util.h>
 #include <dcp/signer_chain.h>
 #include <dcp/signer.h>
+#include <dcp/raw_convert.h>
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
@@ -101,9 +101,9 @@ using std::streampos;
 using std::set_terminate;
 using boost::shared_ptr;
 using boost::thread;
-using boost::lexical_cast;
 using boost::optional;
 using dcp::Size;
+using dcp::raw_convert;
 
 static boost::thread::id ui_thread;
 static boost::filesystem::path backtrace_file;
@@ -687,14 +687,14 @@ int
 get_required_int (multimap<string, string> const & kv, string k)
 {
        string const v = get_required_string (kv, k);
-       return lexical_cast<int> (v);
+       return raw_convert<int> (v);
 }
 
 float
 get_required_float (multimap<string, string> const & kv, string k)
 {
        string const v = get_required_string (kv, k);
-       return lexical_cast<float> (v);
+       return raw_convert<float> (v);
 }
 
 string
@@ -724,7 +724,7 @@ get_optional_int (multimap<string, string> const & kv, string k)
                return 0;
        }
 
-       return lexical_cast<int> (i->second);
+       return raw_convert<int> (i->second);
 }
 
 /** Trip an assert if the caller is not in the UI thread */
@@ -737,7 +737,7 @@ ensure_ui_thread ()
 string
 audio_channel_name (int c)
 {
-       assert (MAX_AUDIO_CHANNELS == 12);
+       assert (MAX_DCP_AUDIO_CHANNELS == 12);
 
        /* TRANSLATORS: these are the names of audio channels; Lfe (sub) is the low-frequency
           enhancement channel (sub-woofer).  HI is the hearing-impaired audio track and
@@ -761,31 +761,12 @@ audio_channel_name (int c)
        return channels[c];
 }
 
-LocaleGuard::LocaleGuard ()
-       : _old (0)
-{
-       char const * old = setlocale (LC_NUMERIC, 0);
-
-       if (old) {
-               _old = strdup (old);
-               if (strcmp (_old, "C")) {
-                       setlocale (LC_NUMERIC, "C");
-               }
-       }
-}
-
-LocaleGuard::~LocaleGuard ()
-{
-       setlocale (LC_NUMERIC, _old);
-       free (_old);
-}
-
 bool
 valid_image_file (boost::filesystem::path f)
 {
        string ext = f.extension().string();
        transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
-       return (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".tga");
+       return (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".tga" || ext == ".dpx");
 }
 
 string
@@ -957,3 +938,47 @@ dependency_version_summary ()
 
        return s.str ();
 }
+
+/** Construct a ScopedTemporary.  A temporary filename is decided but the file is not opened
+ *  until ::open() is called.
+ */
+ScopedTemporary::ScopedTemporary ()
+       : _open (0)
+{
+       _file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path ();
+}
+
+/** Close and delete the temporary file */
+ScopedTemporary::~ScopedTemporary ()
+{
+       close ();       
+       boost::system::error_code ec;
+       boost::filesystem::remove (_file, ec);
+}
+
+/** @return temporary filename */
+char const *
+ScopedTemporary::c_str () const
+{
+       return _file.string().c_str ();
+}
+
+/** Open the temporary file.
+ *  @return File's FILE pointer.
+ */
+FILE*
+ScopedTemporary::open (char const * params)
+{
+       _open = fopen (c_str(), params);
+       return _open;
+}
+
+/** Close the file */
+void
+ScopedTemporary::close ()
+{
+       if (_open) {
+               fclose (_open);
+               _open = 0;
+       }
+}