| 1 |
#include "ifdbuf.h" |
| 2 |
#include <unistd.h> |
| 3 |
#include <ios> |
| 4 |
|
| 5 |
//CLASS |
| 6 |
class IFdSeek: public IFdStreambuf |
| 7 |
{ |
| 8 |
using pos_type = std::streambuf::pos_type; |
| 9 |
using off_type = std::streambuf::off_type; |
| 10 |
using seekdir = std::ios::seekdir; |
| 11 |
using openmode = std::ios::openmode; |
| 12 |
|
| 13 |
public: |
| 14 |
IFdSeek(int fd); |
| 15 |
private: |
| 16 |
pos_type seekoff(off_type offset, seekdir dir, openmode); |
| 17 |
pos_type seekpos(pos_type offset, openmode mode); |
| 18 |
}; |
| 19 |
//= |
| 20 |
//CONS |
| 21 |
inline IFdSeek::IFdSeek(int fd) |
| 22 |
: |
| 23 |
IFdStreambuf(fd) |
| 24 |
{} |
| 25 |
//= |
| 26 |
//SEEKOFF |
| 27 |
IFdSeek::pos_type IFdSeek::seekoff(off_type off, seekdir dir, openmode) |
| 28 |
{ |
| 29 |
pos_type pos = |
| 30 |
lseek |
| 31 |
( |
| 32 |
d_fd, off, |
| 33 |
(dir == std::ios::beg) ? SEEK_SET : |
| 34 |
(dir == std::ios::cur) ? SEEK_CUR : |
| 35 |
SEEK_END |
| 36 |
); |
| 37 |
|
| 38 |
if (pos < 0) |
| 39 |
return -1; |
| 40 |
|
| 41 |
setg(d_buffer, d_buffer + 1, d_buffer + 1); |
| 42 |
return pos; |
| 43 |
} |
| 44 |
//= |
| 45 |
//SEEKPOS |
| 46 |
inline IFdSeek::pos_type IFdSeek::seekpos(pos_type off, openmode mode) |
| 47 |
{ |
| 48 |
return seekoff(off, std::ios::beg, mode); |
| 49 |
} |
| 50 |
//= |