mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpiutil] Return wpi::expected from MemoryBuffer::GetFile (#7069)
This commit is contained in:
@@ -128,50 +128,44 @@ static bool JsonToWindow(const wpi::json& jfile, const char* filename) {
|
||||
}
|
||||
|
||||
static bool LoadWindowStorageImpl(const std::string& filename) {
|
||||
std::error_code ec;
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(filename, ec);
|
||||
if (fileBuffer == nullptr || ec) {
|
||||
auto fileBuffer = wpi::MemoryBuffer::GetFile(filename);
|
||||
if (!fileBuffer) {
|
||||
ImGui::LogText("error opening %s: %s", filename.c_str(),
|
||||
ec.message().c_str());
|
||||
fileBuffer.error().message().c_str());
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return JsonToWindow(wpi::json::parse(fileBuffer.value()->GetCharBuffer()),
|
||||
filename.c_str());
|
||||
} catch (wpi::json::parse_error& e) {
|
||||
ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what());
|
||||
return false;
|
||||
} else {
|
||||
try {
|
||||
return JsonToWindow(wpi::json::parse(fileBuffer->GetCharBuffer()),
|
||||
filename.c_str());
|
||||
} catch (wpi::json::parse_error& e) {
|
||||
ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool LoadStorageRootImpl(Context* ctx, const std::string& filename,
|
||||
std::string_view rootName) {
|
||||
std::error_code ec;
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(filename, ec);
|
||||
if (fileBuffer == nullptr || ec) {
|
||||
auto fileBuffer = wpi::MemoryBuffer::GetFile(filename);
|
||||
if (!fileBuffer) {
|
||||
ImGui::LogText("error opening %s: %s", filename.c_str(),
|
||||
ec.message().c_str());
|
||||
fileBuffer.error().message().c_str());
|
||||
return false;
|
||||
} else {
|
||||
auto& storage = ctx->storageRoots[rootName];
|
||||
bool createdStorage = false;
|
||||
if (!storage) {
|
||||
storage = std::make_unique<Storage>();
|
||||
createdStorage = true;
|
||||
}
|
||||
try {
|
||||
storage->FromJson(wpi::json::parse(fileBuffer->GetCharBuffer()),
|
||||
filename.c_str());
|
||||
} catch (wpi::json::parse_error& e) {
|
||||
ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what());
|
||||
if (createdStorage) {
|
||||
ctx->storageRoots.erase(rootName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
auto& storage = ctx->storageRoots[rootName];
|
||||
bool createdStorage = false;
|
||||
if (!storage) {
|
||||
storage = std::make_unique<Storage>();
|
||||
createdStorage = true;
|
||||
}
|
||||
try {
|
||||
storage->FromJson(wpi::json::parse(fileBuffer.value()->GetCharBuffer()),
|
||||
filename.c_str());
|
||||
} catch (wpi::json::parse_error& e) {
|
||||
ImGui::LogText("Error loading %s: %s", filename.c_str(), e.what());
|
||||
if (createdStorage) {
|
||||
ctx->storageRoots.erase(rootName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -540,16 +540,14 @@ bool FieldInfo::LoadJson(std::span<const char> is, std::string_view filename) {
|
||||
}
|
||||
|
||||
void FieldInfo::LoadJsonFile(std::string_view jsonfile) {
|
||||
std::error_code ec;
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(jsonfile, ec);
|
||||
if (fileBuffer == nullptr || ec) {
|
||||
auto fileBuffer = wpi::MemoryBuffer::GetFile(jsonfile);
|
||||
if (!fileBuffer) {
|
||||
std::fputs("GUI: could not open field JSON file\n", stderr);
|
||||
return;
|
||||
}
|
||||
LoadJson(
|
||||
{reinterpret_cast<const char*>(fileBuffer->begin()), fileBuffer->size()},
|
||||
jsonfile);
|
||||
LoadJson({reinterpret_cast<const char*>(fileBuffer.value()->begin()),
|
||||
fileBuffer.value()->size()},
|
||||
jsonfile);
|
||||
}
|
||||
|
||||
bool FieldInfo::LoadImageImpl(const std::string& fn) {
|
||||
|
||||
Reference in New Issue
Block a user