New git HEAD version
[libdai.git] / src / properties.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <iostream>
10 #include <dai/properties.h>
11 #include <dai/exceptions.h>
12
13
14 namespace dai {
15
16
17 std::ostream& operator<< (std::ostream & os, const Property & p) {
18 os << p.first << "=";
19 if( p.second.type() == typeid(size_t) )
20 os << boost::any_cast<size_t>(p.second);
21 else if( p.second.type() == typeid(int) )
22 os << boost::any_cast<int>(p.second);
23 else if( p.second.type() == typeid(std::string) )
24 os << boost::any_cast<std::string>(p.second);
25 else if( p.second.type() == typeid(double) )
26 os << boost::any_cast<double>(p.second);
27 else if( p.second.type() == typeid(long double) )
28 os << boost::any_cast<long double>(p.second);
29 else if( p.second.type() == typeid(bool) )
30 os << boost::any_cast<bool>(p.second);
31 else if( p.second.type() == typeid(PropertySet) )
32 os << boost::any_cast<PropertySet>(p.second);
33 else
34 DAI_THROW(UNKNOWN_PROPERTY_TYPE);
35 return( os );
36 }
37
38
39 /// Writes a PropertySet object to an output stream
40 std::ostream& operator<< (std::ostream & os, const PropertySet & ps) {
41 os << "[";
42 for( PropertySet::const_iterator p = ps.begin(); p != ps.end(); p++ ) {
43 if( p != ps.begin() )
44 os << ",";
45 os << (Property)*p;
46 }
47 os << "]";
48 return os;
49 }
50
51
52 /// Reads a PropertySet object from an input stream, storing values as strings
53 std::istream& operator>> (std::istream& is, PropertySet & ps) {
54 ps = PropertySet();
55
56 std::string s;
57 is >> s;
58
59 // Check whether s is of the form "[.*]"
60 if( (s.length() < 2) || (s.at(0) != '[') || (s.at(s.length()-1)) != ']' )
61 DAI_THROWE(MALFORMED_PROPERTY,"Malformed PropertySet: " + s);
62
63 size_t N = s.length() - 1;
64 for( size_t token_start = 1; token_start < N; ) {
65 size_t token_end;
66
67 // scan until '=' is found
68 for( token_end = token_start + 1; token_end < N; token_end++ )
69 if( s[token_end] == '=' )
70 break;
71 // we found a key
72 std::string key = s.substr(token_start, token_end - token_start);
73 if( token_end == N )
74 DAI_THROWE(MALFORMED_PROPERTY,"Malformed Property: " + key);
75
76 token_start = token_end + 1;
77 // scan until matching ',' is found
78 int level = 0;
79 for( token_end = token_start; token_end < N; token_end++ ) {
80 if( s[token_end] == '[' )
81 level++;
82 else if( s[token_end] == ']' )
83 level--;
84 else if( (s[token_end] == ',') && (level == 0) )
85 break;
86 }
87 if( !(level == 0) )
88 DAI_THROWE(MALFORMED_PROPERTY,"Malformed Property: " + s.substr(token_start, token_end - token_start));
89 // we found a vlue
90 std::string value = s.substr(token_start, token_end - token_start);
91
92 // store the key,value pair
93 ps.set(key,value);
94
95 // go on with the next one
96 token_start = token_end + 1;
97 }
98
99 return is;
100 }
101
102
103 } // end of namespace dai