Пример #1
0
        /// <summary>
        /// Provide a hook to preprocess a record
        /// </summary>
        /// <param name="e">Record details before read</param>
        /// <returns>True if record to be skipped</returns>
        protected bool OnBeforeReadRecord(Events.BeforeReadEventArgs <T> e)
        {
            if (RecordInfo.NotifyRead)
            {
                ((INotifyRead)e.Record).BeforeRead(e);
            }

            BeforeReadRecord?.Invoke(this, e);

            return(e.SkipThisRecord);
        }
Пример #2
0
        private IList ReadStreamAsList(TextReader reader, int maxRecords, DataTable dt)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader), "The reader of the Stream can´t be null");
            }

            HeaderText = string.Empty;
            FooterText = string.Empty;

            IList result;

            using (var recordReader = new TextReaderWrapper(reader))
            {
                ResetFields();


                if (_objectEngine)
                {
                    result = new ArrayList();
                }
                else
                {
                    result = new List <T>();
                }

                var currentRecord = 0;

                var streamInfo = new StreamInfoProvider(reader);
                using (var freader = new ForwardReader(recordReader, RecordInfo.IgnoreLast))
                {
                    freader.DiscardForward = true;

                    string currentLine, completeLine;

                    LineNumber = 1;

                    completeLine = freader.ReadNextLine();
                    currentLine  = completeLine;

                    if (MustNotifyProgress) // Avoid object creation
                    {
                        OnProgress(new ProgressEventArgs(0, -1, streamInfo.Position, streamInfo.TotalBytes));
                    }

                    if (RecordInfo.IgnoreFirst > 0)
                    {
                        for (var i = 0; i < RecordInfo.IgnoreFirst && currentLine != null; i++)
                        {
                            HeaderText += currentLine + Environment.NewLine;
                            currentLine = freader.ReadNextLine();
                            LineNumber++;
                        }
                    }

                    var byPass = false;

                    if (maxRecords < 0)
                    {
                        maxRecords = int.MaxValue;
                    }

                    var line = new LineInfo(currentLine)
                    {
                        mReader = freader
                    };

                    var values = new object[RecordInfo.FieldCount];

                    while (currentLine != null &&
                           currentRecord < maxRecords)
                    {
                        completeLine = currentLine;

                        try
                        {
                            TotalRecords++;
                            currentRecord++;

                            line.ReLoad(currentLine);

                            var skip = false;

                            var record = (T)RecordInfo.Operations.CreateRecordHandler();

                            if (MustNotifyProgress) // Avoid object creation
                            {
                                OnProgress(new ProgressEventArgs(currentRecord,
                                                                 -1,
                                                                 streamInfo.Position,
                                                                 streamInfo.TotalBytes));
                            }

                            Events.BeforeReadEventArgs <T> e = null;
                            if (MustNotifyRead)
                            {
                                e    = new BeforeReadEventArgs <T>(this, record, currentLine, LineNumber);
                                skip = OnBeforeReadRecord(e);
                                if (e.RecordLineChanged)
                                {
                                    line.ReLoad(e.RecordLine);
                                }
                            }


                            if (skip == false)
                            {
                                if (RecordInfo.Operations.StringToRecord(record, line, values))
                                {
                                    if (MustNotifyRead) // Avoid object creation
                                    {
                                        skip = OnAfterReadRecord(currentLine, record, e.RecordLineChanged, LineNumber);
                                    }

                                    if (skip == false)
                                    {
                                        if (dt == null)
                                        {
                                            result.Add(record);
                                        }
                                        else
                                        {
                                            dt.Rows.Add(RecordInfo.Operations.RecordToValues(record));
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            switch (ErrorManager.ErrorMode)
                            {
                            case ErrorMode.ThrowException:
                                byPass = true;
                                throw;

                            case ErrorMode.IgnoreAndContinue:
                                break;

                            case ErrorMode.SaveAndContinue:
                                var err = new ErrorInfo
                                {
                                    LineNumber    = freader.LineNumber,
                                    ExceptionInfo = ex,
                                    Text          = completeLine
                                };

                                ErrorManager.AddError(err);
                                break;
                            }
                        }
                        finally
                        {
                            if (byPass == false)
                            {
                                currentLine = freader.ReadNextLine();
                                LineNumber++;
                            }
                        }
                    }

                    if (RecordInfo.IgnoreLast > 0)
                    {
                        FooterText = freader.RemainingText;
                    }
                }
            }
            return(result);
        }