mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
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:
@@ -8,22 +8,21 @@
|
||||
|
||||
#include <units/angle.h>
|
||||
#include <units/length.h>
|
||||
#include <wpi/MemoryBuffer.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
using namespace frc;
|
||||
|
||||
AprilTagFieldLayout::AprilTagFieldLayout(std::string_view path) {
|
||||
std::error_code error_code;
|
||||
|
||||
wpi::raw_fd_istream input{path, error_code};
|
||||
if (error_code) {
|
||||
std::error_code ec;
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(path, ec);
|
||||
if (fileBuffer == nullptr || ec) {
|
||||
throw std::runtime_error(fmt::format("Cannot open file: {}", path));
|
||||
}
|
||||
|
||||
wpi::json json;
|
||||
input >> json;
|
||||
wpi::json json = wpi::json::parse({fileBuffer->begin(), fileBuffer->end()});
|
||||
|
||||
for (const auto& tag : json.at("tags").get<std::vector<AprilTag>>()) {
|
||||
m_apriltags[tag.ID] = tag;
|
||||
|
||||
@@ -10,10 +10,10 @@
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
#include <wpi/MemoryBuffer.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/fmt/raw_ostream.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "cameraserver/CameraServer.h"
|
||||
@@ -95,8 +95,9 @@ bool ReadCameraConfig(const wpi::json& config) {
|
||||
bool ReadConfig() {
|
||||
// open config file
|
||||
std::error_code ec;
|
||||
wpi::raw_fd_istream is(configFile, ec);
|
||||
if (ec) {
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(configFile, ec);
|
||||
if (fileBuffer == nullptr || ec) {
|
||||
fmt::print(stderr, "could not open '{}': {}\n", configFile, ec.message());
|
||||
return false;
|
||||
}
|
||||
@@ -104,7 +105,7 @@ bool ReadConfig() {
|
||||
// parse file
|
||||
wpi::json j;
|
||||
try {
|
||||
j = wpi::json::parse(is);
|
||||
j = wpi::json::parse({fileBuffer->begin(), fileBuffer->end()});
|
||||
} catch (const wpi::json::parse_error& e) {
|
||||
fmt::print(stderr, "config error in '{}': byte {}: {}\n", configFile,
|
||||
e.byte, e.what());
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -24,12 +24,12 @@
|
||||
#include <portable-file-dialogs.h>
|
||||
#include <units/angle.h>
|
||||
#include <units/length.h>
|
||||
#include <wpi/MemoryBuffer.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/StringMap.h>
|
||||
#include <wpi/fs.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
#include <wpigui.h>
|
||||
|
||||
#include "glass/Context.h"
|
||||
@@ -238,7 +238,7 @@ class FieldInfo {
|
||||
private:
|
||||
void Reset();
|
||||
bool LoadImageImpl(const std::string& fn);
|
||||
bool LoadJson(wpi::raw_istream& is, std::string_view filename);
|
||||
bool LoadJson(std::span<const char> is, std::string_view filename);
|
||||
void LoadJsonFile(std::string_view jsonfile);
|
||||
|
||||
std::unique_ptr<pfd::open_file> m_fileOpener;
|
||||
@@ -423,12 +423,11 @@ void FieldInfo::LoadImage() {
|
||||
for (auto&& field : fields::GetFields()) {
|
||||
if (field.name == m_builtin) {
|
||||
auto jsonstr = field.getJson();
|
||||
wpi::raw_mem_istream is{jsonstr.data(), jsonstr.size()};
|
||||
auto imagedata = field.getImage();
|
||||
auto texture = gui::Texture::CreateFromImage(
|
||||
reinterpret_cast<const unsigned char*>(imagedata.data()),
|
||||
imagedata.size());
|
||||
if (texture && LoadJson(is, {})) {
|
||||
if (texture && LoadJson({jsonstr.data(), jsonstr.size()}, {})) {
|
||||
m_texture = std::move(texture);
|
||||
m_imageWidth = m_texture.GetWidth();
|
||||
m_imageHeight = m_texture.GetHeight();
|
||||
@@ -445,11 +444,11 @@ void FieldInfo::LoadImage() {
|
||||
}
|
||||
}
|
||||
|
||||
bool FieldInfo::LoadJson(wpi::raw_istream& is, std::string_view filename) {
|
||||
bool FieldInfo::LoadJson(std::span<const char> is, std::string_view filename) {
|
||||
// parse file
|
||||
wpi::json j;
|
||||
try {
|
||||
j = wpi::json::parse(is);
|
||||
j = wpi::json::parse({is.data(), is.size()});
|
||||
} catch (const wpi::json::parse_error& e) {
|
||||
fmt::print(stderr, "GUI: JSON: could not parse: {}\n", e.what());
|
||||
return false;
|
||||
@@ -532,12 +531,15 @@ bool FieldInfo::LoadJson(wpi::raw_istream& is, std::string_view filename) {
|
||||
|
||||
void FieldInfo::LoadJsonFile(std::string_view jsonfile) {
|
||||
std::error_code ec;
|
||||
wpi::raw_fd_istream f(jsonfile, ec);
|
||||
if (ec) {
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(jsonfile, ec);
|
||||
if (fileBuffer == nullptr || ec) {
|
||||
std::fputs("GUI: could not open field JSON file\n", stderr);
|
||||
return;
|
||||
}
|
||||
LoadJson(f, jsonfile);
|
||||
LoadJson(
|
||||
{reinterpret_cast<const char*>(fileBuffer->begin()), fileBuffer->size()},
|
||||
jsonfile);
|
||||
}
|
||||
|
||||
bool FieldInfo::LoadImageImpl(const std::string& fn) {
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/MemoryBuffer.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/fs.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpinet/HttpUtil.h>
|
||||
#include <wpinet/HttpWebSocketServerConnection.h>
|
||||
@@ -310,9 +310,9 @@ void NetworkServer::HandleLocal() {
|
||||
|
||||
void NetworkServer::LoadPersistent() {
|
||||
std::error_code ec;
|
||||
auto size = fs::file_size(m_persistentFilename, ec);
|
||||
wpi::raw_fd_istream is{m_persistentFilename, ec};
|
||||
if (ec.value() != 0) {
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(m_persistentFilename, ec);
|
||||
if (fileBuffer == nullptr || ec.value() != 0) {
|
||||
INFO(
|
||||
"could not open persistent file '{}': {} "
|
||||
"(this can be ignored if you aren't expecting persistent values)",
|
||||
@@ -325,12 +325,8 @@ void NetworkServer::LoadPersistent() {
|
||||
}
|
||||
return;
|
||||
}
|
||||
is.readinto(m_persistentData, size);
|
||||
m_persistentData = std::string{fileBuffer->begin(), fileBuffer->end()};
|
||||
DEBUG4("read data: {}", m_persistentData);
|
||||
if (is.has_error()) {
|
||||
WARN("error reading persistent file");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkServer::SavePersistent(std::string_view filename,
|
||||
|
||||
@@ -9,10 +9,10 @@
|
||||
#include <string_view>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/MemoryBuffer.h>
|
||||
#include <wpi/SmallVector.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/fs.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
#include <wpinet/MimeTypes.h>
|
||||
#include <wpinet/UrlParser.h>
|
||||
#include <wpinet/raw_uv_ostream.h>
|
||||
@@ -125,8 +125,9 @@ void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText,
|
||||
}
|
||||
|
||||
// open file
|
||||
wpi::raw_fd_istream is{filename, ec, true};
|
||||
if (ec) {
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(filename, ec);
|
||||
if (fileBuffer == nullptr || ec) {
|
||||
MySendError(404, "error opening file");
|
||||
return;
|
||||
}
|
||||
@@ -142,16 +143,7 @@ void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText,
|
||||
wpi::SmallVector<uv::Buffer, 4> bodyData;
|
||||
wpi::raw_uv_ostream bodyOs{bodyData, 4096};
|
||||
|
||||
std::string fileBuf;
|
||||
size_t oldSize = 0;
|
||||
|
||||
while (fileBuf.size() < size) {
|
||||
oldSize = fileBuf.size();
|
||||
fileBuf.resize(oldSize + 1);
|
||||
is.read(&(*fileBuf.begin()) + oldSize, 1);
|
||||
}
|
||||
|
||||
bodyOs << fileBuf;
|
||||
bodyOs << fileBuffer->GetBuffer();
|
||||
|
||||
SendData(bodyOs.bufs(), false);
|
||||
if (!m_keepAlive) {
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
#include <system_error>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/MemoryBuffer.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
using namespace frc;
|
||||
@@ -29,15 +29,14 @@ void TrajectoryUtil::ToPathweaverJson(const Trajectory& trajectory,
|
||||
}
|
||||
|
||||
Trajectory TrajectoryUtil::FromPathweaverJson(std::string_view path) {
|
||||
std::error_code error_code;
|
||||
|
||||
wpi::raw_fd_istream input{path, error_code};
|
||||
if (error_code) {
|
||||
std::error_code ec;
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile(path, ec);
|
||||
if (fileBuffer == nullptr || ec) {
|
||||
throw std::runtime_error(fmt::format("Cannot open file: {}", path));
|
||||
}
|
||||
|
||||
wpi::json json;
|
||||
input >> json;
|
||||
wpi::json json = wpi::json::parse({fileBuffer->begin(), fileBuffer->end()});
|
||||
|
||||
return Trajectory{json.get<std::vector<Trajectory::State>>()};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user