summaryrefslogtreecommitdiff
path: root/src/assistant/3rdparty/clucene/src/CLucene/util/Reader.h
blob: 6b018b3aa5364cdf035b20a78537ef988b2ec206 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*------------------------------------------------------------------------------
* 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.
------------------------------------------------------------------------------*/
#ifndef _lucene_util_Reader_
#define _lucene_util_Reader_

#if defined(_LUCENE_PRAGMA_ONCE)
# pragma once
#endif

#include "streambase.h"
#include "stringreader.h"
#include "fileinputstream.h"
#include "bufferedstream.h"

CL_NS_DEF(util)
/**
* An inline wrapper that reads from Jos van den Oever's jstreams
*/
class Reader:LUCENE_BASE {
typedef jstreams::StreamBase<TCHAR> jsReader;
public:
	bool deleteReader;
	jsReader* reader;

	Reader(jsReader* reader, bool deleteReader){
		this->reader = reader;
		this->deleteReader = deleteReader;
	}
	virtual ~Reader(){
		if ( deleteReader )
			delete reader;
		reader = NULL;
	}
	inline int read(){
		const TCHAR*b;
		int32_t nread = reader->read(b, 1,1);
		if ( nread < -1 ) //if not eof
			_CLTHROWA(CL_ERR_IO,reader->getError() );
		else if ( nread == -1 )
			return -1;
		else
			return b[0];
	}
	/**
	* Read at least 1 character, and as much as is conveniently available
	*/
	inline int32_t read(const TCHAR*& start){
		int32_t nread = reader->read(start,1,0);
		if ( nread < -1 ) //if not eof
			_CLTHROWA(CL_ERR_IO,reader->getError());
		else
			return nread;
	}
	inline int32_t read(const TCHAR*& start, int32_t len){
		int32_t nread = reader->read(start, len, len);
		if ( nread < -1 ) //if not eof
			_CLTHROWA(CL_ERR_IO,reader->getError());
		else
			return nread;
	}
	inline int64_t skip(int64_t ntoskip){
		int64_t skipped = reader->skip(ntoskip);
		if ( skipped < 0 ) 
			_CLTHROWA(CL_ERR_IO,reader->getError());
		else
			return skipped;
	}
	inline int64_t mark(int32_t readAheadlimit){
		int64_t pos = reader->mark(readAheadlimit);
		if ( pos < 0 )
			_CLTHROWA(CL_ERR_IO,reader->getError());
		else
			return pos;
	}
	int64_t reset(int64_t pos){
		int64_t r = reader->reset(pos);
		if ( r < 0 )
			_CLTHROWA(CL_ERR_IO,reader->getError());
		else
			return r;
	}
};

///A helper class which constructs a the jstreams StringReader.
class StringReader: public Reader{
public:
	StringReader ( const TCHAR* value );
    StringReader ( const TCHAR* value, const int32_t length );
    StringReader ( const TCHAR* value, const int32_t length, bool copyData );
	~StringReader();
};

/** A very simple inputstreamreader implementation. For a
* more complete InputStreamReader, use the jstreams version
* located in the contrib package
*/
class SimpleInputStreamReader: public jstreams::BufferedInputStream<TCHAR>{
    int32_t decode(TCHAR* start, int32_t space);
	int encoding;
	enum{
		ASCII=1,
		UTF8=2,
		UCS2_LE=3
	};
    bool finishedDecoding;
    jstreams::StreamBase<char>* input;
    int32_t charsLeft;

    jstreams::InputStreamBuffer<char> charbuf;
    int32_t fillBuffer(TCHAR* start, int32_t space);
public:
	SimpleInputStreamReader(jstreams::StreamBase<char> *i, const char* encoding);
	~SimpleInputStreamReader();
};

/**
* A helper class which constructs a FileReader with a specified
* simple encodings, or a given inputstreamreader
*/
class FileReader: public Reader{
	jstreams::FileInputStream* input;
public:
	FileReader ( const char* path, const char* enc,
        const int32_t cachelen = 13,
        const int32_t cachebuff = 14 ); //todo: optimise these cache values
	~FileReader ();

    int32_t read(const TCHAR*& start, int32_t _min, int32_t _max);
    int64_t mark(int32_t readlimit);
    int64_t reset(int64_t);
};

CL_NS_END
#endif