From 82d74297d5b469b0a506d7010a84ab5115cd88ee Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 15 Jun 2011 11:11:57 +0300 Subject: [PATCH] Unify end-of-file handling --- source/io/base.cpp | 9 +++++++++ source/io/base.h | 4 ++++ source/io/console.cpp | 2 +- source/io/file.cpp | 5 +---- source/io/memory.cpp | 4 ++-- source/io/pipe.cpp | 6 +----- source/io/serial.cpp | 6 +++++- 7 files changed, 23 insertions(+), 13 deletions(-) diff --git a/source/io/base.cpp b/source/io/base.cpp index fe706fd..bf4c820 100644 --- a/source/io/base.cpp +++ b/source/io/base.cpp @@ -48,5 +48,14 @@ int Base::get() return static_cast(c); } +void Base::set_eof() +{ + if(!eof_flag) + { + eof_flag = true; + signal_end_of_file.emit(); + } +} + } // namespace IO } // namespace Msp diff --git a/source/io/base.h b/source/io/base.h index 9447c66..700dae7 100644 --- a/source/io/base.h +++ b/source/io/base.h @@ -85,6 +85,10 @@ public: to end-of-file or non-blocking operation. */ virtual int get(); +protected: + void set_eof(); + +public: /** Returns the end-of-file flag. */ bool eof() const { return eof_flag; } }; diff --git a/source/io/console.cpp b/source/io/console.cpp index b695e3c..4d686db 100644 --- a/source/io/console.cpp +++ b/source/io/console.cpp @@ -128,7 +128,7 @@ unsigned Console::do_read(char *buf, unsigned len) unsigned ret = sys_read(handle, buf, len); if(ret==0) - eof_flag = true; + set_eof(); return ret; } diff --git a/source/io/file.cpp b/source/io/file.cpp index cc3f0a9..9032609 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -126,10 +126,7 @@ unsigned File::do_read(char *buf, unsigned size) unsigned ret = sys_read(handle, buf, size); if(ret==0) - { - eof_flag = true; - signal_end_of_file.emit(); - } + set_eof(); return ret; } diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 6ccf846..fa8f905 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -51,7 +51,7 @@ unsigned Memory::do_read(char *buf, unsigned size) { if(pos==end) { - eof_flag = true; + set_eof(); return 0; } @@ -81,7 +81,7 @@ int Memory::get() { if(pos==end) { - eof_flag = true; + set_eof(); return -1; } diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 2e37f11..d8de5df 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -78,12 +78,8 @@ unsigned Pipe::do_read(char *buf, unsigned size) return 0; unsigned ret = reader.read(buf, size); - if(ret==0) - { - eof_flag = true; - signal_end_of_file.emit(); - } + set_eof(); return ret; } diff --git a/source/io/serial.cpp b/source/io/serial.cpp index 5d335b6..ac4f2b7 100644 --- a/source/io/serial.cpp +++ b/source/io/serial.cpp @@ -292,7 +292,11 @@ unsigned Serial::do_read(char *buf, unsigned size) if(size==0) return 0; - return reader.read(buf, size); + unsigned ret = reader.read(buf, size); + if(ret==0) + set_eof(); + + return ret; } } // namespace IO -- 2.45.2