Factor out key-value code.
authorCarl Hetherington <cth@carlh.net>
Mon, 15 Oct 2012 23:10:56 +0000 (00:10 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 15 Oct 2012 23:10:56 +0000 (00:10 +0100)
src/lib/film.cc
src/lib/util.cc
src/lib/util.h

index 636e2d546586cd42c1041ef34268ab9eb037a0f7..e4e7763d5849b9d48c5b7b60bb60357062763f89 100644 (file)
@@ -120,28 +120,10 @@ void
 Film::read_metadata ()
 {
        ifstream f (metadata_file().c_str ());
-       string line;
-       while (getline (f, line)) {
-               if (line.empty ()) {
-                       continue;
-               }
-               
-               if (line[0] == '#') {
-                       continue;
-               }
-
-               if (line[line.size() - 1] == '\r') {
-                       line = line.substr (0, line.size() - 1);
-               }
-
-               size_t const s = line.find (' ');
-               if (s == string::npos) {
-                       continue;
-               }
-
-               _state.read_metadata (line.substr (0, s), line.substr (s + 1));
+       multimap<string, string> kv = read_key_value (f);
+       for (multimap<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
+               _state.read_metadata (i->first, i->second);
        }
-
        _dirty = false;
 }
 
@@ -693,35 +675,18 @@ Film::thumb_subtitle (int n) const
                return pair<Position, string> ();
        }
 
-       ifstream f (sub_file.c_str ());
-       string line;
-
        pair<Position, string> sub;
        
-       while (getline (f, line)) {
-               if (line.empty ()) {
-                       continue;
-               }
-
-               if (line[line.size() - 1] == '\r') {
-                       line = line.substr (0, line.size() - 1);
-               }
-
-               size_t const s = line.find (' ');
-               if (s == string::npos) {
-                       continue;
-               }
-
-               string const k = line.substr (0, s);
-               int const v = lexical_cast<int> (line.substr(s + 1));
-
-               if (k == "x") {
-                       sub.first.x = v;
-               } else if (k == "y") {
-                       sub.first.y = v;
+       ifstream f (sub_file.c_str ());
+       multimap<string, string> kv = read_key_value (f);
+       for (map<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
+               if (i->first == "x") {
+                       sub.first.x = lexical_cast<int> (i->second);
+               } else if (i->first == "y") {
+                       sub.first.y = lexical_cast<int> (i->second);
                        sub.second = String::compose ("%1.sub.png", _state.thumb_base(n));
                }
        }
-
+       
        return sub;
 }
index fbe77461e9ec7ade62b976d11ea74fdc3d871b78..a14db810f590ceab0b5c9819fc1425a9671c7af9 100644 (file)
@@ -621,3 +621,32 @@ round_up (int a, int t)
        return a - (a % t);
 }
 
+multimap<string, string>
+read_key_value (istream &s) 
+{
+       multimap<string, string> kv;
+       
+       string line;
+       while (getline (s, line)) {
+               if (line.empty ()) {
+                       continue;
+               }
+               
+               if (line[0] == '#') {
+                       continue;
+               }
+
+               if (line[line.size() - 1] == '\r') {
+                       line = line.substr (0, line.size() - 1);
+               }
+
+               size_t const s = line.find (' ');
+               if (s == string::npos) {
+                       continue;
+               }
+
+               kv.insert (make_pair (line.substr (0, s), line.substr (s + 1)));
+       }
+
+       return kv;
+}
index 916f47cdb9bd62733b8823ba657a7b0b1b51353a..58528c77527f30277b9d7fb53f24d72200b1dcc9 100644 (file)
@@ -150,6 +150,7 @@ extern std::string crop_string (Position, Size);
 extern int dcp_audio_sample_rate (int);
 extern std::string colour_lut_index_to_name (int index);
 extern int round_up (int, int);
+extern std::multimap<std::string, std::string> read_key_value (std::istream& s);
 
 /** @class Socket
  *  @brief A class to wrap a boost::asio::ip::tcp::socket with some things