CheckArgument.cs 3.9 KB

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