Replace wpi::raw_istream with wpi::MemoryBuffer (#5675)

Instances of wpi::raw_istream were left that are reading incrementally
from file descriptors like USB devices.
This commit is contained in:
Tyler Veness
2023-09-21 23:20:09 -07:00
committed by GitHub
parent 1b6ec5a95d
commit 5ab54ff760
7 changed files with 51 additions and 56 deletions

View File

@@ -12,11 +12,11 @@
#include <imgui.h>
#include <imgui_internal.h>
#include <imgui_stdlib.h>
#include <wpi/MemoryBuffer.h>
#include <wpi/StringExtras.h>
#include <wpi/fs.h>
#include <wpi/json.h>
#include <wpi/json_serializer.h>
#include <wpi/raw_istream.h>
#include <wpi/raw_ostream.h>
#include <wpi/timestamp.h>
#include <wpigui.h>
@@ -130,14 +130,17 @@ static bool JsonToWindow(const wpi::json& jfile, const char* filename) {
static bool LoadWindowStorageImpl(const std::string& filename) {
std::error_code ec;
wpi::raw_fd_istream is{filename, ec};
if (ec) {
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
wpi::MemoryBuffer::GetFile(filename, ec);
if (fileBuffer == nullptr || ec) {
ImGui::LogText("error opening %s: %s", filename.c_str(),
ec.message().c_str());
return false;
} else {
try {
return JsonToWindow(wpi::json::parse(is), filename.c_str());
return JsonToWindow(
wpi::json::parse({fileBuffer->begin(), fileBuffer->end()}),
filename.c_str());
} catch (wpi::json::parse_error& e) {
ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what());
return false;
@@ -148,8 +151,9 @@ static bool LoadWindowStorageImpl(const std::string& filename) {
static bool LoadStorageRootImpl(Context* ctx, const std::string& filename,
std::string_view rootName) {
std::error_code ec;
wpi::raw_fd_istream is{filename, ec};
if (ec) {
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
wpi::MemoryBuffer::GetFile(filename, ec);
if (fileBuffer == nullptr || ec) {
ImGui::LogText("error opening %s: %s", filename.c_str(),
ec.message().c_str());
return false;
@@ -161,7 +165,9 @@ static bool LoadStorageRootImpl(Context* ctx, const std::string& filename,
createdStorage = true;
}
try {
storage->FromJson(wpi::json::parse(is), filename.c_str());
storage->FromJson(
wpi::json::parse({fileBuffer->begin(), fileBuffer->end()}),
filename.c_str());
} catch (wpi::json::parse_error& e) {
ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what());
if (createdStorage) {