Skip to content

Commit

Permalink
LibCore: Make IODevice::read_line() return a String
Browse files Browse the repository at this point in the history
Almost everyone using this API actually wanted String instead of a
ByteBuffer anyway, and there were a bunch of slightly different ways
clients would convert to String.

Let's just cut out all the confusion and make it return String. :^)
  • Loading branch information
awesomekling committed Dec 13, 2020
1 parent 4da327d commit b9b7b2b
Show file tree
Hide file tree
Showing 22 changed files with 50 additions and 66 deletions.
8 changes: 4 additions & 4 deletions Applications/IRCClient/IRCClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ bool IRCClient::connect()
void IRCClient::receive_from_server()
{
while (m_socket->can_read_line()) {
auto line = m_socket->read_line(PAGE_SIZE);
auto line = m_socket->read_line();
if (line.is_null()) {
if (!m_socket->is_connected()) {
outln("IRCClient: Connection closed!");
exit(1);
}
ASSERT_NOT_REACHED();
}
process_line(move(line));
process_line(line);
}
}

void IRCClient::process_line(ByteBuffer&& line)
void IRCClient::process_line(const String& line)
{
Message msg;
Vector<char, 32> prefix;
Expand All @@ -159,7 +159,7 @@ void IRCClient::process_line(ByteBuffer&& line)
} state
= Start;

for (size_t i = 0; i < line.size(); ++i) {
for (size_t i = 0; i < line.length(); ++i) {
char ch = line[i];
if (ch == '\r')
continue;
Expand Down
2 changes: 1 addition & 1 deletion Applications/IRCClient/IRCClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class IRCClient final : public Core::Object {
void send_kick(const String& channel_name, const String& nick, const String&);
void send_list();
void send_whois(const String&);
void process_line(ByteBuffer&&);
void process_line(const String&);
void handle_join(const Message&);
void handle_part(const Message&);
void handle_quit(const Message&);
Expand Down
18 changes: 7 additions & 11 deletions Applications/Welcome/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ static Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
bool started = false;
ContentPage current;
while (file->can_read_line()) {
auto buffer = file->read_line(4096);
auto line = String((const char*)buffer.data(), buffer.size());
if (line.length() > 1)
line = line.substring(0, line.length() - 1); // remove newline
auto line = file->read_line();
if (line.is_empty()) {
if (!current_output_line.to_string().is_empty())
current.content.append(current_output_line.to_string());
current_output_line.clear();
continue;
}
switch (line[0]) {
case '*':
dbgln("menu_item line:\t{}", line);
Expand All @@ -87,13 +90,6 @@ static Optional<Vector<ContentPage>> parse_welcome_file(const String& path)
dbgln("title line:\t{}", line);
current.title = line.substring(2, line.length() - 2);
break;
case '\n':
dbgln("newline");

if (!current_output_line.to_string().is_empty())
current.content.append(current_output_line.to_string());
current_output_line.clear();
break;
case '#':
dbgln("comment line:\t{}", line);
break;
Expand Down
4 changes: 2 additions & 2 deletions Libraries/LibCore/ConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ void ConfigFile::reparse()
HashMap<String, String>* current_group = nullptr;

while (file->can_read_line()) {
auto line = file->read_line(BUFSIZ);
auto* cp = (const char*)line.data();
auto line = file->read_line();
auto* cp = line.characters();

while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))
++cp;
Expand Down
8 changes: 4 additions & 4 deletions Libraries/LibCore/IODevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ ByteBuffer IODevice::read_all()
return ByteBuffer::copy(data.data(), data.size());
}

ByteBuffer IODevice::read_line(size_t max_size)
String IODevice::read_line(size_t max_size)
{
if (m_fd < 0)
return {};
Expand All @@ -187,9 +187,9 @@ ByteBuffer IODevice::read_line(size_t max_size)
dbgprintf("IODevice::read_line: At EOF but there's more than max_size(%zu) buffered\n", max_size);
return {};
}
auto buffer = ByteBuffer::copy(m_buffered_data.data(), m_buffered_data.size());
auto line = String((const char*)m_buffered_data.data(), m_buffered_data.size(), Chomp);
m_buffered_data.clear();
return buffer;
return line;
}
auto line = ByteBuffer::create_uninitialized(max_size + 1);
size_t line_index = 0;
Expand All @@ -201,7 +201,7 @@ ByteBuffer IODevice::read_line(size_t max_size)
new_buffered_data.append(m_buffered_data.data() + line_index, m_buffered_data.size() - line_index);
m_buffered_data = move(new_buffered_data);
line.trim(line_index);
return line;
return String::copy(line, Chomp);
}
}
return {};
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibCore/IODevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class IODevice : public Object {
int read(u8* buffer, int length);

ByteBuffer read(size_t max_size);
ByteBuffer read_line(size_t max_size);
ByteBuffer read_all();
String read_line(size_t max_size = 16384);

bool write(const u8*, int size);
bool write(const StringView&);
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGemini/GeminiJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ bool GeminiJob::can_read_line() const
return m_socket->can_read_line();
}

ByteBuffer GeminiJob::read_line(size_t size)
String GeminiJob::read_line(size_t size)
{
return m_socket->read_line(size);
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGemini/GeminiJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class GeminiJob final : public Job {
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;
virtual ByteBuffer read_line(size_t) override;
virtual String read_line(size_t) override;
virtual bool can_read() const override;
virtual ByteBuffer receive(size_t) override;
virtual bool eof() const override;
Expand Down
4 changes: 2 additions & 2 deletions Libraries/LibGemini/Job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ void Job::on_socket_connected()
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
}

auto parts = String::copy(line, Chomp).split_limit(' ', 2);
auto parts = line.split_limit(' ', 2);
if (parts.size() != 2) {
fprintf(stderr, "Job: Expected 2-part status line, got '%s'\n", line.data());
warnln("Job: Expected 2-part status line, got '{}'", line);
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}

Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGemini/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Job : public Core::NetworkJob {
virtual void register_on_ready_to_read(Function<void()>) = 0;
virtual void register_on_ready_to_write(Function<void()>) = 0;
virtual bool can_read_line() const = 0;
virtual ByteBuffer read_line(size_t) = 0;
virtual String read_line(size_t) = 0;
virtual bool can_read() const = 0;
virtual ByteBuffer receive(size_t) = 0;
virtual bool eof() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibHTTP/HttpJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool HttpJob::can_read_line() const
return m_socket->can_read_line();
}

ByteBuffer HttpJob::read_line(size_t size)
String HttpJob::read_line(size_t size)
{
return m_socket->read_line(size);
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibHTTP/HttpJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class HttpJob final : public Job {
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;
virtual ByteBuffer read_line(size_t) override;
virtual String read_line(size_t) override;
virtual bool can_read() const override;
virtual ByteBuffer receive(size_t) override;
virtual bool eof() const override;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibHTTP/HttpsJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ bool HttpsJob::can_read_line() const
return m_socket->can_read_line();
}

ByteBuffer HttpsJob::read_line(size_t size)
String HttpsJob::read_line(size_t size)
{
return m_socket->read_line(size);
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibHTTP/HttpsJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class HttpsJob final : public Job {
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;
virtual ByteBuffer read_line(size_t) override;
virtual String read_line(size_t) override;
virtual bool can_read() const override;
virtual ByteBuffer receive(size_t) override;
virtual bool eof() const override;
Expand Down
17 changes: 8 additions & 9 deletions Libraries/LibHTTP/Job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ void Job::on_socket_connected()
fprintf(stderr, "Job: Expected HTTP status\n");
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
}
auto parts = String::copy(line, Chomp).split(' ');
auto parts = line.split_view(' ');
if (parts.size() < 3) {
fprintf(stderr, "Job: Expected 3-part HTTP status, got '%s'\n", line.data());
warnln("Job: Expected 3-part HTTP status, got '{}'", line);
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto code = parts[1].to_uint();
Expand All @@ -131,16 +131,15 @@ void Job::on_socket_connected()
fprintf(stderr, "Job: Expected HTTP header\n");
return did_fail(Core::NetworkJob::Error::ProtocolFailed);
}
auto chomped_line = String::copy(line, Chomp);
if (chomped_line.is_empty()) {
if (line.is_empty()) {
if (m_state == State::Trailers) {
return finish_up();
} else {
m_state = State::InBody;
}
return;
}
auto parts = chomped_line.split(':');
auto parts = line.split_view(':');
if (parts.is_empty()) {
if (m_state == State::Trailers) {
// Some servers like to send two ending chunks
Expand All @@ -152,17 +151,17 @@ void Job::on_socket_connected()
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto name = parts[0];
if (chomped_line.length() < name.length() + 2) {
if (line.length() < name.length() + 2) {
if (m_state == State::Trailers) {
// Some servers like to send two ending chunks
// use this fact as an excuse to ignore anything after the last chunk
// that is not a valid trailing header.
return finish_up();
}
fprintf(stderr, "Job: Malformed HTTP header: '%s' (%zu)\n", chomped_line.characters(), chomped_line.length());
warnln("Job: Malformed HTTP header: '{}' ({})", line, line.length());
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto value = chomped_line.substring(name.length() + 2, chomped_line.length() - name.length() - 2);
auto value = line.substring(name.length() + 2, line.length() - name.length() - 2);
m_headers.set(name, value);
#ifdef JOB_DEBUG
dbg() << "Job: [" << name << "] = '" << value << "'";
Expand All @@ -180,7 +179,7 @@ void Job::on_socket_connected()
if (remaining == -1) {
// read size
auto size_data = read_line(PAGE_SIZE);
auto size_lines = StringView { size_data.data(), size_data.size() }.lines();
auto size_lines = size_data.view().lines();
#ifdef JOB_DEBUG
dbg() << "Job: Received a chunk with size _" << size_data << "_";
#endif
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibHTTP/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Job : public Core::NetworkJob {
virtual void register_on_ready_to_read(Function<void()>) = 0;
virtual void register_on_ready_to_write(Function<void()>) = 0;
virtual bool can_read_line() const = 0;
virtual ByteBuffer read_line(size_t) = 0;
virtual String read_line(size_t) = 0;
virtual bool can_read() const = 0;
virtual ByteBuffer receive(size_t) = 0;
virtual bool eof() const = 0;
Expand Down
4 changes: 1 addition & 3 deletions Libraries/LibLine/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ bool Editor::load_history(const String& path)
if (!history_file->open(Core::IODevice::ReadOnly))
return false;
while (history_file->can_read_line()) {
auto buffer = history_file->read_line(1024);
// -1 to skip the newline character
add_to_history(String(reinterpret_cast<const char*>(buffer.data()), buffer.size() - 1));
add_to_history(history_file->read_line());
}
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions Libraries/LibTLS/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ByteBuffer TLSv12::read(size_t max_size)
return {};
}

ByteBuffer TLSv12::read_line(size_t max_size)
String TLSv12::read_line(size_t max_size)
{
if (!can_read_line())
return {};
Expand All @@ -70,7 +70,7 @@ ByteBuffer TLSv12::read_line(size_t max_size)
auto buffer = ByteBuffer::copy(start, offset);
m_context.application_buffer = m_context.application_buffer.slice(offset + 1, m_context.application_buffer.size() - offset - 1);

return buffer;
return String::copy(buffer);
}

bool TLSv12::write(const ByteBuffer& buffer)
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibTLS/TLSv12.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class TLSv12 : public Core::Socket {

bool can_read_line() const { return m_context.application_buffer.size() && memchr(m_context.application_buffer.data(), '\n', m_context.application_buffer.size()); }
bool can_read() const { return m_context.application_buffer.size() > 0; }
ByteBuffer read_line(size_t max_size);
String read_line(size_t max_size);

Function<void(TLSv12&)> on_tls_ready_to_read;
Function<void(TLSv12&)> on_tls_ready_to_write;
Expand Down
3 changes: 1 addition & 2 deletions Services/LookupServer/LookupServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ void LookupServer::load_etc_hosts()
auto line = file->read_line(1024);
if (line.is_empty())
break;
auto str_line = String((const char*)line.data(), line.size() - 1, Chomp);
auto fields = str_line.split('\t');
auto fields = line.split('\t');

auto sections = fields[0].split('.');
IPv4Address addr {
Expand Down
16 changes: 7 additions & 9 deletions Userland/grep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ int main(int argc, char** argv)
match.view.to_string());
last_printed_char_pos = match.global_offset + match.view.length();
}
out("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
outln("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
}

return true;
Expand All @@ -163,11 +163,10 @@ int main(int argc, char** argv)
}

while (file->can_read_line()) {
auto line = file->read_line(1024);
auto is_binary = memchr(line.data(), 0, line.size()) != nullptr;
auto line = file->read_line();
auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;

StringView str { reinterpret_cast<const char*>(line.data()), line.size() };
if (matches(str, filename, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
if (matches(line, filename, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
return true;
}
return true;
Expand All @@ -189,14 +188,13 @@ int main(int argc, char** argv)
if (!files.size() && !recursive) {
auto stdin_file = Core::File::stdin();
for (;;) {
auto line = stdin_file->read_line(4096);
StringView str { line.data(), line.size() };
bool is_binary = str.bytes().contains_slow(0);
auto line = stdin_file->read_line();
bool is_binary = line.bytes().contains_slow(0);

if (is_binary && binary_mode == BinaryFileMode::Skip)
return 1;

if (matches(str, "stdin", false, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
if (matches(line, "stdin", false, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
return 0;
}
} else {
Expand Down
8 changes: 1 addition & 7 deletions Userland/mount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,7 @@ static bool mount_all()

bool all_ok = true;
while (fstab->can_read_line()) {
ByteBuffer buffer = fstab->read_line(1024);
StringView line_view { buffer.data(), buffer.size() };

// Trim the trailing newline, if any.
if (line_view.length() > 0 && line_view[line_view.length() - 1] == '\n')
line_view = line_view.substring_view(0, line_view.length() - 1);
String line = line_view;
auto line = fstab->read_line();

// Skip comments and blank lines.
if (line.is_empty() || line.starts_with("#"))
Expand Down

0 comments on commit b9b7b2b

Please sign in to comment.