Пример #1
0
// 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
        private ExtractedInfo BasicExtractString(LineInfo line)
        {
            if (IsLast && !IsArray)
            {
                var sepPos = line.IndexOf(mSeparator);

                if (sepPos == -1)
                {
                    return(new ExtractedInfo(line));
                }

                // Now check for one extra separator
                var msg = string.Format("Delimiter '{0}' found after the last field '{1}' (the file is wrong or you need to add a field to the record class)", mSeparator, this.FieldInfo.Name, line.mReader.LineNumber);

                throw new BadUsageException(line.mReader.LineNumber, line.mCurrentPos, msg);
            }
            else
            {
                int sepPos;

                sepPos = line.IndexOf(mSeparator);

                if (sepPos == -1)
                {
                    if (IsLast && IsArray)
                    {
                        return(new ExtractedInfo(line));
                    }

                    if (NextIsOptional == false)
                    {
                        string msg;

                        if (IsFirst && line.EmptyFromPos())
                        {
                            msg = string.Format("The line {0} is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.", line.mReader.LineNumber);
                        }
                        else
                        {
                            msg = string.Format("Delimiter '{0}' not found after field '{1}' (the record has less fields, the delimiter is wrong or the next field must be marked as optional).", mSeparator, this.FieldInfo.Name, line.mReader.LineNumber);
                        }

                        throw new FileHelpersException(line.mReader.LineNumber, line.mCurrentPos, msg);
                    }
                    else
                    {
                        sepPos = line.mLineStr.Length;
                    }
                }

                return(new ExtractedInfo(line, sepPos));
            }
        }
Пример #3
0
        private ExtractedInfo BasicExtractString(LineInfo line)
        {
            ExtractedInfo res;

            if (mIsLast)
            {
                res = new ExtractedInfo(line);
            }
            else
            {
                int sepPos;

                sepPos = line.IndexOf(mSeparator);

                if (sepPos == -1)
                {
                    if (this.mNextIsOptional == false)
                    {
                        string msg = null;

                        if (mIsFirst && line.EmptyFromPos())
                        {
                            msg = "The line " + line.mReader.LineNumber.ToString() + " is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.";
                        }
                        else
                        {
                            msg = "The delimiter '" + this.mSeparator + "' can´t be found after the field '" + this.mFieldInfo.Name + "' at line " + line.mReader.LineNumber.ToString() + " (the record has less fields, the delimiter is wrong or the next field must be marked as optional).";
                        }

                        throw new FileHelpersException(msg);
                    }
                    else
                    {
                        sepPos = line.mLine.Length - 1;
                    }
                }

                res = new ExtractedInfo(line, sepPos);
            }
            return(res);
        }
Пример #4
0
        /// <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);
            }
        }
Пример #5
0
        /// <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));
                }
            }
        }
Пример #6
0
        private ExtractedInfo BasicExtractString(LineInfo line)
        {
            if (IsLast && ! IsArray)
                return new ExtractedInfo(line);
            else
            {
                int sepPos;

                sepPos = line.IndexOf(mSeparator);

                if (sepPos == -1)
                {
                    if (IsLast && IsArray)
                        return new ExtractedInfo(line);

                    if (NextIsOptional == false)
                    {
                        string msg;

                        if (IsFirst && line.EmptyFromPos())
                            msg = string.Format("The line {0} is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.", line.mReader.LineNumber);
                        else
                            msg = string.Format("Delimiter '{0}' not found after field '{1}' (the record has less fields, the delimiter is wrong or the next field must be marked as optional).", mSeparator, this.FieldInfo.Name, line.mReader.LineNumber);

                        throw new FileHelpersException(line.mReader.LineNumber, line.mCurrentPos, msg);

                    }
                    else
                        sepPos = line.mLine.Length;
                }

                return new ExtractedInfo(line, sepPos);
            }
        }
Пример #7
0
// 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);
//			}


		}
Пример #8
0
		private ExtractedInfo BasicExtractString(LineInfo line)
		{
			ExtractedInfo res;
			
			if (mIsLast)
				res = new ExtractedInfo(line);
			else
			{
				int sepPos;

				sepPos = line.IndexOf(mSeparator);

				if (sepPos == -1)
				{
					if (this.mNextIsOptional == false)
					{
						string msg = null;

						if (mIsFirst && line.EmptyFromPos())
							msg = "The line " + line.mReader.LineNumber.ToString() + " is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.";
						else
							msg = "The delimiter '" + this.mSeparator + "' can´t be found after the field '" + this.mFieldInfo.Name + "' at line " + line.mReader.LineNumber.ToString() + " (the record has less fields, the delimiter is wrong or the next field must be marked as optional).";

						throw new FileHelpersException(msg);

					}
					else
						sepPos = line.mLine.Length - 1;
				}

				res = new ExtractedInfo(line, sepPos);
			}
			return res;
		}
Пример #9
0
        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);

            }
        }
Пример #10
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));
            }
        }