Merge remote-tracking branch 'joncham/sgen-msvc2'
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / ChangeType.cs
1 //
2 // ChangeType.cs: Changes types for output properties or items.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2005 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;
30 using System.Collections.Generic;
31 using System.Reflection;
32 using System.Text;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
35
36 namespace Microsoft.Build.BuildEngine {
37         internal class ChangeType {
38                 //removed Type type
39                 // FIXME throw exception here
40                 static string ToString (object o)
41                 {
42                         string output = null;
43                         Type type = o.GetType ();
44
45                         // FIXME: there are more types
46                         if (type == typeof (string))
47                                 output = (string) o;
48                         else if (type == typeof (bool) ||
49                                  type == typeof (int) ||
50                                  type == typeof (uint) ||
51                                  type == typeof (float) ||
52                                  type == typeof (double) ||
53                                  type == typeof (DateTime) ||
54                                  type.IsEnum)
55                                 output = o.ToString ();
56                         else
57                                 throw new Exception (String.Format ("Unsupported type : {0}", type));
58                         return output;
59                 }
60
61                 static string ToString (object [] o, Type type)
62                 {
63                 /*
64                         ArrayList al = new ArrayList ();
65                         foreach (object obj in o ) {
66                                 if (type == typeof (bool[])) {
67                                         al.Add (TemporaryTransform (obj, typeof (bool)));
68                                 } else if (type == typeof (string[])) {
69                                         al.Add (TemporaryTransform (obj, typeof (string)));
70                                 } else if (type == typeof (DateTime[])) {
71                                         al.Add (TemporaryTransform (obj, typeof (DateTime)));
72                                 } else if (type == typeof (int[])) {
73                                         al.Add (TemporaryTransform (obj, typeof (int)));
74                                 } else if (type == typeof (uint[])) {
75                                         al.Add (TemporaryTransform (obj, typeof (uint)));
76                                 } else
77                                         throw new Exception (String.Format ("Invalid type: {0}", type.ToString ()));
78                         }
79                         string[] output = new string [al.Count];
80                         int i  = 0;
81                         foreach (string s in al)
82                                 output [i++] = s;
83                         return String.Join (";", output);
84                         */
85
86                         List <string> list = new List <string> ();
87                         foreach (object obj in o)
88                                 list.Add (ToString (obj));
89                         return String.Join (";", list.ToArray ());
90                 }
91
92                 public static BuildProperty ToBuildProperty (object o, Type t, string name)
93                 {
94                         if (t == typeof (ITaskItem)) {
95                                 return new BuildProperty (name, ((ITaskItem) o).ItemSpec);
96                         } else if (t ==  typeof (ITaskItem [])) {
97                         // FIXME move Tostring here
98                                 return new BuildProperty (name, ToString ((ITaskItem []) o));
99                         } else if (t.IsArray) {
100                                 return new BuildProperty (name, ToString ((object []) o, t));
101                         } else {
102                                 return new BuildProperty (name, ToString (o));
103                         }
104                 }
105
106                 public static BuildItemGroup ToBuildItemGroup (object o, Type t, string name)
107                 {
108                         BuildItemGroup big = new BuildItemGroup ();
109
110                         if (t == typeof (ITaskItem)) {
111                                 big.AddItem (name, (ITaskItem) o);
112                         } else if (t ==  typeof (ITaskItem [])) {
113                                 foreach (ITaskItem i in (ITaskItem []) o)
114                                         big.AddItem (name, i);
115                         } else if (t.IsArray) {
116                                 return ToBuildItemGroup (name, ToString ((object []) o, t), true);
117                         } else {
118                                 return ToBuildItemGroup (name, ToString (o), false);
119                         }
120
121                         return big;
122                 }
123                 
124                 static BuildItemGroup ToBuildItemGroup (string name, string items, bool split)
125                 {
126                         BuildItemGroup big = new BuildItemGroup ();
127                         if (split) {
128                                 string [] splitItems = items.Split (';');
129                                 foreach (string item in splitItems)
130                                         big.AddItem (name, new TaskItem (item));
131                         } else {
132                                 big.AddItem (name, new TaskItem (items));
133                         }
134
135                         return big;
136                 }
137                 
138                 static string ToString (ITaskItem [] items)
139                 {
140                         string [] text = new string [items.Length];
141                         int i = 0;
142                         foreach (ITaskItem item in items)
143                                 text [i++] = item.ItemSpec;
144                         return String.Join (";", text);
145                 }
146         }
147 }