diff --git a/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp b/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp index c0b4ca233bb..a205c0ffe6e 100644 --- a/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp +++ b/apriltag/src/main/native/cpp/AprilTagFieldLayout.cpp @@ -15,14 +15,12 @@ using namespace frc; AprilTagFieldLayout::AprilTagFieldLayout(std::string_view path) { - std::error_code ec; - std::unique_ptr fileBuffer = - wpi::MemoryBuffer::GetFile(path, ec); - if (fileBuffer == nullptr || ec) { + auto fileBuffer = wpi::MemoryBuffer::GetFile(path); + if (!fileBuffer) { throw std::runtime_error(fmt::format("Cannot open file: {}", path)); } - wpi::json json = wpi::json::parse(fileBuffer->GetCharBuffer()); + wpi::json json = wpi::json::parse(fileBuffer.value()->GetCharBuffer()); 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 011f8119f08..a794bbc1425 100644 --- a/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp +++ b/cameraserver/multiCameraServer/src/main/native/cpp/main.cpp @@ -94,18 +94,17 @@ bool ReadCameraConfig(const wpi::json& config) { bool ReadConfig() { // open config file - std::error_code ec; - std::unique_ptr fileBuffer = - wpi::MemoryBuffer::GetFile(configFile, ec); - if (fileBuffer == nullptr || ec) { - wpi::print(stderr, "could not open '{}': {}\n", configFile, ec.message()); + auto fileBuffer = wpi::MemoryBuffer::GetFile(configFile); + if (!fileBuffer) { + wpi::print(stderr, "could not open '{}': {}\n", configFile, + fileBuffer.error().message()); return false; } // parse file wpi::json j; try { - j = wpi::json::parse(fileBuffer->GetCharBuffer()); + j = wpi::json::parse(fileBuffer.value()->GetCharBuffer()); } catch (const wpi::json::parse_error& e) { wpi::print(stderr, "config error in '{}': byte {}: {}\n", configFile, e.byte, e.what()); diff --git a/datalogtool/src/main/native/cpp/Exporter.cpp b/datalogtool/src/main/native/cpp/Exporter.cpp index afca913e64d..188473d4e22 100644 --- a/datalogtool/src/main/native/cpp/Exporter.cpp +++ b/datalogtool/src/main/native/cpp/Exporter.cpp @@ -180,17 +180,16 @@ InputFile::~InputFile() { } static std::unique_ptr LoadDataLog(std::string_view filename) { - std::error_code ec; - auto buf = wpi::MemoryBuffer::GetFile(filename, ec); - std::string fn{filename}; - if (ec) { + auto fileBuffer = wpi::MemoryBuffer::GetFile(filename); + if (!fileBuffer) { return std::make_unique( - fn, fmt::format("Could not open file: {}", ec.message())); + filename, + fmt::format("Could not open file: {}", fileBuffer.error().message())); } - wpi::log::DataLogReader reader{std::move(buf)}; + wpi::log::DataLogReader reader{std::move(*fileBuffer)}; if (!reader.IsValid()) { - return std::make_unique(fn, "Not a valid datalog file"); + return std::make_unique(filename, "Not a valid datalog file"); } return std::make_unique( diff --git a/glass/src/lib/native/cpp/Context.cpp b/glass/src/lib/native/cpp/Context.cpp index 5adcd943bb1..1e4f2aa551a 100644 --- a/glass/src/lib/native/cpp/Context.cpp +++ b/glass/src/lib/native/cpp/Context.cpp @@ -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 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 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(); - 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(); + 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; } diff --git a/glass/src/lib/native/cpp/other/Field2D.cpp b/glass/src/lib/native/cpp/other/Field2D.cpp index be6cca6485f..efa80eab616 100644 --- a/glass/src/lib/native/cpp/other/Field2D.cpp +++ b/glass/src/lib/native/cpp/other/Field2D.cpp @@ -540,16 +540,14 @@ bool FieldInfo::LoadJson(std::span is, std::string_view filename) { } void FieldInfo::LoadJsonFile(std::string_view jsonfile) { - std::error_code ec; - std::unique_ptr 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(fileBuffer->begin()), fileBuffer->size()}, - jsonfile); + LoadJson({reinterpret_cast(fileBuffer.value()->begin()), + fileBuffer.value()->size()}, + jsonfile); } bool FieldInfo::LoadImageImpl(const std::string& fn) { diff --git a/hal/src/main/native/athena/HAL.cpp b/hal/src/main/native/athena/HAL.cpp index 7c5f1f64a97..7d04027133b 100644 --- a/hal/src/main/native/athena/HAL.cpp +++ b/hal/src/main/native/athena/HAL.cpp @@ -297,20 +297,15 @@ void HAL_GetSerialNumber(struct WPI_String* serialNumber) { void InitializeRoboRioComments(void) { if (!roboRioCommentsStringInitialized) { - std::error_code ec; - std::unique_ptr fileBuffer = - wpi::MemoryBuffer::GetFile("/etc/machine-info", ec); - - std::string_view fileContents; - if (fileBuffer && !ec) { - fileContents = - std::string_view(reinterpret_cast(fileBuffer->begin()), - fileBuffer->size()); - } else { + auto fileBuffer = wpi::MemoryBuffer::GetFile("/etc/machine-info"); + if (!fileBuffer) { roboRioCommentsStringSize = 0; roboRioCommentsStringInitialized = true; return; } + std::string_view fileContents{ + reinterpret_cast(fileBuffer.value()->begin()), + fileBuffer.value()->size()}; std::string_view searchString = "PRETTY_HOSTNAME=\""; size_t start = fileContents.find(searchString); diff --git a/ntcore/src/main/native/cpp/NetworkServer.cpp b/ntcore/src/main/native/cpp/NetworkServer.cpp index 04f2c052bdb..6158f264df3 100644 --- a/ntcore/src/main/native/cpp/NetworkServer.cpp +++ b/ntcore/src/main/native/cpp/NetworkServer.cpp @@ -351,15 +351,14 @@ void NetworkServer::HandleLocal() { } void NetworkServer::LoadPersistent() { - std::error_code ec; - std::unique_ptr fileBuffer = - wpi::MemoryBuffer::GetFile(m_persistentFilename, ec); - if (fileBuffer == nullptr || ec.value() != 0) { + auto fileBuffer = wpi::MemoryBuffer::GetFile(m_persistentFilename); + if (!fileBuffer) { INFO( "could not open persistent file '{}': {} " "(this can be ignored if you aren't expecting persistent values)", - m_persistentFilename, ec.message()); + m_persistentFilename, fileBuffer.error().message()); // backup file + std::error_code ec; fs::copy_file(m_persistentFilename, m_persistentFilename + ".bak", std::filesystem::copy_options::overwrite_existing, ec); // try to write an empty file so it doesn't happen again @@ -370,7 +369,8 @@ void NetworkServer::LoadPersistent() { } return; } - m_persistentData = std::string{fileBuffer->begin(), fileBuffer->end()}; + m_persistentData = + std::string{fileBuffer.value()->begin(), fileBuffer.value()->end()}; DEBUG4("read data: {}", m_persistentData); } 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 3567b720096..67be8967244 100644 --- a/simulation/halsim_ws_server/src/main/native/cpp/HALSimHttpConnection.cpp +++ b/simulation/halsim_ws_server/src/main/native/cpp/HALSimHttpConnection.cpp @@ -125,9 +125,8 @@ void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText, } // open file - std::unique_ptr fileBuffer = - wpi::MemoryBuffer::GetFile(filename, ec); - if (fileBuffer == nullptr || ec) { + auto fileBuffer = wpi::MemoryBuffer::GetFile(filename); + if (!fileBuffer) { MySendError(404, "error opening file"); return; } @@ -143,7 +142,7 @@ void HALSimHttpConnection::SendFileResponse(int code, std::string_view codeText, wpi::SmallVector bodyData; wpi::raw_uv_ostream bodyOs{bodyData, 4096}; - bodyOs << fileBuffer->GetBuffer(); + bodyOs << fileBuffer.value()->GetBuffer(); SendData(bodyOs.bufs(), false); if (!m_keepAlive) { diff --git a/sysid/src/main/native/cpp/analysis/AnalysisManager.cpp b/sysid/src/main/native/cpp/analysis/AnalysisManager.cpp index b76b4414e5c..4415318d52f 100644 --- a/sysid/src/main/native/cpp/analysis/AnalysisManager.cpp +++ b/sysid/src/main/native/cpp/analysis/AnalysisManager.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include diff --git a/sysid/src/main/native/cpp/view/LogLoader.cpp b/sysid/src/main/native/cpp/view/LogLoader.cpp index fdaa3af11be..6acf00294bd 100644 --- a/sysid/src/main/native/cpp/view/LogLoader.cpp +++ b/sysid/src/main/native/cpp/view/LogLoader.cpp @@ -35,15 +35,15 @@ void LogLoader::Display() { if (!m_opener->result().empty()) { m_filename = m_opener->result()[0]; - std::error_code ec; - auto buf = wpi::MemoryBuffer::GetFile(m_filename, ec); - if (ec) { + auto fileBuffer = wpi::MemoryBuffer::GetFile(m_filename); + if (!fileBuffer) { ImGui::OpenPopup("Error"); - m_error = fmt::format("Could not open file: {}", ec.message()); + m_error = fmt::format("Could not open file: {}", + fileBuffer.error().message()); return; } - wpi::log::DataLogReader reader{std::move(buf)}; + wpi::log::DataLogReader reader{std::move(*fileBuffer)}; if (!reader.IsValid()) { ImGui::OpenPopup("Error"); m_error = "Not a valid datalog file"; diff --git a/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp b/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp index d9cb853a5bf..a78bae641fd 100644 --- a/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp +++ b/wpimath/src/main/native/cpp/trajectory/TrajectoryUtil.cpp @@ -28,14 +28,12 @@ void TrajectoryUtil::ToPathweaverJson(const Trajectory& trajectory, } Trajectory TrajectoryUtil::FromPathweaverJson(std::string_view path) { - std::error_code ec; - std::unique_ptr fileBuffer = - wpi::MemoryBuffer::GetFile(path, ec); - if (fileBuffer == nullptr || ec) { + auto fileBuffer = wpi::MemoryBuffer::GetFile(path); + if (!fileBuffer) { throw std::runtime_error(fmt::format("Cannot open file: {}", path)); } - wpi::json json = wpi::json::parse(fileBuffer->GetCharBuffer()); + wpi::json json = wpi::json::parse(fileBuffer.value()->GetCharBuffer()); return Trajectory{json.get>()}; } diff --git a/wpiutil/build.gradle b/wpiutil/build.gradle index f8ff8778f06..a5a1ab8295b 100644 --- a/wpiutil/build.gradle +++ b/wpiutil/build.gradle @@ -32,7 +32,7 @@ ext { include '*.cpp' } exportedHeaders { - srcDirs 'src/main/native/include', 'src/main/native/thirdparty/llvm/include', 'src/main/native/thirdparty/json/include', 'src/main/native/thirdparty/fmtlib/include' + srcDirs 'src/main/native/include', 'src/main/native/thirdparty/fmtlib/include', 'src/main/native/thirdparty/json/include', 'src/main/native/thirdparty/llvm/include' } } llvmCpp(CppSourceSet) { @@ -41,7 +41,7 @@ ext { include '**/*.cpp' } exportedHeaders { - srcDirs 'src/main/native/include', 'src/main/native/thirdparty/llvm/include', 'src/main/native/thirdparty/fmtlib/include' + srcDirs 'src/main/native/include', 'src/main/native/thirdparty/expected/include', 'src/main/native/thirdparty/fmtlib/include', 'src/main/native/thirdparty/llvm/include' } } mpackCpp(CppSourceSet) { diff --git a/wpiutil/examples/printlog/printlog.cpp b/wpiutil/examples/printlog/printlog.cpp index f89be5f5482..11bed9015b2 100644 --- a/wpiutil/examples/printlog/printlog.cpp +++ b/wpiutil/examples/printlog/printlog.cpp @@ -18,12 +18,13 @@ int main(int argc, const char** argv) { wpi::print(stderr, "Usage: printlog \n"); return EXIT_FAILURE; } - std::error_code ec; - wpi::log::DataLogReader reader{wpi::MemoryBuffer::GetFile(argv[1], ec)}; - if (ec) { - wpi::print(stderr, "could not open file: {}\n", ec.message()); + auto fileBuffer = wpi::MemoryBuffer::GetFile(argv[1]); + if (!fileBuffer) { + wpi::print(stderr, "could not open file: {}\n", + fileBuffer.error().message()); return EXIT_FAILURE; } + wpi::log::DataLogReader reader{std::move(*fileBuffer)}; if (!reader) { wpi::print(stderr, "not a log file\n"); return EXIT_FAILURE; diff --git a/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp b/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp index 3ee53a2d832..158a256855e 100644 --- a/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp +++ b/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp @@ -259,10 +259,14 @@ static std::unique_ptr GetMemoryBufferForStream( return GetMemBufferCopyImpl(buffer, bufferName, ec); } -std::unique_ptr MemoryBuffer::GetFile(std::string_view filename, - std::error_code& ec, - int64_t fileSize) { - return GetFileAux(filename, ec, fileSize, fileSize, 0); +wpi::expected, std::error_code> +MemoryBuffer::GetFile(std::string_view filename, int64_t fileSize) { + std::error_code ec; + auto ret = GetFileAux(filename, ec, fileSize, fileSize, 0); + if (ec) { + return wpi::unexpected{ec}; + } + return ret; } template diff --git a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h index b5eaea406f2..b3b62809603 100644 --- a/wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h +++ b/wpiutil/src/main/native/thirdparty/llvm/include/wpi/MemoryBuffer.h @@ -24,6 +24,7 @@ #include #include #include +#include // Duplicated from fs.h to avoid a dependency namespace fs { @@ -77,9 +78,8 @@ class MemoryBuffer { /// if successful, otherwise returning null. If FileSize is specified, this /// means that the client knows that the file exists and that it has the /// specified size. - static std::unique_ptr GetFile(std::string_view filename, - std::error_code& ec, - int64_t fileSize = -1); + static wpi::expected, std::error_code> + GetFile(std::string_view filename, int64_t fileSize = -1); /// Read all of the specified file into a MemoryBuffer as a stream /// (i.e. until EOF reached). This is useful for special files that