]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/base.cpp
Guard against badly behaving get functions in getline
[libs/core.git] / source / io / base.cpp
index 48de3ee56ca8e3fc304f5d4caca2fcf8a57b8a9a..95a3685fc34021322b800e185e7a52c9343d8521 100644 (file)
@@ -8,13 +8,20 @@ namespace IO {
 
 Base::Base():
        mode(M_READ),
-       events(P_NONE),
-       eof_flag(false)
+       eof_flag(false),
+       mutex(0)
 { }
 
 Base::~Base()
 {
        signal_deleted.emit();
+       delete mutex;
+}
+
+void Base::check_access(Mode m) const
+{
+       if(!(mode&m))
+               throw invalid_access(m);
 }
 
 bool Base::getline(string &line)
@@ -24,7 +31,7 @@ bool Base::getline(string &line)
        if(eof_flag)
                return false;
 
-       while(1)
+       while(!eof())
        {
                int c = get();
                if(c==-1 || c=='\n')
@@ -43,18 +50,32 @@ int Base::get()
        return static_cast<unsigned char>(c);
 }
 
-void Base::set_events(PollEvent e)
+void Base::set_eof()
 {
-       events = e;
-       signal_events_changed.emit(events);
+       if(!eof_flag)
+       {
+               eof_flag = true;
+               signal_end_of_file.emit();
+       }
 }
 
-void Base::event(PollEvent ev)
+const Handle &Base::get_handle(Mode)
 {
-       if(ev&P_INPUT)
-               signal_data_available.emit();
+       throw logic_error("Base::get_handle");
+}
 
-       on_event(ev);
+
+Base::Synchronize::Synchronize(Base &i):
+       io(i)
+{
+       if(!io.mutex)
+               io.mutex = new Mutex;
+       io.mutex->lock();
+}
+
+Base::Synchronize::~Synchronize()
+{
+       io.mutex->unlock();
 }
 
 } // namespace IO