Skip to content

Commit

Permalink
Relative Path
Browse files Browse the repository at this point in the history
Relative Path

Add List Dir utility function

Print

Print again

Print again

Squash this
  • Loading branch information
kartikdutt18 committed May 27, 2020
1 parent 162baa6 commit cf9453b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
32 changes: 32 additions & 0 deletions tests/utils_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,47 @@ BOOST_AUTO_TEST_CASE(RemoveFileTest)

BOOST_AUTO_TEST_CASE(ExtractFilesTest)
{
std::vector<boost::filesystem::path> vec;
Utils::ListDir("./../", vec);
for(auto i : vec)
{
mlpack::Log::Warn << i << std::endl;
}
Utils::DownloadFile("/datasets/mnist.tar.gz", "./../data/mnist.tar.gz", "",
false, true, "www.mlpack.org", true, "./../data/");

vec.clear();
Utils::ListDir("./../data/", vec);
for(auto i : vec)
{
mlpack::Log::Warn << i << std::endl;
}
vec.clear();
Utils::ListDir("./../", vec);
for(auto i : vec)
{
mlpack::Log::Warn << i << std::endl;
}
vec.clear();
Utils::ListDir("./../build/", vec);
for (auto i : vec)
{
mlpack::Log::Warn << i << std::endl;
}
vec.clear();
Utils::ListDir("./../../", vec);
for (auto i : vec)
{
mlpack::Log::Warn << i << std::endl;
}
BOOST_REQUIRE(Utils::PathExists("./../data/mnist.tar.gz"));
BOOST_REQUIRE(Utils::PathExists("./../data/mnist_all.csv"));
BOOST_REQUIRE(Utils::PathExists("./../data/mnist.tar.gz"));

// Clean up.
Utils::RemoveFile("./../data/mnist_all.csv");
Utils::RemoveFile("./../data/mnist_all_centroids.csv");
Utils::RemoveFile("./../data/mnist.tar.gz");
}

BOOST_AUTO_TEST_SUITE_END();
35 changes: 32 additions & 3 deletions utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ class Utils
std::replace(pathForExtractionTemp.begin(), pathForExtractionTemp.end(),
'/', '\\');

command = "tar --force-local -xvzf " +
boost::filesystem::current_path().string() + "\\" +
pathToArchiveTemp + "-C " + pathForExtractionTemp + " --strip-components=1";
command = "tar --force-local -xvzf " + pathToArchiveTemp +
" --strip-components=1";
std::cout << command << " \n " << boost::filesystem::current_path().string();
#else
command = command + boost::filesystem::current_path().string() + "/" +
Expand Down Expand Up @@ -266,5 +265,35 @@ class Utils

return 0;
}

/**
* Fills a vector with paths to all files in directory.
*
* @param path Path to Directory.
* @param pathVector A vector of type filesystem::path, which will be filled
* paths for all files / folders in given directory path.
* @param absolutePath Boolean to determine if path is absolute or relative.
*/
static void ListDir(const std::string& path,
std::vector<boost::filesystem::path>& pathVector,
const bool absolutePath = false)
{
if (Utils::PathExists(path, absolutePath))
{
boost::filesystem::path directoryPath(path);

// Fill the path vector with respective paths.
std::copy(boost::filesystem::directory_iterator(directoryPath),
boost::filesystem::directory_iterator(),
std::back_inserter(pathVector));

// Sort the path vector.
std::sort(pathVector.begin(), pathVector.end());
}
else
{
mlpack::Log::Warn << "The " << path << "Doesn't exist." << std::endl;
}
}
};
#endif

0 comments on commit cf9453b

Please sign in to comment.