]> git.tdb.fi Git - libs/core.git/blob - source/io/base.cpp
95a3685fc34021322b800e185e7a52c9343d8521
[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 const Handle &Base::get_handle(Mode)
63 {
64         throw logic_error("Base::get_handle");
65 }
66
67
68 Base::Synchronize::Synchronize(Base &i):
69         io(i)
70 {
71         if(!io.mutex)
72                 io.mutex = new Mutex;
73         io.mutex->lock();
74 }
75
76 Base::Synchronize::~Synchronize()
77 {
78         io.mutex->unlock();
79 }
80
81 } // namespace IO
82 } // namespace Msp