summaryrefslogtreecommitdiff
path: root/LibCore/CIODevice.cpp
blob: a85efc6b73ffaf612d83e3f6727a05e373d89dae (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <LibCore/CIODevice.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdio.h>

CIODevice::CIODevice(CObject* parent)
    : CObject(parent)
{
}

CIODevice::~CIODevice()
{
}

const char* CIODevice::error_string() const
{
    return strerror(m_error);
}

ByteBuffer CIODevice::read(int max_size)
{
    if (m_fd < 0)
        return { };
    if (!max_size)
        return { };
    auto buffer = ByteBuffer::create_uninitialized(max_size);
    auto* buffer_ptr = (char*)buffer.pointer();
    int remaining_buffer_space = buffer.size();
    if (!m_buffered_data.is_empty()) {
        int taken_from_buffered = min(remaining_buffer_space, m_buffered_data.size());
        memcpy(buffer_ptr, m_buffered_data.data(), taken_from_buffered);
        Vector<byte> new_buffered_data;
        new_buffered_data.append(m_buffered_data.data() + taken_from_buffered, m_buffered_data.size() - taken_from_buffered);
        m_buffered_data = move(new_buffered_data);
        remaining_buffer_space -= taken_from_buffered;
        buffer_ptr += taken_from_buffered;
    }
    if (!remaining_buffer_space)
        return buffer;
    int nread = ::read(m_fd, buffer_ptr, remaining_buffer_space);
    if (nread < 0) {
        set_error(errno);
        return { };
    }
    if (nread == 0) {
        set_eof(true);
        return { };
    }
    buffer.trim(nread);
    return buffer;
}

bool CIODevice::can_read_from_fd() const
{
    // FIXME: Can we somehow remove this once CSocket is implemented using non-blocking sockets?
    fd_set rfds;
    FD_ZERO(&rfds);
    FD_SET(m_fd, &rfds);
    struct timeval timeout { 0, 0 };
    int rc = select(m_fd + 1, &rfds, nullptr, nullptr, &timeout);
    if (rc < 0) {
        // NOTE: We don't set m_error here.
        perror("CIODevice::can_read: select");
        return false;
    }
    return FD_ISSET(m_fd, &rfds);
}

bool CIODevice::can_read_line()
{
    if (m_eof && !m_buffered_data.is_empty())
        return true;
    if (m_buffered_data.contains_slow('\n'))
        return true;
    if (!can_read_from_fd())
        return false;
    populate_read_buffer();
    return m_buffered_data.contains_slow('\n');
}

bool CIODevice::can_read() const
{
    return !m_buffered_data.is_empty() || can_read_from_fd();
}

ByteBuffer CIODevice::read_all()
{
    ByteBuffer buffer;
    if (!m_buffered_data.is_empty()) {
        buffer = ByteBuffer::copy(m_buffered_data.data(), m_buffered_data.size());
        m_buffered_data.clear();
    }

    while (can_read_from_fd()) {
        char read_buffer[4096];
        int nread = ::read(m_fd, read_buffer, sizeof(read_buffer));
        if (nread < 0) {
            set_error(nread);
            return buffer;
        }
        if (nread == 0) {
            set_eof(true);
            break;
        }
        buffer.append(read_buffer, nread);
    }
    return buffer;
}

ByteBuffer CIODevice::read_line(int max_size)
{
    if (m_fd < 0)
        return { };
    if (!max_size)
        return { };
    if (!can_read_line())
        return { };
    if (m_eof) {
        if (m_buffered_data.size() > max_size) {
            printf("CIODevice::read_line: At EOF but there's more than max_size(%d) buffered\n", max_size);
            return { };
        }
        auto buffer = ByteBuffer::copy(m_buffered_data.data(), m_buffered_data.size());
        m_buffered_data.clear();
        return buffer;
    }
    auto line = ByteBuffer::create_uninitialized(max_size + 1);
    int line_index = 0;
    while (line_index < max_size) {
        byte ch = m_buffered_data[line_index];
        line[line_index++] = ch;
        if (ch == '\n') {
            Vector<byte> new_buffered_data;
            new_buffered_data.append(m_buffered_data.data() + line_index, m_buffered_data.size() - line_index);
            m_buffered_data = move(new_buffered_data);
            line[line_index] = '\0';
            line.trim(line_index + 1);
            return line;
        }
    }
    return { };
}

bool CIODevice::populate_read_buffer()
{
    if (m_fd < 0)
        return false;
    auto buffer = ByteBuffer::create_uninitialized(PAGE_SIZE);
    int nread = ::read(m_fd, buffer.pointer(), buffer.size());
    if (nread < 0) {
        set_error(errno);
        return false;
    }
    if (nread == 0) {
        set_eof(true);
        return false;
    }
    buffer.trim(nread);
    m_buffered_data.append(buffer.pointer(), buffer.size());
    return true;
}

bool CIODevice::close()
{
    if (fd() < 0 || mode() == NotOpen)
        return false;
    int rc = ::close(fd());
    if (rc < 0) {
        set_error(rc);
        return false;
    }
    set_fd(-1);
    set_mode(CIODevice::NotOpen);
    return true;
}

bool CIODevice::seek(signed_qword offset)
{
    int rc = lseek(m_fd, offset, SEEK_SET);
    if (rc < 0) {
        perror("CIODevice::seek: lseek");
        return false;
    }
    m_buffered_data.clear();
    return true;
}