* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Microsoft.Build.Utilities / Microsoft.Build.Utilities / CommandLineBuilder.cs
index 75104c0fac455a5ddc3649adfc83ae1210d36b89..3d1fee852ab248efa29a3bc4ac96e569eb228dfb 100644 (file)
@@ -28,7 +28,9 @@
 #if NET_2_0
 
 using System;
+using System.Collections;
 using System.Text;
+using System.Text.RegularExpressions;
 using Microsoft.Build.Framework;
 
 namespace Microsoft.Build.Utilities
@@ -36,7 +38,21 @@ namespace Microsoft.Build.Utilities
        public class CommandLineBuilder
        {
                StringBuilder commandLine;
+               static Hashtable chars;
        
+               static CommandLineBuilder ()
+               {
+                       chars = new Hashtable ();
+
+                       chars.Add (' ', null);
+                       chars.Add ('\t', null);
+                       chars.Add ('\n', null);
+                       chars.Add ('\u000b', null);
+                       chars.Add ('\u000c', null);
+                       chars.Add ('\'', null);
+                       chars.Add ('\"', null);
+               }
+               
                public CommandLineBuilder ()
                {
                        commandLine = new StringBuilder ();
@@ -46,6 +62,7 @@ namespace Microsoft.Build.Utilities
                {
                        if (fileName == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.Append (fileName);
                }
@@ -54,6 +71,7 @@ namespace Microsoft.Build.Utilities
                {
                        if (fileItem == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.Append (fileItem.ToString());
                }
@@ -61,8 +79,12 @@ namespace Microsoft.Build.Utilities
                public void AppendFileNamesIfNotNull (string[] fileNames,
                                                      string delimiter)
                {
+                       if (delimiter == null)
+                               throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
+               
                        if (fileNames == null)
                                return;
+                       
                        bool appendDelimiter = false;
                        AppendSpaceIfNotEmpty ();
                        for (int i = 0; i < fileNames.Length; i++) {
@@ -81,8 +103,12 @@ namespace Microsoft.Build.Utilities
                public void AppendFileNamesIfNotNull (ITaskItem[] fileItems,
                                                      string delimiter)
                {
+                       if (delimiter == null)
+                               throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
+               
                        if (fileItems == null)
                                return;
+                       
                        bool appendDelimiter = false;
                        AppendSpaceIfNotEmpty ();
                        for (int i = 0; i < fileItems.Length; i++) {
@@ -98,7 +124,7 @@ namespace Microsoft.Build.Utilities
                        }
                }
                
-               protected void AppendFileNamesWithQuoting (string fileName)
+               protected void AppendFileNameWithQuoting (string fileName)
                {
                        if (IsQuotingRequired (fileName))
                                commandLine.AppendFormat ("\"{0}\"",fileName);
@@ -115,7 +141,8 @@ namespace Microsoft.Build.Utilities
                public void AppendSwitch (string switchName)
                {
                        if (switchName == null)
-                               return;
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+
                        AppendSpaceIfNotEmpty ();
                        commandLine.Append (switchName);
                }
@@ -123,8 +150,12 @@ namespace Microsoft.Build.Utilities
                public void AppendSwitchIfNotNull (string switchName,
                                                   string parameter)
                {
-                       if (switchName == null || parameter == null)
+                       if (switchName == null)
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+               
+                       if (parameter == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.AppendFormat ("{0}{1}",switchName,
                                parameter);
@@ -133,8 +164,12 @@ namespace Microsoft.Build.Utilities
                public void AppendSwitchIfNotNull (string switchName,
                                                   ITaskItem parameter)
                {
-                       if (switchName == null || parameter == null)
+                       if (switchName == null)
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+               
+                       if (parameter == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.AppendFormat ("{0}{1}",switchName,
                                parameter.ToString ());
@@ -144,8 +179,15 @@ namespace Microsoft.Build.Utilities
                                                   string[] parameters,
                                                   string delimiter)
                {
-                       if (switchName == null || parameters == null)
+                       if (switchName == null)
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+               
+                       if (delimiter == null)
+                               throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
+
+                       if (parameters == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.AppendFormat ("{0}",switchName);
                        bool appendDelimiter = false;
@@ -166,8 +208,15 @@ namespace Microsoft.Build.Utilities
                                                   ITaskItem[] parameters,
                                                   string delimiter)
                {
-                       if (switchName == null || parameters == null)
+                       if (switchName == null)
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+               
+                       if (delimiter == null)
+                               throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
+
+                       if (parameters == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.AppendFormat ("{0}",switchName);
                        bool appendDelimiter = false;
@@ -187,8 +236,12 @@ namespace Microsoft.Build.Utilities
                public void AppendSwitchUnquotedIfNotNull (string switchName,
                                                           string parameter)
                {
-                       if (switchName == null || parameter == null)
+                       if (switchName == null)
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+               
+                       if (parameter == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.AppendFormat ("{0}{1}", switchName, parameter);
                }
@@ -196,18 +249,29 @@ namespace Microsoft.Build.Utilities
                public void AppendSwitchUnquotedIfNotNull (string switchName,
                                                           ITaskItem parameter)
                {
-                       if (switchName == null || parameter == null)
+                       if (switchName == null)
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+               
+                       if (parameter == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
-                       commandLine.AppendFormat ("{0}{1}", switchName, parameter.GetMetadata ("Include"));
+                       commandLine.AppendFormat ("{0}{1}", switchName, parameter.ItemSpec);
                }
 
                public void AppendSwitchUnquotedIfNotNull (string switchName,
                                                           string[] parameters,
                                                           string delimiter)
                {
-                       if (switchName == null || delimiter == null || parameters == null)
+                       if (switchName == null)
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+               
+                       if (delimiter == null)
+                               throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
+
+                       if (parameters == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.AppendFormat ("{0}",switchName);
                        bool appendDelimiter = false;
@@ -228,8 +292,15 @@ namespace Microsoft.Build.Utilities
                                                           ITaskItem[] parameters,
                                                           string delimiter)
                {
-                       if (switchName == null || delimiter == null || parameters == null)
+                       if (switchName == null)
+                               throw new ArgumentNullException (null, "Parameter \"switchName\" cannot be null.");
+               
+                       if (delimiter == null)
+                               throw new ArgumentNullException (null, "Parameter \"delimiter\" cannot be null.");
+
+                       if (parameters == null)
                                return;
+                       
                        AppendSpaceIfNotEmpty ();
                        commandLine.AppendFormat ("{0}",switchName);
                        bool appendDelimiter = false;
@@ -264,12 +335,18 @@ namespace Microsoft.Build.Utilities
                        parameter.Trim ();
                        // FIXME: change this to regex
                        foreach (char c in parameter) {
-                               if (c == ' ' || c == '\t' || c == '\u000b' || c == '\u000c')
+                               if (chars.Contains (c))
                                        return true;
                        }
                        return false;
                }
                
+               [MonoTODO]
+               protected virtual void VerifyThrowNoEmbeddedDoubleQuotes (string switchName,
+                                                                        string parameter)
+               {
+               }
+               
                public override string ToString ()
                {
                        return commandLine.ToString ();
@@ -283,4 +360,4 @@ namespace Microsoft.Build.Utilities
        }
 }
 
-#endif
\ No newline at end of file
+#endif