CheckArgument.cs 3.8 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.Private
  16. {
  17. /// <summary>
  18. /// A utility class to assist with various argument validations in System.IO
  19. /// </summary>
  20. public 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. 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, true, false);
  148. }
  149. }
  150. } // namespace System.IO.Private