Skip to content

Commit c66e043

Browse files
Add fileToString utility function
1 parent c5023d3 commit c66e043

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

tesseract_common/include/tesseract_common/utils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ std::string strFormat(const std::string& format, Args... args)
6767
return std::string{ buf.get(), buf.get() + size - 1 }; // We don't want the '\0' inside
6868
}
6969

70+
/**
71+
* @brief Read in the contents of the file into a string
72+
* @param filepath The file to read
73+
* @return The contents of the file
74+
*/
75+
std::string fileToString(const tesseract_common::fs::path& filepath);
76+
7077
/**
7178
* @brief Change the reference point of the provided Twist
7279
* @param twist The current Twist which gets modified in place

tesseract_common/src/utils.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
3030
#include <string>
3131
#include <type_traits>
3232
#include <console_bridge/console.h>
33+
#include <fstream>
3334
TESSERACT_COMMON_IGNORE_WARNINGS_POP
3435

3536
#include <tesseract_common/utils.h>
@@ -68,6 +69,20 @@ std::enable_if_t<std::is_polymorphic<E>::value> my_rethrow_if_nested(const E& e)
6869
p->rethrow_nested();
6970
}
7071

72+
std::string fileToString(const tesseract_common::fs::path& filepath)
73+
{
74+
std::ifstream ifs(filepath.c_str());
75+
std::string contents;
76+
77+
ifs.seekg(0, std::ios::end);
78+
contents.reserve(ifs.tellg());
79+
ifs.seekg(0, std::ios::beg);
80+
81+
contents.assign(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
82+
ifs.close();
83+
return contents;
84+
}
85+
7186
// LCOV_EXCL_START
7287
// These are tested in both tesseract_kinematics and tesseract_state_solver
7388
void twistChangeRefPoint(Eigen::Ref<Eigen::VectorXd> twist, const Eigen::Ref<const Eigen::Vector3d>& ref_point)

0 commit comments

Comments
 (0)