Fri Jun 14 16:21:54 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / CheckArgument.cs
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 //------------------------------------------------------------------------------
13
14
15 using System;
16 using System.IO;
17
18 namespace System.IO
19 {
20         /// <summary>
21         /// A utility class to assist with various argument validations in System.IO
22         /// </summary>
23         internal sealed class CheckArgument
24         {
25                 /// <summary>
26                 /// Generates and exception if arg contains whitepace only
27                 /// </summary>
28                 public static void WhitespaceOnly (string arg, string desc)
29                 {
30                         if (arg != null && arg.Length > 0)
31                         {
32                                 string temp = arg;
33                                 temp.Trim ();
34                                 if (temp.Length == 0)
35                                 {
36                                         throw new ArgumentException (desc);
37                                 }
38                         }
39                 }
40                 
41                 /// <summary>
42                 /// Generates and exception if arg contains whitepace only
43                 /// </summary>
44                 public static void WhitespaceOnly (string arg)
45                 {
46                         WhitespaceOnly (arg, "Argument string consists of whitespace characters only.");
47                 }
48                 
49                 /// <summary>
50                 /// Generates and exception if arg is empty
51                 /// </summary>
52                 public static void Empty (string arg, string desc)
53                 {
54                         if (arg != null && arg.Length == 0)
55                         {
56                                 throw new ArgumentException (desc);
57                         }
58                 }
59                 
60                 /// <summary>
61                 /// Generates and exception if arg is empty
62                 /// </summary>
63                 public static void Empty (string arg)
64                 {
65                         Empty (arg, "Argument string is empty.");
66                 }
67                 
68                 /// <summary>
69                 /// Generates and exception if arg is null
70                 /// </summary>
71                 public static void Null (Object arg, string desc)
72                 {
73                         if (arg == null)
74                         {
75                                 throw new ArgumentNullException (desc);
76                         }
77                 }
78                 
79                 /// <summary>
80                 /// Generates and exception if arg is null
81                 /// </summary>
82                 public static void Null (Object arg)
83                 {
84                         if (arg == null)
85                         {
86                                 throw new ArgumentNullException ();
87                         }
88                 }
89                 
90                 /// <summary>
91                 /// Generates and exception if path contains invalid path characters
92                 /// </summary>
93                 public static void PathChars (string path, string desc)
94                 {
95                         if (path != null)
96                         {
97                                 if (path.IndexOfAny (System.IO.Path.InvalidPathChars) > -1)
98                                 {
99                                         throw new ArgumentException (desc);
100                                 }
101                         }
102                 }
103                 
104                 /// <summary>
105                 /// Generates and exception if path contains invalid path characters
106                 /// </summary>
107                 public static void PathChars (string path)
108                 {
109                         PathChars (path, "Path contains invalid characters");
110                 }
111                 
112                 /// <summary>
113                 /// Generates and exception if path too long
114                 /// </summary>
115                 [MonoTODO]
116                 public static void PathLength (string path, string desc)
117                 {
118                         //TODO: find out how long is too long
119                 }
120                 
121                 /// <summary>
122                 /// Generates and exception if path too long
123                 /// </summary>
124                 public static void PathLength (string path)
125                 {
126                         PathLength (path);
127                 }
128                 
129                 /// <summary>
130                 /// Generates and exception if path is illegal
131                 /// </summary>
132                 public static void Path (string path, bool bAllowNull, bool bLength)
133                 {
134                         if (path != null) //allow null
135                         {
136                                 Empty (path, "Path cannot be the empty string");        // path can't be empty
137                                 WhitespaceOnly (path, "Path cannot be all whitespace"); // path can't be all whitespace
138                                 PathChars (path);       // path can't contain invalid characters
139                                 if (bLength)
140                                 {
141                                         PathLength ("Path too long");
142                                 }
143                         }
144                         else if (!bAllowNull)
145                         {
146                                 throw new ArgumentNullException ("Parameter name: path");
147                         }
148                 }
149                 
150                 /// <summary>
151                 /// Generates and exception if path is illegal
152                 /// </summary>
153                 public static void Path (string path, bool bAllowNull)
154                 {
155                         Path (path, bAllowNull, false);
156                 }
157                 
158                 /// <summary>
159                 /// Generates and exception if path is illegal
160                 /// </summary>
161                 public static void Path (string path)
162                 {
163                         Path (path, false, false);
164                 }
165
166         }
167 }       // namespace System.IO.Private