Exemple #1
0
// object[] values, int index, ForwardReader reader
        internal object ExtractValue(LineInfo line)
        {
            //-> extract only what I need

            if (mInNewLine)
            {
                if (line.EmptyFromPos() == false)
                {
                    throw new BadUsageException("Text '" + line.CurrentString +
                                                "' found before the new line of the field: " + mFieldInfo.Name +
                                                " (this is not allowed when you use [FieldInNewLine])");
                }

                line.ReLoad(line.mReader.ReadNextLine());

                if (line.mLineStr == null)
                {
                    throw new BadUsageException("End of stream found parsing the field " + mFieldInfo.Name +
                                                ". Please check the class record.");
                }
            }

            var info = ExtractFieldString(line);

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

            line.mCurrentPos += mCharsToDiscard; //total;

            return(AssignFromString(info, line));


            //-> discard the part that I use


            //TODO: Uncoment this for Quoted Handling
//			if (info.NewRestOfLine != null)
//			{
//				if (info.NewRestOfLine.Length < CharsToDiscard())
//					return info.NewRestOfLine;
//				else
//					return info.NewRestOfLine.Substring(CharsToDiscard());
//			}
//			else
//			{
//				int total;
//				if (info.CharsRemoved >= line.mLine.Length)
//					total = line.mLine.Length;
//				else
//					total = info.CharsRemoved + CharsToDiscard();

            //return buffer.Substring(total);
//			}
        }
        private ExtractedInfo BasicExtractString(LineInfo line)
        {
            ExtractedInfo res;

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

                sepPos = line.IndexOf(mSeparator);

                if (sepPos == -1)
                {
                    if (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 '" + mSeparator + "' can´t be found after the field '" + 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);
        }
Exemple #3
0
        private Visyn.Windows.Io.FileHelper.Core.ExtractedInfo BasicExtractString(LineInfo line)
        {
            if (IsLast && !IsArray)
            {
                if (line.IndexOf(Separator) == -1)
                {
                    return(new Visyn.Windows.Io.FileHelper.Core.ExtractedInfo(line));
                }

                // Now check for one extra separator
                throw new BadUsageException(line.mReader.LineNumber, line.mCurrentPos,
                                            $"Delimiter '{Separator}' found after the last field '{FieldInfo.Name}' (the file is wrong or you need to add a field to the record class)");
            }
            var sepPos = line.IndexOf(Separator);

            if (sepPos != -1)
            {
                return(new Visyn.Windows.Io.FileHelper.Core.ExtractedInfo(line, sepPos));
            }
            if (IsLast && IsArray)
            {
                return(new Visyn.Windows.Io.FileHelper.Core.ExtractedInfo(line));
            }

            if (NextIsOptional == false)
            {
                if (IsFirst && line.EmptyFromPos())
                {
                    throw new FileHelpersException(line.mReader.LineNumber, line.mCurrentPos,
                                                   $"The line {line.mReader.LineNumber} is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.");
                }
                throw new FileHelpersException(line.mReader.LineNumber, line.mCurrentPos,
                                               $"Delimiter '{Separator}' not found after field '{FieldInfo.Name}' (the record has less fields, the delimiter is wrong or the next field must be marked as optional).");
            }
            sepPos = line.mLineStr.Length;

            return(new Visyn.Windows.Io.FileHelper.Core.ExtractedInfo(line, sepPos));
        }
Exemple #4
0
        /// <summary>
        /// Get the data out of the records
        /// </summary>
        /// <param name="line">Line handler containing text</param>
        /// <returns></returns>
        public object ExtractFieldValue(LineInfo line)
        {
            //-> extract only what I need

            if (InNewLine)
            {   // Any trailing characters, terminate
                if (line.EmptyFromPos() == false)
                {
                    throw new BadUsageException(line, $"Text '{line.CurrentString}' found before the new line of the field: {FieldInfo.Name} (this is not allowed when you use [FieldInNewLine])");
                }

                line.ReLoad(line.mReader.ReadNextLine());

                if (line.mLineStr == null)
                {
                    throw new BadUsageException(line, $"End of stream found parsing the field {FieldInfo.Name}. Please check the class record.");
                }
            }

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

                line.mCurrentPos += CharsToDiscard; //total;

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

            var i = 0;

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

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

                line.mCurrentPos += CharsToDiscard;

                try
                {
                    var value = AssignFromString(info, line);

                    if (value.NullValueUsed && i == 0 && line.IsEOL())
                    {
                        break;
                    }

                    res.Add(value.Value);
                }
                catch (NullValueNotFoundException)
                {
                    if (i == 0)
                    {
                        break;
                    }
                    throw;
                }
                i++;
            }

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

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

            // TODO:   is there a reason we go through all the array processing then discard it
            return(Discarded ? null : res.ToArray(ArrayType));
        }