Skip to content

Commit

Permalink
replace boost filesystem exists (#5907)
Browse files Browse the repository at this point in the history
* replace boost filesystem exists

* fix clang-tidy errors

* revert some changes

* fix some typos

* address review changes

* address more review changes

* Review changes
  • Loading branch information
cybaol authored Jan 5, 2024
1 parent 6711098 commit 6e51b16
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
12 changes: 9 additions & 3 deletions io/src/ifs_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

#include <cstring>
#include <cerrno>
#include <boost/filesystem.hpp> // for exists
#include <boost/iostreams/device/mapped_file.hpp> // for mapped_file_source

///////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -60,15 +59,22 @@ pcl::IFSReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &c
//cloud.is_dense = true;

std::uint32_t nr_points = 0;

if (file_name.empty ())
{
PCL_ERROR ("[pcl::IFSReader::readHeader] No file name given!\n");
return (-1);
}

std::ifstream fs;
fs.open (file_name.c_str (), std::ios::binary);

if (file_name.empty() || !boost::filesystem::exists (file_name))
if (!fs.good ())
{
PCL_ERROR ("[pcl::IFSReader::readHeader] Could not find file '%s'.\n", file_name.c_str ());
return (-1);
}

fs.open (file_name.c_str (), std::ios::binary);
if (!fs.is_open () || fs.fail ())
{
PCL_ERROR ("[pcl::IFSReader::readHeader] Could not open file '%s'! Error : %s\n", file_name.c_str (), strerror(errno));
Expand Down
36 changes: 29 additions & 7 deletions io/src/obj_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,20 @@ int
pcl::MTLReader::read (const std::string& obj_file_name,
const std::string& mtl_file_name)
{
if (obj_file_name.empty() || !boost::filesystem::exists (obj_file_name))
if (obj_file_name.empty ())
{
PCL_ERROR ("[pcl::MTLReader::read] No OBJ file name given!\n");
return (-1);
}

if (!boost::filesystem::exists (obj_file_name))
{
PCL_ERROR ("[pcl::MTLReader::read] Could not find file '%s'!\n",
obj_file_name.c_str ());
return (-1);
}

if (mtl_file_name.empty())
if (mtl_file_name.empty ())
{
PCL_ERROR ("[pcl::MTLReader::read] MTL file name is empty!\n");
return (-1);
Expand All @@ -168,14 +174,23 @@ pcl::MTLReader::read (const std::string& obj_file_name,
int
pcl::MTLReader::read (const std::string& mtl_file_path)
{
if (mtl_file_path.empty() || !boost::filesystem::exists (mtl_file_path))
if (mtl_file_path.empty ())
{
PCL_ERROR ("[pcl::MTLReader::read] Could not find file '%s'.\n", mtl_file_path.c_str ());
PCL_ERROR ("[pcl::MTLReader::read] No file name given!\n");
return (-1);
}

// Open file in binary mode to avoid problem of
// std::getline() corrupting the result of ifstream::tellg()
std::ifstream mtl_file;
mtl_file.open (mtl_file_path.c_str (), std::ios::binary);

if (!mtl_file.good ())
{
PCL_ERROR ("[pcl::MTLReader::read] Could not find file '%s'.\n", mtl_file_path.c_str ());
return (-1);
}

if (!mtl_file.is_open () || mtl_file.fail ())
{
PCL_ERROR ("[pcl::MTLReader::read] Could not open file '%s'! Error : %s\n",
Expand Down Expand Up @@ -340,18 +355,25 @@ pcl::OBJReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &c
data_type = 0;
data_idx = offset;

std::ifstream fs;
std::string line;

if (file_name.empty() || !boost::filesystem::exists (file_name))
if (file_name.empty ())
{
PCL_ERROR ("[pcl::OBJReader::readHeader] Could not find file '%s'.\n", file_name.c_str ());
PCL_ERROR ("[pcl::OBJReader::readHeader] No file name given!\n");
return (-1);
}

// Open file in binary mode to avoid problem of
// std::getline() corrupting the result of ifstream::tellg()
std::ifstream fs;
fs.open (file_name.c_str (), std::ios::binary);

if (!fs.good ())
{
PCL_ERROR ("[pcl::OBJReader::readHeader] Could not find file '%s'.\n", file_name.c_str ());
return (-1);
}

if (!fs.is_open () || fs.fail ())
{
PCL_ERROR ("[pcl::OBJReader::readHeader] Could not open file '%s'! Error : %s\n", file_name.c_str (), strerror(errno));
Expand Down
19 changes: 16 additions & 3 deletions io/src/pcd_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,23 @@ pcl::PCDReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &c
Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
int &pcd_version, int &data_type, unsigned int &data_idx, const int offset)
{
if (file_name.empty() || !boost::filesystem::exists (file_name))
if (file_name.empty ())
{
PCL_ERROR ("[pcl::PCDReader::readHeader] Could not find file '%s'.\n", file_name.c_str ());
PCL_ERROR ("[pcl::PCDReader::readHeader] No file name given!\n");
return (-1);
}

// Open file in binary mode to avoid problem of
// std::getline() corrupting the result of ifstream::tellg()
std::ifstream fs;
fs.open (file_name.c_str (), std::ios::binary);

if (!fs.good ())
{
PCL_ERROR ("[pcl::PCDReader::readHeader] Could not find file '%s'.\n", file_name.c_str ());
return (-1);
}

if (!fs.is_open () || fs.fail ())
{
PCL_ERROR ("[pcl::PCDReader::readHeader] Could not open file '%s'! Error : %s\n", file_name.c_str (), strerror (errno));
Expand Down Expand Up @@ -664,7 +671,13 @@ pcl::PCDReader::read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
pcl::console::TicToc tt;
tt.tic ();

if (file_name.empty() || !boost::filesystem::exists (file_name))
if (file_name.empty ())
{
PCL_ERROR ("[pcl::PCDReader::read] No file name given!\n");
return (-1);
}

if (!boost::filesystem::exists (file_name))
{
PCL_ERROR ("[pcl::PCDReader::read] Could not find file '%s'.\n", file_name.c_str ());
return (-1);
Expand Down

0 comments on commit 6e51b16

Please sign in to comment.