]> git.tdb.fi Git - builder.git/blob - source/condition.cpp
Eliminate all global typedefs, making misc.h finally unnecessary
[builder.git] / source / condition.cpp
1 #include <msp/strings/utils.h>
2 #include "builder.h"
3 #include "condition.h"
4 #include "sourcepackage.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 Condition::Condition(SourcePackage &p, const string &expr):
10         pkg(p)
11 {
12         vector<string> parts = split(expr);
13
14         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
15         {
16                 if(*i=="and")
17                         continue;
18
19                 string::size_type token = i->find_first_of("=!");
20                 if(token==string::npos)
21                         expression.insert(Expression::value_type(*i, "="));
22                 else if(token==0 && (*i)[0]=='!')
23                         expression.insert(Expression::value_type(i->substr(1), "!"));
24                 else
25                         expression.insert(Expression::value_type(i->substr(0, token), i->substr(token)));
26         }
27 }
28
29 bool Condition::eval()
30 {
31         const Config &conf = pkg.get_config();
32
33         bool result = true;
34         for(Expression::iterator i=expression.begin(); i!=expression.end(); ++i)
35         {
36                 bool neg = (i->second[0]=='!');
37                 unsigned start = 1;
38                 if(i->second[1]=='=')
39                         ++start;
40                 string value = i->second.substr(start);
41
42                 bool match = false;
43                 if(conf.is_option(i->first))
44                 {
45                         if(value.empty())
46                                 match = lexical_cast<bool>(conf.get_option(i->first).value);
47                         else
48                                 match = (conf.get_option(i->first).value==value);
49                 }
50                 else if(i->first=="arch")
51                         match = pkg.get_builder().get_current_arch().match_name(value);
52
53                 if(match==neg)
54                         result = false;
55         }
56
57         return result;
58 }
59
60
61 Condition::Loader::Loader(Condition &c):
62         DataFile::ObjectLoader<Condition>(c)
63 {
64         add("require",    &Loader::require);
65         add("build_info", &Loader::build_info);
66 }
67
68 void Condition::Loader::require(const string &pkg)
69 {
70         obj.requires.push_back(pkg);
71 }
72
73 void Condition::Loader::build_info()
74 {
75         load_sub(obj.build_info);
76 }