LibCore: Convert CFile to ObjectPtr

This commit is contained in:
Andreas Kling 2019-09-21 20:50:06 +02:00
parent 31b38ed88f
commit 8d550c174e
Notes: sideshowbarker 2024-07-19 12:00:53 +09:00
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"));
LibThread::Thread sound_thread([piano_widget = piano_widget.ptr()] {
CFile audio("/dev/audio");
if (!audio.open(CIODevice::WriteOnly)) {
dbgprintf("Can't open audio device: %s", audio.error_string());
auto audio = CFile::construct("/dev/audio");
if (!audio->open(CIODevice::WriteOnly)) {
dbgprintf("Can't open audio device: %s", audio->error_string());
return 1;
}
for (;;) {
u8 buffer[4096];
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::wake();
}

View file

@ -95,11 +95,11 @@ GVariant DevicesModel::data(const GModelIndex& index, Role) const
void DevicesModel::update()
{
CFile proc_devices { "/proc/devices" };
if (!proc_devices.open(CIODevice::OpenMode::ReadOnly))
auto proc_devices = CFile::construct("/proc/devices");
if (!proc_devices->open(CIODevice::OpenMode::ReadOnly))
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();
json.for_each([this](auto& value) {

View file

@ -11,9 +11,9 @@
MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph, GWidget* parent)
: GWidget(parent)
, 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();
set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
set_preferred_size(0, 72);
@ -59,9 +59,9 @@ static inline size_t bytes_to_kb(size_t bytes)
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();
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_kmalloc_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()
{
CFile file(String::format("/proc/%d/stack", m_pid));
if (!file.open(CIODevice::ReadOnly)) {
m_stacks_editor->set_text(String::format("Unable to open %s", file.filename().characters()));
auto file = CFile::construct(String::format("/proc/%d/stack", m_pid));
if (!file->open(CIODevice::ReadOnly)) {
m_stacks_editor->set_text(String::format("Unable to open %s", file->filename().characters()));
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)
{
CFile file(path);
if (!file.open(CIODevice::ReadOnly)) {
auto file = CFile::construct(path);
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());
return;
}
m_document_dirty = false;
m_editor->set_text(file.read_all());
m_editor->set_text(file->read_all());
set_path(FileSystemPath(path));
}

View file

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

View file

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

View file

@ -356,13 +356,13 @@ void VBForm::mousemove_event(GMouseEvent& event)
void VBForm::load_from_file(const String& path)
{
CFile file(path);
if (!file.open(CIODevice::ReadOnly)) {
auto file = CFile::construct(path);
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());
return;
}
auto file_contents = file.read_all();
auto file_contents = file->read_all();
auto form_json = JsonValue::from_string(file_contents);
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)
{
CFile file(path);
if (!file.open(CIODevice::WriteOnly)) {
auto file = CFile::construct(path);
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());
return;
}
@ -414,7 +414,7 @@ void VBForm::write_to_file(const String& path)
widget_array.append(widget_object);
}
form_object.set("widgets", widget_array);
file.write(form_object.to_string());
file->write(form_object.to_string());
}
void VBForm::dump()

View file

@ -6,10 +6,10 @@
#include <limits>
AWavLoader::AWavLoader(const StringView& path)
: m_file(path)
: m_file(CFile::construct(path))
{
if (!m_file.open(CIODevice::ReadOnly)) {
m_error_string = String::format("Can't open file: %s", m_file.error_string());
if (!m_file->open(CIODevice::ReadOnly)) {
m_error_string = String::format("Can't open file: %s", m_file->error_string());
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);
#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())
return nullptr;
@ -33,7 +33,7 @@ RefPtr<ABuffer> AWavLoader::get_more_samples(size_t max_bytes_to_read_from_input
bool AWavLoader::parse_header()
{
CIODeviceStreamReader stream(m_file);
CIODeviceStreamReader stream(*m_file);
#define CHECK_OK(msg) \
do { \

View file

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

View file

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

View file

@ -6,11 +6,6 @@
class CFile final : public CIODevice {
C_OBJECT(CFile)
public:
CFile(CObject* parent = nullptr)
: CIODevice(parent)
{
}
explicit CFile(const StringView&, CObject* parent = nullptr);
virtual ~CFile() override;
String filename() const { return m_filename; }
@ -25,6 +20,12 @@ public:
bool open(int fd, CIODevice::OpenMode, ShouldCloseFileDescription);
private:
CFile(CObject* parent = nullptr)
: CIODevice(parent)
{
}
explicit CFile(const StringView&, CObject* parent = nullptr);
String m_filename;
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()
{
CFile file("/proc/all");
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "CProcessStatisticsReader: Failed to open /proc/all: %s\n", file.error_string());
auto file = CFile::construct("/proc/all");
if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "CProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string());
return {};
}
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() });
json.as_array().for_each([&](auto& value) {
const JsonObject& process_object = value.as_object();

View file

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

View file

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

View file

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

View file

@ -63,7 +63,7 @@ public:
private:
Vector<NonnullRefPtr<ASBufferQueue>> m_pending_mixing;
CFile m_device;
ObjectPtr<CFile> m_device;
LibThread::Lock m_lock;
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()
{
dbgprintf("LookupServer: Loading hosts from /etc/hosts\n");
CFile file("/etc/hosts");
if (!file.open(CIODevice::ReadOnly))
auto file = CFile::construct("/etc/hosts");
if (!file->open(CIODevice::ReadOnly))
return;
while (!file.eof()) {
auto line = file.read_line(1024);
while (!file->eof()) {
auto line = file->read_line(1024);
if (line.is_empty())
break;
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()
{
CFile f("/proc/cmdline");
if (!f.open(CIODevice::ReadOnly)) {
dbg() << "Failed to read command line: " << f.error_string();
auto f = CFile::construct("/proc/cmdline");
if (!f->open(CIODevice::ReadOnly)) {
dbg() << "Failed to read command line: " << f->error_string();
ASSERT(false);
}
const String cmd = String::copy(f.read_all());
const String cmd = String::copy(f->read_all());
dbg() << "Read command line: " << cmd;
if (cmd.matches("*testmode=1*")) {
// 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()
{
CFile history_file(get_history_path());
if (!history_file.open(CIODevice::ReadOnly))
auto history_file = CFile::construct(get_history_path());
if (!history_file->open(CIODevice::ReadOnly))
return;
while (history_file.can_read_line()) {
auto b = history_file.read_line(1024);
while (history_file->can_read_line()) {
auto b = history_file->read_line(1024);
// skip the newline and terminating bytes
editor.add_to_history(String(reinterpret_cast<const char*>(b.pointer()), b.size() - 2));
}
@ -826,12 +826,12 @@ void load_history()
void save_history()
{
CFile history_file(get_history_path());
if (!history_file.open(CIODevice::WriteOnly))
auto history_file = CFile::construct(get_history_path());
if (!history_file->open(CIODevice::WriteOnly))
return;
for (const auto& line : editor.history()) {
history_file.write(line);
history_file.write("\n");
history_file->write(line);
history_file->write("\n");
}
}
@ -878,13 +878,13 @@ int main(int argc, char** argv)
}
if (argc == 2 && argv[1][0] != '-') {
CFile file(argv[1]);
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "Failed to open %s: %s\n", file.filename().characters(), file.error_string());
auto file = CFile::construct(argv[1]);
if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Failed to open %s: %s\n", file->filename().characters(), file->error_string());
return 1;
}
for (;;) {
auto line = file.read_line(4096);
auto line = file->read_line(4096);
if (line.is_null())
break;
run_command(String::copy(line, Chomp));

View file

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

View file

@ -19,14 +19,14 @@ struct FileSystem {
int main(int, char**)
{
CFile file("/proc/df");
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "Failed to open /proc/df: %s\n", file.error_string());
auto file = CFile::construct("/proc/df");
if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "Failed to open /proc/df: %s\n", file->error_string());
return 1;
}
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();
json.for_each([](auto& value) {
auto fs_object = value.as_object();

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -17,14 +17,14 @@ static String read_var(const String& name)
builder.append("/proc/sys/");
builder.append(name);
auto path = builder.to_string();
CFile f(path);
if (!f.open(CIODevice::ReadOnly)) {
fprintf(stderr, "open: %s", f.error_string());
auto f = CFile::construct(path);
if (!f->open(CIODevice::ReadOnly)) {
fprintf(stderr, "open: %s", f->error_string());
exit(1);
}
const auto& b = f.read_all();
if (f.error() < 0) {
fprintf(stderr, "read: %s", f.error_string());
const auto& b = f->read_all();
if (f->error() < 0) {
fprintf(stderr, "read: %s", f->error_string());
exit(1);
}
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(name);
auto path = builder.to_string();
CFile f(path);
if (!f.open(CIODevice::WriteOnly)) {
fprintf(stderr, "open: %s", f.error_string());
auto f = CFile::construct(path);
if (!f->open(CIODevice::WriteOnly)) {
fprintf(stderr, "open: %s", f->error_string());
exit(1);
}
f.write(value);
if (f.error() < 0) {
fprintf(stderr, "write: %s", f.error_string());
f->write(value);
if (f->error() < 0) {
fprintf(stderr, "write: %s", f->error_string());
exit(1);
}
}

View file

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