view Implab/Formats/TextScanner.cs @ 174:983df35b3ca1 ref20160224

sync
author cin
date Fri, 18 Mar 2016 18:10:30 +0300
parents ecfece82ca11
children 96a89dcb4060
line wrap: on
line source

using System;
using Implab.Components;

namespace Implab.Formats {
    public abstract class TextScanner<TTag> : Disposable {

        readonly int[] m_buffer;
        int m_bufferOffset;
        int m_dataLength;
        int m_tokenLength;

        TTag[] m_tags;

        BufferScanner<TTag> m_scanner;

        protected bool ReadTokenInternal() {
            if (EOF)
                return false;
            
            // create a new scanner from template (scanners are value types)
            var inst = m_scanner;

            m_tokenLength = 0;

            while (inst.Scan(m_buffer, m_bufferOffset, m_dataLength)) {
                m_tokenLength += m_dataLength;

                var actual = Read(m_buffer, 0, m_buffer.Length);
                    
                m_bufferOffset = 0;
                m_dataLength = actual;

                if (actual == 0) {
                    inst.Eof();
                    break;
                }
            }

            var len = inst.Position - m_bufferOffset;
            m_tokenLength += len;
            m_dataLength -= len;
            m_bufferOffset = inst.Position;

            // save result;
        
            m_tags = inst.GetTokenTags();
        }

        protected abstract int Read(int[] buffer, int offset, int size);

        protected TTag[] Tags {
            get {
                return m_tags; 
            }
        }

        public abstract bool EOF { get; }
 
    }
}