64#include <visp3/core/vpEndian.h>
65#include <visp3/core/vpIoException.h>
66#include <visp3/core/vpIoTools.h>
67#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
74# pragma clang diagnostic push
75# pragma clang diagnostic ignored "-Wnonportable-system-include-path"
81# pragma clang diagnostic pop
93#if defined(__APPLE__) && defined(__MACH__)
94#include <TargetConditionals.h>
99#define PATH_MAX _MAX_PATH
105#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
107#elif defined(_WIN32) && defined(__MINGW32__)
118#if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
119#if (TARGET_OS_IOS == 0) && !defined(__ANDROID__)
120void replaceAll(std::string &str,
const std::string &search,
const std::string &replace)
122 size_t start_pos = 0;
123 while ((start_pos =
str.find(search, start_pos)) != std::string::npos) {
124 str.replace(start_pos, search.length(), replace);
125 start_pos += replace.length();
139 VP_ATTRIBUTE_NO_DESTROY
static std::string build_info =
140#include "version_string.inc"
202#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
203 std::string username;
205 return "/tmp/" + username;
206#elif defined(_WIN32) && !defined(WINRT)
209 TCHAR lpTempPathBuffer[MAX_PATH];
210 DWORD dwRetVal = GetTempPath(MAX_PATH , lpTempPathBuffer );
211 if (dwRetVal > MAX_PATH || (dwRetVal == 0)) {
214 std::string temp_path(lpTempPathBuffer);
215 if (!temp_path.empty()) {
216 if (temp_path.back() ==
'\\') {
217 temp_path.resize(temp_path.size() - 1);
221 temp_path =
"C:\temp";
251#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
253 char *logname =
::getenv(
"LOGNAME");
255 username =
"unknown";
262 unsigned int info_buffer_size = 1024;
263 TCHAR *infoBuf =
new TCHAR[info_buffer_size];
264 DWORD bufCharCount =
static_cast<DWORD
>(info_buffer_size);
266 if (!GetUserName(infoBuf, &bufCharCount)) {
267 username =
"unknown";
275 username =
"unknown";
278 username =
"unknown";
298 std::string username;
339#if defined(_WIN32) && defined(WINRT)
341 "implemented on Universal Windows Platform"));
345 char *v_value =
::getenv(env.c_str());
364void vpIoTools::getVersion(
const std::string &version,
unsigned int &major,
unsigned int &minor,
unsigned int &patch)
366 if (version.size() == 0) {
372 size_t major_pos = version.find(
'.');
373 std::string major_str = version.substr(0, major_pos);
374 major =
static_cast<unsigned>(atoi(major_str.c_str()));
376 if (major_pos != std::string::npos) {
377 size_t minor_pos = version.find(
'.', major_pos + 1);
378 std::string minor_str = version.substr(major_pos + 1, (minor_pos - (major_pos + 1)));
379 minor =
static_cast<unsigned>(atoi(minor_str.c_str()));
381 if (minor_pos != std::string::npos) {
382 std::string patch_str = version.substr(minor_pos + 1);
383 patch =
static_cast<unsigned>(atoi(patch_str.c_str()));
409#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
411#elif defined(_WIN32) && defined(__MINGW32__)
417 if (dirname.empty()) {
421 std::string path_dirname =
path(dirname);
423 if (VP_STAT(path_dirname.c_str(), &stbuf) != 0) {
425 if (path_dirname.at(path_dirname.size() - 1) !=
separator) {
426 if (VP_STAT((path_dirname +
separator).c_str(), &stbuf) != 0) {
431 if (path_dirname.at(path_dirname.size() - 1) ==
separator) {
432 if (VP_STAT((path_dirname.substr(0, path_dirname.size() - 1)).c_str(), &stbuf) != 0) {
438#if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
439 if ((stbuf.st_mode & S_IFDIR) == 0)
444#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
445 if ((stbuf.st_mode & S_IWUSR) == 0)
447 if ((stbuf.st_mode & S_IWRITE) == 0)
469#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
472 std::string v_filename =
path(fifofilename);
473 if (stat(v_filename.c_str(), &stbuf) != 0) {
476 if ((stbuf.st_mode & S_IFIFO) == 0) {
479 if ((stbuf.st_mode & S_IRUSR) == 0)
491#ifndef DOXYGEN_SHOULD_SKIP_THIS
492int vpIoTools::mkdir_p(
const std::string &path,
int mode)
495 if (
path.size() > PATH_MAX) {
496 errno = ENAMETOOLONG;
501 std::string cpy_path =
path;
502 std::string sub_path;
504 while (pos != std::string::npos) {
505 sub_path += cpy_path.substr(0, pos + 1);
507 bool stop_for_loop =
false;
509 cpy_path.erase(0, pos + 1);
510 stop_for_loop =
true;
512 if (!stop_for_loop) {
513#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
514 if (mkdir(sub_path.c_str(),
static_cast<mode_t
>(mode)) != 0)
520 if (errno != EEXIST) {
524 cpy_path.erase(0, pos + 1);
529 if (!cpy_path.empty()) {
530 sub_path += cpy_path;
531#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
532 if (mkdir(sub_path.c_str(),
static_cast<mode_t
>(mode)) != 0)
535 if (_mkdir(sub_path.c_str()) != 0)
538 if (errno != EEXIST) {
563#if ((!defined(__unix__) && !defined(__unix) && (!defined(__APPLE__) || !defined(__MACH__)))) && !defined(_WIN32)
564 std::cerr <<
"Unsupported platform for vpIoTools::makeDirectory()!" << std::endl;
568#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
570#elif defined(_WIN32) && defined(__MINGW32__)
576 if (dirname.empty()) {
580 std::string v_dirname =
path(dirname);
582#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
583 if (stat(v_dirname.c_str(), &stbuf) != 0)
584#elif defined(_WIN32) && defined(__MINGW32__)
585 if (stat(v_dirname.c_str(), &stbuf) != 0)
587 if (_stat(v_dirname.c_str(), &stbuf) != 0)
590 if (vpIoTools::mkdir_p(v_dirname, 0755) != 0) {
614#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
619 "Unable to create fifo file. '%s' is an existing directory.", fifoname.c_str()));
633 else if (mkfifo(fifoname.c_str(), 0666) < 0) {
642#if defined(_WIN32) && !defined(WINRT)
643std::string getUuid();
648 if (UuidCreate(&uuid) != RPC_S_OK) {
653 if (UuidToString(&uuid, &stringUuid) != RPC_S_OK) {
657 return reinterpret_cast<char *
>(stringUuid);
723#if defined(WINRT) || !defined(_WIN32) && !(defined(__unix__) || defined(__unix) || \
724 (defined(__APPLE__) && defined(__MACH__)))
728 std::string dirname_cpy = std::string(dirname);
729 std::string correctEnding =
"XXXXXX";
733#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
735 if (dirname_cpy.rfind(correctEnding) == std::string::npos) {
736 if (dirname_cpy.at(dirname_cpy.length() - 1) !=
'/') {
737 dirname_cpy = dirname_cpy +
"/";
746 dirname_cpy = dirname_cpy +
"XXXXXX";
749#elif defined(_WIN32) && !defined(WINRT)
751 dirname_cpy = dirname_cpy.substr(0, dirname_cpy.rfind(correctEnding));
753 dirname_cpy = dirname_cpy + getUuid();
759#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
760 if (dirname_cpy.at(dirname_cpy.length() - 1) !=
'/') {
761 dirname_cpy = dirname_cpy +
"/";
763 dirname_cpy = dirname_cpy +
"XXXXXX";
764#elif defined(_WIN32) && !defined(WINRT)
769#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
770 char *dirname_char =
new char[dirname_cpy.length() + 1];
771 strcpy(dirname_char, dirname_cpy.c_str());
773 char *computedDirname = mkdtemp(dirname_char);
775 if (!computedDirname) {
776 delete[] dirname_char;
780 std::string res(computedDirname);
781 delete[] dirname_char;
783#elif defined(_WIN32) && !defined(WINRT)
801#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
807 if (filename.empty()) {
811 std::string v_filename =
path(filename);
812#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
813 if (stat(v_filename.c_str(), &stbuf) != 0)
815 if (_stat(v_filename.c_str(), &stbuf) != 0)
820 if ((stbuf.st_mode & S_IFREG) == 0) {
823#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
824 if ((stbuf.st_mode & S_IRUSR) == 0)
826 if ((stbuf.st_mode & S_IREAD) == 0)
846#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
847#if TARGET_OS_IOS == 0
849 std::stringstream cmd;
854 int ret = system(cmd.str().c_str());
865 std::stringstream cmd;
870 int ret = system(cmd.str().c_str());
877 src.c_str(), dst.c_str()));
883#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
884#if TARGET_OS_IOS == 0
886 std::stringstream cmd;
891 int ret = system(cmd.str().c_str());
902 std::stringstream cmd;
907 int ret = system(cmd.str().c_str());
914 src.c_str(), dst.c_str()));
919 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
938#
if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
942 if (
::remove(file_or_dir.c_str()) != 0) {
950#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
951#if TARGET_OS_IOS == 0
953 std::stringstream cmd;
957 int ret = system(cmd.str().c_str());
964 file_or_dir.c_str()));
968 std::stringstream cmd;
969 cmd <<
"rmdir /S /Q ";
972 int ret = system(cmd.str().c_str());
979 file_or_dir.c_str()));
984 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
1000 if (
::rename(oldfilename.c_str(), newfilename.c_str()) != 0) {
1020 std::string
path(pathname);
1023 for (
unsigned int i = 0; i <
path.length(); ++i)
1026#elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
1027 unsigned int path_length =
path.length();
1028 for (
unsigned int i = 0; i < path_length; ++i) {
1029 if (
path[i] ==
'\\') {
1033#if TARGET_OS_IOS == 0
1038 wordexp_t exp_result;
1041 replaceAll(
path,
"'",
"'\\''");
1043 wordexp(std::string(
"'" +
path +
"'").c_str(), &exp_result, 0);
1044 path = exp_result.we_wordc == 1 ? exp_result.we_wordv[0] :
"";
1045 wordfree(&exp_result);
1068 std::string data_path;
1069 std::string file_to_test(
"mbt/cube.cao");
1070 std::string filename;
1074 filename = data_path +
"/" + file_to_test;
1083 filename = data_path +
"/" + file_to_test;
1092 filename = data_path +
"/" + file_to_test;
1100#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1102 data_path =
"/usr/share/visp-images-data/ViSP-images";
1103 filename = data_path +
"/" + file_to_test;
1107 data_path =
"/usr/share/visp-images-data/visp-images";
1108 filename = data_path +
"/" + file_to_test;
1156 std::string sep =
"\\";
1157 std::string altsep =
"/";
1158 std::string extsep =
".";
1161 std::string sep =
"/";
1162 std::string altsep =
"";
1163 std::string extsep =
".";
1196 int sepIndex =
static_cast<int>(pathname.rfind(sep));
1197 if (!altsep.empty()) {
1198 int altsepIndex =
static_cast<int>(pathname.rfind(altsep));
1199 sepIndex = std::max<int>(sepIndex, altsepIndex);
1202 size_t dotIndex = pathname.rfind(extsep);
1203 if (dotIndex != std::string::npos) {
1205 size_t npos = std::string::npos;
1206 if (((sepIndex !=
static_cast<int>(npos)) && (
static_cast<int>(dotIndex) > sepIndex)) ||
1207 (sepIndex ==
static_cast<int>(npos))) {
1208 if (sepIndex ==
static_cast<int>(npos)) {
1211 size_t filenameIndex =
static_cast<size_t>(sepIndex) +
static_cast<size_t>(1);
1213 while (filenameIndex < dotIndex) {
1214 if (pathname.compare(filenameIndex, 1, extsep) != 0) {
1215 return pathname.substr(dotIndex);
1232 if (pathname.size() > 0) {
1236 if (index != std::string::npos) {
1237 return convertedPathname.substr(index + 1);
1240 return convertedPathname;
1255 size_t found = name.find_last_of(
".");
1256 std::string name_we = name.substr(0, found);
1299 size_t indexBegin = format.find_last_of(
'%');
1300 size_t indexEnd = format.find_first_of(
'd', indexBegin);
1301 size_t suffixLength = format.length() - indexEnd - 1;
1303 if (filename.length() <= (suffixLength + indexBegin)) {
1306 size_t indexLength = filename.length() - suffixLength - indexBegin;
1307 std::string indexSubstr = filename.substr(indexBegin, indexLength);
1308 std::istringstream ss(indexSubstr);
1311 if (ss.fail() || (index < 0) || (!ss.eof())) {
1316 char nameByFormat[FILENAME_MAX];
1317 snprintf(nameByFormat, FILENAME_MAX, format.c_str(), index);
1318 if (std::string(nameByFormat) != filename) {
1341 if (pathname.size() > 0) {
1345 if (index != std::string::npos) {
1346 return convertedPathname.substr(0, index);
1367#if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1368 const std::string::const_iterator it_end = input.cend();
1369 for (std::string::const_iterator it = input.cbegin(); it != it_end; ++it) {
1371 const std::string::const_iterator it_end = input.end();
1372 for (std::string::const_iterator it = input.begin(); it != it_end; ++it) {
1374 out += std::tolower(*it);
1390#if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1391 const std::string::const_iterator it_end = input.cend();
1392 for (std::string::const_iterator it = input.cbegin(); it != it_end; ++it) {
1394 const std::string::const_iterator it_end = input.end();
1395 for (std::string::const_iterator it = input.begin(); it != it_end; ++it) {
1397 out += std::toupper(*it);
1438 const char *fmt = name.c_str();
1439 int sz = std::snprintf(
nullptr, 0, fmt, val);
1441 std::vector<char> buf(sz);
1442 std::snprintf(buf.data(), sz, fmt, val);
1443 std::string str(buf.begin(), buf.end());
1444#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
1447 str.erase(str.end()-1, str.end());
1463#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1464 std::string real_path_str = pathname;
1465 char *real_path = realpath(pathname.c_str(),
nullptr);
1468 real_path_str = real_path;
1471 return real_path_str;
1472#elif defined(_WIN32)
1473#if (!defined(WINRT))
1474 std::string real_path_str = pathname;
1476 TCHAR buffer[4096] = TEXT(
"");
1478 retval = GetFullPathName(pathname.c_str(), 4096, buffer, 0);
1480 real_path_str = buffer;
1482 return real_path_str;
1485 "Cannot get absolute path of %s: not implemented on "
1486 "Universal Windows Platform",
1504 if ((child.size() == 0) && (parent.size() == 0)) {
1508 if (child.size() == 0) {
1512 if (parent.size() == 0) {
1519 std::stringstream ss;
1521 std::string stringSeparator;
1522 ss >> stringSeparator;
1524 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1525 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1527 if (lastConvertedParentChar == stringSeparator) {
1528 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1531 if (firstConvertedChildChar == stringSeparator) {
1532 convertedChild = convertedChild.substr(1);
1557 return (
path.size() > 0) && ((
path.substr(0, 1) ==
"/") || (
path.substr(0, 1) ==
"\\"));
1577 return (path1_normalize == path2_normalize);
1640#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1641 return std::pair<std::string, std::string>(
"", pathname);
1643 const std::string sep =
"\\";
1644 const std::string sepsep =
"\\\\";
1645 const std::string altsep =
"/";
1647 if (pathname.size() > 1) {
1648 std::string normPathname = pathname;
1649 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1651 if (normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1656 size_t index = normPathname.find(sep, 2);
1657 if (index == std::string::npos) {
1658 return std::pair<std::string, std::string>(
"", pathname);
1661 size_t index2 = normPathname.find(sep, index + 1);
1664 if (index2 == index + 1) {
1665 return std::pair<std::string, std::string>(
"", pathname);
1668 if (index2 == std::string::npos) {
1669 index2 = pathname.size();
1672 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1675 if (normPathname[1] ==
':') {
1676 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1680 return std::pair<std::string, std::string>(
"", pathname);
1738 size_t startIndex = 0;
1740 std::string chainToSplit = chain;
1741 std::vector<std::string> subChain;
1742 size_t sepIndex = chainToSplit.find(sep);
1744 while (sepIndex != std::string::npos) {
1745 std::string sub = chainToSplit.substr(startIndex, sepIndex);
1747 subChain.push_back(sub);
1749 chainToSplit = chainToSplit.substr(sepIndex + 1, chain.size() - 1);
1751 sepIndex = chainToSplit.find(sep);
1753 if (!chainToSplit.empty()) {
1754 subChain.push_back(chainToSplit);
1773 std::string dirName =
path(pathname);
1775#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1777 std::vector<std::string> files;
1778 struct dirent **list =
nullptr;
1779 int filesCount = scandir(dirName.c_str(), &list,
nullptr,
nullptr);
1780 if (filesCount == -1) {
1783 for (
int i = 0; i < filesCount; ++i) {
1784 std::string fileName = list[i]->d_name;
1785 if ((fileName !=
".") && (fileName !=
"..")) {
1786 files.push_back(fileName);
1791 std::sort(files.begin(), files.end());
1794#elif defined(_WIN32)
1795#if (!defined(WINRT))
1797 std::vector<std::string> files;
1798 std::string fileMask = dirName;
1799 fileMask.append(
"\\*");
1800 WIN32_FIND_DATA FindFileData;
1801 HANDLE hFind = FindFirstFile(fileMask.c_str(), &FindFileData);
1803 if (HandleToLong(&hFind) == ERROR_FILE_NOT_FOUND) {
1806 if (hFind == INVALID_HANDLE_VALUE) {
1810 std::string fileName = FindFileData.cFileName;
1811 if (fileName !=
"." && fileName !=
"..") {
1812 files.push_back(fileName);
1814 }
while (FindNextFile(hFind, &FindFileData));
1816 std::sort(files.begin(), files.end());
1821 "Cannot read files of directory %s: not implemented on "
1822 "Universal Windows Platform",
error that can be emitted by ViSP classes.
@ notImplementedError
Not implemented.
Error that can be emitted by the vpIoTools class and its derivatives.
@ invalidDirectoryName
Directory name is invalid.
@ cantCreateDirectory
Unable to create a directory.
@ cantGetenv
Cannot get environment variable value.