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 |
|
|
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 |
|
|
virtual const char *what() const throw() { return message; } |
29 |
|
|
}; |
30 |
|
|
|
31 |
|
|
inline int CheckError(int status) |
32 |
|
|
{ |
33 |
|
|
if (status == -1) |
34 |
|
|
throw Error(); |
35 |
|
|
|
36 |
|
|
return status; |
37 |
|
|
} |
38 |
|
|
|
39 |
douglas |
14 |
#define _SysCall(call) \ |
40 |
|
|
int status; \ |
41 |
|
|
\ |
42 |
|
|
while ((status = call) == -1) \ |
43 |
|
|
if (errno != EINTR) \ |
44 |
|
|
throw Error(); \ |
45 |
|
|
else \ |
46 |
|
|
continue; \ |
47 |
|
|
\ |
48 |
|
|
return status |
49 |
|
|
|
50 |
douglas |
26 |
template <typename Function_, typename Arg0_> |
51 |
|
|
int SysCall(Function_ function, Arg0_ arg0) |
52 |
|
|
{ |
53 |
|
|
_SysCall(function(arg0)); |
54 |
|
|
} |
55 |
|
|
|
56 |
douglas |
14 |
template <typename Function_, typename Arg0_, typename Arg1_> |
57 |
|
|
int SysCall(Function_ function, Arg0_ arg0, Arg1_ arg1) |
58 |
|
|
{ |
59 |
|
|
_SysCall(function(arg0, arg1)); |
60 |
douglas |
2 |
} |
61 |
|
|
|
62 |
douglas |
14 |
template <typename Function_, typename Arg0_, typename Arg1_, typename Arg2_> |
63 |
|
|
int SysCall(Function_ function, Arg0_ arg0, Arg1_ arg1, Arg2_ arg2) |
64 |
|
|
{ |
65 |
|
|
_SysCall(function(arg0, arg1, arg2)); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
#undef _SysCall |
69 |
|
|
|
70 |
|
|
int Open(const std::string &path, int flags, ::mode_t mode); |
71 |
|
|
|
72 |
|
|
inline int Open(const std::string &path, int flags) |
73 |
|
|
{ |
74 |
|
|
return Open(path, flags, 0); |
75 |
|
|
} |
76 |
|
|
|
77 |
douglas |
26 |
void Close(int fd); |
78 |
|
|
|
79 |
douglas |
14 |
size_t Read(int fd, void *buffer, size_t bytes); |
80 |
|
|
size_t ReadMost(int fd, void *buffer, size_t bytes); |
81 |
douglas |
26 |
size_t Write(int fd, const void *buffer, size_t bytes); |
82 |
douglas |
14 |
|
83 |
|
|
} |
84 |
|
|
|
85 |
douglas |
2 |
#endif//_posix_hpp_ |