예제 #1
0
        internal void EnterField(FtField aField)
        {
            field = aField;

            // cache field values
            fieldSubstitutionsEnabled = core.SubstitutionsEnabled;
            fieldSubstitutionChar     = core.SubstitutionChar;
            fieldDelimiterChar        = core.DelimiterChar;
            fieldQuoteChar            = core.QuoteChar;

            if (headings)
            {
                fieldQuotedType = field.HeadingQuotedType;
            }
            else
            {
                fieldQuotedType = field.ValueQuotedType;
            }

            switch (fieldQuotedType)
            {
            case FtQuotedType.Never:
                quotedState = QuotedState.NeverOpen;
                break;

            case FtQuotedType.Optional:
                quotedState = QuotedState.CanOpen;
                break;

            case FtQuotedType.Always:
                quotedState = QuotedState.MustOpen;
                break;

            default:
                throw FtInternalException.Create(InternalError.DelimitedFieldParser_EnterField_UnsupportedQuotedType, fieldQuotedType.ToString());
            }


            textBuilder.Clear();
            position  = charReader.Position;
            rawOffset = -1;
            rawLength = 0;
        }
예제 #2
0
        internal DelimitedFieldParser(SerializationCore myCore, CharReader myCharReader, bool forHeadings)
        {
            headings    = forHeadings;
            charReader  = myCharReader;
            core        = myCore;
            textBuilder = new StringBuilder(20);

            field                     = null;
            fieldQuotedType           = FtQuotedType.Optional;
            fieldSubstitutionsEnabled = false;
            fieldSubstitutionChar     = '\0';
            fieldDelimiterChar        = '\0';
            fieldQuoteChar            = '\0';

            quotedState        = QuotedState.CanOpen;
            substitutionActive = false;
            position           = -1;
            rawOffset          = -1;
            rawLength          = 0;
        }
예제 #3
0
        internal void ParseChar(char aChar, out bool finished)
        {
            if (substitutionActive)
            {
                AppendSubstitution(aChar);
                substitutionActive = false;
                rawLength++;
                finished = false;
            }
            else
            {
                if (fieldSubstitutionsEnabled && aChar == fieldSubstitutionChar)
                {
                    substitutionActive = true;
                    rawLength++;
                    finished = false;
                }
                else
                {
                    switch (quotedState)
                    {
                    case QuotedState.NeverOpen:
                        if (aChar == fieldDelimiterChar)
                        {
                            finished = true;
                        }
                        else
                        {
                            finished = false;
                            AppendValueChar(aChar);
                        }
                        break;

                    case QuotedState.CanOpen:
                        if (aChar == fieldQuoteChar)
                        {
                            finished    = false;
                            quotedState = QuotedState.Opened;
                        }
                        else
                        {
                            if (char.IsWhiteSpace(aChar))
                            {
                                finished = false;
                                rawLength++;
                                textBuilder.Append(aChar);
                            }
                            else
                            {
                                quotedState = QuotedState.NeverOpen;
                                ParseChar(aChar, out finished);
                            }
                        }
                        break;

                    case QuotedState.MustOpen:
                        if (char.IsWhiteSpace(aChar))
                        {
                            finished = false;     // ignore white space before quote is opened
                        }
                        else
                        {
                            if (aChar == fieldDelimiterChar)
                            {
                                finished = true;     // null
                            }
                            else
                            {
                                if (aChar == fieldQuoteChar)
                                {
                                    finished    = false;
                                    quotedState = QuotedState.Opened;
                                }
                                else
                                {
                                    FtSerializationError error = headings ? FtSerializationError.HeadingNonWhiteSpaceCharBeforeQuotesOpened : FtSerializationError.ValueNonWhiteSpaceCharBeforeQuotesOpened;
                                    string message             = headings ? Properties.Resources.DelimitedFieldParser_ParseChar_HeadingNonWhiteSpaceCharBeforeQuotesOpened : Properties.Resources.DelimitedFieldParser_ParseChar_ValueNonWhiteSpaceCharBeforeQuotesOpened;
                                    throw new FtSerializationException(error, field, message);
                                }
                            }
                        }
                        break;

                    case QuotedState.Opened:
                        rawOffset   = (int)(charReader.Position - position);
                        rawLength   = 0;
                        quotedState = QuotedState.Open;
                        ParseChar(aChar, out finished);
                        break;

                    case QuotedState.Open:
                        finished = false;
                        if (aChar != fieldQuoteChar)
                        {
                            AppendValueChar(aChar);
                        }
                        else
                        {
                            if (charReader.Peek() == fieldQuoteChar)
                            {
                                rawLength++;
                                quotedState = QuotedState.Stuffed;
                            }
                            else
                            {
                                rawLength   = (int)(charReader.Position - position) + rawOffset;
                                quotedState = QuotedState.Closed;
                            }
                        }
                        break;

                    case QuotedState.Stuffed:
                        AppendValueChar(fieldQuoteChar);
                        quotedState = QuotedState.Open;
                        finished    = false;
                        break;

                    case QuotedState.Closed:
                        if (aChar == fieldDelimiterChar)
                        {
                            finished = true;
                        }
                        else
                        {
                            if (char.IsWhiteSpace(aChar))
                            {
                                finished = false;
                            }
                            else
                            {
                                FtSerializationError error = headings ? FtSerializationError.HeadingNonWhiteSpaceCharAfterQuotesClosed : FtSerializationError.ValueNonWhiteSpaceCharAfterQuotesClosed;
                                string message             = headings ? Properties.Resources.DelimitedFieldParser_ParseChar_HeadingNonWhiteSpaceCharAfterQuotesClosed : Properties.Resources.DelimitedFieldParser_ParseChar_ValueNonWhiteSpaceCharAfterQuotesClosed;
                                throw new FtSerializationException(error, field, message);
                            }
                        }

                        break;

                    default:
                        throw FtInternalException.Create(InternalError.DelimitedFieldParser_ParseChar_UnsupportedQuotedState, quotedState.ToString());
                    }
                }
            }
        }