Make a copy of the old ZipLib
[mono.git] / mcs / class / Microsoft.Build.Utilities / Microsoft.Build.Utilities / CommandLineBuilder.cs
1 //
2 // CommandLineBuilder.cs: Builds command line options string
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 #if NET_2_0
29
30 using System;
31 using System.Text;
32 using Microsoft.Build.Framework;
33
34 namespace Microsoft.Build.Utilities
35 {
36         public class CommandLineBuilder
37         {
38                 StringBuilder commandLine;
39         
40                 public CommandLineBuilder ()
41                 {
42                         commandLine = new StringBuilder ();
43                 }
44                 
45                 public void AppendFileNameIfNotNull (string fileName)
46                 {
47                         if (fileName == null)
48                                 return;
49                         AppendSpaceIfNotEmpty ();
50                         commandLine.Append (fileName);
51                 }
52                 
53                 public void AppendFileNameIfNotNull (ITaskItem fileItem)
54                 {
55                         if (fileItem == null)
56                                 return;
57                         AppendSpaceIfNotEmpty ();
58                         commandLine.Append (fileItem.ToString());
59                 }
60                 
61                 public void AppendFileNamesIfNotNull (string[] fileNames,
62                                                       string delimiter)
63                 {
64                         if (fileNames == null)
65                                 return;
66                         bool appendDelimiter = false;
67                         AppendSpaceIfNotEmpty ();
68                         for (int i = 0; i < fileNames.Length; i++) {
69                                 if (fileNames [i] == null)
70                                         continue;
71                                 if (appendDelimiter) {
72                                         commandLine.Append (delimiter);
73                                         commandLine.Append (fileNames [i]);
74                                 } else {
75                                         commandLine.Append (fileNames [i]);
76                                         appendDelimiter = true;
77                                 }
78                         }
79                 }
80                 
81                 public void AppendFileNamesIfNotNull (ITaskItem[] fileItems,
82                                                       string delimiter)
83                 {
84                         if (fileItems == null)
85                                 return;
86                         bool appendDelimiter = false;
87                         AppendSpaceIfNotEmpty ();
88                         for (int i = 0; i < fileItems.Length; i++) {
89                                 if (fileItems [i] == null)
90                                         continue;
91                                 if (appendDelimiter) {
92                                         commandLine.Append (delimiter);
93                                         commandLine.Append (fileItems [i].ToString ());
94                                 } else {
95                                         commandLine.Append (fileItems [i].ToString ());
96                                         appendDelimiter = true;
97                                 }
98                         }
99                 }
100                 
101                 protected void AppendFileNamesWithQuoting (string fileName)
102                 {
103                         if (IsQuotingRequired (fileName))
104                                 commandLine.AppendFormat ("\"{0}\"",fileName);
105                         else
106                                 commandLine.Append (fileName);
107                 }
108                 
109                 protected void AppendSpaceIfNotEmpty ()
110                 {
111                         if (commandLine.Length != 0)
112                                 commandLine.Append (' ');
113                 }
114                 
115                 public void AppendSwitch (string switchName)
116                 {
117                         if (switchName == null)
118                                 return;
119                         AppendSpaceIfNotEmpty ();
120                         commandLine.Append (switchName);
121                 }
122                 
123                 public void AppendSwitchIfNotNull (string switchName,
124                                                    string parameter)
125                 {
126                         if (switchName == null || parameter == null)
127                                 return;
128                         AppendSpaceIfNotEmpty ();
129                         commandLine.AppendFormat ("{0}{1}",switchName,
130                                 parameter);
131                 }
132                 
133                 public void AppendSwitchIfNotNull (string switchName,
134                                                    ITaskItem parameter)
135                 {
136                         if (switchName == null || parameter == null)
137                                 return;
138                         AppendSpaceIfNotEmpty ();
139                         commandLine.AppendFormat ("{0}{1}",switchName,
140                                 parameter.ToString ());
141                 }
142                 
143                 public void AppendSwitchIfNotNull (string switchName,
144                                                    string[] parameters,
145                                                    string delimiter)
146                 {
147                         if (switchName == null || parameters == null)
148                                 return;
149                         AppendSpaceIfNotEmpty ();
150                         commandLine.AppendFormat ("{0}",switchName);
151                         bool appendDelimiter = false;
152                         for (int i = 0; i < parameters.Length; i++) {
153                                 if (parameters [i] == null)
154                                         continue;
155                                 if (appendDelimiter) {
156                                         commandLine.Append (delimiter);
157                                         commandLine.Append (parameters [i]);
158                                 } else {
159                                         commandLine.Append (parameters [i]);
160                                         appendDelimiter = true;
161                                 }
162                         }
163                 }
164                 
165                 public void AppendSwitchIfNotNull (string switchName,
166                                                    ITaskItem[] parameters,
167                                                    string delimiter)
168                 {
169                         if (switchName == null || parameters == null)
170                                 return;
171                         AppendSpaceIfNotEmpty ();
172                         commandLine.AppendFormat ("{0}",switchName);
173                         bool appendDelimiter = false;
174                         for (int i = 0; i < parameters.Length; i++) {
175                                 if (parameters [i] == null)
176                                         continue;
177                                 if (appendDelimiter) {
178                                         commandLine.Append (delimiter);
179                                         commandLine.Append (parameters [i].ToString ());
180                                 } else {
181                                         commandLine.Append (parameters [i].ToString ());
182                                         appendDelimiter = true;
183                                 }
184                         }
185                 }
186                 
187                 public void AppendSwitchUnquotedIfNotNull (string switchName,
188                                                            string parameter)
189                 {
190                         if (switchName == null || parameter == null)
191                                 return;
192                         AppendSpaceIfNotEmpty ();
193                         commandLine.AppendFormat ("{0}{1}", switchName, parameter);
194                 }
195
196                 public void AppendSwitchUnquotedIfNotNull (string switchName,
197                                                            ITaskItem parameter)
198                 {
199                         if (switchName == null || parameter == null)
200                                 return;
201                         AppendSpaceIfNotEmpty ();
202                         commandLine.AppendFormat ("{0}{1}", switchName, parameter.GetMetadata ("Include"));
203                 }
204
205                 public void AppendSwitchUnquotedIfNotNull (string switchName,
206                                                            string[] parameters,
207                                                            string delimiter)
208                 {
209                         if (switchName == null || delimiter == null || parameters == null)
210                                 return;
211                         AppendSpaceIfNotEmpty ();
212                         commandLine.AppendFormat ("{0}",switchName);
213                         bool appendDelimiter = false;
214                         for (int i = 0; i < parameters.Length; i++) {
215                                 if (parameters [i] == null)
216                                         continue; 
217                                 if (appendDelimiter) {
218                                         commandLine.Append (delimiter);
219                                         commandLine.Append (parameters [i]);
220                                 } else {
221                                         commandLine.Append (parameters [i]);
222                                         appendDelimiter = true;
223                                 }
224                         }
225                 }
226
227                 public void AppendSwitchUnquotedIfNotNull (string switchName,
228                                                            ITaskItem[] parameters,
229                                                            string delimiter)
230                 {
231                         if (switchName == null || delimiter == null || parameters == null)
232                                 return;
233                         AppendSpaceIfNotEmpty ();
234                         commandLine.AppendFormat ("{0}",switchName);
235                         bool appendDelimiter = false;
236                         for (int i = 0; i < parameters.Length; i++) {
237                                 if (parameters [i] == null)
238                                         continue;
239                                 if (appendDelimiter) {
240                                         commandLine.Append (delimiter);
241                                         commandLine.Append (parameters [i].ToString ());
242                                 } else {
243                                         commandLine.Append (parameters [i].ToString ());
244                                         appendDelimiter = true;
245                                 }
246                         }
247                 }
248                 
249                 protected void AppendTextUnquoted (string textToAppend)
250                 {
251                         commandLine.Append (textToAppend);
252                 }
253                 
254                 protected void AppendTextWithQuoting (string textToAppend)
255                 {
256                         if (IsQuotingRequired (textToAppend))
257                                 commandLine.AppendFormat ("\"{0}\"",textToAppend);
258                         else
259                                 commandLine.Append (textToAppend);
260                 }
261                 
262                 protected virtual bool IsQuotingRequired (string parameter)
263                 {
264                         parameter.Trim ();
265                         // FIXME: change this to regex
266                         foreach (char c in parameter) {
267                                 if (c == ' ' || c == '\t' || c == '\u000b' || c == '\u000c')
268                                         return true;
269                         }
270                         return false;
271                 }
272                 
273                 public override string ToString ()
274                 {
275                         return commandLine.ToString ();
276                 }
277                 
278                 protected StringBuilder CommandLine {
279                         get {
280                                 return commandLine;
281                         }
282                 }
283         }
284 }
285
286 #endif