Merge remote-tracking branch 'joncham/sgen-msvc2'
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / ConditionFunctionExpression.cs
1 //
2 // ConditionFunctionExpression.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2006 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 using System.Xml;
33
34 namespace Microsoft.Build.BuildEngine {
35         internal sealed class ConditionFunctionExpression : ConditionExpression {
36         
37                 List <ConditionFactorExpression>        args;
38                 string                                  name;
39                 
40                 static Dictionary <string, MethodInfo>  functions;
41                 
42                 static ConditionFunctionExpression ()
43                 {
44                         Type t = typeof (ConditionFunctionExpression);
45                         string [] names = new string [] { "Exists", "HasTrailingSlash" };
46                 
47                         functions = new Dictionary <string, MethodInfo> (StringComparer.OrdinalIgnoreCase);
48                         foreach (string name in names)
49                                 functions.Add (name, t.GetMethod (name, BindingFlags.NonPublic | BindingFlags.Static));
50                 }
51         
52                 public ConditionFunctionExpression (string name, List <ConditionFactorExpression> args)
53                 {
54                         this.args = args;
55                         this.name = name;
56                 }
57                 
58                 public override  bool BoolEvaluate (Project context)
59                 {
60                         if (!functions.ContainsKey (name))
61                                 throw new InvalidOperationException ("Unknown function named: " + name);
62                         
63                         if (functions [name] == null)
64                                 throw new InvalidOperationException ("Unknown function named: " + name);
65                                 
66                         MethodInfo mi = functions [name];
67                         object [] argsArr = new object [args.Count + 1];
68                         int i = 0;
69                         foreach (ConditionFactorExpression cfe in args)
70                                 argsArr [i++] = cfe.StringEvaluate (context);
71                         argsArr [i] = context;
72                                 
73                         return (bool) mi.Invoke (null, argsArr);
74                 }
75                 
76                 public override float NumberEvaluate (Project context)
77                 {
78                         throw new NotSupportedException ();
79                 }
80                 
81                 public override string StringEvaluate (Project context)
82                 {
83                         throw new NotSupportedException ();
84                 }
85                 
86                 public override bool CanEvaluateToBool (Project context)
87                 {
88                         return functions.ContainsKey (name);
89                 }
90                 
91                 public override bool CanEvaluateToNumber (Project context)
92                 {
93                         return false;
94                 }
95                 
96                 public override bool CanEvaluateToString (Project context)
97                 {
98                         return false;
99                 }
100
101 #pragma warning disable 0169
102 #region Functions
103                 // FIXME imported projects
104                 static bool Exists (string file, Project context)
105                 {
106                         string directory  = null;
107                         
108                         if (context.FullFileName != String.Empty)
109                                 directory = Path.GetDirectoryName (context.FullFileName);
110                                 
111                         if (!Path.IsPathRooted (file) && directory != null && directory != String.Empty)
112                                 file = Path.Combine (directory, file);
113                 
114                         return File.Exists (file) || Directory.Exists (file);
115                 }
116
117                 static bool HasTrailingSlash (string file, Project context)
118                 {
119                         if (file == null)
120                                 return false;
121
122                         file = file.Trim ();
123
124                         int len = file.Length;
125                         if (len == 0)
126                                 return false;
127
128                         return file [len - 1] == '\\' || file [len - 1] == '/';
129                 }
130
131 #endregion
132 #pragma warning restore 0169
133
134         }
135 }