diff options
| author | Carl Hetherington <cth@carlh.net> | 2012-10-16 00:10:56 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2012-10-16 00:10:56 +0100 |
| commit | 5428006e97b37d757a03c14024d7a0fb363bdcc6 (patch) | |
| tree | 6526477293153aae8b2a6174cd017b332fe99558 /src/lib | |
| parent | 63b7da59cee71ab2ade744a8b547b5d8f2ff6bfc (diff) | |
Factor out key-value code.
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/film.cc | 57 | ||||
| -rw-r--r-- | src/lib/util.cc | 29 | ||||
| -rw-r--r-- | src/lib/util.h | 1 |
3 files changed, 41 insertions, 46 deletions
diff --git a/src/lib/film.cc b/src/lib/film.cc index 636e2d546..e4e7763d5 100644 --- a/src/lib/film.cc +++ b/src/lib/film.cc @@ -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; } diff --git a/src/lib/util.cc b/src/lib/util.cc index fbe77461e..a14db810f 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -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; +} diff --git a/src/lib/util.h b/src/lib/util.h index 916f47cdb..58528c775 100644 --- a/src/lib/util.h +++ b/src/lib/util.h @@ -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 |
