44 #include <visp3/core/vpIoTools.h> 49 void checkReadBinaryValue(std::ifstream &file,
const T checkValue)
53 if (value != checkValue) {
55 ss <<
"Read: " << value <<
" ; Expected: " << checkValue;
61 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;
73 void checkReadBinaryValue<double>(std::ifstream &file,
const double checkValue)
77 if (!
vpMath::equal(value, checkValue, std::numeric_limits<double>::epsilon())) {
79 ss <<
"Read: " << value <<
" ; Expected: " << checkValue;
85 int main(
int argc,
const char **argv)
89 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;
161 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"c:\\foo\\bar").second <<
" should be=\\foo\\bar" << std::endl;
174 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"c:/foo/bar").second <<
" should be=/foo/bar" << std::endl;
177 if (strcmp(
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").first.c_str(),
"\\\\conky\\mountpoint") == 0) {
182 <<
" should be=\\\\conky\\mountpoint" << std::endl;
184 if (strcmp(
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").second.c_str(),
"\\foo\\bar") == 0) {
188 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"\\\\conky\\mountpoint\\foo\\bar").second <<
" should be=\\foo\\bar" 192 if (strcmp(
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").first.c_str(),
"//conky/mountpoint") == 0) {
196 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").first <<
" should be=//conky/mountpoint" 203 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky/mountpoint/foo/bar").second <<
" should be=/foo/bar" 212 <<
" should be=" << std::endl;
215 "\\\\\\conky\\mountpoint\\foo\\bar") == 0) {
220 <<
" should be=\\\\\\conky\\mountpoint\\foo\\bar" << std::endl;
227 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").first <<
" should be=" << std::endl;
229 if (strcmp(
vpIoTools::splitDrive(
"///conky/mountpoint/foo/bar").second.c_str(),
"///conky/mountpoint/foo/bar") == 0) {
234 <<
" should be=///conky/mountpoint/foo/bar" << std::endl;
242 <<
" should be=" << std::endl;
245 "\\\\conky\\\\mountpoint\\foo\\bar") == 0) {
250 <<
" should be=\\\\conky\\\\mountpoint\\foo\\bar" << std::endl;
257 std::cout <<
"Fail=" <<
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").first <<
" should be=" << std::endl;
259 if (strcmp(
vpIoTools::splitDrive(
"//conky//mountpoint/foo/bar").second.c_str(),
"//conky//mountpoint/foo/bar") == 0) {
264 <<
" should be=//conky//mountpoint/foo/bar" << std::endl;
267 std::cout <<
"Test vpIoTools::splitDrive (Win32) - passed: " << nbOk <<
"/" << (nbOk + nbFail) << std::endl;
270 std::cerr <<
"Failed test: vpIoTools::splitDrive (Win32)" << std::endl;
350 std::cout <<
"Test vpIoTools::getFileExtension (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
354 std::cerr <<
"Failed test: vpIoTools::getFileExtension (WIN32 platform)" << std::endl;
445 std::cout <<
"Test vpIoTools::getFileExtension (Unix-like platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
451 std::string username, directory_filename;
454 std::string tmp_dir =
"C:/temp/" + username;
455 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 456 std::string tmp_dir =
"/tmp/" + username;
459 directory_filename = tmp_dir +
"/test_directory1/test directory 2/";
460 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 461 directory_filename = tmp_dir +
"/test_directory1/test directory 2/";
465 std::cout <<
"Create directories: " << directory_filename << std::endl;
468 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
473 directory_filename = tmp_dir +
"/test_directory1/test directory 3";
474 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 475 directory_filename = tmp_dir +
"/test_directory1/test directory 3";
478 std::cout <<
"Create directories: " << directory_filename << std::endl;
481 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
486 directory_filename =
"C:\\temp/" + username +
"\\test_directory1\\test directory 4";
487 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 488 directory_filename =
"/tmp\\" + username +
"\\test_directory1\\test directory 4";
492 std::cout <<
"Create directories: " << directory_filename << std::endl;
495 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
500 directory_filename =
"C:\\temp/" + username +
"\\test_directory1\\test directory 5 . dir/test directory 6";
501 #elif (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 502 directory_filename =
"/tmp\\" + username +
"\\test_directory1\\test directory 5 . dir/test directory 6";
505 std::cout <<
"Create directories: " << directory_filename << std::endl;
508 std::cerr <<
"Error: " << directory_filename <<
" is not a directory" << std::endl;
514 std::cerr <<
"Error: cannot remove directory: " << tmp_dir <<
"/test_directory1" << std::endl;
518 std::cerr <<
"Exception: " << e.
what() << std::endl;
523 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 525 std::string username, fifo_file;
527 std::string fifo_tmp_dir =
"/tmp/" + username +
"/fifo_test_directory";
534 fifo_file = fifo_tmp_dir +
"/" +
"fifo_testfile";
537 std::cout <<
"Create fifo file: " << fifo_file << std::endl;
540 std::cerr <<
"Error: file " << fifo_file <<
" is not a fifo file as expected" << std::endl;
546 std::cerr <<
"Error: cannot remove fifo: " << fifo_file << std::endl;
550 std::cerr <<
"Error: cannot remove directory: " << fifo_tmp_dir << std::endl;
555 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
561 #if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX or Windows 563 std::string directory_filename_tmp;
567 directory_filename_tmp = tmp_dir +
"/" +
"vpIoTools_test_XXXXXX";
571 std::cout <<
"Create temp directory: " << converted_dirname_tmp << std::endl;
574 std::cerr <<
"Error: " << converted_dirname_tmp <<
" is not a tmp directory" << std::endl;
580 std::cerr <<
"Error: cannot remove temp directory: " << converted_dirname_tmp << std::endl;
588 std::cout <<
"Create temp directory: " << converted_dirname_tmp << std::endl;
591 std::cerr <<
"Error: " << converted_dirname_tmp <<
" is not a temp directory" << std::endl;
597 std::cerr <<
"Cannot remove directory: " << converted_dirname_tmp << std::endl;
602 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
609 std::ofstream dummy_file;
613 std::string path1 =
"tmp/test/file.txt";
614 std::string path2 =
"tmp/test/../test/file.txt";
621 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
622 nbOk = res ? nbOk + 1 : nbOk;
623 nbFail = res ? nbFail : nbFail + 1;
625 path1 =
".\\tmp/test/file.txt";
627 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
628 nbOk = res ? nbOk + 1 : nbOk;
629 nbFail = res ? nbFail : nbFail + 1;
631 path1 =
".\\tmp/test\\../fake dir/..\\test\\file.txt";
633 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
634 nbOk = res ? nbOk + 1 : nbOk;
635 nbFail = res ? nbFail : nbFail + 1;
637 path2 =
"/tmp/test/../test/file.txt";
639 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
640 nbOk = res ? nbOk : nbOk + 1;
641 nbFail = res ? nbFail + 1 : nbFail;
643 std::cout <<
"Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
646 std::cerr <<
"Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
655 std::string path1 =
"/tmp/" + username +
"/test/file.txt";
656 std::string path2 =
"/tmp/" + username +
"/test/../test/file.txt";
657 dummy_file.open(path1.c_str());
658 if (!dummy_file.is_open()) {
668 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
669 nbOk = res ? nbOk + 1 : nbOk;
670 nbFail = res ? nbFail : nbFail + 1;
672 path1 =
"\\tmp/" + username +
"/./test/file.txt";
674 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
675 nbOk = res ? nbOk + 1 : nbOk;
676 nbFail = res ? nbFail : nbFail + 1;
678 path1 =
"\\tmp/" + username +
"/test\\../dummy dir/..\\test\\file.txt";
680 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
681 nbOk = res ? nbOk + 1 : nbOk;
682 nbFail = res ? nbFail : nbFail + 1;
684 path2 =
"/tmp/" + username +
"/test/../test";
686 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
687 nbOk = res ? nbOk : nbOk + 1;
688 nbFail = res ? nbFail + 1 : nbFail;
690 path1 =
"/tmp/" + username +
"/test/";
692 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
693 nbOk = res ? nbOk + 1 : nbOk;
694 nbFail = res ? nbFail : nbFail + 1;
696 std::cout <<
"Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
701 std::cerr <<
"Cannot remove directory: " <<
"/tmp/" << username <<
"/test" << std::endl;
704 std::cerr <<
"Cannot remove directory: " <<
"/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: " <<
"/tmp/" << username <<
"/directory (1) with ' quote and spaces" << std::endl;
752 std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
753 if (file_endianness.is_open()) {
754 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
755 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
757 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
758 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
760 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
761 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
763 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
764 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
766 checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
767 checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
769 checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
770 checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
772 std::cout <<
"Test endianness is ok." << std::endl;
774 std::cout <<
"Cannot open file: " << filename_endianness << std::endl;
778 std::cout << std::endl <<
"Test succeed" << std::endl;
Used to indicate that a value is not in the allowed range.
static bool equal(double x, double y, double s=0.001)
error that can be emited by ViSP classes.
const char * what() const