1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:57:35 +00:00

IPCMagicLinter: Port to Core::File

This commit is contained in:
Lucas CHOLLET 2023-04-26 20:20:57 -04:00 committed by Andreas Kling
parent b35a6b206b
commit c50b072852

View file

@ -4,10 +4,11 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/DeprecatedString.h>
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/DeprecatedFile.h> #include <LibCore/File.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
// Exit code is bitwise-or of these values: // Exit code is bitwise-or of these values:
@ -25,32 +26,49 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
HashMap<u32, Vector<DeprecatedString>> inverse_hashes; HashMap<u32, Vector<DeprecatedString>> inverse_hashes;
bool had_errors = false; bool had_errors = false;
for (auto filename : arguments.strings.slice(1)) { for (auto filename : arguments.strings.slice(1)) {
auto file_or_error = Core::DeprecatedFile::open(filename, Core::OpenMode::ReadOnly);
auto const open_file = [](StringView filename) -> ErrorOr<NonnullOwnPtr<Core::BufferedFile>> {
auto file = TRY(Core::File::open(filename, Core::File::OpenMode::Read));
return Core::BufferedFile::create(move(file));
};
auto file_or_error = open_file(filename);
if (file_or_error.is_error()) { if (file_or_error.is_error()) {
warnln("Error: Cannot open '{}': {}", filename, file_or_error.error()); warnln("Error: Cannot open '{}': {}", filename, file_or_error.error());
had_errors = true; had_errors = true;
continue; // next file continue; // next file
} }
auto file = file_or_error.value();
auto file = file_or_error.release_value();
DeprecatedString endpoint_name; DeprecatedString endpoint_name;
while (true) {
DeprecatedString line = file->read_line(); auto const read_lines = [&]() -> ErrorOr<void> {
if (file->error() != 0 || line.is_null()) while (TRY(file->can_read_line())) {
break; Array<u8, 1024> buffer;
if (!line.starts_with("endpoint "sv)) auto line = TRY(file->read_line(buffer));
continue;
auto line_endpoint_name = line.substring("endpoint "sv.length()); if (!line.starts_with("endpoint "sv))
if (!endpoint_name.is_null()) { continue;
// Note: If there are three or more endpoints defined in a file, these errors will look a bit wonky. auto line_endpoint_name = line.substring_view("endpoint "sv.length());
// However, that's fine, because it shouldn't happen in the first place. if (!endpoint_name.is_null()) {
warnln("Error: Multiple endpoints in file '{}': Found {} and {}", filename, file->error()); // Note: If there are three or more endpoints defined in a file, these errors will look a bit wonky.
had_errors = true; // However, that's fine, because it shouldn't happen in the first place.
continue; // next line warnln("Error: Multiple endpoints in file '{}': Found {} and {}", filename, line_endpoint_name);
had_errors = true;
continue; // next line
}
endpoint_name = line_endpoint_name;
} }
endpoint_name = line_endpoint_name;
} return {};
if (file->error() != 0) { };
warnln("Error: Failed to read '{}': {}", filename, file->error());
auto maybe_error = read_lines();
if (maybe_error.is_error()) {
warnln("Error: Failed to read '{}': {}", filename, maybe_error.release_error());
had_errors = true; had_errors = true;
continue; // next file continue; // next file
} }