blob: f9fbe9b10febb7ad17368d779f87404ab5c96da4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
*
* Distributable under the terms of either the Apache License (Version 2.0) or
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#include "CLucene/StdHeader.h"
#include "FastCharStream.h"
#include "CLucene/util/Reader.h"
CL_NS_DEF(util)
const int32_t FastCharStream::maxRewindSize = LUCENE_MAX_WORD_LEN*2;
FastCharStream::FastCharStream(Reader* reader):
pos(0),
rewindPos(0),
resetPos(0),
col(1),
line(1),
input(reader)
{
input->mark(maxRewindSize);
}
FastCharStream::~FastCharStream(){
}
void FastCharStream::readChar(TCHAR &c) {
try{
int32_t r = input->read();
if ( r == -1 )
input = NULL;
c = r;
}catch(CLuceneError& err){
if ( err.number() == CL_ERR_IO )
input = 0;
throw err;
}
}
int FastCharStream::GetNext()
{
// printf("getnext\n");
if (input == 0 ) // end of file
{
_CLTHROWA(CL_ERR_IO,"warning : FileReader.GetNext : Read TCHAR over EOS.");
}
// this is rather inefficient
// implementing the functions from the java version of
// charstream will be much more efficient.
++pos;
if ( pos > resetPos + maxRewindSize && rewindPos == 0) {
// move the marker one position (~expensive)
resetPos = pos-(maxRewindSize/2);
if ( resetPos != input->reset(resetPos) )
_CLTHROWA(CL_ERR_IO,"Unexpected reset() result");
input->mark(maxRewindSize);
input->skip((maxRewindSize/2) - 1);
}
TCHAR ch;
readChar(ch);
if (input == NULL) { // eof
return -1;
}
if (rewindPos == 0) {
col += 1;
if(ch == '\n') {
line++;
col = 1;
}
} else {
rewindPos--;
}
return ch;
}
void FastCharStream::UnGet(){
// printf("UnGet \n");
if (input == 0)
return;
if ( pos == 0 ) {
_CLTHROWA(CL_ERR_IO,"error : No character can be UnGet");
}
rewindPos++;
input->reset(pos-1);
pos--;
}
int FastCharStream::Peek() {
int c = GetNext();
UnGet();
return c;
}
bool FastCharStream::Eos() const {
return input==NULL;
}
int32_t FastCharStream::Column() const {
return col;
}
int32_t FastCharStream::Line() const {
return line;
}
CL_NS_END
|