Skip to content

Commit

Permalink
LibCore: Convert CFile to ObjectPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Sep 21, 2019
1 parent 31b38ed commit 8d550c1
Show file tree
Hide file tree
Showing 30 changed files with 135 additions and 134 deletions.
8 changes: 4 additions & 4 deletions Applications/Piano/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions Applications/SystemMonitor/DevicesModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions Applications/SystemMonitor/MemoryStatsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Applications/SystemMonitor/MemoryStatsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class MemoryStatsWidget final : public GWidget {
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;
};
8 changes: 4 additions & 4 deletions Applications/SystemMonitor/ProcessStacksWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
6 changes: 3 additions & 3 deletions Applications/TextEditor/TextEditorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
8 changes: 4 additions & 4 deletions DevTools/FormCompiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
8 changes: 4 additions & 4 deletions DevTools/IPCCompiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 6 additions & 6 deletions DevTools/VisualBuilder/VBForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions Libraries/LibAudio/AWavLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;

Expand All @@ -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 { \
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibAudio/AWavLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AWavLoader {

private:
bool parse_header();
CFile m_file;
ObjectPtr<CFile> m_file;
String m_error_string;

u32 m_sample_rate { 0 };
Expand Down
8 changes: 4 additions & 4 deletions Libraries/LibCore/CConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
11 changes: 6 additions & 5 deletions Libraries/LibCore/CFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -25,6 +20,12 @@ class CFile final : public CIODevice {
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 };
};
8 changes: 4 additions & 4 deletions Libraries/LibCore/CProcessStatisticsReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions Libraries/LibGUI/GJsonArrayModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions Libraries/LibHTML/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions Servers/AudioServer/ASMixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -88,7 +88,7 @@ void ASMixer::mix()

if (stream.offset() != 0) {
buffer.trim(stream.offset());
m_device.write(buffer);
m_device->write(buffer);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Servers/AudioServer/ASMixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ASMixer : public CObject {
private:
Vector<NonnullRefPtr<ASBufferQueue>> m_pending_mixing;

CFile m_device;
ObjectPtr<CFile> m_device;
LibThread::Lock m_lock;

LibThread::Thread m_sound_thread;
Expand Down
8 changes: 4 additions & 4 deletions Servers/LookupServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions Servers/SystemServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 8d550c1

Please sign in to comment.