diff --git a/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp b/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp index 31e4cacb83..c14df6353c 100644 --- a/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp +++ b/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp @@ -8,22 +8,21 @@ #include #include +#include #include -#include #include 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 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>()) { m_apriltags[tag.ID] = tag; diff --git a/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp b/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp index 2a87baa29f..bd4ae2e77c 100644 --- a/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp +++ b/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp @@ -10,10 +10,10 @@ #include #include +#include #include #include #include -#include #include #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 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()); diff --git a/glass/src/lib/native/cpp/Context.cpp b/glass/src/lib/native/cpp/Context.cpp index 036c38d172..4b9837f12b 100644 --- a/glass/src/lib/native/cpp/Context.cpp +++ b/glass/src/lib/native/cpp/Context.cpp @@ -12,11 +12,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include @@ -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 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 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) { diff --git a/glass/src/lib/native/cpp/other/Field2D.cpp b/glass/src/lib/native/cpp/other/Field2D.cpp index 67946da6fd..1d1cee7c1f 100644 --- a/glass/src/lib/native/cpp/other/Field2D.cpp +++ b/glass/src/lib/native/cpp/other/Field2D.cpp @@ -24,12 +24,12 @@ #include #include #include +#include #include #include #include #include #include -#include #include #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 is, std::string_view filename); void LoadJsonFile(std::string_view jsonfile); std::unique_ptr 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(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 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 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(fileBuffer->begin()), fileBuffer->size()}, + jsonfile); } bool FieldInfo::LoadImageImpl(const std::string& fn) { diff --git a/ntcore/src/main/native/cpp/NetworkServer.cpp b/ntcore/src/main/native/cpp/NetworkServer.cpp index f3e26535df..e8bfd16669 100644 --- a/ntcore/src/main/native/cpp/NetworkServer.cpp +++ b/ntcore/src/main/native/cpp/NetworkServer.cpp @@ -11,11 +11,11 @@ #include #include +#include #include #include #include #include -#include #include #include #include @@ -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 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, diff --git a/simulation/halsim_ws_server/src/main/native/cpp/HALSimHttpConnection.cpp b/simulation/halsim_ws_server/src/main/native/cpp/HALSimHttpConnection.cpp index ba4212171c..ee188b3208 100644 --- a/simulation/halsim_ws_server/src/main/native/cpp/HALSimHttpConnection.cpp +++ b/simulation/halsim_ws_server/src/main/native/cpp/HALSimHttpConnection.cpp @@ -9,10 +9,10 @@ #include #include +#include #include #include #include -#include #include #include #include @@ -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 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 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) { diff --git a/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp b/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp index 169b642a6c..23b17daf72 100644 --- a/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp +++ b/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp @@ -7,9 +7,9 @@ #include #include +#include #include #include -#include #include 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 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>()}; }