]> git.tdb.fi Git - libs/core.git/blob - source/io/base.cpp
Implement controls for file descriptor inheritance
[libs/core.git] / source / io / base.cpp
1 #include "base.h"
2 #include "poll.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace IO {
8
9 Base::Base():
10         mode(M_READ),
11         eof_flag(false),
12         mutex(0)
13 { }
14
15 Base::~Base()
16 {
17         signal_deleted.emit();
18         delete mutex;
19 }
20
21 void Base::check_access(Mode m) const
22 {
23         if(!(mode&m))
24                 throw invalid_access(m);
25 }
26
27 bool Base::getline(string &line)
28 {
29         line.clear();
30
31         if(eof_flag)
32                 return false;
33
34         while(!eof())
35         {
36                 int c = get();
37                 if(c==-1 || c=='\n')
38                         break;
39                 line += c;
40         }
41
42         return !eof_flag || !line.empty();
43 }
44
45 int Base::get()
46 {
47         char c;
48         if(do_read(&c, 1)==0)
49                 return -1;
50         return static_cast<unsigned char>(c);
51 }
52
53 void Base::set_eof()
54 {
55         if(!eof_flag)
56         {
57                 eof_flag = true;
58                 signal_end_of_file.emit();
59         }
60 }
61
62
63 Base::Synchronize::Synchronize(Base &i):
64         io(i)
65 {
66         if(!io.mutex)
67                 io.mutex = new Mutex;
68         io.mutex->lock();
69 }
70
71 Base::Synchronize::~Synchronize()
72 {
73         io.mutex->unlock();
74 }
75
76 } // namespace IO
77 } // namespace Msp