예제 #1
0
        protected override void BeginProcessing()
        {
            base.BeginProcessing();

            if (root == null && !noFullPath)
            {
                root = System.IO.Path.GetDirectoryName(Path);
            }
            else if (noFullPath)
            {
                root = null;
            }

            parser = new ParseOPT(quote: Quote, delimiter: Delimiter, root: root);

            stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);

            if (psEncoding == FileSystemCmdletProviderEncoding.Default)
            {
                reader = new StreamReader(stream, true);
            }
            else
            {
                reader = new StreamReader(stream, CmdletEncoding.Convert(psEncoding));
            }
        }
예제 #2
0
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            parser = new ParseDelimited(header: Header, quote: Quote, delimiter: Delimiter);
            WriteVerbose(String.Format("Determining encoding for {0}.", path));

            Encoding[] encodings = new System.Text.Encoding[] { CmdletEncoding.Convert(psEncoding), System.Text.Encoding.UTF8, System.Text.Encoding.ASCII, System.Text.Encoding.Default, System.Text.Encoding.UTF32 };

            int tried = 0;

            foreach (Encoding encoding in encodings)
            {
                try {
                    tried++;
                    WriteVerbose(String.Format("Trying {0}", encoding.EncodingName));
                    checkEncoding(encoding);
                    break;
                } catch (InvalidDataException e) {
                    stream.Close();
                    reader.Close();
                    if (psEncoding != FileSystemCmdletProviderEncoding.Unknown)
                    {
                        throw new InvalidDataException(String.Format("Could not parse using {0} - ({1}). Ommiting encoding flag will try all common encodings.", encoding.EncodingName, e.Message));
                    }
                    else if (tried == encodings.Length)
                    {
                        throw new InvalidDataException(String.Format("Could not determine encoding ({0}). Check input data or specify a non standard encoding using the -Encoding flag.", e.Message));
                    }
                }
            }
            WriteVerbose(String.Format("Reading {0} with {1} encoding", path, reader.CurrentEncoding.EncodingName));
        }
예제 #3
0
 protected override void BeginProcessing()
 {
     base.BeginProcessing();
     stringifier = new StringifyDelimited(header: Header, quote: Quote, delimiter: Delimiter);
     stream      = File.Open(Path, Append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Write);
     writer      = new StreamWriter(stream, CmdletEncoding.Convert(this.psEncoding));
 }
예제 #4
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            if (!parallel)
            {
                string line;
                if (psEncoding == FileSystemCmdletProviderEncoding.Default)
                {
                    reader = new StreamReader(stream, true);
                }
                else
                {
                    reader = new StreamReader(stream, CmdletEncoding.Convert(psEncoding));
                }

                while ((line = reader.ReadLine()) != null)
                {
                    Dictionary <string, object> output = parser.ReadLine(line);
                    if (output != null)
                    {
                        WriteObject(TypeConversion.DictToPSObject(output));
                    }
                }
            }
            else
            {
                ConcurrentQueue <PSObject> outputQueue = new ConcurrentQueue <PSObject>();
                Task processingTask = Task.Factory.StartNew(() =>
                                                            System.Threading.Tasks.Parallel.ForEach(ParseIDX.splitIDX(stream, CmdletEncoding.Convert(psEncoding)), (byte[] docBytes) => {
                    string doc = CmdletEncoding.Convert(psEncoding).GetString(docBytes);
                    Dictionary <string, object> parsed = ParseIDX.ParseBlock(doc);
                    outputQueue.Enqueue(TypeConversion.DictToPSObject(parsed));
                })
                                                            );
                while (!processingTask.IsCompleted || !outputQueue.IsEmpty)
                {
                    if (outputQueue.TryDequeue(out PSObject output))
                    {
                        WriteObject(output);
                    }
                    else
                    {
                        System.Threading.Thread.Yield();
                    }
                }
            }
        }
예제 #5
0
 protected override void BeginProcessing()
 {
     base.BeginProcessing();
     stream = File.Open(Path, Append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Write);
     writer = new StreamWriter(stream, CmdletEncoding.Convert(this.psEncoding));
 }