Beispiel #1
0
        private void LoadFile(CsvLoader loader)
        {
            if (RecordType != null)
            {
                ColumnNameMapHolder.ColumnNameMap = ColumnNameMap;

                var classMap = typeof(DynamicClassMap <>).MakeGenericType(new Type[] { RecordType });
                _config.CsvHelperConfiguration.RegisterClassMap(classMap);

                while (loader.Read())
                {
                    var r = loader.GetRecord(RecordType);
                    Output(r);
                }
            }
            else
            {
                if (AsDictionary)
                {
                    Output(loader.LoadToDictionary());
                }
                else
                {
                    EnumerateAsPSObject(loader);
                }
            }
        }
Beispiel #2
0
        private void EnumerateAsPSObject(CsvLoader loader)
        {
            while (loader.Read())
            {
                var columnNames = loader.ColumnNames;
                var record      = loader.Record;
                var pso         = new PSObject();

                int i;
                for (i = 0; i < Math.Min(columnNames.Length, record.Length); ++i)
                {
                    var r = loader.Convert(record[i], i);
                    pso.Properties.Add(new PSNoteProperty(columnNames[i], r));
                }

                for (; i < columnNames.Length; ++i)
                {
                    pso.Properties.Add(new PSNoteProperty(columnNames[i], string.Empty));
                }

                for (; i < record.Length; ++i)
                {
                    var r = loader.Convert(record[i], i);
                    pso.Properties.Add(new PSNoteProperty("Column" + (i + 1), r));
                }

                Output(pso);
            }
        }
Beispiel #3
0
        protected override void BeginProcessing()
        {
            var csvHelperConfig = Configuration ?? new Configuration();

            csvHelperConfig.IncludePrivateMembers = true;
            csvHelperConfig.MemberTypes           = MemberTypes.Fields | MemberTypes.Properties;


            csvHelperConfig.AllowComments    = AllowComments;
            csvHelperConfig.BufferSize       = BufferSize;
            csvHelperConfig.Comment          = CommentChar;
            csvHelperConfig.HasHeaderRecord  = !NoHeaderRecord;
            csvHelperConfig.IgnoreBlankLines = !KeepBlankLines;
            csvHelperConfig.IgnoreQuotes     = IgnoreQuote;
            csvHelperConfig.TrimOptions      = TrimOption;

            var p = MyInvocation.BoundParameters;

            if (p.ContainsKey("Delimiter"))
            {
                csvHelperConfig.Delimiter = Delimiter;
            }

            if (p.ContainsKey("EscapeChar"))
            {
                csvHelperConfig.Escape = EscapeChar;
            }

            if (p.ContainsKey("QuoteChar"))
            {
                csvHelperConfig.Quote = QuoteChar;
            }

            _config = new Config()
            {
                CsvHelperConfiguration = csvHelperConfig,
                InitialCapacity        = InitialCapacity,
                ColumnNames            = ColumnNames,
                ColumnNameMap          = ColumnNameMap,
                ColumnTypes            = ColumnTypes,
                Strict  = Strict,
                Culture = Culture
            };

            if (AsDataTable)
            {
                // The current version does not support combination of -AsDataTable and input stream.

                if (string.IsNullOrEmpty(Path))
                {
                    WriteError(new ErrorRecord(new ArgumentException("-Path is required when -AsDataTable is set"), "", ErrorCategory.InvalidArgument, null));
                    return;
                }

                using (var reader = new StreamReader(Path, Encoding))
                    using (var csvReader = new CsvReader(reader, _config.CsvHelperConfiguration))
                        using (var csvDataReader = new CsvDataReader(csvReader))
                        {
                            var dt = new DataTable();

                            if (ColumnTypes != null)
                            {
                                foreach (DictionaryEntry entry in ColumnTypes)
                                {
                                    dt.Columns.Add((string)entry.Key, (Type)entry.Value);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < csvDataReader.FieldCount; i++)
                                {
                                    dt.Columns.Add(csvDataReader.GetName(i), csvDataReader.GetFieldType(i));
                                }
                            }

                            if (ReadCount > 0)
                            {
                                int rowCount = 0;
                                while (csvDataReader.Read())
                                {
                                    if (rowCount % ReadCount == 0)
                                    {
                                        WriteVerbose($"Starting batch of {ReadCount}  ({rowCount} records processed)");
                                    }

                                    string[] row = new String[csvDataReader.FieldCount];

                                    csvDataReader.GetValues(row);
                                    dt.LoadDataRow(row, true);

                                    rowCount++;

                                    if (rowCount % ReadCount == 0)
                                    {
                                        WriteObject(dt.Copy());
                                        dt.Clear();
                                    }
                                }
                                // Write out remaining rows, if any.
                                if (dt.Rows.Count > 0)
                                {
                                    WriteObject(dt);
                                }
                            }
                            else
                            {
                                dt.Load(csvDataReader);
                                WriteObject(dt);
                            }
                        }

                return;
            }

            if (!string.IsNullOrEmpty(Path))
            {
                using (var reader = new StreamReader(Path, Encoding))
                    using (var loader = new CsvLoader(reader, _config))
                    {
                        LoadFile(loader);
                    }

                return;
            }

            _output        = new BlockingCollection <object>();
            _completeEvent = new ManualResetEvent(false);

            _loader = new CsvLoader(null, _config);

            var thread = new Thread(() => {
                try
                {
                    LoadFile(_loader);
                }
                catch (Exception e)
                {
                    _exception = e;
                }
                finally
                {
                    _completeEvent.Set();
                }
            });

            thread.Name = "Import-Csv2 loader thread";
            thread.Start();
        }