1 |
douglas |
2 |
// 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 |
douglas |
14 |
#include <string> |
14 |
douglas |
2 |
|
15 |
douglas |
35 |
#include <signal.h> |
16 |
|
|
|
17 |
douglas |
2 |
namespace Posix |
18 |
|
|
{ |
19 |
|
|
|
20 |
|
|
inline int CheckError(int status); |
21 |
|
|
|
22 |
|
|
class Error : public std::exception |
23 |
|
|
{ |
24 |
|
|
int code; |
25 |
|
|
char message[1024]; |
26 |
|
|
|
27 |
|
|
public: |
28 |
|
|
Error(); |
29 |
douglas |
31 |
Error(int code); |
30 |
douglas |
2 |
virtual ~Error() throw() {} |
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 |
douglas |
14 |
#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 |
douglas |
26 |
template <typename Function_, typename Arg0_> |
54 |
|
|
int SysCall(Function_ function, Arg0_ arg0) |
55 |
|
|
{ |
56 |
|
|
_SysCall(function(arg0)); |
57 |
|
|
} |
58 |
|
|
|
59 |
douglas |
14 |
template <typename Function_, typename Arg0_, typename Arg1_> |
60 |
|
|
int SysCall(Function_ function, Arg0_ arg0, Arg1_ arg1) |
61 |
|
|
{ |
62 |
|
|
_SysCall(function(arg0, arg1)); |
63 |
douglas |
2 |
} |
64 |
|
|
|
65 |
douglas |
14 |
template <typename Function_, typename Arg0_, typename Arg1_, typename Arg2_> |
66 |
|
|
int SysCall(Function_ function, Arg0_ arg0, Arg1_ arg1, Arg2_ arg2) |
67 |
|
|
{ |
68 |
|
|
_SysCall(function(arg0, arg1, arg2)); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
#undef _SysCall |
72 |
|
|
|
73 |
|
|
int Open(const std::string &path, int flags, ::mode_t mode); |
74 |
|
|
|
75 |
|
|
inline int Open(const std::string &path, int flags) |
76 |
|
|
{ |
77 |
|
|
return Open(path, flags, 0); |
78 |
|
|
} |
79 |
|
|
|
80 |
douglas |
26 |
void Close(int fd); |
81 |
|
|
|
82 |
douglas |
14 |
size_t Read(int fd, void *buffer, size_t bytes); |
83 |
|
|
size_t ReadMost(int fd, void *buffer, size_t bytes); |
84 |
douglas |
26 |
size_t Write(int fd, const void *buffer, size_t bytes); |
85 |
douglas |
35 |
int SigAction(int signal, const struct sigaction *action, struct sigaction *old); |
86 |
|
|
int SigEmptySet(sigset_t *set); |
87 |
|
|
int SigFillSet(sigset_t *set); |
88 |
|
|
int SigAddSet(sigset_t *set, int signal); |
89 |
|
|
int SigDelSet(sigset_t *set, int signal); |
90 |
|
|
int SigIsMember(const sigset_t *set, int signal); |
91 |
douglas |
14 |
|
92 |
|
|
} |
93 |
|
|
|
94 |
douglas |
2 |
#endif//_posix_hpp_ |