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 563 std::string username, directory_filename_tmp;
565 std::string tmp_dir =
"/tmp/" + username;
568 directory_filename_tmp = tmp_dir +
"/" +
"vpIoTools_test_XXXXXX";
572 std::cout <<
"Create temp directory: " << converted_dirname_tmp << std::endl;
575 std::cerr <<
"Error: " << converted_dirname_tmp <<
" is not a tmp directory" << std::endl;
581 std::cerr <<
"Error: cannot remove temp directory: " << converted_dirname_tmp << std::endl;
589 std::cout <<
"Create temp directory: " << converted_dirname_tmp << std::endl;
592 std::cerr <<
"Error: " << converted_dirname_tmp <<
" is not a temp directory" << std::endl;
598 std::cerr <<
"Cannot remove directory: " << converted_dirname_tmp << std::endl;
603 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
610 std::ofstream dummy_file;
614 std::string path1 =
"tmp/test/file.txt";
615 std::string path2 =
"tmp/test/../test/file.txt";
622 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
623 nbOk = res ? nbOk + 1 : nbOk;
624 nbFail = res ? nbFail : nbFail + 1;
626 path1 =
".\\tmp/test/file.txt";
628 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
629 nbOk = res ? nbOk + 1 : nbOk;
630 nbFail = res ? nbFail : nbFail + 1;
632 path1 =
".\\tmp/test\\../fake dir/..\\test\\file.txt";
634 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
635 nbOk = res ? nbOk + 1 : nbOk;
636 nbFail = res ? nbFail : nbFail + 1;
638 path2 =
"/tmp/test/../test/file.txt";
640 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
641 nbOk = res ? nbOk : nbOk + 1;
642 nbFail = res ? nbFail + 1 : nbFail;
644 std::cout <<
"Test vpIoTools::isSamePathname (WIN32 platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
647 std::cerr <<
"Failed test: vpIoTools::isSamePathname (WIN32 platform)" << std::endl;
656 std::string path1 =
"/tmp/" + username +
"/test/file.txt";
657 std::string path2 =
"/tmp/" + username +
"/test/../test/file.txt";
658 dummy_file.open(path1.c_str());
659 if (!dummy_file.is_open()) {
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/" + username +
"/./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 path1 =
"\\tmp/" + username +
"/test\\../dummy dir/..\\test\\file.txt";
681 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
682 nbOk = res ? nbOk + 1 : nbOk;
683 nbFail = res ? nbFail : nbFail + 1;
685 path2 =
"/tmp/" + username +
"/test/../test";
687 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
688 nbOk = res ? nbOk : nbOk + 1;
689 nbFail = res ? nbFail + 1 : nbFail;
691 path1 =
"/tmp/" + username +
"/test/";
693 std::cout <<
"vpIoTools::isSamePathname(" << path1 <<
", " << path2 <<
")? " << res << std::endl;
694 nbOk = res ? nbOk + 1 : nbOk;
695 nbFail = res ? nbFail : nbFail + 1;
697 std::cout <<
"Test vpIoTools::isSamePathname (Unix platform) - passed: " << nbOk <<
"/" << (nbOk + nbFail)
702 std::cerr <<
"Cannot remove directory: " <<
"/tmp/" << username <<
"/test" << std::endl;
705 std::cerr <<
"Cannot remove directory: " <<
"/tmp/" << username <<
"/dummy dir" << std::endl;
709 std::cerr <<
"Failed test: vpIoTools::isSamePathname (Unix platform)" << std::endl;
716 path1 =
"/tmp/" + username +
717 "/directory (1) with ' quote and spaces/file with ' quote (1) and " 719 dummy_file.open(path1.c_str());
720 if (!dummy_file.is_open()) {
726 std::cerr <<
"Problem with checkFilename(" << path1 <<
")!" << std::endl;
729 std::cout <<
"Test vpIoTools::checkFilename() is ok." << std::endl;
732 if (!
vpIoTools::remove(
"/tmp/" + username +
"/directory (1) with ' quote and spaces")) {
733 std::cerr <<
"Cannot remove directory: " <<
"/tmp/" << username <<
"/directory (1) with ' quote and spaces" << std::endl;
739 std::ifstream file_endianness(filename_endianness.c_str(), std::ios::in | std::ios::binary);
740 if (file_endianness.is_open()) {
741 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::min());
742 checkReadBinaryValue<short>(file_endianness, std::numeric_limits<short>::max());
744 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::min());
745 checkReadBinaryValue<unsigned short>(file_endianness, std::numeric_limits<unsigned short>::max());
747 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::min());
748 checkReadBinaryValue<int>(file_endianness, std::numeric_limits<int>::max());
750 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::min());
751 checkReadBinaryValue<unsigned int>(file_endianness, std::numeric_limits<unsigned int>::max());
753 checkReadBinaryValue<float>(file_endianness, -std::numeric_limits<float>::max());
754 checkReadBinaryValue<float>(file_endianness, std::numeric_limits<float>::max());
756 checkReadBinaryValue<double>(file_endianness, -std::numeric_limits<double>::max());
757 checkReadBinaryValue<double>(file_endianness, std::numeric_limits<double>::max());
759 std::cout <<
"Test endianness is ok." << std::endl;
761 std::cout <<
"Cannot open file: " << filename_endianness << std::endl;
765 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