diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index aa04fec7..7d781533 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -76,15 +76,47 @@ BOOST_AUTO_TEST_CASE(RemoveFileTest) BOOST_AUTO_TEST_CASE(ExtractFilesTest) { + std::vector 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(); diff --git a/utils/utils.hpp b/utils/utils.hpp index 981f2f0c..1add62f3 100644 --- a/utils/utils.hpp +++ b/utils/utils.hpp @@ -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() + "/" + @@ -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& 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