1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:17:45 +00:00

LibUnicode: Port the CLDR locale generator to the stream API

This adds a generator utility to read an entire file and parse it as a
JSON value. This is heavily used by the CLDR generators. The idea here
is to put the file reading details in the utility so that when we have a
good story for generically reading an entire stream in LibCore, we can
update the generators to use that by only touching this helper.
This commit is contained in:
Timothy Flynn 2022-02-06 15:55:17 -05:00 committed by Tim Flynn
parent a64a7940e4
commit a338e9403b
2 changed files with 44 additions and 61 deletions

View file

@ -9,11 +9,13 @@
#include <AK/Format.h>
#include <AK/HashFunctions.h>
#include <AK/HashMap.h>
#include <AK/JsonValue.h>
#include <AK/LexicalPath.h>
#include <AK/Optional.h>
#include <AK/QuickSort.h>
#include <AK/SourceGenerator.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/Traits.h>
#include <AK/Vector.h>
@ -248,6 +250,22 @@ inline ErrorOr<NonnullOwnPtr<Core::Stream::BufferedFile>> open_file(StringView p
return Core::Stream::BufferedFile::create(move(file));
}
inline ErrorOr<JsonValue> read_json_file(StringView path)
{
auto file = TRY(open_file(path, Core::Stream::OpenMode::Read));
StringBuilder builder;
Array<u8, 4096> buffer;
// FIXME: When Core::Stream supports reading an entire file, use that.
while (TRY(file->can_read_line())) {
auto nread = TRY(file->read(buffer));
TRY(builder.try_append(reinterpret_cast<char const*>(buffer.data()), nread));
}
return JsonValue::from_string(builder.build());
}
inline ErrorOr<Core::DirIterator> path_to_dir_iterator(String path, StringView subpath = "main"sv)
{
LexicalPath lexical_path(move(path));