Some maths operations with Time.
[libdcp.git] / src / xml.cc
index 0f0d2d4764f3ed965e1e8eb55b880ce0f77cf524..35c4fe1274de2ef8e8f0e0701d6a9091f2d9b805 100644 (file)
@@ -126,17 +126,27 @@ XMLNode::ignore_node (string name)
 Time
 XMLNode::time_attribute (string name)
 {
-       string const t = string_attribute (name);
+       return Time (string_attribute (name));
+}
 
-       vector<string> b;
-       boost::split (b, t, is_any_of (":"));
-       assert (b.size() == 4);
+string
+XMLNode::string_attribute (string name)
+{
+       xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
+       if (!e) {
+               throw XMLError ("missing attribute");
+       }
+       
+       xmlpp::Attribute* a = e->get_attribute (name);
+       if (!a) {
+               throw XMLError ("missing attribute");
+       }
 
-       return Time (lexical_cast<int> (b[0]), lexical_cast<int> (b[1]), lexical_cast<int> (b[2]), lexical_cast<int> (b[3]));
+       return a->get_value ();
 }
 
 string
-XMLNode::string_attribute (string name)
+XMLNode::optional_string_attribute (string name)
 {
        xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
        if (!e) {
@@ -163,6 +173,43 @@ XMLNode::int64_attribute (string name)
        return lexical_cast<int64_t> (string_attribute (name));
 }
 
+int64_t
+XMLNode::optional_int64_attribute (string name)
+{
+       string const s = optional_string_attribute (name);
+       if (s.empty ()) {
+               return 0;
+       }
+       
+       return lexical_cast<int64_t> (s);
+}
+
+optional<bool>
+XMLNode::optional_bool_attribute (string name)
+{
+       string const s = optional_string_attribute (name);
+       if (s.empty ()) {
+               return optional<bool> ();
+       }
+
+       if (s == "1" || s == "yes") {
+               return optional<bool> (true);
+       }
+
+       return optional<bool> (false);
+}
+
+optional<Color>
+XMLNode::optional_color_attribute (string name)
+{
+       string const s = optional_string_attribute (name);
+       if (s.empty ()) {
+               return optional<Color> ();
+       }
+
+       return optional<Color> (Color (s));
+}
+
 void
 XMLNode::done ()
 {