]> git.tdb.fi Git - builder.git/blob - source/condition.cpp
b5833e9ea39295fb3539ecb9aa42f506e8ed3b92
[builder.git] / source / condition.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/strings/utils.h>
9 #include "condition.h"
10 #include "package.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 #include <iostream>
16
17 Condition::Condition(Package &p, const string &expr):
18         pkg(p)
19 {
20         vector<string> parts=split(expr);
21
22         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
23         {
24                 if(*i=="and")
25                         continue;
26                 
27                 unsigned token=i->find_first_of("=!");
28                 if(token==string::npos)
29                         expression.insert(StringMap::value_type(*i, "!0"));
30                 else if(token==0 && (*i)[0]=='!')
31                         expression.insert(StringMap::value_type(*i, "=0"));
32                 else
33                         expression.insert(StringMap::value_type(i->substr(0, token), i->substr(token)));
34         }
35 }
36
37 void Condition::resolve_refs()
38 {
39         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
40                 i->resolve();
41 }
42
43 bool Condition::eval()
44 {
45         const Config &conf=pkg.get_config();
46
47         bool result=true;
48         for(StringMap::iterator i=expression.begin(); i!=expression.end(); ++i)
49         {
50                 bool neg=(i->second[0]=='!');
51                 unsigned start=1;
52                 if(i->second[1]=='=')
53                         ++start;
54
55                 if((conf.get_option(i->first).value==i->second.substr(start))==neg)
56                         result=false;
57         }
58
59         return result;
60 }
61
62
63 Condition::Loader::Loader(Condition &c):
64         cond(c)
65 {
66         add("require",    &Loader::require);
67         add("build_info", &Loader::build_info);
68 }
69
70 void Condition::Loader::require(const string &pkg)
71 {
72         cond.requires.push_back(PackageRef(cond.pkg.get_builder(), pkg));
73 }
74
75 void Condition::Loader::build_info()
76 {
77         load_sub(cond.build_info);
78 }