]> git.tdb.fi Git - builder.git/blob - source/condition.cpp
Split class Package into SourcePackage and BinaryPackage
[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 void Condition::resolve_refs()
36 {
37         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
38                 i->resolve();
39 }
40
41 bool Condition::eval()
42 {
43         const Config &conf=pkg.get_config();
44
45         bool result=true;
46         for(StringMap::iterator i=expression.begin(); i!=expression.end(); ++i)
47         {
48                 bool neg=(i->second[0]=='!');
49                 unsigned start=1;
50                 if(i->second[1]=='=')
51                         ++start;
52
53                 if((conf.get_option(i->first).value==i->second.substr(start))==neg)
54                         result=false;
55         }
56
57         return result;
58 }
59
60
61 Condition::Loader::Loader(Condition &c):
62         cond(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         cond.requires.push_back(PackageRef(cond.pkg.get_builder(), pkg));
71 }
72
73 void Condition::Loader::build_info()
74 {
75         load_sub(cond.build_info);
76 }