Skip to content

Commit

Permalink
LibCore: Move GIODevice hierarchy from LibGUI to LibCore.
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Apr 10, 2019
1 parent fc1d307 commit cfd6e6c
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 112 deletions.
2 changes: 1 addition & 1 deletion Applications/IRCClient/IRCClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ IRCClient::IRCClient()
, m_client_window_list_model(IRCWindowListModel::create(*this))
, m_log(IRCLogBuffer::create())
{
m_socket = new GTCPSocket(this);
m_socket = new CTCPSocket(this);
}

IRCClient::~IRCClient()
Expand Down
4 changes: 2 additions & 2 deletions Applications/IRCClient/IRCClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <AK/HashMap.h>
#include <AK/CircularQueue.h>
#include <AK/Function.h>
#include <LibGUI/GTCPSocket.h>
#include <LibCore/CTCPSocket.h>
#include "IRCLogBuffer.h"
#include "IRCWindow.h"

Expand Down Expand Up @@ -114,7 +114,7 @@ class IRCClient final : public CObject {
String m_hostname;
int m_port { 6667 };

GTCPSocket* m_socket { nullptr };
CTCPSocket* m_socket { nullptr };

String m_nickname;
Vector<char> m_line_buffer;
Expand Down
6 changes: 3 additions & 3 deletions Applications/ProcessManager/ProcessModel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "ProcessModel.h"
#include <LibGUI/GFile.h>
#include <LibCore/CFile.h>
#include <fcntl.h>
#include <stdio.h>
#include <pwd.h>
Expand Down Expand Up @@ -125,8 +125,8 @@ GVariant ProcessModel::data(const GModelIndex& index, Role role) const

void ProcessModel::update()
{
GFile file("/proc/all");
if (!file.open(GIODevice::ReadOnly)) {
CFile file("/proc/all");
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "ProcessManager: Failed to open /proc/all: %s\n", file.error_string());
exit(1);
return;
Expand Down
6 changes: 3 additions & 3 deletions Applications/TextEditor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <LibGUI/GTextEditor.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GFontDatabase.h>
#include <LibGUI/GFile.h>
#include <LibCore/CFile.h>
#include <AK/StringBuilder.h>
#include <unistd.h>
#include <stdio.h>
Expand All @@ -35,9 +35,9 @@ int main(int argc, char** argv)
String path = "/tmp/TextEditor.save.txt";
if (argc >= 2) {
path = argv[1];
GFile file(path);
CFile file(path);

if (!file.open(GIODevice::ReadOnly)) {
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "Opening %s: %s\n", path.characters(), file.error_string());
return 1;
}
Expand Down
20 changes: 10 additions & 10 deletions LibGUI/GFile.cpp → LibCore/CFile.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
#include <LibGUI/GFile.h>
#include <LibCore/CFile.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

GFile::GFile(const String& filename)
CFile::CFile(const String& filename)
: m_filename(filename)
{
}

GFile::~GFile()
CFile::~CFile()
{
if (mode() != NotOpen)
close();
}

bool GFile::open(GIODevice::OpenMode mode)
bool CFile::open(CIODevice::OpenMode mode)
{
int flags = 0;
if ((mode & GIODevice::ReadWrite) == GIODevice::ReadWrite) {
if ((mode & CIODevice::ReadWrite) == CIODevice::ReadWrite) {
flags |= O_RDWR | O_CREAT;
} else if (mode & GIODevice::ReadOnly) {
} else if (mode & CIODevice::ReadOnly) {
flags |= O_RDONLY;
} else if (mode & GIODevice::WriteOnly) {
} else if (mode & CIODevice::WriteOnly) {
flags |= O_WRONLY | O_CREAT;
}
if (mode & GIODevice::Append)
if (mode & CIODevice::Append)
flags |= O_APPEND;
if (mode & GIODevice::Truncate)
if (mode & CIODevice::Truncate)
flags |= O_TRUNC;
if (mode & GIODevice::MustBeNew)
if (mode & CIODevice::MustBeNew)
flags |= O_EXCL;
int fd = ::open(m_filename.characters(), flags, 0666);
if (fd < 0) {
Expand Down
21 changes: 21 additions & 0 deletions LibCore/CFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <LibCore/CIODevice.h>
#include <AK/AKString.h>

class CFile final : public CIODevice {
public:
CFile() { }
explicit CFile(const String&);
virtual ~CFile() override;

String filename() const { return m_filename; }
void set_filename(const String& filename) { m_filename = filename; }

virtual bool open(CIODevice::OpenMode) override;

virtual const char* class_name() const override { return "CFile"; }

private:
String m_filename;
};
32 changes: 16 additions & 16 deletions LibGUI/GIODevice.cpp → LibCore/CIODevice.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include <LibGUI/GIODevice.h>
#include <LibCore/CIODevice.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdio.h>

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

GIODevice::~GIODevice()
CIODevice::~CIODevice()
{
}

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

ByteBuffer GIODevice::read(int max_size)
ByteBuffer CIODevice::read(int max_size)
{
if (m_fd < 0)
return { };
Expand Down Expand Up @@ -50,23 +50,23 @@ ByteBuffer GIODevice::read(int max_size)
return buffer;
}

bool GIODevice::can_read_from_fd() const
bool CIODevice::can_read_from_fd() const
{
// FIXME: Can we somehow remove this once GSocket is implemented using non-blocking sockets?
// 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("GIODevice::can_read: select");
perror("CIODevice::can_read: select");
return false;
}
return FD_ISSET(m_fd, &rfds);
}

bool GIODevice::can_read_line()
bool CIODevice::can_read_line()
{
if (m_eof && !m_buffered_data.is_empty())
return true;
Expand All @@ -78,12 +78,12 @@ bool GIODevice::can_read_line()
return m_buffered_data.contains_slow('\n');
}

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

ByteBuffer GIODevice::read_all()
ByteBuffer CIODevice::read_all()
{
ByteBuffer buffer;
if (!m_buffered_data.is_empty()) {
Expand All @@ -107,7 +107,7 @@ ByteBuffer GIODevice::read_all()
return buffer;
}

ByteBuffer GIODevice::read_line(int max_size)
ByteBuffer CIODevice::read_line(int max_size)
{
if (m_fd < 0)
return { };
Expand All @@ -117,7 +117,7 @@ ByteBuffer GIODevice::read_line(int max_size)
return { };
if (m_eof) {
if (m_buffered_data.size() > max_size) {
printf("GIODevice::read_line: At EOF but there's more than max_size(%d) buffered\n", 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());
Expand All @@ -141,7 +141,7 @@ ByteBuffer GIODevice::read_line(int max_size)
return { };
}

bool GIODevice::populate_read_buffer()
bool CIODevice::populate_read_buffer()
{
if (m_fd < 0)
return false;
Expand All @@ -160,7 +160,7 @@ bool GIODevice::populate_read_buffer()
return true;
}

bool GIODevice::close()
bool CIODevice::close()
{
if (fd() < 0 || mode() == NotOpen)
return false;
Expand All @@ -170,6 +170,6 @@ bool GIODevice::close()
return false;
}
set_fd(-1);
set_mode(GIODevice::NotOpen);
set_mode(CIODevice::NotOpen);
return true;
}
10 changes: 5 additions & 5 deletions LibGUI/GIODevice.h → LibCore/CIODevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <LibCore/CObject.h>
#include <AK/ByteBuffer.h>

class GIODevice : public CObject {
class CIODevice : public CObject {
public:
enum OpenMode {
NotOpen = 0,
Expand All @@ -15,7 +15,7 @@ class GIODevice : public CObject {
MustBeNew = 16,
};

virtual ~GIODevice() override;
virtual ~CIODevice() override;

int fd() const { return m_fd; }
unsigned mode() const { return m_mode; }
Expand All @@ -35,13 +35,13 @@ class GIODevice : public CObject {

bool can_read() const;

virtual bool open(GIODevice::OpenMode) = 0;
virtual bool open(CIODevice::OpenMode) = 0;
virtual bool close();

virtual const char* class_name() const override { return "GIODevice"; }
virtual const char* class_name() const override { return "CIODevice"; }

protected:
explicit GIODevice(CObject* parent = nullptr);
explicit CIODevice(CObject* parent = nullptr);

void set_fd(int fd) { m_fd = fd; }
void set_mode(OpenMode mode) { m_mode = mode; }
Expand Down
24 changes: 12 additions & 12 deletions LibGUI/GSocket.cpp → LibCore/CSocket.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <LibGUI/GSocket.h>
#include <LibCore/CSocket.h>
#include <LibCore/CNotifier.h>
#include <sys/socket.h>
#include <netinet/in.h>
Expand All @@ -8,33 +8,33 @@
#include <netdb.h>
#include <errno.h>

GSocket::GSocket(Type type, CObject* parent)
: GIODevice(parent)
CSocket::CSocket(Type type, CObject* parent)
: CIODevice(parent)
, m_type(type)
{
}

GSocket::~GSocket()
CSocket::~CSocket()
{
}

bool GSocket::connect(const String& hostname, int port)
bool CSocket::connect(const String& hostname, int port)
{
auto* hostent = gethostbyname(hostname.characters());
if (!hostent) {
dbgprintf("GSocket::connect: Unable to resolve '%s'\n", hostname.characters());
dbgprintf("CSocket::connect: Unable to resolve '%s'\n", hostname.characters());
return false;
}

IPv4Address host_address((const byte*)hostent->h_addr_list[0]);
dbgprintf("GSocket::connect: Resolved '%s' to %s\n", hostname.characters(), host_address.to_string().characters());
dbgprintf("CSocket::connect: Resolved '%s' to %s\n", hostname.characters(), host_address.to_string().characters());
return connect(host_address, port);
}

bool GSocket::connect(const GSocketAddress& address, int port)
bool CSocket::connect(const CSocketAddress& address, int port)
{
ASSERT(!is_connected());
ASSERT(address.type() == GSocketAddress::Type::IPv4);
ASSERT(address.type() == CSocketAddress::Type::IPv4);
ASSERT(port > 0 && port <= 65535);

struct sockaddr_in addr;
Expand Down Expand Up @@ -71,17 +71,17 @@ bool GSocket::connect(const GSocketAddress& address, int port)
return true;
}

ByteBuffer GSocket::receive(int max_size)
ByteBuffer CSocket::receive(int max_size)
{
auto buffer = read(max_size);
if (eof()) {
dbgprintf("GSocket{%p}: Connection appears to have closed in receive().\n", this);
dbgprintf("CSocket{%p}: Connection appears to have closed in receive().\n", this);
m_connected = false;
}
return buffer;
}

bool GSocket::send(const ByteBuffer& data)
bool CSocket::send(const ByteBuffer& data)
{
int nsent = ::send(fd(), data.pointer(), data.size(), 0);
if (nsent < 0) {
Expand Down
Loading

0 comments on commit cfd6e6c

Please sign in to comment.