Tags -> Metadata
[libdcp.git] / src / util.cc
index aab2e184ad2654e819950e9bc1fc13a31da20056..2968ef55f49885fb4a488f9db5bc54fc52735532 100644 (file)
 
 */
 
+/** @file  src/util.cc
+ *  @brief Utility methods.
+ */
+
 #include <stdexcept>
 #include <sstream>
+#include <iostream>
 #include <iomanip>
+#include <boost/filesystem.hpp>
 #include <openssl/sha.h>
 #include "KM_util.h"
 #include "KM_fileio.h"
 #include "util.h"
 
 using namespace std;
+using namespace boost;
 
-bool libdcp::libdcp_test = false;
-
-/** Create a UUID.
- *  @return UUID.
- */
 string
 libdcp::make_uuid ()
 {
        char buffer[64];
        Kumu::UUID id;
-
-       if (libdcp_test) {
-               static int N = 0;
-               byte_t t[16];
-               for (int i = 0; i < 16; ++i) {
-                       t[i] = N;
-               }
-               ++N;
-               
-               id = Kumu::UUID (t);
-       } else {
-               Kumu::GenRandomValue (id);
-       }
-       
+       Kumu::GenRandomValue (id);
        id.EncodeHex (buffer, 64);
        return string (buffer);
 }
 
-/** Create a digest for a file.
- *  @param filename File name.
- *  @return Digest.
- */
 string
-libdcp::make_digest (string filename)
+libdcp::make_digest (string filename, sigc::signal1<void, float>* progress)
 {
+       int const file_size = filesystem::file_size (filename);
+       
        Kumu::FileReader reader;
        if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) {
                throw runtime_error ("could not open file to compute digest");
@@ -72,6 +59,7 @@ libdcp::make_digest (string filename)
        SHA1_Init (&sha);
        
        Kumu::ByteString read_buffer (65536);
+       int done = 0;
        while (1) {
                ui32_t read = 0;
                Kumu::Result_t r = reader.Read (read_buffer.Data(), read_buffer.Capacity(), &read);
@@ -83,6 +71,11 @@ libdcp::make_digest (string filename)
                }
                
                SHA1_Update (&sha, read_buffer.Data(), read);
+               done += read;
+
+               if (progress) {
+                       (*progress) (0.5 + (0.5 * done / file_size));
+               }
        }
 
        byte_t byte_buffer[20];
@@ -90,6 +83,5 @@ libdcp::make_digest (string filename)
 
        stringstream s;
        char digest[64];
-       s << setfill('0') << setw(36) << Kumu::base64encode (byte_buffer, 20, digest, 64);
-       return s.str ();
+       return Kumu::base64encode (byte_buffer, 20, digest, 64);
 }