MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections;5using System.Collections.Generic;6using System.Collections.ObjectModel;7using System.IO;8 9using Microsoft.PowerShell.Commands;10 11namespace System.Management.Automation12{13 /// <summary>14 /// This class holds the integer constants used in Session State.15 /// </summary>16 internal static class SessionStateConstants17 {18 /// <summary>19 /// The default maximum for the number of variables.20 /// </summary>21 internal const int DefaultVariableCapacity = 4096;22 23 /// <summary>24 /// Max # of variables allowed in a scope in Session State.25 /// </summary>26 internal const int MaxVariablesCapacity = 32768;27 28 /// <summary>29 /// Min # of variables allows in a scope in Session State.30 /// </summary>31 internal const int MinVariablesCapacity = 1024;32 33 /// <summary>34 /// The default maximum for the number of aliases.35 /// </summary>36 internal const int DefaultAliasCapacity = 4096;37 38 /// <summary>39 /// Max # of aliases allowed in a scope in Session State.40 /// </summary>41 internal const int MaxAliasCapacity = 32768;42 43 /// <summary>44 /// Min # of aliases allowed in a scope in Session State.45 /// </summary>46 internal const int MinAliasCapacity = 1024;47 48 /// <summary>49 /// The default maximum for the number of functions.50 /// </summary>51 internal const int DefaultFunctionCapacity = 4096;52 53 /// <summary>54 /// Max # of functions allowed in a scope in Session State.55 /// </summary>56 internal const int MaxFunctionCapacity = 32768;57 58 /// <summary>59 /// Min # of functions allowed in a scope in Session State.60 /// </summary>61 internal const int MinFunctionCapacity = 1024;62 63 /// <summary>64 /// The default maximum for the number of drives.65 /// </summary>66 internal const int DefaultDriveCapacity = 4096;67 68 /// <summary>69 /// Max # of drives allowed in a scope in Session State.70 /// </summary>71 internal const int MaxDriveCapacity = 32768;72 73 /// <summary>74 /// Min # of drives allowed in a scope in Session State.75 /// </summary>76 internal const int MinDriveCapacity = 1024;77 78 /// <summary>79 /// The default maximum for the number of errors.80 /// </summary>81 internal const int DefaultErrorCapacity = 256;82 83 /// <summary>84 /// Max # of errors allowed in a scope in Session State.85 /// </summary>86 internal const int MaxErrorCapacity = 32768;87 88 /// <summary>89 /// Min # of errors allowed in a scope in Session State.90 /// </summary>91 internal const int MinErrorCapacity = 256;92 93 /// <summary>94 /// The default capacity for a Dictionary store.95 /// </summary>96 internal const int DefaultDictionaryCapacity = 100;97 98 /// <summary>99 /// Default load factor on a hash table.100 /// </summary>101 internal const float DefaultHashTableLoadFactor = 0.25F;102 }103 104 /// <summary>105 /// This class has static methods that are used in Session State.106 /// </summary>107 internal static class SessionStateUtilities108 {109 /// <summary>110 /// Converts the specified array into a collection of the specified type.111 /// </summary>112 /// <param name="array">113 /// The array to be converted.114 /// </param>115 /// <returns>116 /// A collection of the elements that were in the array.117 /// </returns>118 internal static Collection<T> ConvertArrayToCollection<T>(T[] array)119 {120 Collection<T> result = new Collection<T>();121 if (array != null)122 {123 foreach (T element in array)124 {125 result.Add(element);126 }127 }128 129 return result;130 }131 132 /// <summary>133 /// Compares the elements in the specified collection with value specified. If134 /// the string comparer is specified it is used for the comparison, else the135 /// .Equals method is used.136 /// </summary>137 /// <param name="collection">138 /// The collection to check for the value.139 /// </param>140 /// <param name="value">141 /// The value to check for.142 /// </param>143 /// <param name="comparer">144 /// If specified the comparer will be used instead of .Equals.145 /// </param>146 /// <returns>147 /// true if the value is contained in the collection or false otherwise.148 /// </returns>149 /// <exception cref="ArgumentNullException">150 /// If <paramref name="collection"/> is null.151 /// </exception>152 internal static bool CollectionContainsValue(IEnumerable collection, object value, IComparer comparer)153 {154 ArgumentNullException.ThrowIfNull(collection);155 156 bool result = false;157 158 foreach (object item in collection)159 {160 if (comparer != null)161 {162 if (comparer.Compare(item, value) == 0)163 {164 result = true;165 break;166 }167 }168 else169 {170 if (item.Equals(value))171 {172 result = true;173 break;174 }175 }176 }177 178 return result;179 }180 181 /// <summary>182 /// Constructs a collection of WildcardPatterns for the specified183 /// string collection.184 /// </summary>185 /// <param name="globPatterns">186 /// The string patterns to construct the WildcardPatterns for.187 /// </param>188 /// <param name="options">189 /// The options to create the WildcardPatterns with.190 /// </param>191 /// <returns>192 /// A collection of WildcardPatterns that represent the string patterns193 /// that were passed.194 /// </returns>195 internal static Collection<WildcardPattern> CreateWildcardsFromStrings(196 IEnumerable<string> globPatterns,197 WildcardOptions options)198 {199 Collection<WildcardPattern> result = new Collection<WildcardPattern>();200 201 if (globPatterns != null)202 {203 // Loop through the patterns and construct a wildcard pattern for each one204 205 foreach (string pattern in globPatterns)206 {207 if (!string.IsNullOrEmpty(pattern))208 {209 result.Add(210 WildcardPattern.Get(211 pattern,212 options));213 }214 }215 }216 217 return result;218 }219 220 /// <summary>221 /// Determines if the specified text matches any of the patterns.222 /// </summary>223 /// <param name="text">224 /// The text to check against the wildcard pattern.225 /// </param>226 /// <param name="patterns">227 /// An array of wildcard patterns. If the array is empty or null the text is deemed228 /// to be a match.229 /// </param>230 /// <param name="defaultValue">231 /// The default value that should be returned if <paramref name="patterns"/>232 /// is empty or null.233 /// </param>234 /// <returns>235 /// True if the text matches any of the patterns OR if patterns is null or empty and defaultValue is True.236 /// </returns>237 internal static bool MatchesAnyWildcardPattern(238 string text,239 IEnumerable<WildcardPattern> patterns,240 bool defaultValue)241 {242 bool result = false;243 bool patternsNonEmpty = false;244 245 if (patterns != null)246 {247 // Loop through each of the patterns until a match is found248 foreach (WildcardPattern pattern in patterns)249 {250 patternsNonEmpty = true;251 if (pattern.IsMatch(text))252 {253 result = true;254 break;255 }256 }257 }258 259 if (!patternsNonEmpty)260 {261 // Since no pattern was specified return the default value262 result = defaultValue;263 }264 265 return result;266 }267 268 /// <summary>269 /// Converts an OpenMode enum value to a FileMode.270 /// </summary>271 /// <param name="openMode">272 /// The OpenMode value to be converted.273 /// </param>274 /// <returns>275 /// The FileMode representation of the OpenMode.276 /// </returns>277 internal static FileMode GetFileModeFromOpenMode(OpenMode openMode)278 {279 FileMode result = FileMode.Create;280 281 switch (openMode)282 {283 case OpenMode.Add:284 result = FileMode.Append;285 break;286 287 case OpenMode.New:288 result = FileMode.CreateNew;289 break;290 291 case OpenMode.Overwrite:292 result = FileMode.Create;293 break;294 }295 296 return result;297 }298 }299}300 301namespace Microsoft.PowerShell.Commands302{303 /// <summary>304 /// The enum used by commands to allow the user to specify how305 /// a file (or other item) should be opened.306 /// </summary>307 public enum OpenMode308 {309 /// <summary>310 /// This opens the file for appending (similar to FileMode.Append)311 /// </summary>312 Add,313 314 /// <summary>315 /// The file must be created new. If the file exists it is an error (similar to FileMode.CreateNew)316 /// </summary>317 New,318 319 /// <summary>320 /// Creates a new file, if the file already exists it is overwritten (similar to FileMode.Create)321 /// </summary>322 Overwrite323 }324}325 