CheckArgument.cs 3.9 KB

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