public void WriteStream(TextWriter writer, IEnumerable <T> records, int maxRecords) #endif { if (writer == null) { throw new ArgumentNullException("writer", "The writer of the Stream can be null"); } if (records == null) { throw new ArgumentNullException("records", "The records can be null. Try with an empty array."); } ResetFields(); if (mHeaderText != null && mHeaderText.Length != 0) { if (mHeaderText.EndsWith(StringHelper.NewLine)) { writer.Write(mHeaderText); } else { writer.WriteLine(mHeaderText); } } string currentLine = null; //ConstructorInfo constr = mType.GetConstructor(new Type[] {}); int max = maxRecords; if (records is IList) { max = Math.Min(max < 0 ? int.MaxValue : max, ((IList)records).Count); } #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, 0, max); #endif int recIndex = 0; bool first = true; #if !GENERICS foreach (object rec in records) #else foreach (T rec in records) #endif { if (recIndex == maxRecords) { break; } mLineNumber++; try { if (rec == null) { throw new BadUsageException("The record at index " + recIndex.ToString() + " is null."); } if (first) { first = false; if (mRecordInfo.mRecordType.IsInstanceOfType(rec) == false) { throw new BadUsageException("This engine works with record of type " + mRecordInfo.mRecordType.Name + " and you use records of type " + rec.GetType().Name); } } bool skip = false; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, recIndex + 1, max); skip = OnBeforeWriteRecord(rec); #endif if (skip == false) { currentLine = mRecordInfo.RecordToString(rec); #if !MINI currentLine = OnAfterWriteRecord(currentLine, rec); #endif writer.WriteLine(currentLine); } } catch (Exception ex) { switch (mErrorManager.ErrorMode) { case ErrorMode.ThrowException: throw; case ErrorMode.IgnoreAndContinue: break; case ErrorMode.SaveAndContinue: ErrorInfo err = new ErrorInfo(); err.mLineNumber = mLineNumber; err.mExceptionInfo = ex; // err.mColumnNumber = mColumnNum; err.mRecordString = currentLine; mErrorManager.AddError(err); break; } } recIndex++; } mTotalRecords = recIndex; if (mFooterText != null && mFooterText != string.Empty) { if (mFooterText.EndsWith(StringHelper.NewLine)) { writer.Write(mFooterText); } else { writer.WriteLine(mFooterText); } } }
private T[] ReadStream(TextReader reader, int maxRecords, DataTable dt) #endif { #endif if (reader == null) { throw new ArgumentNullException("reader", "The reader of the Stream can´t be null"); } ResetFields(); mHeaderText = String.Empty; mFooterText = String.Empty; ArrayList resArray = new ArrayList(); int currentRecord = 0; ForwardReader freader = new ForwardReader(reader, mRecordInfo.mIgnoreLast); freader.DiscardForward = true; string currentLine, completeLine; mLineNumber = 1; completeLine = freader.ReadNextLine(); currentLine = completeLine; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, 0, -1); #endif if (mRecordInfo.mIgnoreFirst > 0) { for (int i = 0; i < mRecordInfo.mIgnoreFirst && currentLine != null; i++) { mHeaderText += currentLine + StringHelper.NewLine; currentLine = freader.ReadNextLine(); mLineNumber++; } } bool byPass = false; if (maxRecords < 0) { maxRecords = int.MaxValue; } LineInfo line = new LineInfo(currentLine); line.mReader = freader; while (currentLine != null && currentRecord < maxRecords) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); bool skip = false; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1); skip = OnBeforeReadRecord(currentLine); #endif if (skip == false) { object record = mRecordInfo.StringToRecord(line); #if !MINI #if !GENERICS skip = OnAfterReadRecord(currentLine, record); #else skip = OnAfterReadRecord(currentLine, (T)record); #endif #endif if (skip == false && record != null) { #if MINI resArray.Add(record); #else if (dt == null) { resArray.Add(record); } else { dt.Rows.Add(mRecordInfo.RecordToValues(record)); } #endif } } } catch (Exception ex) { switch (mErrorManager.ErrorMode) { case ErrorMode.ThrowException: byPass = true; throw; case ErrorMode.IgnoreAndContinue: break; case ErrorMode.SaveAndContinue: ErrorInfo err = new ErrorInfo(); err.mLineNumber = freader.LineNumber; err.mExceptionInfo = ex; // err.mColumnNumber = mColumnNum; err.mRecordString = completeLine; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = freader.ReadNextLine(); completeLine = currentLine; mLineNumber++; } } } if (mRecordInfo.mIgnoreLast > 0) { mFooterText = freader.RemainingText; } #if !GENERICS return((object[]) #else return (T[]) #endif resArray.ToArray(RecordType)); }