summaryrefslogtreecommitdiff
path: root/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-18 14:38:30 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-18 14:38:30 +0100
commit9ad076178a4bb13436e2edf90e62a26fb2cf32ea (patch)
tree478cedcbcc44e7e98348d67787216c25a318b3dc /LibGUI
parent8e3d0a23d555b2464aba5a07a90092535cec19e1 (diff)
downloadserenity-9ad076178a4bb13436e2edf90e62a26fb2cf32ea.zip
GIODevice: Add a read_all() that returns a ByteBuffer with all we can read.
Use this to implement file opening in TextEditor.
Diffstat (limited to 'LibGUI')
-rw-r--r--LibGUI/GIODevice.cpp33
-rw-r--r--LibGUI/GIODevice.h2
2 files changed, 33 insertions, 2 deletions
diff --git a/LibGUI/GIODevice.cpp b/LibGUI/GIODevice.cpp
index 0b0f8de1d1..7be5bcd09f 100644
--- a/LibGUI/GIODevice.cpp
+++ b/LibGUI/GIODevice.cpp
@@ -46,7 +46,7 @@ ByteBuffer GIODevice::read(int max_size)
return buffer;
}
-bool GIODevice::can_read() const
+bool GIODevice::can_read_from_fd() const
{
// FIXME: Can we somehow remove this once GSocket is implemented using non-blocking sockets?
fd_set rfds;
@@ -68,12 +68,41 @@ bool GIODevice::can_read_line()
return true;
if (m_buffered_data.contains_slow('\n'))
return true;
- if (!can_read())
+ if (!can_read_from_fd())
return false;
populate_read_buffer();
return m_buffered_data.contains_slow('\n');
}
+bool GIODevice::can_read() const
+{
+ return !m_buffered_data.is_empty() || can_read_from_fd();
+}
+
+ByteBuffer GIODevice::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 GIODevice::read_line(int max_size)
{
if (m_fd < 0)
diff --git a/LibGUI/GIODevice.h b/LibGUI/GIODevice.h
index d7d7cd094c..d2aca72b06 100644
--- a/LibGUI/GIODevice.h
+++ b/LibGUI/GIODevice.h
@@ -28,6 +28,7 @@ public:
ByteBuffer read(int max_size);
ByteBuffer read_line(int max_size);
+ ByteBuffer read_all();
// FIXME: I would like this to be const but currently it needs to call populate_read_buffer().
bool can_read_line();
@@ -49,6 +50,7 @@ protected:
private:
bool populate_read_buffer();
+ bool can_read_from_fd() const;
int m_fd { -1 };
int m_error { 0 };