42 #include <visp3/core/vpIoTools.h>
44 #ifdef ENABLE_VISP_NAMESPACE
50 template <
typename T>
void checkReadBinaryValue(std::ifstream &file,
const T checkValue)
54 if (value != checkValue) {
56 ss <<
"Read: " << value <<
" ; Expected: " << checkValue;
61 template <>
void checkReadBinaryValue<float>(std::ifstream &file,
const float checkValue)
65 if (!
vpMath::equal(value, checkValue, std::numeric_limits<float>::epsilon())) {
67 ss <<
"Read: " << value <<
" ; Expected: " << checkValue;
72 template <>
void checkReadBinaryValue<double>(std::ifstream &file,
const double checkValue)
76 if (!
vpMath::equal(value, checkValue, std::numeric_limits<double>::epsilon())) {
78 ss <<
"Read: " << value <<
" ; Expected: " << checkValue;
84 int main(
int argc,
const char **argv)
88 std::cout <<
"The directory separator character is '" << c <<
"' (Windows platform)." << std::endl;
91 std::cout <<
"The directory separator character is '" << c <<
"' (Unix like platform)." << std::endl;
95 std::string pathname =
"C:\\Program Files (x86)\\Java\\jre7";
97 std::string pathname =
"/usr/bin/java";
101 std::cout <<
"Name of " << pathname <<
" is " <<
vpIoTools::getName(pathname) << std::endl;
103 if (argc == 3 && std::string(argv[1]) == std::string(
"-i")) {
105 std::cout <<
"Name of " << argv[2] <<
" is " <<
vpIoTools::getName(argv[2]) << std::endl;
108 std::string windowsPathnameStyle =
"\\usr\\bin\\java";
109 std::cout <<
"Parent of " << windowsPathnameStyle <<
" is " <<
vpIoTools::getParent(windowsPathnameStyle)
111 std::cout <<
"Name of " << windowsPathnameStyle <<
" is " <<
vpIoTools::getName(windowsPathnameStyle) << std::endl;
113 std::string parent =
"/usr/toto/", child =
"\\blabla\\java";
115 std::cout <<
"Create file path from parent=" << parent <<
" and child=" << child <<
" is "
118 std::string expandPath =
"~/Documents/fictional directory/fictional file";
119 std::cout <<
"Path for " << expandPath <<
" is " <<
vpIoTools::path(expandPath) << std::endl;
121 std::cout <<
"Test get name with an empty pathname=" <<
vpIoTools::getName(
"") << std::endl;
124 expandPath =
"~/Documents/fictional dir/fictional file.txt";
125 std::cout <<
"Get name with a unix expand pathname " << expandPath <<
"=" <<
vpIoTools::getName(expandPath)
127 std::cout <<
"Get parent with a unix expand pathname " << expandPath <<
"=" <<
vpIoTools::getParent(expandPath)
136 pathname =
"c:/dir/fictional directory/fictional file.txt";
139 pathname =
"/home/user/Documents/fictional directory/fictional file.txt";
142 pathname =
"~/Documents/fictional directory/fictional file.txt";
145 pathname =
"fictional directory/fictional file.txt";
149 unsigned int nbFail = 0, nbOk = 0;
163 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"c:\\foo\\bar").second <<
" should be=\\foo\\bar" << std::endl;
178 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"c:/foo/bar").second <<
" should be=/foo/bar" << std::endl;
181 if (strcmp(
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").first.c_str(),
"\\\\conky\\mountpoint") == 0) {
187 <<
" should be=\\\\conky\\mountpoint" << std::endl;
189 if (strcmp(
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").second.c_str(),
"\\foo\\bar") == 0) {
194 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").second <<
" should be=\\foo\\bar"
198 if (strcmp(
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").first.c_str(),
"//conky/mountpoint") == 0) {
203 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").first <<
" should be=//conky/mountpoint"
211 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").second <<
" should be=/foo/bar"
221 <<
" should be=" << std::endl;
224 "\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
230 <<
" should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
238 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").first <<
" should be=" << std::endl;
240 if (strcmp(
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").second.c_str(),
"///conky/mountpoint/foo/bar") == 0) {
246 <<
" should be=///conky/mountpoint/foo/bar" << std::endl;
255 <<
" should be=" << std::endl;
258 "\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
264 <<
" should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
272 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").first <<
" should be=" << std::endl;
274 if (strcmp(
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").second.c_str(),
"//conky//mountpoint/foo/bar") == 0) {
280 <<
" should be=//conky//mountpoint/foo/bar" << std::endl;
283 std::cout <<
"Test vpIoTools::splitDrive (Win32) - passed: " << nbOk <<
"/" << (nbOk + nbFail) << std::endl;
286 std::cerr <<
"Failed test: vpIoTools::splitDrive (Win32)" << std::endl;
376 std::cout <<
"Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
380 std::cerr <<
"Failed test: vpIoTools::getFileExtension (WIN32 platform)" << std::endl;
483 std::cout <<
"Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
489 std::string username, directory_filename;
492 std::string tmp_dir =
"C:/temp/" + username;
493 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
494 std::string tmp_dir =
"/tmp/" + username;
497 directory_filename = tmp_dir +
"/test_directory1/test directory 2/";
498 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
499 directory_filename = tmp_dir +
"/test_directory1/test directory 2/";
503 std::cout <<
"Create directories: " << directory_filename << std::endl;
506 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
511 directory_filename = tmp_dir +
"/test_directory1/test directory 3";
512 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
513 directory_filename = tmp_dir +
"/test_directory1/test directory 3";
516 std::cout <<
"Create directories: " << directory_filename << std::endl;
519 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
524 directory_filename =
"C:\\temp/" + username +
"\\test_directory1\\test directory 4";
525 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
526 directory_filename =
"/tmp\\" + username +
"\\test_directory1\\test directory 4";
530 std::cout <<
"Create directories: " << directory_filename << std::endl;
533 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
538 directory_filename =
"C:\\temp/" + username +
"\\test_directory1\\test directory 5 . dir/test directory 6";
539 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
540 directory_filename =
"/tmp\\" + username +
"\\test_directory1\\test directory 5 . dir/test directory 6";
543 std::cout <<
"Create directories: " << directory_filename << std::endl;
546 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
552 std::cerr <<
"Error: cannot remove directory: " << tmp_dir <<
"/test_directory1" << std::endl;
557 std::cerr <<
"Exception: " << e.
what() << std::endl;
562 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
564 std::string username, fifo_file;
566 std::string fifo_tmp_dir =
"/tmp/" + username +
"/fifo_test_directory";
573 fifo_file = fifo_tmp_dir +
"/" +
"fifo_testfile";
576 std::cout <<
"Create fifo file: " << fifo_file << std::endl;
579 std::cerr <<
"Error: file " << fifo_file <<
" is not a fifo file as expected" << std::endl;
585 std::cerr <<
"Error: cannot remove fifo: " << fifo_file << std::endl;
589 std::cerr <<
"Error: cannot remove directory: " << fifo_tmp_dir << std::endl;
595 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
601 #if defined(_WIN32) || \
602 (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
604 std::string directory_filename_tmp;
608 directory_filename_tmp = tmp_dir +
"/" +
"vpIoTools_test_XXXXXX";
612 std::cout <<
"Create temp directory: " << converted_dirname_tmp << std::endl;
615 std::cerr <<
"Error: " << converted_dirname_tmp <<
" is not a tmp directory" << std::endl;
621 std::cerr <<
"Error: cannot remove temp directory: " << converted_dirname_tmp << std::endl;
629 std::cout <<
"Create temp directory: " << converted_dirname_tmp << std::endl;
632 std::cerr <<
"Error: " << converted_dirname_tmp <<
" is not a temp directory" << std::endl;
638 std::cerr <<
"Cannot remove directory: " << converted_dirname_tmp << std::endl;
644 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
651 std::ofstream dummy_file;
655 std::string path1 =
"tmp/test/file.txt";
656 std::string path2 =
"tmp/test/../test/file.txt";
663 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
664 nbOk = res ? nbOk + 1 : nbOk;
665 nbFail = res ? nbFail : nbFail + 1;
667 path1 =
".\\tmp/test/file.txt";
669 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
670 nbOk = res ? nbOk + 1 : nbOk;
671 nbFail = res ? nbFail : nbFail + 1;
673 path1 =
".\\tmp/test\\../fake dir/..\\test\\file.txt";
675 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
676 nbOk = res ? nbOk + 1 : nbOk;
677 nbFail = res ? nbFail : nbFail + 1;
679 path2 =
"/tmp/test/../test/file.txt";
681 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
682 nbOk = res ? nbOk : nbOk + 1;
683 nbFail = res ? nbFail + 1 : nbFail;
685 std::cout <<
"Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
688 std::cerr <<
"Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
697 std::string path1 =
"/tmp/" + username +
"/test/file.txt";
698 std::string path2 =
"/tmp/" + username +
"/test/../test/file.txt";
699 dummy_file.open(path1.c_str());
700 if (!dummy_file.is_open()) {
710 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
711 nbOk = res ? nbOk + 1 : nbOk;
712 nbFail = res ? nbFail : nbFail + 1;
714 path1 =
"\\tmp/" + username +
"/./test/file.txt";
716 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
717 nbOk = res ? nbOk + 1 : nbOk;
718 nbFail = res ? nbFail : nbFail + 1;
720 path1 =
"\\tmp/" + username +
"/test\\../dummy dir/..\\test\\file.txt";
722 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
723 nbOk = res ? nbOk + 1 : nbOk;
724 nbFail = res ? nbFail : nbFail + 1;
726 path2 =
"/tmp/" + username +
"/test/../test";
728 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
729 nbOk = res ? nbOk : nbOk + 1;
730 nbFail = res ? nbFail + 1 : nbFail;
732 path1 =
"/tmp/" + username +
"/test/";
734 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
735 nbOk = res ? nbOk + 1 : nbOk;
736 nbFail = res ? nbFail : nbFail + 1;
738 std::cout <<
"Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
743 std::cerr <<
"Cannot remove directory: "
744 <<
"/tmp/" << username <<
"/test" << std::endl;
747 std::cerr <<
"Cannot remove directory: "
748 <<
"/tmp/" << username <<
"/dummy dir" << std::endl;
752 std::cerr <<
"Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
759 std::cerr <<
"Failed test: vpIoTools::getIndex(\"file-1.txt\", \"file-%d.txt\")" << std::endl;
763 std::cerr <<
"Failed test: vpIoTools::getIndex(\"/tmp/file0040.txt\", \"/tmp/file%04d.txt\")" << std::endl;
767 std::cerr <<
"Failed test: vpIoTools::getIndex(\"file.txt\", \"file%d.txt\")" << std::endl;
773 path1 =
"/tmp/" + username +
774 "/directory (1) with ' quote and spaces/file with ' quote (1) and "
776 dummy_file.open(path1.c_str());
777 if (!dummy_file.is_open()) {
783 std::cerr <<
"Problem with checkFilename(" << path1 <<
")!" << std::endl;
786 std::cout <<
"Test vpIoTools::checkFilename() is ok." << std::endl;
789 if (!
vpIoTools::remove(
"/tmp/" + username +
"/directory (1) with ' quote and spaces")) {
790 std::cerr <<
"Cannot remove directory: "
791 <<
"/tmp/" << username <<
"/directory (1) with ' quote and spaces" << std::endl;
796 std::string filename_endianness =
798 std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
799 if (file_endianness.is_open()) {
800 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
801 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
803 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
804 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
806 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
807 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
809 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
810 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
812 checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
813 checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
815 checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
816 checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
818 std::cout <<
"Test endianness is ok." << std::endl;
821 std::cout <<
"Cannot open file: " << filename_endianness << std::endl;
829 std::string testString = std::string(
"Yolo-V3");
830 std::string expectedLower = std::string(
"yolo-v3");
831 std::string expectedUpper = std::string(
"YOLO-V3");
839 std::cout <<
"Fail=" <<
vpIoTools::toLowerCase(testString).c_str() <<
" should be=" << expectedLower << std::endl;
847 std::cout <<
"Fail=" <<
vpIoTools::toUpperCase(testString).c_str() <<
" should be=" << expectedUpper << std::endl;
850 std::cout <<
"Test vpIoTools::toLowerCase (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
854 std::cerr <<
"Failed test: vpIoTools::toLowerCase (WIN32 platform)" << std::endl;
863 std::cout <<
"Fail=" <<
vpIoTools::toLowerCase(testString).c_str() <<
" should be=" << expectedLower << std::endl;
871 std::cout <<
"Fail=" <<
vpIoTools::toUpperCase(testString).c_str() <<
" should be=" << expectedUpper << std::endl;
874 std::cout <<
"Test vpIoTools::toLowerCase (Unix-like platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
878 std::cerr <<
"Failed test: vpIoTools::toLowerCase (Unix-like platform)" << std::endl;
885 std::cout << std::endl <<
"Test succeed" << std::endl;
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
const char * what() const
static bool equal(double x, double y, double threshold=0.001)