/// <summary> /// get The next line from the system and reset the line pointer to zero /// </summary> public void ReadNextLine() { mLineStr = mReader.ReadNextLine(); //mLine = mLineStr.ToCharArray(); mCurrentPos = 0; }
public void BasicProperties() { var reader = new ForwardReader(new NewLineDelimitedRecordReader(new StreamReader(FileTest.Good.CustomersTab.Path)), 0, 0); reader.DiscardForward.AssertEqualTo(false); reader.FowardLines.AssertEqualTo(0); reader.LineNumber.AssertEqualTo(0); reader.ReadNextLine(); reader.LineNumber.AssertEqualTo(1); }
// public ConverterBase ConvertProvider // { // get { return mConvertProvider; } // set { mConvertProvider = value; } // } // // #endregion // #region " TrimMode " // // public TrimMode TrimMode // { // get { return mTrimMode; } // } // // #endregion // // #region " TrimChars " // // public char[] TrimChars // { // get { return mTrimChars; } // } // // #endregion // // #region " IsOptional " // // public bool IsOptional // { // get { return mIsOptional; } // } // // #endregion // // #region " NextIsOptional " // // internal bool NextIsOptional // { // get { return mNextIsOptional; } // set { mNextIsOptional = value; } // } // // #endregion // #region " InNewLine " // public bool InNewLine // { // get { return mInNewLine; } // } // // #endregion // // // #region " FieldType " // // public Type FieldType // { // get { return mFieldType; } // } // // #endregion #region " ExtractAndAssignFromString " internal string ExtractAndAssignFromString(string buffer, object record, ForwardReader reader) { //-> extract only what I need if (this.mInNewLine == true) { if (buffer.Trim() != String.Empty) { throw new BadUsageException("Text '" + buffer + "' found before the new line of the field: " + mFieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); } buffer = reader.ReadNextLine(); if (buffer == null) { throw new BadUsageException("End of stream found parsing the field " + mFieldInfo.Name + ". Please check the class record."); } } ExtractedInfo info = ExtractFieldString(buffer, reader); AssignFromString(info.ExtractedString, record); //-> discard the part that I use if (info.NewRestOfLine != null) { if (info.NewRestOfLine.Length < CharsToDiscard()) { return(info.NewRestOfLine); } else { return(info.NewRestOfLine.Substring(CharsToDiscard())); } } else { int total; if (info.CharsRemoved >= buffer.Length) { total = buffer.Length; } else { total = info.CharsRemoved + CharsToDiscard(); } return(buffer.Substring(total)); } }
private void ReadNextRecord() { string currentLine = mAsyncReader.ReadNextLine(); mLineNumber++; bool byPass = false; mLastRecord = null; var line = new LineInfo(currentLine) { mReader = mAsyncReader }; while (true) { if (currentLine != null) { try { mTotalRecords++; Type currType = mRecordSelector(this, currentLine); line.ReLoad(currentLine); 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."); } var values = new object[info.FieldCount]; mLastRecord = info.Operations.StringToRecord(line, values); 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 { mLineNumber = mAsyncReader.LineNumber, mExceptionInfo = ex, mRecordString = currentLine }; // err.mColumnNumber = mColumnNum; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = mAsyncReader.ReadNextLine(); mLineNumber = mAsyncReader.LineNumber; } } } else { mLastRecord = null; if (RecordInfo.IgnoreLast > 0) { mFooterText = mAsyncReader.RemainingText; } try { mAsyncReader.Close(); } catch { } return; } } }
/// <include file='MultiRecordEngine.docs.xml' path='doc/ReadStream/*'/> public object[] 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 ArrayList(); using (var freader = new ForwardReader(reader, mMultiRecordInfo[0].IgnoreLast)) { freader.DiscardForward = true; string currentLine, completeLine; mLineNumber = 1; completeLine = freader.ReadNextLine(); currentLine = completeLine; #if !MINI if (MustNotifyProgress) // Avoid object creation { OnProgress(new ProgressEventArgs(0, -1)); } #endif 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; var line = new LineInfo(currentLine) { mReader = freader }; while (currentLine != null) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); var skip = false; Type currType = null; 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."); } var record = info.Operations.CreateRecordHandler(); #if !MINI if (MustNotifyProgress) // Avoid object creation { OnProgress(new ProgressEventArgs(currentRecord, -1)); } BeforeReadEventArgs <object> e = null; if (MustNotifyRead) // Avoid object creation { e = new BeforeReadEventArgs <object>(this, record, currentLine, LineNumber); skip = OnBeforeReadRecord(e); if (e.RecordLineChanged) { line.ReLoad(e.RecordLine); } } #endif if (skip == false) { var values = new object[info.FieldCount]; if (info.Operations.StringToRecord(record, line, values)) { #if !MINI if (MustNotifyRead) // Avoid object creation { skip = OnAfterReadRecord(currentLine, record, e.RecordLineChanged, LineNumber); } #endif 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 }; // err.mColumnNumber = mColumnNum; 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 void ReadNextRecord() { string currentLine = mAsyncReader.ReadNextLine(); mLineNumber++; bool byPass = false; mLastRecord = null; LineInfo line = new LineInfo(currentLine); line.mReader = mAsyncReader; while (true) { if (currentLine != null) { try { mTotalRecords++; Type currType = mRecordSelector(this, currentLine); line.ReLoad(currentLine); if (currType != null) { RecordInfo info = (RecordInfo)mRecordInfoHash[currType]; object[] values = new object[info.mFieldCount]; mLastRecord = info.StringToRecord(line, values); 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: ErrorInfo 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; } } }
/// <include file='MultiRecordEngine.docs.xml' path='doc/ReadStream/*'/> public object[] 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; ArrayList resArray = new ArrayList(); using (ForwardReader 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 int currentRecord = 0; if (mMultiRecordInfo[0].mIgnoreFirst > 0) { for (int i = 0; i < mMultiRecordInfo[0].mIgnoreFirst && currentLine != null; i++) { mHeaderText += currentLine + StringHelper.NewLine; currentLine = freader.ReadNextLine(); mLineNumber++; } } bool byPass = false; // MasterDetails record = null; ArrayList tmpDetails = new ArrayList(); LineInfo line = new LineInfo(currentLine); line.mReader = freader; while (currentLine != null) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); bool skip = false; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1); skip = OnBeforeReadRecord(currentLine); #endif Type currType = mRecordSelector(this, currentLine); if (currType != null) { RecordInfo info = (RecordInfo)mRecordInfoHash[currType]; if (skip == false) { object[] values = new object[info.mFieldCount]; object record = info.StringToRecord(line, values); #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: 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 = freader.LineNumber; } } } if (mMultiRecordInfo[0].mIgnoreLast > 0) { mFooterText = freader.RemainingText; } } return(resArray.ToArray()); }
internal static ExtractedInfo ExtractQuotedString(string source, ForwardReader reader, char quoteChar) { StringBuilder res = new StringBuilder(32); int lines = 0; bool beginEscape = false; if (reader == null) { throw new BadUsageException("The reader can´t be null"); } if (source == null || source.Length == 0) { throw new BadUsageException("An empty String found and can be parsed like a QuotedString try to use SafeExtractQuotedString"); } if (source[0] != quoteChar) { throw new BadUsageException("The source string not begins with the quote char: " + quoteChar); } int i = 1; bool mustContinue = true; while (mustContinue && source != null) { while (i < source.Length) { if (source[i] == quoteChar) { if (beginEscape == true) { beginEscape = false; res.Append(quoteChar); } else { beginEscape = true; } } else { if (beginEscape) { // End of the String ExtractedInfo ei = new ExtractedInfo(res.ToString(), i, lines); if (lines != 0) { ei.NewRestOfLine = source.Substring(i); } return(ei); } else { res.Append(source[i]); } } i++; } if (beginEscape) { return(new ExtractedInfo(res.ToString(), i + 1, lines)); } else { source = reader.ReadNextLine(); res.Append(StringHelper.NewLine); lines++; i = 0; } } throw new BadUsageException("The current field has an UnClosed quoted string. Complete Filed String: " + res.ToString()); }
public T[] ReadStream(TextReader reader) #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; while (currentLine != null) { try { mTotalRecords++; currentRecord++; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1); #endif object record = mRecordInfo.StringToRecord(currentLine, freader); if (record != null) { resArray.Add(record); } } 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)); }
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"); } NewLineDelimitedRecordReader recordReader = new NewLineDelimitedRecordReader(reader); ResetFields(); mHeaderText = String.Empty; mFooterText = String.Empty; ArrayList resArray = new ArrayList(); int currentRecord = 0; using (ForwardReader freader = new ForwardReader(recordReader, 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; object[] values = new object[mRecordInfo.mFieldCount]; while (currentLine != null && currentRecord < maxRecords) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); bool skip = false; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1); #if !GENERICS BeforeReadRecordEventArgs e = new BeforeReadRecordEventArgs(currentLine, LineNumber); #else BeforeReadRecordEventArgs <T> e = new BeforeReadRecordEventArgs <T>(currentLine, LineNumber); #endif skip = OnBeforeReadRecord(e); if (e.RecordLineChanged) { line.ReLoad(e.RecordLine); } #endif if (skip == false) { object record = mRecordInfo.StringToRecord(line, values); #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)); }
private void ReadNextRecord() { string currentLine = mAsyncReader.ReadNextLine(); mLineNumber++; bool byPass = false; #if !GENERICS mLastRecord = null; #else mLastRecord = default(T); #endif while (true) { if (currentLine != null) { try { mTotalRecords++; #if !GENERICS mLastRecord = mRecordInfo.StringToRecord(currentLine, mAsyncReader); #else mLastRecord = (T)mRecordInfo.StringToRecord(currentLine, mAsyncReader); #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: ErrorInfo 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(); } catch { } return; } } }
public void ReLoadAsNextLine() { ReLoad(mReader.ReadNextLine()); }
/// <include file='MultiRecordEngine.docs.xml' path='doc/ReadStream/*'/> public object[] ReadStream(IRecordReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(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 non-null Selector in the constructor."); } ResetFields(); HeaderText = string.Empty; mFooterText = string.Empty; var resArray = new ArrayList(); 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++) { HeaderText += currentLine + Environment.NewLine; currentLine = freader.ReadNextLine(); mLineNumber++; } } bool byPass = false; var line = new LineInfo(currentLine) { mReader = freader }; while (currentLine != null) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); Type currentType; try { currentType = mRecordSelector(this, currentLine); } catch (Exception ex) { throw new Exception("Selector failed to process correctly", ex); } if (currentType != null) { mRecordInfoTable.TryGetValue(currentType, out IRecordInfo info); if (info == null) { throw new BadUsageException("A record is of type '" + currentType.Name + "' which this engine is not configured to handle. Try adding this type to the constructor."); } object record = ReadRecord(info, currentRecord, line); if (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 { mLineNumber = freader.LineNumber, mExceptionInfo = ex, mRecordString = completeLine, mRecordTypeName = RecordInfo.RecordType.Name }; 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()); }