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

sync
author cin
date Fri, 18 Mar 2016 18:10:30 +0300
parents ecfece82ca11
children 96a89dcb4060
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
1 using System;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
2 using Implab.Components;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
3
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
4 namespace Implab.Formats {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
5 public abstract class TextScanner<TTag> : Disposable {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
6
174
cin
parents: 173
diff changeset
7 readonly int[] m_buffer;
cin
parents: 173
diff changeset
8 int m_bufferOffset;
cin
parents: 173
diff changeset
9 int m_dataLength;
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
10 int m_tokenLength;
174
cin
parents: 173
diff changeset
11
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
12 TTag[] m_tags;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
13
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
14 BufferScanner<TTag> m_scanner;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
15
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
16 protected bool ReadTokenInternal() {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
17 if (EOF)
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
18 return false;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
19
174
cin
parents: 173
diff changeset
20 // create a new scanner from template (scanners are value types)
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
21 var inst = m_scanner;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
22
174
cin
parents: 173
diff changeset
23 m_tokenLength = 0;
cin
parents: 173
diff changeset
24
cin
parents: 173
diff changeset
25 while (inst.Scan(m_buffer, m_bufferOffset, m_dataLength)) {
cin
parents: 173
diff changeset
26 m_tokenLength += m_dataLength;
cin
parents: 173
diff changeset
27
cin
parents: 173
diff changeset
28 var actual = Read(m_buffer, 0, m_buffer.Length);
cin
parents: 173
diff changeset
29
cin
parents: 173
diff changeset
30 m_bufferOffset = 0;
cin
parents: 173
diff changeset
31 m_dataLength = actual;
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
32
174
cin
parents: 173
diff changeset
33 if (actual == 0) {
cin
parents: 173
diff changeset
34 inst.Eof();
cin
parents: 173
diff changeset
35 break;
cin
parents: 173
diff changeset
36 }
cin
parents: 173
diff changeset
37 }
cin
parents: 173
diff changeset
38
cin
parents: 173
diff changeset
39 var len = inst.Position - m_bufferOffset;
cin
parents: 173
diff changeset
40 m_tokenLength += len;
cin
parents: 173
diff changeset
41 m_dataLength -= len;
cin
parents: 173
diff changeset
42 m_bufferOffset = inst.Position;
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
43
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
44 // save result;
174
cin
parents: 173
diff changeset
45
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
46 m_tags = inst.GetTokenTags();
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
47 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
48
174
cin
parents: 173
diff changeset
49 protected abstract int Read(int[] buffer, int offset, int size);
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
50
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
51 protected TTag[] Tags {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
52 get {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
53 return m_tags;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
54 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
55 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
56
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
57 public abstract bool EOF { get; }
174
cin
parents: 173
diff changeset
58
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
59 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
60 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
61