1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:28:12 +00:00

LibCore: Convert CFile to ObjectPtr

This commit is contained in:
Andreas Kling 2019-09-21 20:50:06 +02:00
parent 31b38ed88f
commit 8d550c174e
30 changed files with 135 additions and 134 deletions

View file

@ -27,16 +27,16 @@ int main(int argc, char** argv)
window->set_icon(load_png("/res/icons/16x16/app-piano.png")); window->set_icon(load_png("/res/icons/16x16/app-piano.png"));
LibThread::Thread sound_thread([piano_widget = piano_widget.ptr()] { LibThread::Thread sound_thread([piano_widget = piano_widget.ptr()] {
CFile audio("/dev/audio"); auto audio = CFile::construct("/dev/audio");
if (!audio.open(CIODevice::WriteOnly)) { if (!audio->open(CIODevice::WriteOnly)) {
dbgprintf("Can't open audio device: %s", audio.error_string()); dbgprintf("Can't open audio device: %s", audio->error_string());
return 1; return 1;
} }
for (;;) { for (;;) {
u8 buffer[4096]; u8 buffer[4096];
piano_widget->fill_audio_buffer(buffer, sizeof(buffer)); piano_widget->fill_audio_buffer(buffer, sizeof(buffer));
audio.write(buffer, sizeof(buffer)); audio->write(buffer, sizeof(buffer));
GEventLoop::current().post_event(*piano_widget, make<CCustomEvent>(0)); GEventLoop::current().post_event(*piano_widget, make<CCustomEvent>(0));
GEventLoop::wake(); GEventLoop::wake();
} }

View file

@ -95,11 +95,11 @@ GVariant DevicesModel::data(const GModelIndex& index, Role) const
void DevicesModel::update() void DevicesModel::update()
{ {
CFile proc_devices { "/proc/devices" }; auto proc_devices = CFile::construct("/proc/devices");
if (!proc_devices.open(CIODevice::OpenMode::ReadOnly)) if (!proc_devices->open(CIODevice::OpenMode::ReadOnly))
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
auto json = JsonValue::from_string(proc_devices.read_all()).as_array(); auto json = JsonValue::from_string(proc_devices->read_all()).as_array();
m_devices.clear(); m_devices.clear();
json.for_each([this](auto& value) { json.for_each([this](auto& value) {

View file

@ -11,9 +11,9 @@
MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph, GWidget* parent) MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph, GWidget* parent)
: GWidget(parent) : GWidget(parent)
, m_graph(graph) , m_graph(graph)
, m_proc_memstat("/proc/memstat") , m_proc_memstat(CFile::construct("/proc/memstat"))
{ {
if (!m_proc_memstat.open(CIODevice::OpenMode::ReadOnly)) if (!m_proc_memstat->open(CIODevice::OpenMode::ReadOnly))
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
set_preferred_size(0, 72); set_preferred_size(0, 72);
@ -59,9 +59,9 @@ static inline size_t bytes_to_kb(size_t bytes)
void MemoryStatsWidget::refresh() void MemoryStatsWidget::refresh()
{ {
m_proc_memstat.seek(0); m_proc_memstat->seek(0);
auto file_contents = m_proc_memstat.read_all(); auto file_contents = m_proc_memstat->read_all();
auto json = JsonValue::from_string(file_contents).as_object(); auto json = JsonValue::from_string(file_contents).as_object();
unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32(); unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();

View file

@ -22,5 +22,5 @@ private:
ObjectPtr<GLabel> m_supervisor_physical_pages_label; ObjectPtr<GLabel> m_supervisor_physical_pages_label;
ObjectPtr<GLabel> m_kmalloc_label; ObjectPtr<GLabel> m_kmalloc_label;
ObjectPtr<GLabel> m_kmalloc_count_label; ObjectPtr<GLabel> m_kmalloc_count_label;
CFile m_proc_memstat; ObjectPtr<CFile> m_proc_memstat;
}; };

View file

@ -28,11 +28,11 @@ void ProcessStacksWidget::set_pid(pid_t pid)
void ProcessStacksWidget::refresh() void ProcessStacksWidget::refresh()
{ {
CFile file(String::format("/proc/%d/stack", m_pid)); auto file = CFile::construct(String::format("/proc/%d/stack", m_pid));
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
m_stacks_editor->set_text(String::format("Unable to open %s", file.filename().characters())); m_stacks_editor->set_text(String::format("Unable to open %s", file->filename().characters()));
return; return;
} }
m_stacks_editor->set_text(file.read_all()); m_stacks_editor->set_text(file->read_all());
} }

View file

@ -270,14 +270,14 @@ void TextEditorWidget::update_title()
void TextEditorWidget::open_sesame(const String& path) void TextEditorWidget::open_sesame(const String& path)
{ {
CFile file(path); auto file = CFile::construct(path);
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window()); GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
return; return;
} }
m_document_dirty = false; m_document_dirty = false;
m_editor->set_text(file.read_all()); m_editor->set_text(file->read_all());
set_path(FileSystemPath(path)); set_path(FileSystemPath(path));
} }

View file

@ -13,13 +13,13 @@ int main(int argc, char** argv)
return 0; return 0;
} }
CFile file(argv[1]); auto file = CFile::construct(argv[1]);
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file.error_string()); fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file->error_string());
return 1; return 1;
} }
auto file_contents = file.read_all(); auto file_contents = file->read_all();
auto json = JsonValue::from_string(file_contents); auto json = JsonValue::from_string(file_contents);
if (!json.is_object()) { if (!json.is_object()) {

View file

@ -37,13 +37,13 @@ int main(int argc, char** argv)
return 0; return 0;
} }
CFile file(argv[1]); auto file = CFile::construct(argv[1]);
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file.error_string()); fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file->error_string());
return 1; return 1;
} }
auto file_contents = file.read_all(); auto file_contents = file->read_all();
Vector<Endpoint> endpoints; Vector<Endpoint> endpoints;

View file

@ -356,13 +356,13 @@ void VBForm::mousemove_event(GMouseEvent& event)
void VBForm::load_from_file(const String& path) void VBForm::load_from_file(const String& path)
{ {
CFile file(path); auto file = CFile::construct(path);
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
GMessageBox::show(String::format("Could not open '%s' for reading", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window()); GMessageBox::show(String::format("Could not open '%s' for reading", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
return; return;
} }
auto file_contents = file.read_all(); auto file_contents = file->read_all();
auto form_json = JsonValue::from_string(file_contents); auto form_json = JsonValue::from_string(file_contents);
if (!form_json.is_object()) { if (!form_json.is_object()) {
@ -392,8 +392,8 @@ void VBForm::load_from_file(const String& path)
void VBForm::write_to_file(const String& path) void VBForm::write_to_file(const String& path)
{ {
CFile file(path); auto file = CFile::construct(path);
if (!file.open(CIODevice::WriteOnly)) { if (!file->open(CIODevice::WriteOnly)) {
GMessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window()); GMessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
return; return;
} }
@ -414,7 +414,7 @@ void VBForm::write_to_file(const String& path)
widget_array.append(widget_object); widget_array.append(widget_object);
} }
form_object.set("widgets", widget_array); form_object.set("widgets", widget_array);
file.write(form_object.to_string()); file->write(form_object.to_string());
} }
void VBForm::dump() void VBForm::dump()

View file

@ -6,10 +6,10 @@
#include <limits> #include <limits>
AWavLoader::AWavLoader(const StringView& path) AWavLoader::AWavLoader(const StringView& path)
: m_file(path) : m_file(CFile::construct(path))
{ {
if (!m_file.open(CIODevice::ReadOnly)) { if (!m_file->open(CIODevice::ReadOnly)) {
m_error_string = String::format("Can't open file: %s", m_file.error_string()); m_error_string = String::format("Can't open file: %s", m_file->error_string());
return; return;
} }
@ -22,7 +22,7 @@ RefPtr<ABuffer> AWavLoader::get_more_samples(size_t max_bytes_to_read_from_input
dbgprintf("Read WAV of format PCM with num_channels %u sample rate %u, bits per sample %u\n", m_num_channels, m_sample_rate, m_bits_per_sample); dbgprintf("Read WAV of format PCM with num_channels %u sample rate %u, bits per sample %u\n", m_num_channels, m_sample_rate, m_bits_per_sample);
#endif #endif
auto raw_samples = m_file.read(max_bytes_to_read_from_input); auto raw_samples = m_file->read(max_bytes_to_read_from_input);
if (raw_samples.is_empty()) if (raw_samples.is_empty())
return nullptr; return nullptr;
@ -33,7 +33,7 @@ RefPtr<ABuffer> AWavLoader::get_more_samples(size_t max_bytes_to_read_from_input
bool AWavLoader::parse_header() bool AWavLoader::parse_header()
{ {
CIODeviceStreamReader stream(m_file); CIODeviceStreamReader stream(*m_file);
#define CHECK_OK(msg) \ #define CHECK_OK(msg) \
do { \ do { \

View file

@ -29,7 +29,7 @@ public:
private: private:
bool parse_header(); bool parse_header();
CFile m_file; ObjectPtr<CFile> m_file;
String m_error_string; String m_error_string;
u32 m_sample_rate { 0 }; u32 m_sample_rate { 0 };

View file

@ -36,14 +36,14 @@ void CConfigFile::reparse()
{ {
m_groups.clear(); m_groups.clear();
CFile file(m_file_name); auto file = CFile::construct(m_file_name);
if (!file.open(CIODevice::OpenMode::ReadOnly)) if (!file->open(CIODevice::OpenMode::ReadOnly))
return; return;
HashMap<String, String>* current_group = nullptr; HashMap<String, String>* current_group = nullptr;
while (file.can_read_line()) { while (file->can_read_line()) {
auto line = file.read_line(BUFSIZ); auto line = file->read_line(BUFSIZ);
auto* cp = (const char*)line.pointer(); auto* cp = (const char*)line.pointer();
while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n')) while (*cp && (*cp == ' ' || *cp == '\t' || *cp == '\n'))

View file

@ -6,11 +6,6 @@
class CFile final : public CIODevice { class CFile final : public CIODevice {
C_OBJECT(CFile) C_OBJECT(CFile)
public: public:
CFile(CObject* parent = nullptr)
: CIODevice(parent)
{
}
explicit CFile(const StringView&, CObject* parent = nullptr);
virtual ~CFile() override; virtual ~CFile() override;
String filename() const { return m_filename; } String filename() const { return m_filename; }
@ -25,6 +20,12 @@ public:
bool open(int fd, CIODevice::OpenMode, ShouldCloseFileDescription); bool open(int fd, CIODevice::OpenMode, ShouldCloseFileDescription);
private: private:
CFile(CObject* parent = nullptr)
: CIODevice(parent)
{
}
explicit CFile(const StringView&, CObject* parent = nullptr);
String m_filename; String m_filename;
ShouldCloseFileDescription m_should_close_file_descriptor { ShouldCloseFileDescription::Yes }; ShouldCloseFileDescription m_should_close_file_descriptor { ShouldCloseFileDescription::Yes };
}; };

View file

@ -10,15 +10,15 @@ HashMap<uid_t, String> CProcessStatisticsReader::s_usernames;
HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_all() HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_all()
{ {
CFile file("/proc/all"); auto file = CFile::construct("/proc/all");
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "CProcessStatisticsReader: Failed to open /proc/all: %s\n", file.error_string()); fprintf(stderr, "CProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string());
return {}; return {};
} }
HashMap<pid_t, CProcessStatistics> map; HashMap<pid_t, CProcessStatistics> map;
auto file_contents = file.read_all(); auto file_contents = file->read_all();
auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() }); auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() });
json.as_array().for_each([&](auto& value) { json.as_array().for_each([&](auto& value) {
const JsonObject& process_object = value.as_object(); const JsonObject& process_object = value.as_object();

View file

@ -4,13 +4,13 @@
void GJsonArrayModel::update() void GJsonArrayModel::update()
{ {
CFile file(m_json_path); auto file = CFile::construct(m_json_path);
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
dbg() << "Unable to open " << file.filename(); dbg() << "Unable to open " << file->filename();
return; return;
} }
auto json = JsonValue::from_string(file.read_all()); auto json = JsonValue::from_string(file->read_all());
ASSERT(json.is_array()); ASSERT(json.is_array());
m_array = json.as_array(); m_array = json.as_array();

View file

@ -12,9 +12,9 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
CFile f(argc == 1 ? "/home/anon/small.html" : argv[1]); auto f = CFile::construct(argc == 1 ? "/home/anon/small.html" : argv[1]);
if (!f.open(CIODevice::ReadOnly)) { if (!f->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: %s\n", f.error_string()); fprintf(stderr, "Error: %s\n", f->error_string());
return 1; return 1;
} }
@ -24,7 +24,7 @@ int main(int argc, char** argv)
auto sheet = parse_css(css); auto sheet = parse_css(css);
dump_sheet(sheet); dump_sheet(sheet);
String html = String::copy(f.read_all()); String html = String::copy(f->read_all());
auto document = parse_html(html); auto document = parse_html(html);
dump_tree(document); dump_tree(document);
document->add_sheet(*sheet); document->add_sheet(*sheet);

View file

@ -4,14 +4,14 @@
#include <limits> #include <limits>
ASMixer::ASMixer() ASMixer::ASMixer()
: m_device("/dev/audio", this) : m_device(CFile::construct("/dev/audio", this))
, m_sound_thread([this] { , m_sound_thread([this] {
mix(); mix();
return 0; return 0;
}) })
{ {
if (!m_device.open(CIODevice::WriteOnly)) { if (!m_device->open(CIODevice::WriteOnly)) {
dbgprintf("Can't open audio device: %s\n", m_device.error_string()); dbgprintf("Can't open audio device: %s\n", m_device->error_string());
return; return;
} }
@ -88,7 +88,7 @@ void ASMixer::mix()
if (stream.offset() != 0) { if (stream.offset() != 0) {
buffer.trim(stream.offset()); buffer.trim(stream.offset());
m_device.write(buffer); m_device->write(buffer);
} }
} }
} }

View file

@ -63,7 +63,7 @@ public:
private: private:
Vector<NonnullRefPtr<ASBufferQueue>> m_pending_mixing; Vector<NonnullRefPtr<ASBufferQueue>> m_pending_mixing;
CFile m_device; ObjectPtr<CFile> m_device;
LibThread::Lock m_lock; LibThread::Lock m_lock;
LibThread::Thread m_sound_thread; LibThread::Thread m_sound_thread;

View file

@ -35,11 +35,11 @@ static String parse_dns_name(const u8*, int& offset, int max_offset);
static void load_etc_hosts() static void load_etc_hosts()
{ {
dbgprintf("LookupServer: Loading hosts from /etc/hosts\n"); dbgprintf("LookupServer: Loading hosts from /etc/hosts\n");
CFile file("/etc/hosts"); auto file = CFile::construct("/etc/hosts");
if (!file.open(CIODevice::ReadOnly)) if (!file->open(CIODevice::ReadOnly))
return; return;
while (!file.eof()) { while (!file->eof()) {
auto line = file.read_line(1024); auto line = file->read_line(1024);
if (line.is_empty()) if (line.is_empty())
break; break;
auto str_line = String((const char*)line.pointer(), line.size() - 1, Chomp); auto str_line = String((const char*)line.pointer(), line.size() - 1, Chomp);

View file

@ -58,12 +58,12 @@ void start_process(const String& program, const Vector<String>& arguments, int p
static void check_for_test_mode() static void check_for_test_mode()
{ {
CFile f("/proc/cmdline"); auto f = CFile::construct("/proc/cmdline");
if (!f.open(CIODevice::ReadOnly)) { if (!f->open(CIODevice::ReadOnly)) {
dbg() << "Failed to read command line: " << f.error_string(); dbg() << "Failed to read command line: " << f->error_string();
ASSERT(false); ASSERT(false);
} }
const String cmd = String::copy(f.read_all()); const String cmd = String::copy(f->read_all());
dbg() << "Read command line: " << cmd; dbg() << "Read command line: " << cmd;
if (cmd.matches("*testmode=1*")) { if (cmd.matches("*testmode=1*")) {
// Eventually, we should run a test binary and wait for it to finish // Eventually, we should run a test binary and wait for it to finish

View file

@ -814,11 +814,11 @@ static String get_history_path()
void load_history() void load_history()
{ {
CFile history_file(get_history_path()); auto history_file = CFile::construct(get_history_path());
if (!history_file.open(CIODevice::ReadOnly)) if (!history_file->open(CIODevice::ReadOnly))
return; return;
while (history_file.can_read_line()) { while (history_file->can_read_line()) {
auto b = history_file.read_line(1024); auto b = history_file->read_line(1024);
// skip the newline and terminating bytes // skip the newline and terminating bytes
editor.add_to_history(String(reinterpret_cast<const char*>(b.pointer()), b.size() - 2)); editor.add_to_history(String(reinterpret_cast<const char*>(b.pointer()), b.size() - 2));
} }
@ -826,12 +826,12 @@ void load_history()
void save_history() void save_history()
{ {
CFile history_file(get_history_path()); auto history_file = CFile::construct(get_history_path());
if (!history_file.open(CIODevice::WriteOnly)) if (!history_file->open(CIODevice::WriteOnly))
return; return;
for (const auto& line : editor.history()) { for (const auto& line : editor.history()) {
history_file.write(line); history_file->write(line);
history_file.write("\n"); history_file->write("\n");
} }
} }
@ -878,13 +878,13 @@ int main(int argc, char** argv)
} }
if (argc == 2 && argv[1][0] != '-') { if (argc == 2 && argv[1][0] != '-') {
CFile file(argv[1]); auto file = CFile::construct(argv[1]);
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Failed to open %s: %s\n", file.filename().characters(), file.error_string()); fprintf(stderr, "Failed to open %s: %s\n", file->filename().characters(), file->error_string());
return 1; return 1;
} }
for (;;) { for (;;) {
auto line = file.read_line(4096); auto line = file->read_line(4096);
if (line.is_null()) if (line.is_null())
break; break;
run_command(String::copy(line, Chomp)); run_command(String::copy(line, Chomp));

View file

@ -68,13 +68,13 @@ Options parse_options(int argc, char* argv[])
options.data = builder.to_string(); options.data = builder.to_string();
} else { } else {
// Copy our stdin. // Copy our stdin.
CFile c_stdin; auto c_stdin = CFile::construct();
bool success = c_stdin.open( bool success = c_stdin->open(
STDIN_FILENO, STDIN_FILENO,
CIODevice::OpenMode::ReadOnly, CIODevice::OpenMode::ReadOnly,
CFile::ShouldCloseFileDescription::No); CFile::ShouldCloseFileDescription::No);
ASSERT(success); ASSERT(success);
auto buffer = c_stdin.read_all(); auto buffer = c_stdin->read_all();
dbg() << "Read size " << buffer.size(); dbg() << "Read size " << buffer.size();
options.data = String((char*)buffer.data(), buffer.size()); options.data = String((char*)buffer.data(), buffer.size());
} }

View file

@ -19,14 +19,14 @@ struct FileSystem {
int main(int, char**) int main(int, char**)
{ {
CFile file("/proc/df"); auto file = CFile::construct("/proc/df");
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Failed to open /proc/df: %s\n", file.error_string()); fprintf(stderr, "Failed to open /proc/df: %s\n", file->error_string());
return 1; return 1;
} }
printf("Filesystem Blocks Used Available Mount point\n"); printf("Filesystem Blocks Used Available Mount point\n");
auto file_contents = file.read_all(); auto file_contents = file->read_all();
auto json = JsonValue::from_string(file_contents).as_array(); auto json = JsonValue::from_string(file_contents).as_array();
json.for_each([](auto& value) { json.for_each([](auto& value) {
auto fs_object = value.as_object(); auto fs_object = value.as_object();

View file

@ -8,12 +8,12 @@ int main(int argc, char** argv)
{ {
(void)argc; (void)argc;
(void)argv; (void)argv;
CFile f("/proc/dmesg"); auto f = CFile::construct("/proc/dmesg");
if (!f.open(CIODevice::ReadOnly)) { if (!f->open(CIODevice::ReadOnly)) {
fprintf(stderr, "open: failed to open /proc/dmesg: %s", f.error_string()); fprintf(stderr, "open: failed to open /proc/dmesg: %s", f->error_string());
return 1; return 1;
} }
const auto& b = f.read_all(); const auto& b = f->read_all();
for (auto i = 0; i < b.size(); ++i) for (auto i = 0; i < b.size(); ++i)
putchar(b[i]); putchar(b[i]);
return 0; return 0;

View file

@ -21,13 +21,13 @@ int main(int argc, char** argv)
UNUSED_PARAM(argc); UNUSED_PARAM(argc);
UNUSED_PARAM(argv); UNUSED_PARAM(argv);
CFile file("/proc/net/adapters"); auto file = CFile::construct("/proc/net/adapters");
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: %s\n", file.error_string()); fprintf(stderr, "Error: %s\n", file->error_string());
return 1; return 1;
} }
auto file_contents = file.read_all(); auto file_contents = file->read_all();
auto json = JsonValue::from_string(file_contents).as_array(); auto json = JsonValue::from_string(file_contents).as_array();
json.for_each([](auto& value) { json.for_each([](auto& value) {
auto if_object = value.as_object(); auto if_object = value.as_object();

View file

@ -18,13 +18,13 @@ int main(int argc, char** argv)
fprintf(stderr, "usage: jp <file>\n"); fprintf(stderr, "usage: jp <file>\n");
return 0; return 0;
} }
CFile file(argv[1]); auto file = CFile::construct(argv[1]);
if (!file.open(CIODevice::ReadOnly)) { if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Couldn't open %s for reading: %s\n", argv[1], file.error_string()); fprintf(stderr, "Couldn't open %s for reading: %s\n", argv[1], file->error_string());
return 1; return 1;
} }
auto file_contents = file.read_all(); auto file_contents = file->read_all();
auto json = JsonValue::from_string(file_contents); auto json = JsonValue::from_string(file_contents);
print(json); print(json);

View file

@ -14,13 +14,13 @@ int main(int argc, char** argv)
if (!db) if (!db)
fprintf(stderr, "Couldn't open PCI ID database\n"); fprintf(stderr, "Couldn't open PCI ID database\n");
CFile proc_pci("/proc/pci"); auto proc_pci = CFile::construct("/proc/pci");
if (!proc_pci.open(CIODevice::ReadOnly)) { if (!proc_pci->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: %s\n", proc_pci.error_string()); fprintf(stderr, "Error: %s\n", proc_pci->error_string());
return 1; return 1;
} }
auto file_contents = proc_pci.read_all(); auto file_contents = proc_pci->read_all();
auto json = JsonValue::from_string(file_contents).as_array(); auto json = JsonValue::from_string(file_contents).as_array();
json.for_each([db](auto& value) { json.for_each([db](auto& value) {
auto dev = value.as_object(); auto dev = value.as_object();

View file

@ -11,15 +11,15 @@ bool mount_all()
// Mount all filesystems listed in /etc/fstab. // Mount all filesystems listed in /etc/fstab.
dbg() << "Mounting all filesystems..."; dbg() << "Mounting all filesystems...";
CFile fstab { "/etc/fstab" }; auto fstab = CFile::construct("/etc/fstab");
if (!fstab.open(CIODevice::OpenMode::ReadOnly)) { if (!fstab->open(CIODevice::OpenMode::ReadOnly)) {
fprintf(stderr, "Failed to open /etc/fstab: %s\n", fstab.error_string()); fprintf(stderr, "Failed to open /etc/fstab: %s\n", fstab->error_string());
return false; return false;
} }
bool all_ok = true; bool all_ok = true;
while (fstab.can_read_line()) { while (fstab->can_read_line()) {
ByteBuffer buffer = fstab.read_line(1024); ByteBuffer buffer = fstab->read_line(1024);
StringView line_view = (const char*)buffer.data(); StringView line_view = (const char*)buffer.data();
// Trim the trailing newline, if any. // Trim the trailing newline, if any.
@ -59,13 +59,13 @@ bool mount_all()
bool print_mounts() bool print_mounts()
{ {
// Output info about currently mounted filesystems. // Output info about currently mounted filesystems.
CFile df("/proc/df"); auto df = CFile::construct("/proc/df");
if (!df.open(CIODevice::ReadOnly)) { if (!df->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Failed to open /proc/df: %s\n", df.error_string()); fprintf(stderr, "Failed to open /proc/df: %s\n", df->error_string());
return false; return false;
} }
auto content = df.read_all(); auto content = df->read_all();
auto json = JsonValue::from_string(content).as_array(); auto json = JsonValue::from_string(content).as_array();
json.for_each([](auto& value) { json.for_each([](auto& value) {

View file

@ -17,14 +17,14 @@ static String read_var(const String& name)
builder.append("/proc/sys/"); builder.append("/proc/sys/");
builder.append(name); builder.append(name);
auto path = builder.to_string(); auto path = builder.to_string();
CFile f(path); auto f = CFile::construct(path);
if (!f.open(CIODevice::ReadOnly)) { if (!f->open(CIODevice::ReadOnly)) {
fprintf(stderr, "open: %s", f.error_string()); fprintf(stderr, "open: %s", f->error_string());
exit(1); exit(1);
} }
const auto& b = f.read_all(); const auto& b = f->read_all();
if (f.error() < 0) { if (f->error() < 0) {
fprintf(stderr, "read: %s", f.error_string()); fprintf(stderr, "read: %s", f->error_string());
exit(1); exit(1);
} }
return String((const char*)b.pointer(), b.size(), Chomp); return String((const char*)b.pointer(), b.size(), Chomp);
@ -36,14 +36,14 @@ static void write_var(const String& name, const String& value)
builder.append("/proc/sys/"); builder.append("/proc/sys/");
builder.append(name); builder.append(name);
auto path = builder.to_string(); auto path = builder.to_string();
CFile f(path); auto f = CFile::construct(path);
if (!f.open(CIODevice::WriteOnly)) { if (!f->open(CIODevice::WriteOnly)) {
fprintf(stderr, "open: %s", f.error_string()); fprintf(stderr, "open: %s", f->error_string());
exit(1); exit(1);
} }
f.write(value); f->write(value);
if (f.error() < 0) { if (f->error() < 0) {
fprintf(stderr, "write: %s", f.error_string()); fprintf(stderr, "write: %s", f->error_string());
exit(1); exit(1);
} }
} }

View file

@ -95,13 +95,13 @@ int main(int argc, char* argv[])
line_count = DEFAULT_LINE_COUNT; line_count = DEFAULT_LINE_COUNT;
} }
CFile f(values[0]); auto f = CFile::construct(values[0]);
if (!f.open(CIODevice::ReadOnly)) { if (!f->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error opening file %s: %s\n", f.filename().characters(), strerror(errno)); fprintf(stderr, "Error opening file %s: %s\n", f->filename().characters(), strerror(errno));
exit(1); exit(1);
} }
bool flag_follow = args.is_present("f"); bool flag_follow = args.is_present("f");
auto pos = find_seek_pos(f, line_count); auto pos = find_seek_pos(*f, line_count);
return tail_from_pos(f, pos, flag_follow); return tail_from_pos(*f, pos, flag_follow);
} }