**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.IO / CheckArgument.cs
blob2e70e65daecd04d747e1e730e35b51394b15b574
1 //------------------------------------------------------------------------------
2 //
3 // System.IO.CheckArgument.cs
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 //
7 // Author: Jim Richardson, develop@wtfo-guru.com
8 // Created: Saturday, August 25, 2001
9 //
10 // NOTE: All contributors can freely add to this class or make modifications
11 // that do not break existing usage of methods
12 //------------------------------------------------------------------------------
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 //
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 //
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System;
39 using System.IO;
41 namespace System.IO
43 /// <summary>
44 /// A utility class to assist with various argument validations in System.IO
45 /// </summary>
46 internal sealed class CheckArgument
48 /// <summary>
49 /// Generates and exception if arg contains whitepace only
50 /// </summary>
51 public static void WhitespaceOnly (string arg, string desc)
53 if (arg != null && arg.Length > 0)
55 string temp = arg.Trim ();
56 if (temp.Length == 0)
58 throw new ArgumentException (desc);
63 /// <summary>
64 /// Generates and exception if arg contains whitepace only
65 /// </summary>
66 public static void WhitespaceOnly (string arg)
68 WhitespaceOnly (arg, "Argument string consists of whitespace characters only.");
71 /// <summary>
72 /// Generates and exception if arg is empty
73 /// </summary>
74 public static void Empty (string arg, string desc)
76 if (arg != null && arg.Length == 0)
78 throw new ArgumentException (desc);
82 /// <summary>
83 /// Generates and exception if arg is empty
84 /// </summary>
85 public static void Empty (string arg)
87 Empty (arg, "Argument string is empty.");
90 /// <summary>
91 /// Generates and exception if arg is null
92 /// </summary>
93 public static void Null (Object arg, string desc)
95 if (arg == null)
97 throw new ArgumentNullException (desc);
101 /// <summary>
102 /// Generates and exception if arg is null
103 /// </summary>
104 public static void Null (Object arg)
106 if (arg == null)
108 throw new ArgumentNullException ();
112 /// <summary>
113 /// Generates and exception if path contains invalid path characters
114 /// </summary>
115 public static void PathChars (string path, string desc)
117 if (path != null)
119 if (path.IndexOfAny (System.IO.Path.InvalidPathChars) > -1)
121 throw new ArgumentException (desc);
126 /// <summary>
127 /// Generates and exception if path contains invalid path characters
128 /// </summary>
129 public static void PathChars (string path)
131 PathChars (path, "Path contains invalid characters");
134 /// <summary>
135 /// Generates and exception if path too long
136 /// </summary>
137 [MonoTODO]
138 public static void PathLength (string path, string desc)
140 //TODO: find out how long is too long
143 /// <summary>
144 /// Generates and exception if path too long
145 /// </summary>
146 public static void PathLength (string path)
148 PathLength (path, "Path is too long");
151 /// <summary>
152 /// Generates and exception if path is illegal
153 /// </summary>
154 public static void Path (string path, bool bAllowNull, bool bLength)
156 if (path != null) //allow null
158 Empty (path, "Path cannot be the empty string"); // path can't be empty
159 WhitespaceOnly (path, "Path cannot be all whitespace"); // path can't be all whitespace
160 PathChars (path); // path can't contain invalid characters
161 if (bLength)
163 PathLength ("Path too long");
166 else if (!bAllowNull)
168 throw new ArgumentNullException ("Parameter name: path");
172 /// <summary>
173 /// Generates and exception if path is illegal
174 /// </summary>
175 public static void Path (string path, bool bAllowNull)
177 Path (path, bAllowNull, false);
180 /// <summary>
181 /// Generates and exception if path is illegal
182 /// </summary>
183 public static void Path (string path)
185 Path (path, false, false);
189 } // namespace System.IO.Private