예제 #1
0
        /// <summary>
        /// retrieve the encoding paramater from the command line
        /// it throws if the encoding does not match the known ones
        /// </summary>
        /// <returns>a System.Text.Encoding object (null if no encoding specified)</returns>
        internal static Encoding Convert(Cmdlet cmdlet, string encoding)
        {
            if (string.IsNullOrEmpty(encoding))
            {
                // no parameter passed, default to Unicode (OS preferred)
                return(System.Text.Encoding.Unicode);
            }

            // Default to unicode (this matches Get-Content)
            if (string.Equals(encoding, Unknown, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.Unicode);
            }

            if (string.Equals(encoding, String, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.Unicode);
            }

            // these are the encodings the CLR supports
            if (string.Equals(encoding, Unicode, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.Unicode);
            }

            if (string.Equals(encoding, BigEndianUnicode, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.BigEndianUnicode);
            }

            if (string.Equals(encoding, Utf8, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.UTF8);
            }

            if (string.Equals(encoding, Ascii, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.ASCII);
            }

            if (string.Equals(encoding, Utf7, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.UTF7);
            }

            if (string.Equals(encoding, Utf32, StringComparison.OrdinalIgnoreCase))
            {
                return(System.Text.Encoding.UTF32);
            }

            if (string.Equals(encoding, Default, StringComparison.OrdinalIgnoreCase))
            {
                return(ClrFacade.GetDefaultEncoding());
            }

            if (string.Equals(encoding, OEM, StringComparison.OrdinalIgnoreCase))
            {
                return(ClrFacade.GetOEMEncoding());
            }

            // error condition: unknown encoding value
            string validEncodingValues = string.Join(
                ", ",
                new string[] { Unknown, String, Unicode, BigEndianUnicode, Ascii, Utf8, Utf7, Utf32, Default, OEM });
            string msg = StringUtil.Format(PathUtilsStrings.OutFile_WriteToFileEncodingUnknown,
                                           encoding, validEncodingValues);

            ErrorRecord errorRecord = new ErrorRecord(
                PSTraceSource.NewArgumentException("Encoding"),
                "WriteToFileEncodingUnknown",
                ErrorCategory.InvalidArgument,
                null);

            errorRecord.ErrorDetails = new ErrorDetails(msg);
            cmdlet.ThrowTerminatingError(errorRecord);

            return(null);
        }