44 #include <visp3/core/vpIoTools.h>
48 template <
typename T>
void checkReadBinaryValue(std::ifstream &file,
const T checkValue)
52 if (value != checkValue) {
54 ss <<
"Read: " << value <<
" ; Expected: " << checkValue;
59 template <>
void checkReadBinaryValue<float>(std::ifstream &file,
const float checkValue)
63 if (!
vpMath::equal(value, checkValue, std::numeric_limits<float>::epsilon())) {
65 ss <<
"Read: " << value <<
" ; Expected: " << checkValue;
70 template <>
void checkReadBinaryValue<double>(std::ifstream &file,
const double checkValue)
74 if (!
vpMath::equal(value, checkValue, std::numeric_limits<double>::epsilon())) {
76 ss <<
"Read: " << value <<
" ; Expected: " << checkValue;
82 int main(
int argc,
const char **argv)
86 std::cout <<
"The directory separator character is '" << c <<
"' (Windows platform)." << std::endl;
88 std::cout <<
"The directory separator character is '" << c <<
"' (Unix like platform)." << std::endl;
92 std::string pathname =
"C:\\Program Files (x86)\\Java\\jre7";
94 std::string pathname =
"/usr/bin/java";
98 std::cout <<
"Name of " << pathname <<
" is " <<
vpIoTools::getName(pathname) << std::endl;
100 if (argc == 3 && std::string(argv[1]) == std::string(
"-i")) {
102 std::cout <<
"Name of " << argv[2] <<
" is " <<
vpIoTools::getName(argv[2]) << std::endl;
105 std::string windowsPathnameStyle =
"\\usr\\bin\\java";
106 std::cout <<
"Parent of " << windowsPathnameStyle <<
" is " <<
vpIoTools::getParent(windowsPathnameStyle)
108 std::cout <<
"Name of " << windowsPathnameStyle <<
" is " <<
vpIoTools::getName(windowsPathnameStyle) << std::endl;
110 std::string parent =
"/usr/toto/", child =
"\\blabla\\java";
112 std::cout <<
"Create file path from parent=" << parent <<
" and child=" << child <<
" is "
115 std::string expandPath =
"~/Documents/fictional directory/fictional file";
116 std::cout <<
"Path for " << expandPath <<
" is " <<
vpIoTools::path(expandPath) << std::endl;
118 std::cout <<
"Test get name with an empty pathname=" <<
vpIoTools::getName(
"") << std::endl;
121 expandPath =
"~/Documents/fictional dir/fictional file.txt";
122 std::cout <<
"Get name with a unix expand pathname " << expandPath <<
"=" <<
vpIoTools::getName(expandPath)
124 std::cout <<
"Get parent with a unix expand pathname " << expandPath <<
"=" <<
vpIoTools::getParent(expandPath)
133 pathname =
"c:/dir/fictional directory/fictional file.txt";
136 pathname =
"/home/user/Documents/fictional directory/fictional file.txt";
139 pathname =
"~/Documents/fictional directory/fictional file.txt";
142 pathname =
"fictional directory/fictional file.txt";
146 unsigned int nbFail = 0, nbOk = 0;
158 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"c:\\foo\\bar").second <<
" should be=\\foo\\bar" << std::endl;
171 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"c:/foo/bar").second <<
" should be=/foo/bar" << std::endl;
174 if (strcmp(
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").first.c_str(),
"\\\\conky\\mountpoint") == 0) {
179 <<
" should be=\\\\conky\\mountpoint" << std::endl;
181 if (strcmp(
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").second.c_str(),
"\\foo\\bar") == 0) {
185 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").second <<
" should be=\\foo\\bar"
189 if (strcmp(
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").first.c_str(),
"//conky/mountpoint") == 0) {
193 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").first <<
" should be=//conky/mountpoint"
200 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").second <<
" should be=/foo/bar"
209 <<
" should be=" << std::endl;
212 "\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
217 <<
" should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
224 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").first <<
" should be=" << std::endl;
226 if (strcmp(
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").second.c_str(),
"///conky/mountpoint/foo/bar") == 0) {
231 <<
" should be=///conky/mountpoint/foo/bar" << std::endl;
239 <<
" should be=" << std::endl;
242 "\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
247 <<
" should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
254 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").first <<
" should be=" << std::endl;
256 if (strcmp(
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").second.c_str(),
"//conky//mountpoint/foo/bar") == 0) {
261 <<
" should be=//conky//mountpoint/foo/bar" << std::endl;
264 std::cout <<
"Test vpIoTools::splitDrive (Win32) - passed: " << nbOk <<
"/" << (nbOk + nbFail) << std::endl;
267 std::cerr <<
"Failed test: vpIoTools::splitDrive (Win32)" << std::endl;
347 std::cout <<
"Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
351 std::cerr <<
"Failed test: vpIoTools::getFileExtension (WIN32 platform)" << std::endl;
442 std::cout <<
"Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
448 std::string username, directory_filename;
451 std::string tmp_dir =
"C:/temp/" + username;
452 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
453 std::string tmp_dir =
"/tmp/" + username;
456 directory_filename = tmp_dir +
"/test_directory1/test directory 2/";
457 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
458 directory_filename = tmp_dir +
"/test_directory1/test directory 2/";
462 std::cout <<
"Create directories: " << directory_filename << std::endl;
465 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
470 directory_filename = tmp_dir +
"/test_directory1/test directory 3";
471 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
472 directory_filename = tmp_dir +
"/test_directory1/test directory 3";
475 std::cout <<
"Create directories: " << directory_filename << std::endl;
478 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
483 directory_filename =
"C:\\temp/" + username +
"\\test_directory1\\test directory 4";
484 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
485 directory_filename =
"/tmp\\" + username +
"\\test_directory1\\test directory 4";
489 std::cout <<
"Create directories: " << directory_filename << std::endl;
492 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
497 directory_filename =
"C:\\temp/" + username +
"\\test_directory1\\test directory 5 . dir/test directory 6";
498 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
499 directory_filename =
"/tmp\\" + username +
"\\test_directory1\\test directory 5 . dir/test directory 6";
502 std::cout <<
"Create directories: " << directory_filename << std::endl;
505 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
511 std::cerr <<
"Error: cannot remove directory: " << tmp_dir <<
"/test_directory1" << std::endl;
515 std::cerr <<
"Exception: " << e.
what() << std::endl;
520 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
522 std::string username, fifo_file;
524 std::string fifo_tmp_dir =
"/tmp/" + username +
"/fifo_test_directory";
531 fifo_file = fifo_tmp_dir +
"/" +
"fifo_testfile";
534 std::cout <<
"Create fifo file: " << fifo_file << std::endl;
537 std::cerr <<
"Error: file " << fifo_file <<
" is not a fifo file as expected" << std::endl;
543 std::cerr <<
"Error: cannot remove fifo: " << fifo_file << std::endl;
547 std::cerr <<
"Error: cannot remove directory: " << fifo_tmp_dir << std::endl;
552 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
558 #if defined(_WIN32) || \
559 (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
561 std::string directory_filename_tmp;
565 directory_filename_tmp = tmp_dir +
"/" +
"vpIoTools_test_XXXXXX";
569 std::cout <<
"Create temp directory: " << converted_dirname_tmp << std::endl;
572 std::cerr <<
"Error: " << converted_dirname_tmp <<
" is not a tmp directory" << std::endl;
578 std::cerr <<
"Error: cannot remove temp directory: " << converted_dirname_tmp << std::endl;
586 std::cout <<
"Create temp directory: " << converted_dirname_tmp << std::endl;
589 std::cerr <<
"Error: " << converted_dirname_tmp <<
" is not a temp directory" << std::endl;
595 std::cerr <<
"Cannot remove directory: " << converted_dirname_tmp << std::endl;
600 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
607 std::ofstream dummy_file;
611 std::string path1 =
"tmp/test/file.txt";
612 std::string path2 =
"tmp/test/../test/file.txt";
619 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
620 nbOk = res ? nbOk + 1 : nbOk;
621 nbFail = res ? nbFail : nbFail + 1;
623 path1 =
".\\tmp/test/file.txt";
625 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
626 nbOk = res ? nbOk + 1 : nbOk;
627 nbFail = res ? nbFail : nbFail + 1;
629 path1 =
".\\tmp/test\\../fake dir/..\\test\\file.txt";
631 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
632 nbOk = res ? nbOk + 1 : nbOk;
633 nbFail = res ? nbFail : nbFail + 1;
635 path2 =
"/tmp/test/../test/file.txt";
637 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
638 nbOk = res ? nbOk : nbOk + 1;
639 nbFail = res ? nbFail + 1 : nbFail;
641 std::cout <<
"Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
644 std::cerr <<
"Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
653 std::string path1 =
"/tmp/" + username +
"/test/file.txt";
654 std::string path2 =
"/tmp/" + username +
"/test/../test/file.txt";
655 dummy_file.open(path1.c_str());
656 if (!dummy_file.is_open()) {
666 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
667 nbOk = res ? nbOk + 1 : nbOk;
668 nbFail = res ? nbFail : nbFail + 1;
670 path1 =
"\\tmp/" + username +
"/./test/file.txt";
672 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
673 nbOk = res ? nbOk + 1 : nbOk;
674 nbFail = res ? nbFail : nbFail + 1;
676 path1 =
"\\tmp/" + username +
"/test\\../dummy dir/..\\test\\file.txt";
678 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
679 nbOk = res ? nbOk + 1 : nbOk;
680 nbFail = res ? nbFail : nbFail + 1;
682 path2 =
"/tmp/" + username +
"/test/../test";
684 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
685 nbOk = res ? nbOk : nbOk + 1;
686 nbFail = res ? nbFail + 1 : nbFail;
688 path1 =
"/tmp/" + username +
"/test/";
690 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
691 nbOk = res ? nbOk + 1 : nbOk;
692 nbFail = res ? nbFail : nbFail + 1;
694 std::cout <<
"Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
699 std::cerr <<
"Cannot remove directory: "
700 <<
"/tmp/" << username <<
"/test" << std::endl;
703 std::cerr <<
"Cannot remove directory: "
704 <<
"/tmp/" << username <<
"/dummy dir" << std::endl;
708 std::cerr <<
"Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
715 std::cerr <<
"Failed test: vpIoTools::getIndex(\"file-1.txt\", \"file-%d.txt\")" << std::endl;
719 std::cerr <<
"Failed test: vpIoTools::getIndex(\"/tmp/file0040.txt\", \"/tmp/file%04d.txt\")" << std::endl;
723 std::cerr <<
"Failed test: vpIoTools::getIndex(\"file.txt\", \"file%d.txt\")" << std::endl;
729 path1 =
"/tmp/" + username +
730 "/directory (1) with ' quote and spaces/file with ' quote (1) and "
732 dummy_file.open(path1.c_str());
733 if (!dummy_file.is_open()) {
739 std::cerr <<
"Problem with checkFilename(" << path1 <<
")!" << std::endl;
742 std::cout <<
"Test vpIoTools::checkFilename() is ok." << std::endl;
745 if (!
vpIoTools::remove(
"/tmp/" + username +
"/directory (1) with ' quote and spaces")) {
746 std::cerr <<
"Cannot remove directory: "
747 <<
"/tmp/" << username <<
"/directory (1) with ' quote and spaces" << std::endl;
752 std::string filename_endianness =
754 std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
755 if (file_endianness.is_open()) {
756 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
757 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
759 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
760 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
762 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
763 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
765 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
766 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
768 checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
769 checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
771 checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
772 checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
774 std::cout <<
"Test endianness is ok." << std::endl;
776 std::cout <<
"Cannot open file: " << filename_endianness << std::endl;
784 std::string testString = std::string(
"Yolo-V3");
785 std::string expectedLower = std::string(
"yolo-v3");
786 std::string expectedUpper = std::string(
"YOLO-V3");
793 std::cout <<
"Fail=" <<
vpIoTools::toLowerCase(testString).c_str() <<
" should be=" << expectedLower << std::endl;
800 std::cout <<
"Fail=" <<
vpIoTools::toUpperCase(testString).c_str() <<
" should be=" << expectedUpper << std::endl;
803 std::cout <<
"Test vpIoTools::toLowerCase (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
807 std::cerr <<
"Failed test: vpIoTools::toLowerCase (WIN32 platform)" << std::endl;
815 std::cout <<
"Fail=" <<
vpIoTools::toLowerCase(testString).c_str() <<
" should be=" << expectedLower << std::endl;
822 std::cout <<
"Fail=" <<
vpIoTools::toUpperCase(testString).c_str() <<
" should be=" << expectedUpper << std::endl;
825 std::cout <<
"Test vpIoTools::toLowerCase (Unix-like platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
829 std::cerr <<
"Failed test: vpIoTools::toLowerCase (Unix-like platform)" << std::endl;
836 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)