1 |
// Truck Computer Dooom! |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#ifndef _posix_hpp_ |
8 |
#define _posix_hpp_ |
9 |
|
10 |
#include <cerrno> |
11 |
#include <cstring> |
12 |
#include <exception> |
13 |
#include <string> |
14 |
|
15 |
namespace Posix |
16 |
{ |
17 |
|
18 |
inline int CheckError(int status); |
19 |
|
20 |
class Error : public std::exception |
21 |
{ |
22 |
int code; |
23 |
char message[1024]; |
24 |
|
25 |
public: |
26 |
Error(); |
27 |
virtual ~Error() throw() {} |
28 |
|
29 |
virtual int GetCode() const { return code; } |
30 |
virtual std::string GetMessage() const { return message; } |
31 |
virtual const char *what() const throw() { return message; } |
32 |
}; |
33 |
|
34 |
inline int CheckError(int status) |
35 |
{ |
36 |
if (status == -1) |
37 |
throw Error(); |
38 |
|
39 |
return status; |
40 |
} |
41 |
|
42 |
#define _SysCall(call) \ |
43 |
int status; \ |
44 |
\ |
45 |
while ((status = call) == -1) \ |
46 |
if (errno != EINTR) \ |
47 |
throw Error(); \ |
48 |
else \ |
49 |
continue; \ |
50 |
\ |
51 |
return status |
52 |
|
53 |
template <typename Function_, typename Arg0_, typename Arg1_> |
54 |
int SysCall(Function_ function, Arg0_ arg0, Arg1_ arg1) |
55 |
{ |
56 |
_SysCall(function(arg0, arg1)); |
57 |
} |
58 |
|
59 |
template <typename Function_, typename Arg0_, typename Arg1_, typename Arg2_> |
60 |
int SysCall(Function_ function, Arg0_ arg0, Arg1_ arg1, Arg2_ arg2) |
61 |
{ |
62 |
_SysCall(function(arg0, arg1, arg2)); |
63 |
} |
64 |
|
65 |
#undef _SysCall |
66 |
|
67 |
int Open(const std::string &path, int flags, ::mode_t mode); |
68 |
|
69 |
inline int Open(const std::string &path, int flags) |
70 |
{ |
71 |
return Open(path, flags, 0); |
72 |
} |
73 |
|
74 |
size_t Read(int fd, void *buffer, size_t bytes); |
75 |
size_t ReadMost(int fd, void *buffer, size_t bytes); |
76 |
|
77 |
} |
78 |
|
79 |
#endif//_posix_hpp_ |