From 2476a66cd91c47142030387153473dbb182fccf2 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 17 Jun 2021 15:02:48 -0700 Subject: [PATCH] Revert "util.cc: refactor read_file (#21295)" This reverts commit 32dd5c2454e3f3fca028dacd24bc4758e1134295. old-commit-hash: 7dec2064370255e00f719ea790c26bd00edbdb6f --- selfdrive/common/util.cc | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/selfdrive/common/util.cc b/selfdrive/common/util.cc index ab623caed4..af87d9c22f 100644 --- a/selfdrive/common/util.cc +++ b/selfdrive/common/util.cc @@ -54,24 +54,26 @@ int set_core_affinity(int core) { namespace util { std::string read_file(const std::string& fn) { - std::ifstream f(fn, std::ios::binary | std::ios::in | std::ios::ate); - if (f) { - int pos = f.tellg(); + std::ifstream ifs(fn, std::ios::binary | std::ios::ate); + if (ifs) { + int pos = ifs.tellg(); if (pos > 0) { std::string result; result.resize(pos); - f.seekg(0, std::ios::beg); - if (f.read(result.data(), pos)) { + ifs.seekg(0, std::ios::beg); + ifs.read(result.data(), pos); + if (ifs) { return result; } - } else { - // fallback for files created on read, e.g. procfs - std::stringstream buffer; - buffer << f.rdbuf(); - return buffer.str(); } } - return std::string(); + ifs.close(); + + // fallback for files created on read, e.g. procfs + std::ifstream f(fn); + std::stringstream buffer; + buffer << f.rdbuf(); + return buffer.str(); } int read_files_in_dir(const std::string &path, std::map *contents) {