]> git.tdb.fi Git - builder.git/blob - source/condition.cpp
Refactor code to get rid of class PackageRef
[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 "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 Condition::Condition(SourcePackage &p, const string &expr):
16         pkg(p)
17 {
18         vector<string> parts=split(expr);
19
20         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
21         {
22                 if(*i=="and")
23                         continue;
24
25                 unsigned token=i->find_first_of("=!");
26                 if(token==string::npos)
27                         expression.insert(StringMap::value_type(*i, "!0"));
28                 else if(token==0 && (*i)[0]=='!')
29                         expression.insert(StringMap::value_type(*i, "=0"));
30                 else
31                         expression.insert(StringMap::value_type(i->substr(0, token), i->substr(token)));
32         }
33 }
34
35 bool Condition::eval()
36 {
37         const Config &conf=pkg.get_config();
38
39         bool result=true;
40         for(StringMap::iterator i=expression.begin(); i!=expression.end(); ++i)
41         {
42                 bool neg=(i->second[0]=='!');
43                 unsigned start=1;
44                 if(i->second[1]=='=')
45                         ++start;
46
47                 if((conf.get_option(i->first).value==i->second.substr(start))==neg)
48                         result=false;
49         }
50
51         return result;
52 }
53
54
55 Condition::Loader::Loader(Condition &c):
56         cond(c)
57 {
58         add("require",    &Loader::require);
59         add("build_info", &Loader::build_info);
60 }
61
62 void Condition::Loader::require(const string &pkg)
63 {
64         cond.requires.push_back(pkg);
65 }
66
67 void Condition::Loader::build_info()
68 {
69         load_sub(cond.build_info);
70 }