DirectoryΒΆ
To copy directory with resources use $<GAUZE_RESOURCE_DIR:...>
:
hunter_add_package(Boost COMPONENTS filesystem system)
find_package(Boost REQUIRED COMPONENTS filesystem system)
add_executable(gauze_directory main.cpp)
target_link_libraries(gauze_directory PRIVATE Boost::filesystem Boost::system)
set_target_properties(
gauze_directory PROPERTIES BUILD_RPATH "${BOOST_ROOT}/lib"
)
set(data_dir "${CMAKE_CURRENT_LIST_DIR}/resdir")
gauze_add_test(
NAME gauze_directory
COMMAND
gauze_directory
arg1
arg2
arg3
$<GAUZE_RESOURCE_DIR:${data_dir}>
${data_dir}/just_a_string.txt
--directory=$<GAUZE_RESOURCE_DIR:${data_dir}>
)
if(WIN32 OR CYGWIN)
set(new_path "${BOOST_ROOT}/lib")
list(APPEND new_path $ENV{PATH})
if(WIN32)
string(REPLACE ";" "\;" new_path "${new_path}")
elseif(CYGWIN)
string(REPLACE ";" ":" new_path "${new_path}")
else()
message(FATAL_ERROR "Unreachable")
endif()
set_tests_properties(
gauze_directory PROPERTIES ENVIRONMENT "PATH=${new_path}"
)
endif()
Read files in directory:
#include <cstdlib> // EXIT_SUCCESS
#include <fstream> // std::ifstream
#include <iostream> // std::cout
#include <string> // std::getline
#include <boost/filesystem.hpp>
#include <sstream> // std::ostringstream
int gauze_main(int argc, char** argv) {
std::cout << "argc = " << argc << std::endl;
for (int i=0; i<argc; ++i) {
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
}
if(argc < 7) {
std::cerr << "Unexpected number of arguments: " << argc << std::endl;
return EXIT_FAILURE;
}
boost::filesystem::path resdir(argv[4]);
for (int i=0; i<3; ++i) {
std::ostringstream file_name;
file_name << "file." << i;
boost::filesystem::path file_path(resdir);
file_path /= file_name.str();
std::cout << "Processing file: " << file_path << std::endl;
std::ifstream file(file_path.string());
std::string content;
std::getline(file, content);
if(!file) {
std::cerr << "Can't read file: " << file_path << std::endl;
return EXIT_FAILURE;
}
std::cout << "Content: '" << content << "'" << std::endl;
}
return EXIT_SUCCESS;
}
Running this test on Android device:
> ctest -VV -R gauze_directory
4: Command output (with exit code):
4: *** BEGIN ***
4: argc = 6
4: argv[0] = /data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/bin/gauze_directory
4: argv[1] = arg1
4: argv[2] = arg2
4: argv[3] = arg3
4: argv[4] = /data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/data/resdir
4: argv[5] = /.../gauze/test/gauze/directory/resdir/just_a_string.txt
4: Processing file: "/data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/data/resdir/file.0"
4: Content: 'Content 0'
4: Processing file: "/data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/data/resdir/file.1"
4: Content: 'Content 1'
4: Processing file: "/data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/data/resdir/file.2"
4: Content: 'Content 2'
4: 0
4: *** END ***
4: Done
1/1 Test #4: gauze_directory .................. Passed 0.54 sec