Read UTF8 string lengths correctly when checking closed captions (part of #1446).
[dcpomatic.git] / src / lib / util.cc
index a1ea8f9f16c645436fc830cc9ada4e75c48bf4bb..cd2d2e753b3827c74e6491c49583b2595c97ed7f 100644 (file)
@@ -815,6 +815,25 @@ checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path pat
        }
 }
 
+size_t
+utf8_strlen (string s)
+{
+       size_t const len = s.length ();
+       int N = 0;
+       for (size_t i = 0; i < len; ++i) {
+               unsigned char c = s[i];
+               if ((c & 0xe0) == 0xc0) {
+                       ++i;
+               } else if ((c & 0xf0) == 0xe0) {
+                       i += 2;
+               } else if ((c & 0xf8) == 0xf0) {
+                       i += 3;
+               }
+               ++N;
+       }
+       return N;
+}
+
 #ifdef DCPOMATIC_VARIANT_SWAROOP
 
 /* Make up a key from the machine UUID */
@@ -888,8 +907,6 @@ read_swaroop_chain (boost::filesystem::path path)
 void
 write_swaroop_chain (shared_ptr<const dcp::CertificateChain> chain, boost::filesystem::path output)
 {
-       cout << "write " << output.string() << "\n";
-
        scoped_array<uint8_t> buffer (new uint8_t[65536]);
        Header* header = (Header *) buffer.get();
        memset (header, 0, sizeof(Header));