Merge branch 'pletscher'
[libdai.git] / src / properties.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <iostream>
13 #include <dai/properties.h>
14 #include <dai/exceptions.h>
15
16
17 namespace dai {
18
19
20 std::ostream& operator<< (std::ostream & os, const Property & p) {
21 os << p.first << "=";
22 if( p.second.type() == typeid(size_t) )
23 os << boost::any_cast<size_t>(p.second);
24 else if( p.second.type() == typeid(std::string) )
25 os << boost::any_cast<std::string>(p.second);
26 else if( p.second.type() == typeid(double) )
27 os << boost::any_cast<double>(p.second);
28 else if( p.second.type() == typeid(bool) )
29 os << boost::any_cast<bool>(p.second);
30 else if( p.second.type() == typeid(PropertySet) )
31 os << boost::any_cast<PropertySet>(p.second);
32 else
33 DAI_THROW(UNKNOWN_PROPERTY_TYPE);
34 return( os );
35 }
36
37
38 /// Writes a PropertySet object to an output stream
39 std::ostream& operator<< (std::ostream & os, const PropertySet & ps) {
40 os << "[";
41 for( PropertySet::const_iterator p = ps.begin(); p != ps.end(); p++ ) {
42 if( p != ps.begin() )
43 os << ",";
44 os << (Property)*p;
45 }
46 os << "]";
47 return os;
48 }
49
50
51 /// Reads a PropertySet object from an input stream, storing values as strings
52 std::istream& operator>> (std::istream& is, PropertySet & ps) {
53 ps = PropertySet();
54
55 std::string s;
56 is >> s;
57
58 // Check whether s is of the form "[.*]"
59 if( (s.length() < 2) || (s.at(0) != '[') || (s.at(s.length()-1)) != ']' )
60 DAI_THROWE(MALFORMED_PROPERTY,"Malformed PropertySet: " + s);
61
62 size_t N = s.length() - 1;
63 for( size_t token_start = 1; token_start < N; ) {
64 size_t token_end;
65
66 // scan until '=' is found
67 for( token_end = token_start + 1; token_end < N; token_end++ )
68 if( s[token_end] == '=' )
69 break;
70 // we found a key
71 std::string key = s.substr(token_start, token_end - token_start);
72 if( token_end == N )
73 DAI_THROWE(MALFORMED_PROPERTY,"Malformed Property: " + key);
74
75 token_start = token_end + 1;
76 // scan until matching ',' is found
77 int level = 0;
78 for( token_end = token_start; token_end < N; token_end++ ) {
79 if( s[token_end] == '[' )
80 level++;
81 else if( s[token_end] == ']' )
82 level--;
83 else if( (s[token_end] == ',') && (level == 0) )
84 break;
85 }
86 if( !(level == 0) )
87 DAI_THROWE(MALFORMED_PROPERTY,"Malformed Property: " + s.substr(token_start, token_end - token_start));
88 // we found a vlue
89 std::string value = s.substr(token_start, token_end - token_start);
90
91 // store the key,value pair
92 ps.Set(key,value);
93
94 // go on with the next one
95 token_start = token_end + 1;
96 }
97
98 return is;
99 }
100
101
102 } // end of namespace dai