-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.hpp
More file actions
115 lines (106 loc) · 4.42 KB
/
Stream.hpp
File metadata and controls
115 lines (106 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
* libunix++: C++ wrapper for Linux system calls
* Generic stream operations
*
* © 2019—2025, Sauron <libunixpp@saur0n.science>
******************************************************************************/
#ifndef __UNIXPP_STREAM_HPP
#define __UNIXPP_STREAM_HPP
#include <initializer_list>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include "AtomicBool.hpp"
namespace upp {
/**/
class InterruptedException {};
/** Base class for all descriptor-based objects **/
class Stream {
friend class Poll;
public:
/** Standard I/O streams **/
enum Standard { IN, OUT, ERR };
/**/
Stream(Stream &&other);
/** Duplicate a file descriptor **/
Stream(const Stream &other);
/** Duplicate a file descriptor on the specified one **/
Stream(const Stream &other, int newfd);
/** Duplicate a file descriptor on the specified one **/
Stream(const Stream &other, int newfd, int flags);
/** Close **/
virtual ~Stream();
/** Reposition read/write file offset **/
off_t seek(off_t offset, int whence=SEEK_SET);
/** Returns current read/write file offset **/
off_t tell() { return seek(0, SEEK_CUR); }
/** Read from the stream **/
size_t read(void * buffer, size_t count);
/** Read from the stream **/
size_t read(AtomicBool &interrupted, void * buffer, size_t count);
/** Read from the stream at a given offset **/
size_t read(void * buffer, size_t count, off_t offset);
/** Read from the stream at a given offset **/
size_t read(AtomicBool &interrupted, void * buffer, size_t count, off_t offset);
/** Read from the stream to multiple buffers **/
size_t read(const std::initializer_list<struct iovec> &vec);
/** Read from the stream to multiple buffers **/
size_t read(AtomicBool &interrupted, const std::initializer_list<struct iovec> &vec);
/** Read from the stream to multiple buffers **/
size_t read(const std::initializer_list<struct iovec> &vec, off_t offset);
/** Read from the stream to multiple buffers **/
size_t read(AtomicBool &interrupted, const std::initializer_list<struct iovec> &vec, off_t offset);
/** Read from the stream to multiple buffers **/
size_t read(const std::initializer_list<struct iovec> &vec, off_t offset,
int flags);
/** Read from the stream to multiple buffers **/
size_t read(AtomicBool &interrupted, const std::initializer_list<struct iovec> &vec, off_t offset,
int flags);
/** Write to the stream **/
size_t write(const void * buffer, size_t count);
/** Write to the stream at a given offset **/
size_t write(const void * buffer, size_t count, off_t offset);
/** Write multiple buffers to the stream **/
size_t write(const std::initializer_list<struct iovec> &vec);
/** Write multiple buffers to the stream at a given offset **/
size_t write(const std::initializer_list<struct iovec> &vec, off_t offset);
/** Write multiple buffers to the stream at a given offset **/
size_t write(const std::initializer_list<struct iovec> &vec, off_t offset,
int flags);
/** Get file status **/
void stat(struct stat * statbuf);
/** Returns file descriptor flags **/
unsigned getFlags();
/** Set file descritor flags **/
void setFlags(unsigned flags);
/** Returns file status flags **/
unsigned getStatusFlags();
/** Set file status flags **/
void setStatusFlags(unsigned flags);
/** Returns ID of process receiving signals about events on the descriptor **/
int getOwner();
/** Write a file into the file descriptor **/
size_t sendfile(Stream &from, size_t count);
/** Write a file into the file descriptor **/
size_t sendfile(Stream &from, off_t &offset, size_t count);
/** Returns file descriptor **/
int getDescriptor() const { return fd; }
/** Returns the standard stream **/
static Stream &get(Standard no);
protected:
/** Initialize by file descriptor **/
Stream(int fd);
/** Perform an operation on file descriptor **/
unsigned ioctl(int request, void * argp);
/** Perform an operation on file descriptor **/
unsigned ioctl(int request, int argp);
/** Manipulate file descriptor **/
unsigned fcntl(int cmd);
/** Manipulate file descriptor **/
unsigned fcntl(int cmd, int arg);
private:
Stream &operator =(Stream &other)=delete;
int fd;
};
}
#endif