From: Mikko Rasa Date: Wed, 15 Jun 2011 08:11:57 +0000 (+0300) Subject: Unify end-of-file handling X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=82d74297d5b469b0a506d7010a84ab5115cd88ee Unify end-of-file handling --- 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