]> git.tdb.fi Git - libs/core.git/commitdiff
Unify end-of-file handling
authorMikko Rasa <tdb@tdb.fi>
Wed, 15 Jun 2011 08:11:57 +0000 (11:11 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 18 Jun 2011 18:36:06 +0000 (21:36 +0300)
source/io/base.cpp
source/io/base.h
source/io/console.cpp
source/io/file.cpp
source/io/memory.cpp
source/io/pipe.cpp
source/io/serial.cpp

index fe706fd5b3cf5cad6b7c5b52e5f229992d5ac933..bf4c82018dc60d9a6d4ddd18be350468f9a4c0a0 100644 (file)
@@ -48,5 +48,14 @@ int Base::get()
        return static_cast<unsigned char>(c);
 }
 
+void Base::set_eof()
+{
+       if(!eof_flag)
+       {
+               eof_flag = true;
+               signal_end_of_file.emit();
+       }
+}
+
 } // namespace IO
 } // namespace Msp
index 9447c66fc9a6551dcdcb5644c9333b882de7dcec..700dae7646180266d5393991e2b4d799ac7c1d2c 100644 (file)
@@ -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; }
 };
index b695e3c33a90c14bfe521b1e57ad3dc3c08390d1..4d686db1aafb5ec33dc74ad2cd212a5a84d0c853 100644 (file)
@@ -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;
 }
index cc3f0a9e4da52d40fc9893efb984801b1a09d672..90326093b8b643751446d83658a4b2583dbf3fbb 100644 (file)
@@ -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;
 }
index 6ccf84654764cbe8086a3ac841330580d302dcd1..fa8f9053de029e3965f1fd4c261a43e6c4a69c1c 100644 (file)
@@ -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;
        }
 
index 2e37f119aeaf3dc67726341d2047cc30748044d3..d8de5df2745ca60a1ad0f74912d767435b2f5a4a 100644 (file)
@@ -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;
 }
index 5d335b6faa82e0226c043d14ba3a158e8a84d8ae..ac4f2b764d8666332de5cf932e62e66cd5d764b2 100644 (file)
@@ -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