From 99388c5355f42043178e6391a66d19840764b0b2 Mon Sep 17 00:00:00 2001
From: Adam Radziszewski <adam.radziszewski@pwr.wroc.pl>
Date: Fri, 20 May 2011 14:48:45 +0200
Subject: [PATCH] add has_more to readers

---
 libcorpus2/io/reader.cpp    | 16 ++++++++++++++++
 libcorpus2/io/reader.h      | 11 +++++++++++
 swig/libcorpustokenreader.i |  1 +
 3 files changed, 28 insertions(+)

diff --git a/libcorpus2/io/reader.cpp b/libcorpus2/io/reader.cpp
index 46a158a..7dacd10 100644
--- a/libcorpus2/io/reader.cpp
+++ b/libcorpus2/io/reader.cpp
@@ -222,6 +222,22 @@ boost::shared_ptr<Chunk> BufferedChunkReader::get_next_chunk()
 	}
 }
 
+bool BufferedChunkReader::has_more()
+{
+	ensure_more();
+	return !chunk_buf_.empty();
+}
+
+bool BufferedSentenceReader::has_more()
+{
+	if (sentence_buf_ != NULL) {
+		return true;
+	}
+	sentence_buf_ = actual_next_sentence();
+	return (sentence_buf_ != NULL);
+}
+
+
 BufferedSentenceReader::BufferedSentenceReader(const Tagset& tagset)
 	: TokenReader(tagset), chunkify_(true)
 	, sentence_buf_(), token_buf_()
diff --git a/libcorpus2/io/reader.h b/libcorpus2/io/reader.h
index 417497d..e4e242f 100644
--- a/libcorpus2/io/reader.h
+++ b/libcorpus2/io/reader.h
@@ -90,6 +90,13 @@ public:
 	 */
 	virtual boost::shared_ptr<Chunk> get_next_chunk() = 0;
 
+
+	/**
+	 * Checks if there is anything left to be returned. Non-const because it
+	 * might read ahead and fill the buffer.
+	 */
+	virtual bool has_more() = 0;
+
 	/**
 	 * General option setter.
 	 */
@@ -297,6 +304,8 @@ public:
 
 	boost::shared_ptr<Chunk> get_next_chunk();
 
+	bool has_more();
+
 	void set_option(const std::string& option) {
 		TokenReader::set_option(option);
 	}
@@ -332,6 +341,8 @@ public:
 
 	Sentence::Ptr get_next_sentence();
 
+	bool has_more();
+
 	boost::shared_ptr<Chunk> get_next_chunk();
 
 	void set_option(const std::string& option) {
diff --git a/swig/libcorpustokenreader.i b/swig/libcorpustokenreader.i
index 0a367d5..c5d64d1 100644
--- a/swig/libcorpustokenreader.i
+++ b/swig/libcorpustokenreader.i
@@ -61,6 +61,7 @@ namespace Corpus2 {
     virtual Token* get_next_token() = 0;
     virtual Sentence::Ptr get_next_sentence() = 0;
     virtual boost::shared_ptr<Chunk> get_next_chunk() = 0;
+    virtual bool has_more() = 0;
 
     /* --------------------------------------------------------------------- */
     virtual void set_option(const std::string& option);
-- 
GitLab