コード例 #1
0
ファイル: FieldBase.cs プロジェクト: pjeconde/CedFCIC
// object[] values, int index, ForwardReader reader
        internal object ExtractValue(LineInfo line)
        {
            //-> extract only what I need

            if (this.mInNewLine == true)
            {
                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.");
                }
            }

            ExtractedInfo 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);
//			}
        }
コード例 #2
0
ファイル: EventEngineBase.cs プロジェクト: ywscr/FileHelpers
        internal object ReadRecord(
            IRecordInfo recordInfo,
            int currentRecord,
            LineInfo line)
        {
            T record = (T)recordInfo.Operations.CreateRecordHandler();

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

            var skip = false;
            BeforeReadEventArgs <T> e = null;
            bool notifyRead           = MustNotifyReadForRecord(recordInfo);

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

            if (skip == false)
            {
                var values = new object[recordInfo.FieldCount];
                if (recordInfo.Operations.StringToRecord(record, line, values))
                {
                    if (notifyRead)
                    {
                        skip = OnAfterReadRecord(line.mLineStr, record, e.RecordLineChanged, LineNumber);
                    }

                    if (skip == false)
                    {
                        return(record);
                    }
                }
            }

            return(null);
        }
コード例 #3
0
ファイル: FieldBase.cs プロジェクト: rmegal/FileHelpers
        /// <summary>
        /// Get the data out of the records
        /// </summary>
        /// <param name="line">Line handler containing text</param>
        /// <returns></returns>
        internal 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) {
                ExtractedInfo info = ExtractFieldString(line);
                if (info.mCustomExtractedString == null)
                    line.mCurrentPos = info.ExtractedTo + 1;

                line.mCurrentPos += CharsToDiscard; //total;

                if (Discarded)
                    return GetDiscardedNullValue();
                else
                    return AssignFromString(info, line).Value;
            }
            else {
                if (ArrayMinLength <= 0)
                    ArrayMinLength = 0;

                int i = 0;

                var res = new ArrayList(Math.Max(ArrayMinLength, 10));

                while (line.mCurrentPos - CharsToDiscard < line.mLineStr.Length &&
                       i < ArrayMaxLength) {
                    ExtractedInfo 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;
                        else
                            throw;
                    }
                    i++;
                }

                if (res.Count < ArrayMinLength) {
                    throw new InvalidOperationException(
                        string.Format(
                            "Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}",
                            line.mReader.LineNumber.ToString(),
                            line.mCurrentPos.ToString(),
                            FieldInfo.Name,
                            res.Count,
                            ArrayMinLength));
                }
                else if (IsLast && line.IsEOL() == false) {
                    throw new InvalidOperationException(
                        string.Format(
                            "Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}",
                            line.mReader.LineNumber,
                            line.mCurrentPos,
                            FieldInfo.Name,
                            ArrayMaxLength));
                }

                // TODO:   is there a reason we go through all the array processing then discard it
                if (Discarded)
                    return null;
                else
                    return res.ToArray(ArrayType);
            }
        }
コード例 #4
0
        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;
                }
            }
        }
コード例 #5
0
        /// <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());
        }
コード例 #6
0
        private void ReadNextRecord()
        {
            string currentLine = mAsyncReader.ReadNextLine();

            mLineNumber++;

            bool byPass = false;

#if !GENERICS
            mLastRecord = null;
#else
            mLastRecord = default(T);
#endif


            LineInfo line = new LineInfo(string.Empty);
            line.mReader = mAsyncReader;

            if (mLastRecordValues == null)
            {
                mLastRecordValues = new object[mRecordInfo.mFieldCount];
            }

            while (true)
            {
                if (currentLine != null)
                {
                    try
                    {
                        mTotalRecords++;
                        line.ReLoad(currentLine);

                        bool skip = false;

#if !MINI
    #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)
                        {
#if !GENERICS
                            mLastRecord = mRecordInfo.StringToRecord(line, mLastRecordValues);
#else
                            mLastRecord = (T)mRecordInfo.StringToRecord(line, mLastRecordValues);
#endif

#if !MINI
#if !GENERICS
                            skip = OnAfterReadRecord(currentLine, mLastRecord);
#else
                            skip = OnAfterReadRecord(currentLine, (T)mLastRecord);
#endif
#endif

                            if (skip == false && 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
                {
                    mLastRecordValues = null;

#if !GENERICS
                    mLastRecord = null;
#else
                    mLastRecord = default(T);
#endif


                    if (mRecordInfo.mIgnoreLast > 0)
                    {
                        mFooterText = mAsyncReader.RemainingText;
                    }

                    try
                    {
                        mAsyncReader.Close();
                        //mAsyncReader = null;
                    }
                    catch
                    {
                    }

                    return;
                }
            }
        }
コード例 #7
0
ファイル: FieldBase.cs プロジェクト: keif888/TextFileSplitter
        /// <summary>
        /// Get the data out of the records
        /// </summary>
        /// <param name="line">Line handler containing text</param>
        /// <returns></returns>
        internal 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)
            {
                ExtractedInfo info = ExtractFieldString(line);
                if (info.mCustomExtractedString == null)
                {
                    line.mCurrentPos = info.ExtractedTo + 1;
                }

                line.mCurrentPos += CharsToDiscard; //total;

                if (Discarded)
                {
                    return(GetDiscardedNullValue());
                }
                else
                {
                    return(AssignFromString(info, line).Value);
                }
            }
            else
            {
                if (ArrayMinLength <= 0)
                {
                    ArrayMinLength = 0;
                }

                int i = 0;

                var res = new ArrayList(Math.Max(ArrayMinLength, 10));

                while (line.mCurrentPos - CharsToDiscard < line.mLineStr.Length && i < ArrayMaxLength)
                {
                    ExtractedInfo 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;
                        }
                        else
                        {
                            throw;
                        }
                    }
                    i++;
                }

                if (res.Count < ArrayMinLength)
                {
                    throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength));
                }
                else if (IsLast && line.IsEOL() == false)
                {
                    throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength));
                }

                // TODO:   is there a reason we go through all the array processing then discard it
                if (Discarded)
                {
                    return(null);
                }
                else
                {
                    return(res.ToArray(ArrayType));
                }
            }
        }
コード例 #8
0
        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;
                }
            }
        }
コード例 #9
0
        /// <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());
        }
コード例 #10
0
        private void ReadNextRecord()
        {
            string currentLine = mAsyncReader.ReadNextLine();

            mLineNumber++;

            bool byPass = false;

#if !GENERICS
            mLastRecord = null;
#else
            mLastRecord = default(T);
#endif


            LineInfo 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:
                            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();
                        //mAsyncReader = null;
                    }
                    catch
                    {
                    }

                    return;
                }
            }
        }
コード例 #11
0
        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));
        }
コード例 #12
0
        /// <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());
        }
コード例 #13
0
ファイル: FieldBase.cs プロジェクト: pjeconde/CedForecast
// object[] values, int index, ForwardReader reader
		internal object ExtractValue(LineInfo line)
		{
			//-> extract only what I need

			if (this.mInNewLine == true)
			{
				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.");
			}

			ExtractedInfo 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);
//			}


		}
コード例 #14
0
ファイル: FieldBase.cs プロジェクト: jalchr/FileHelpers
        internal object ExtractFieldValue(LineInfo line)
        {
            //-> extract only what I need

            if (InNewLine)
            {
                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)
            {
                ExtractedInfo info = ExtractFieldString(line);
                if (info.mCustomExtractedString == null)
                    line.mCurrentPos = info.ExtractedTo + 1;

                line.mCurrentPos += CharsToDiscard; //total;

                return AssignFromString(info, line);
            }
            else
            {
                if (ArrayMinLength <= 0)
                    ArrayMinLength = 0;

                int i = 0;

                var res = new ArrayList(Math.Max(ArrayMinLength, 10));

                while (line.mCurrentPos - CharsToDiscard < line.mLine.Length && i < ArrayMaxLength)
                {
                    ExtractedInfo info = ExtractFieldString(line);
                    if (info.mCustomExtractedString == null)
                        line.mCurrentPos = info.ExtractedTo + 1;

                    line.mCurrentPos += CharsToDiscard;

                    res.Add(AssignFromString(info, line));
                    i++;
                }

                if (res.Count < ArrayMinLength)
                    throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength));

                else if (IsLast && line.IsEOL() == false)
                    throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength));

                return res.ToArray(ArrayType);

            }
        }
コード例 #15
0
        internal object ExtractFieldValue(LineInfo line)
        {
            //-> extract only what I need

            if (mInNewLine)
            {
                if (line.EmptyFromPos() == false)
                {
                    throw new BadUsageException(line, "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(line, "End of stream found parsing the field " + mFieldInfo.Name +
                                                ". Please check the class record.");
                }
            }

            if (mIsArray == false)
            {
                ExtractedInfo info = ExtractFieldString(line);
                if (info.mCustomExtractedString == null)
                {
                    line.mCurrentPos = info.ExtractedTo + 1;
                }

                line.mCurrentPos += mCharsToDiscard; //total;

                return(AssignFromString(info, line));
            }
            else
            {
                if (mArrayMinLength <= 0)
                {
                    mArrayMinLength = 0;
                }

                int i = 0;

                ArrayList res = new ArrayList(Math.Max(mArrayMinLength, 10));

                while (line.mCurrentPos - mCharsToDiscard < line.mLine.Length && i < mArrayMaxLength)
                {
                    ExtractedInfo info = ExtractFieldString(line);
                    if (info.mCustomExtractedString == null)
                    {
                        line.mCurrentPos = info.ExtractedTo + 1;
                    }

                    line.mCurrentPos += mCharsToDiscard;

                    res.Add(AssignFromString(info, line));
                    i++;
                }

                if (res.Count < mArrayMinLength)
                {
                    throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), mFieldInfo.Name, res.Count, mArrayMinLength));
                }

                else if (mIsLast && line.IsEOL() == false)
                {
                    throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, mFieldInfo.Name, mArrayMaxLength));
                }

                return(res.ToArray(mArrayType));
            }
        }