Add filesystem utils functions
This commit is contained in:
@ -397,6 +397,51 @@ bool utils::is_double(const std::string & str) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
strip off leading part of path, return just the filename
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
std::string utils::path_basename(const std::string & path) {
|
||||
#if defined(_WIN32)
|
||||
size_t start = path.find_last_of('/\\');
|
||||
#else
|
||||
size_t start = path.find_last_of('/');
|
||||
#endif
|
||||
|
||||
if (start == std::string::npos) {
|
||||
start = 0;
|
||||
} else {
|
||||
start += 1;
|
||||
}
|
||||
|
||||
return path.substr(start);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
join two paths
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
std::string utils::path_join(const std::string & a, const std::string & b) {
|
||||
#if defined(_WIN32)
|
||||
return fmt::format("{}\\{}", a, b);
|
||||
#else
|
||||
return fmt::format("{}/{}", a, b);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
try to open file for reading
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
bool utils::file_is_readable(const std::string & path) {
|
||||
FILE * fp = fopen(path.c_str(), "r");
|
||||
if(fp) {
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
extern "C" {
|
||||
|
||||
22
src/utils.h
22
src/utils.h
@ -163,6 +163,28 @@ namespace LAMMPS_NS {
|
||||
* \return true, if string contains valid floating-point number, false otherwise
|
||||
*/
|
||||
bool is_double(const std::string & str);
|
||||
|
||||
/**
|
||||
* \brief Strip off leading part of path, return just the filename
|
||||
* \param path file path
|
||||
* \return file name
|
||||
*/
|
||||
std::string path_basename(const std::string & path);
|
||||
|
||||
/**
|
||||
* \brief Join two paths
|
||||
* \param a first path
|
||||
* \param b second path
|
||||
* \return combined path
|
||||
*/
|
||||
std::string path_join(const std::string & a, const std::string & b);
|
||||
|
||||
/**
|
||||
* \brief Check if file exists and is readable
|
||||
* \param path file path
|
||||
* \return true if file exists and is readable
|
||||
*/
|
||||
bool file_is_readable(const std::string & path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -182,3 +182,19 @@ TEST(Utils, strmatch_char_range) {
|
||||
TEST(Utils, strmatch_opt_range) {
|
||||
ASSERT_TRUE(utils::strmatch("rigid","^[0-9]*[p-s]igid"));
|
||||
}
|
||||
|
||||
TEST(Utils, path_join) {
|
||||
#if defined(_WIN32)
|
||||
ASSERT_THAT(utils::path_join("c:\\parent\\folder", "filename"), Eq("c:\\parent\\folder\\filename"));
|
||||
#else
|
||||
ASSERT_THAT(utils::path_join("/parent/folder", "filename"), Eq("/parent/folder/filename"));
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(Utils, path_basename) {
|
||||
#if defined(_WIN32)
|
||||
ASSERT_THAT(utils::path_basename("c:\\parent\\folder\\filename"), Eq("filename"));
|
||||
#else
|
||||
ASSERT_THAT(utils::path_basename("/parent/folder/filename"), Eq("filename"));
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user