// object[] values, int index, ForwardReader reader internal object ExtractValue(LineInfo line) { //-> extract only what I need if (mInNewLine) { if (line.EmptyFromPos() == false) { throw new BadUsageException("Text '" + line.CurrentString + "' found before the new line of the field: " + mFieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); } line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) { throw new BadUsageException("End of stream found parsing the field " + mFieldInfo.Name + ". Please check the class record."); } } var info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += mCharsToDiscard; //total; return(AssignFromString(info, line)); //-> discard the part that I use //TODO: Uncoment this for Quoted Handling // if (info.NewRestOfLine != null) // { // if (info.NewRestOfLine.Length < CharsToDiscard()) // return info.NewRestOfLine; // else // return info.NewRestOfLine.Substring(CharsToDiscard()); // } // else // { // int total; // if (info.CharsRemoved >= line.mLine.Length) // total = line.mLine.Length; // else // total = info.CharsRemoved + CharsToDiscard(); //return buffer.Substring(total); // } }
private void ReadNextRecord() { var currentLine = mAsyncReader.ReadNextLine(); mLineNumber++; var byPass = false; mLastRecord = null; var line = new LineInfo(currentLine); line.mReader = mAsyncReader; while (true) { if (currentLine != null) { try { mTotalRecords++; var currType = mRecordSelector(this, currentLine); line.ReLoad(currentLine); if (currType != null) { var info = (RecordInfo)mRecordInfoHash[currType]; mLastRecord = info.StringToRecord(line); if (mLastRecord != null) { byPass = true; return; } } } catch (Exception ex) { switch (mErrorManager.ErrorMode) { case ErrorMode.ThrowException: byPass = true; throw; case ErrorMode.IgnoreAndContinue: break; case ErrorMode.SaveAndContinue: var err = new ErrorInfo(); err.mLineNumber = mAsyncReader.LineNumber; err.mExceptionInfo = ex; // err.mColumnNumber = mColumnNum; err.mRecordString = currentLine; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = mAsyncReader.ReadNextLine(); mLineNumber = mAsyncReader.LineNumber; } } } else { mLastRecord = null; if (mRecordInfo.mIgnoreLast > 0) { mFooterText = mAsyncReader.RemainingText; } try { mAsyncReader.Close(); } catch { } return; } } }
public MasterDetails <M, D>[] ReadStream(TextReader reader) #endif { if (reader == null) { throw new ArgumentNullException("reader", "The reader of the Stream cant be null"); } if (mRecordSelector == null) { throw new BadUsageException("The Recordselector cant be null, please pass a not null Selector in the constructor."); } ResetFields(); mHeaderText = String.Empty; mFooterText = String.Empty; ArrayList resArray = new ArrayList(); ForwardReader freader = new ForwardReader(reader, mMasterInfo.mIgnoreLast); freader.DiscardForward = true; string currentLine, completeLine; mLineNumber = 1; completeLine = freader.ReadNextLine(); currentLine = completeLine; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, 0, -1); #endif int currentRecord = 0; if (mMasterInfo.mIgnoreFirst > 0) { for (int i = 0; i < mMasterInfo.mIgnoreFirst && currentLine != null; i++) { mHeaderText += currentLine + StringHelper.NewLine; currentLine = freader.ReadNextLine(); mLineNumber++; } } bool byPass = false; #if !GENERICS MasterDetails record = null; #else MasterDetails <M, D> record = null; #endif ArrayList tmpDetails = new ArrayList(); LineInfo line = new LineInfo(currentLine); line.mReader = freader; while (currentLine != null) { try { currentRecord++; line.ReLoad(currentLine); #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1); #endif RecordAction action = mRecordSelector(currentLine); switch (action) { case RecordAction.Master: if (record != null) { #if !GENERICS record.mDetails = tmpDetails.ToArray(); #else record.mDetails = (D[])tmpDetails.ToArray(); #endif resArray.Add(record); } mTotalRecords++; #if !GENERICS record = new MasterDetails(); #else record = new MasterDetails <M, D>(); #endif tmpDetails.Clear(); #if !GENERICS object lastMaster = mMasterInfo.StringToRecord(line); #else M lastMaster = (M)mMasterInfo.StringToRecord(line); #endif if (lastMaster != null) { record.mMaster = lastMaster; } break; case RecordAction.Detail: #if !GENERICS object lastChild = mRecordInfo.StringToRecord(line); #else D lastChild = (D)mRecordInfo.StringToRecord(line); #endif if (lastChild != null) { tmpDetails.Add(lastChild); } break; default: break; } } 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 = mLineNumber; err.mExceptionInfo = ex; // err.mColumnNumber = mColumnNum; err.mRecordString = completeLine; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = freader.ReadNextLine(); completeLine = currentLine; mLineNumber = freader.LineNumber; } } } if (record != null) { #if !GENERICS record.mDetails = tmpDetails.ToArray(); #else record.mDetails = (D[])tmpDetails.ToArray(); #endif resArray.Add(record); } if (mMasterInfo.mIgnoreLast > 0) { mFooterText = freader.RemainingText; } #if !GENERICS return((MasterDetails[])resArray.ToArray(typeof(MasterDetails))); #else return((MasterDetails <M, D>[])resArray.ToArray(typeof(MasterDetails <M, D>))); #endif }
/// <include file='MultiRecordEngine.docs.xml' path='doc/ReadStream/*' /> public object[] ReadStream(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader", "The reader of the Stream can´t be null"); } if (mRecordSelector == null) { throw new BadUsageException("The Recordselector can´t be null, please pass a not null Selector in the constructor."); } ResetFields(); mHeaderText = String.Empty; mFooterText = String.Empty; var resArray = new ArrayList(); var freader = new ForwardReader(reader, mMultiRecordInfo[0].mIgnoreLast); freader.DiscardForward = true; string currentLine, completeLine; mLineNumber = 1; completeLine = freader.ReadNextLine(); currentLine = completeLine; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, 0, -1); #endif var currentRecord = 0; if (mMultiRecordInfo[0].mIgnoreFirst > 0) { for (var i = 0; i < mMultiRecordInfo[0].mIgnoreFirst && currentLine != null; i++) { mHeaderText += currentLine + StringHelper.NewLine; currentLine = freader.ReadNextLine(); mLineNumber++; } } var byPass = false; // MasterDetails record = null; var tmpDetails = new ArrayList(); var line = new LineInfo(currentLine); line.mReader = freader; while (currentLine != null) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); var skip = false; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1); skip = OnBeforeReadRecord(currentLine); #endif var currType = mRecordSelector(this, currentLine); if (currType != null) { var info = (RecordInfo)mRecordInfoHash[currType]; if (skip == false) { var record = info.StringToRecord(line); #if !MINI skip = OnAfterReadRecord(currentLine, record); #endif if (skip == false && record != null) { resArray.Add(record); } } } } catch (Exception ex) { switch (mErrorManager.ErrorMode) { case ErrorMode.ThrowException: byPass = true; throw; case ErrorMode.IgnoreAndContinue: break; case ErrorMode.SaveAndContinue: var 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 = freader.LineNumber; } } } if (mMultiRecordInfo[0].mIgnoreLast > 0) { mFooterText = freader.RemainingText; } return(resArray.ToArray()); }
/// <include file='MultiRecordEngine.docs.xml' path='doc/ReadStream/*'/> public MasterMultiDetails[] ReadStream(IRecordReader reader) { if (reader == null) { throw new ArgumentNullException("reader", "The reader of the Stream can´t be null"); } if (mRecordSelector == null) { throw new BadUsageException( "The Recordselector can´t be null, please pass a not null Selector in the constructor."); } ResetFields(); mHeaderText = String.Empty; mFooterText = String.Empty; var resArray = new List <MasterMultiDetails>(); using (var freader = new ForwardReader(reader, mMultiRecordInfo[0].IgnoreLast)) { freader.DiscardForward = true; mLineNumber = 1; var completeLine = freader.ReadNextLine(); var currentLine = completeLine; if (MustNotifyProgress) // Avoid object creation { OnProgress(new ProgressEventArgs(0, -1)); } int currentRecord = 0; if (mMultiRecordInfo[0].IgnoreFirst > 0) { for (int i = 0; i < mMultiRecordInfo[0].IgnoreFirst && currentLine != null; i++) { mHeaderText += currentLine + StringHelper.NewLine; currentLine = freader.ReadNextLine(); mLineNumber++; } } bool byPass = false; MasterMultiDetails record = null; var tmpDetails = new ArrayList(); var line = new LineInfo(currentLine) { mReader = freader }; while (currentLine != null) { try { mTotalRecords++; currentRecord++; var skip = false; Type currType; try { currType = mRecordSelector(this, currentLine); } catch (Exception ex) { throw new Exception("Selector failed to process correctly", ex); } if (currType != null) { var info = (RecordInfo)mRecordInfoHash[currType]; if (info == null) { throw new BadUsageException("A record is of type '" + currType.Name + "' which this engine is not configured to handle. Try adding this type to the constructor."); } record = new MasterMultiDetails(); var lastMaster = info.Operations.CreateRecordHandler(); var masterEndIndex = currentLine.Length; foreach (var detailSelector in mDetailSelectorHash[currType]) { var detailIndex = currentLine.IndexOf(detailSelector.Start); if (detailIndex > 0 && detailIndex < masterEndIndex) { masterEndIndex = detailIndex; } } var lineMaster = currentLine.Substring(0, masterEndIndex); line.ReLoad(lineMaster); if (MustNotifyProgress) // Avoid object creation { OnProgress(new ProgressEventArgs(currentRecord, -1)); } BeforeReadEventArgs <object> e = null; if (MustNotifyRead) // Avoid object creation { e = new BeforeReadEventArgs <object>(this, lastMaster, lineMaster, LineNumber); skip = OnBeforeReadRecord(e); if (e.RecordLineChanged) { line.ReLoad(e.RecordLine); } } if (skip == false) { var values = new object[info.FieldCount]; if (info.Operations.StringToRecord(lastMaster, line, values)) { if (MustNotifyRead) // Avoid object creation { skip = OnAfterReadRecord(lineMaster, lastMaster, e.RecordLineChanged, LineNumber); } if (skip == false) { record.Master = lastMaster; } } if (currentLine.Length > lineMaster.Length) { var detailsLine = currentLine.Substring(masterEndIndex); foreach (var detailSelector in mDetailSelectorHash[currType]) { var detailStartIndex = detailsLine.IndexOf(detailSelector.Start) + 1; if (detailStartIndex < 1) { continue; } var detailEndIndex = detailsLine.IndexOf(detailSelector.End, detailStartIndex); if (detailEndIndex < 0) { detailEndIndex = detailsLine.Length; } var lineDetails = detailsLine.Substring(detailStartIndex, detailEndIndex - detailStartIndex); var detailInfo = (RecordInfo)mRecordInfoHash[detailSelector.Detail]; var valuesDetail = new object[detailInfo.FieldCount]; tmpDetails.Clear(); foreach (var lineDetail in lineDetails.Split(new [] { detailSelector.Separator }, StringSplitOptions.RemoveEmptyEntries)) { try { line.ReLoad(lineDetail); var lastChild = detailInfo.Operations.StringToRecord(line, valuesDetail); if (lastChild != null) { tmpDetails.Add(lastChild); } } catch (Exception ex) { switch (mErrorManager.ErrorMode) { case ErrorMode.ThrowException: throw; case ErrorMode.IgnoreAndContinue: break; case ErrorMode.SaveAndContinue: var err = new ErrorInfo { mLineNumber = mLineNumber, mExceptionInfo = ex, mRecordString = completeLine }; // err.mColumnNumber = mColumnNum; mErrorManager.AddError(err); break; } } } record.Details.Add(detailSelector.Detail, tmpDetails.ToArray()); } } if (skip == false) { resArray.Add(record); } } } } catch (Exception ex) { switch (mErrorManager.ErrorMode) { case ErrorMode.ThrowException: byPass = true; throw; case ErrorMode.IgnoreAndContinue: break; case ErrorMode.SaveAndContinue: var err = new ErrorInfo { mLineNumber = freader.LineNumber, mExceptionInfo = ex, mRecordString = completeLine }; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = freader.ReadNextLine(); completeLine = currentLine; mLineNumber = freader.LineNumber; } } } if (mMultiRecordInfo[0].IgnoreLast > 0) { mFooterText = freader.RemainingText; } } return(resArray.ToArray()); }
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; var resArray = new ArrayList(); var currentRecord = 0; var 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 (var i = 0; i < mRecordInfo.mIgnoreFirst && currentLine != null; i++) { mHeaderText += currentLine + StringHelper.NewLine; currentLine = freader.ReadNextLine(); mLineNumber++; } } var byPass = false; if (maxRecords < 0) { maxRecords = int.MaxValue; } var line = new LineInfo(currentLine); line.mReader = freader; while (currentLine != null && currentRecord < maxRecords) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); var skip = false; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1); skip = OnBeforeReadRecord(currentLine); #endif if (skip == false) { var 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: var 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)); }
private void ReadNextRecord() { var currentLine = mAsyncReader.ReadNextLine(); mLineNumber++; var byPass = false; #if !GENERICS mLastRecord = null; #else mLastRecord = default(T); #endif var line = new LineInfo(string.Empty); line.mReader = mAsyncReader; while (true) { if (currentLine != null) { try { mTotalRecords++; line.ReLoad(currentLine); #if !GENERICS mLastRecord = mRecordInfo.StringToRecord(line); #else mLastRecord = (T)mRecordInfo.StringToRecord(line); #endif if (mLastRecord != null) { byPass = true; return; } } catch (Exception ex) { switch (mErrorManager.ErrorMode) { case ErrorMode.ThrowException: byPass = true; throw; case ErrorMode.IgnoreAndContinue: break; case ErrorMode.SaveAndContinue: var err = new ErrorInfo(); err.mLineNumber = mAsyncReader.LineNumber; err.mExceptionInfo = ex; // err.mColumnNumber = mColumnNum; err.mRecordString = currentLine; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = mAsyncReader.ReadNextLine(); mLineNumber = mAsyncReader.LineNumber; } } } else { #if !GENERICS mLastRecord = null; #else mLastRecord = default(T); #endif if (mRecordInfo.mIgnoreLast > 0) { mFooterText = mAsyncReader.RemainingText; } try { mAsyncReader.Close(); //mAsyncReader = null; } catch { } return; } } }
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); }
/// <summary> /// Get the data out of the records /// </summary> /// <param name="line">Line handler containing text</param> /// <returns></returns> public object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (InNewLine) { // Any trailing characters, terminate if (line.EmptyFromPos() == false) { throw new BadUsageException(line, $"Text '{line.CurrentString}' found before the new line of the field: {FieldInfo.Name} (this is not allowed when you use [FieldInNewLine])"); } line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) { throw new BadUsageException(line, $"End of stream found parsing the field {FieldInfo.Name}. Please check the class record."); } } if (IsArray == false) { var info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += CharsToDiscard; //total; return(Discarded ? GetDiscardedNullValue() : AssignFromString(info, line).Value); } if (ArrayMinLength <= 0) { ArrayMinLength = 0; } var i = 0; var res = new ArrayList(Math.Max(ArrayMinLength, 10)); while (line.mCurrentPos - CharsToDiscard < line.mLineStr.Length && i < ArrayMaxLength) { var info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += CharsToDiscard; try { var value = AssignFromString(info, line); if (value.NullValueUsed && i == 0 && line.IsEOL()) { break; } res.Add(value.Value); } catch (NullValueNotFoundException) { if (i == 0) { break; } throw; } i++; } if (res.Count < ArrayMinLength) { throw new InvalidOperationException($"Line: {line.mReader.LineNumber} Column: {line.mCurrentPos} Field: {FieldInfo.Name}. The array has only {res.Count} values, less than the minimum length of {ArrayMinLength}"); } if (IsLast && line.IsEOL() == false) { throw new InvalidOperationException($"Line: {line.mReader.LineNumber} Column: {line.mCurrentPos} Field: {FieldInfo.Name}. The array has more values than the maximum length of {ArrayMaxLength}"); } // TODO: is there a reason we go through all the array processing then discard it return(Discarded ? null : res.ToArray(ArrayType)); }