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();
140#ifdef ENABLE_BUILD_INFO
141 VP_ATTRIBUTE_NO_DESTROY
static const std::string build_info =
142#include "version_string.inc"
146 VP_ATTRIBUTE_NO_DESTROY
static const std::string build_info =
"Build info is not available";
208#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
209 std::string username;
211 return "/tmp/" + username;
212#elif defined(_WIN32) && !defined(WINRT)
215 TCHAR lpTempPathBuffer[MAX_PATH];
216 DWORD dwRetVal = GetTempPath(MAX_PATH , lpTempPathBuffer );
217 if (dwRetVal > MAX_PATH || (dwRetVal == 0)) {
220 std::string temp_path(lpTempPathBuffer);
221 if (!temp_path.empty()) {
222 if (temp_path.back() ==
'\\') {
223 temp_path.resize(temp_path.size() - 1);
227 temp_path =
"C:\temp";
257#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
259 char *logname =
::getenv(
"LOGNAME");
261 username =
"unknown";
268 unsigned int info_buffer_size = 1024;
269 TCHAR *infoBuf =
new TCHAR[info_buffer_size];
270 DWORD bufCharCount =
static_cast<DWORD
>(info_buffer_size);
272 if (!GetUserName(infoBuf, &bufCharCount)) {
273 username =
"unknown";
281 username =
"unknown";
284 username =
"unknown";
304 std::string username;
345#if defined(_WIN32) && defined(WINRT)
347 "implemented on Universal Windows Platform"));
351 char *v_value =
::getenv(env.c_str());
370void vpIoTools::getVersion(
const std::string &version,
unsigned int &major,
unsigned int &minor,
unsigned int &patch)
372 if (version.size() == 0) {
378 size_t major_pos = version.find(
'.');
379 std::string major_str = version.substr(0, major_pos);
380 major =
static_cast<unsigned>(atoi(major_str.c_str()));
382 if (major_pos != std::string::npos) {
383 size_t minor_pos = version.find(
'.', major_pos + 1);
384 std::string minor_str = version.substr(major_pos + 1, (minor_pos - (major_pos + 1)));
385 minor =
static_cast<unsigned>(atoi(minor_str.c_str()));
387 if (minor_pos != std::string::npos) {
388 std::string patch_str = version.substr(minor_pos + 1);
389 patch =
static_cast<unsigned>(atoi(patch_str.c_str()));
415#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
417#elif defined(_WIN32) && defined(__MINGW32__)
423 if (dirname.empty()) {
427 std::string path_dirname =
path(dirname);
429 if (VP_STAT(path_dirname.c_str(), &stbuf) != 0) {
431 if (path_dirname.at(path_dirname.size() - 1) !=
separator) {
432 if (VP_STAT((path_dirname +
separator).c_str(), &stbuf) != 0) {
437 if (path_dirname.at(path_dirname.size() - 1) ==
separator) {
438 if (VP_STAT((path_dirname.substr(0, path_dirname.size() - 1)).c_str(), &stbuf) != 0) {
444#if defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
445 if ((stbuf.st_mode & S_IFDIR) == 0)
450#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
451 if ((stbuf.st_mode & S_IWUSR) == 0)
453 if ((stbuf.st_mode & S_IWRITE) == 0)
475#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
478 std::string v_filename =
path(fifofilename);
479 if (stat(v_filename.c_str(), &stbuf) != 0) {
482 if ((stbuf.st_mode & S_IFIFO) == 0) {
485 if ((stbuf.st_mode & S_IRUSR) == 0)
497#ifndef DOXYGEN_SHOULD_SKIP_THIS
498int vpIoTools::mkdir_p(
const std::string &path,
int mode)
501 if (
path.size() > PATH_MAX) {
502 errno = ENAMETOOLONG;
507 std::string cpy_path =
path;
508 std::string sub_path;
510 while (pos != std::string::npos) {
511 sub_path += cpy_path.substr(0, pos + 1);
513 bool stop_for_loop =
false;
515 cpy_path.erase(0, pos + 1);
516 stop_for_loop =
true;
518 if (!stop_for_loop) {
519#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
520 if (mkdir(sub_path.c_str(),
static_cast<mode_t
>(mode)) != 0)
526 if (errno != EEXIST) {
530 cpy_path.erase(0, pos + 1);
535 if (!cpy_path.empty()) {
536 sub_path += cpy_path;
537#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
538 if (mkdir(sub_path.c_str(),
static_cast<mode_t
>(mode)) != 0)
541 if (_mkdir(sub_path.c_str()) != 0)
544 if (errno != EEXIST) {
569#if ((!defined(__unix__) && !defined(__unix) && (!defined(__APPLE__) || !defined(__MACH__)))) && !defined(_WIN32)
570 std::cerr <<
"Unsupported platform for vpIoTools::makeDirectory()!" << std::endl;
574#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
576#elif defined(_WIN32) && defined(__MINGW32__)
582 if (dirname.empty()) {
586 std::string v_dirname =
path(dirname);
588#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
589 if (stat(v_dirname.c_str(), &stbuf) != 0)
590#elif defined(_WIN32) && defined(__MINGW32__)
591 if (stat(v_dirname.c_str(), &stbuf) != 0)
593 if (_stat(v_dirname.c_str(), &stbuf) != 0)
596 if (vpIoTools::mkdir_p(v_dirname, 0755) != 0) {
620#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
625 "Unable to create fifo file. '%s' is an existing directory.", fifoname.c_str()));
639 else if (mkfifo(fifoname.c_str(), 0666) < 0) {
648#if defined(_WIN32) && !defined(WINRT)
649std::string getUuid();
654 if (UuidCreate(&uuid) != RPC_S_OK) {
659 if (UuidToString(&uuid, &stringUuid) != RPC_S_OK) {
663 return reinterpret_cast<char *
>(stringUuid);
729#if defined(WINRT) || !defined(_WIN32) && !(defined(__unix__) || defined(__unix) || \
730 (defined(__APPLE__) && defined(__MACH__)))
734 std::string dirname_cpy = std::string(dirname);
735 std::string correctEnding =
"XXXXXX";
739#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
741 if (dirname_cpy.rfind(correctEnding) == std::string::npos) {
742 if (dirname_cpy.at(dirname_cpy.length() - 1) !=
'/') {
743 dirname_cpy = dirname_cpy +
"/";
752 dirname_cpy = dirname_cpy +
"XXXXXX";
755#elif defined(_WIN32) && !defined(WINRT)
757 dirname_cpy = dirname_cpy.substr(0, dirname_cpy.rfind(correctEnding));
759 dirname_cpy = dirname_cpy + getUuid();
765#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
766 if (dirname_cpy.at(dirname_cpy.length() - 1) !=
'/') {
767 dirname_cpy = dirname_cpy +
"/";
769 dirname_cpy = dirname_cpy +
"XXXXXX";
770#elif defined(_WIN32) && !defined(WINRT)
775#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
776 char *dirname_char =
new char[dirname_cpy.length() + 1];
777 strcpy(dirname_char, dirname_cpy.c_str());
779 char *computedDirname = mkdtemp(dirname_char);
781 if (!computedDirname) {
782 delete[] dirname_char;
786 std::string res(computedDirname);
787 delete[] dirname_char;
789#elif defined(_WIN32) && !defined(WINRT)
807#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
813 if (filename.empty()) {
817 std::string v_filename =
path(filename);
818#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
819 if (stat(v_filename.c_str(), &stbuf) != 0)
821 if (_stat(v_filename.c_str(), &stbuf) != 0)
826 if ((stbuf.st_mode & S_IFREG) == 0) {
829#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
830 if ((stbuf.st_mode & S_IRUSR) == 0)
832 if ((stbuf.st_mode & S_IREAD) == 0)
852#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
853#if TARGET_OS_IOS == 0
855 std::stringstream cmd;
860 int ret = system(cmd.str().c_str());
871 std::stringstream cmd;
876 int ret = system(cmd.str().c_str());
883 src.c_str(), dst.c_str()));
889#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
890#if TARGET_OS_IOS == 0
892 std::stringstream cmd;
897 int ret = system(cmd.str().c_str());
908 std::stringstream cmd;
913 int ret = system(cmd.str().c_str());
920 src.c_str(), dst.c_str()));
925 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
944#
if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
948 if (
::remove(file_or_dir.c_str()) != 0) {
956#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
957#if TARGET_OS_IOS == 0
959 std::stringstream cmd;
963 int ret = system(cmd.str().c_str());
970 file_or_dir.c_str()));
974 std::stringstream cmd;
975 cmd <<
"rmdir /S /Q ";
978 int ret = system(cmd.str().c_str());
985 file_or_dir.c_str()));
990 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
1006 if (
::rename(oldfilename.c_str(), newfilename.c_str()) != 0) {
1026 std::string
path(pathname);
1029 for (
unsigned int i = 0; i <
path.length(); ++i)
1032#elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
1033 unsigned int path_length =
path.length();
1034 for (
unsigned int i = 0; i < path_length; ++i) {
1035 if (
path[i] ==
'\\') {
1039#if TARGET_OS_IOS == 0
1044 wordexp_t exp_result;
1047 replaceAll(
path,
"'",
"'\\''");
1049 wordexp(std::string(
"'" +
path +
"'").c_str(), &exp_result, 0);
1050 path = exp_result.we_wordc == 1 ? exp_result.we_wordv[0] :
"";
1051 wordfree(&exp_result);
1074 std::string data_path;
1075 std::string file_to_test(
"mbt/cube.cao");
1076 std::string filename;
1080 filename = data_path +
"/" + file_to_test;
1089 filename = data_path +
"/" + file_to_test;
1098 filename = data_path +
"/" + file_to_test;
1106#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1108 data_path =
"/usr/share/visp-images-data/ViSP-images";
1109 filename = data_path +
"/" + file_to_test;
1113 data_path =
"/usr/share/visp-images-data/visp-images";
1114 filename = data_path +
"/" + file_to_test;
1162 std::string sep =
"\\";
1163 std::string altsep =
"/";
1164 std::string extsep =
".";
1167 std::string sep =
"/";
1168 std::string altsep =
"";
1169 std::string extsep =
".";
1202 int sepIndex =
static_cast<int>(pathname.rfind(sep));
1203 if (!altsep.empty()) {
1204 int altsepIndex =
static_cast<int>(pathname.rfind(altsep));
1205 sepIndex = std::max<int>(sepIndex, altsepIndex);
1208 size_t dotIndex = pathname.rfind(extsep);
1209 if (dotIndex != std::string::npos) {
1211 size_t npos = std::string::npos;
1212 if (((sepIndex !=
static_cast<int>(npos)) && (
static_cast<int>(dotIndex) > sepIndex)) ||
1213 (sepIndex ==
static_cast<int>(npos))) {
1214 if (sepIndex ==
static_cast<int>(npos)) {
1217 size_t filenameIndex =
static_cast<size_t>(sepIndex) +
static_cast<size_t>(1);
1219 while (filenameIndex < dotIndex) {
1220 if (pathname.compare(filenameIndex, 1, extsep) != 0) {
1221 return pathname.substr(dotIndex);
1238 if (pathname.size() > 0) {
1242 if (index != std::string::npos) {
1243 return convertedPathname.substr(index + 1);
1246 return convertedPathname;
1261 size_t found = name.find_last_of(
".");
1262 std::string name_we = name.substr(0, found);
1305 size_t indexBegin = format.find_last_of(
'%');
1306 size_t indexEnd = format.find_first_of(
'd', indexBegin);
1307 size_t suffixLength = format.length() - indexEnd - 1;
1309 if (filename.length() <= (suffixLength + indexBegin)) {
1312 size_t indexLength = filename.length() - suffixLength - indexBegin;
1313 std::string indexSubstr = filename.substr(indexBegin, indexLength);
1314 std::istringstream ss(indexSubstr);
1317 if (ss.fail() || (index < 0) || (!ss.eof())) {
1322 char nameByFormat[FILENAME_MAX];
1323 snprintf(nameByFormat, FILENAME_MAX, format.c_str(), index);
1324 if (std::string(nameByFormat) != filename) {
1347 if (pathname.size() > 0) {
1351 if (index != std::string::npos) {
1352 return convertedPathname.substr(0, index);
1373#if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1374 const std::string::const_iterator it_end = input.cend();
1375 for (std::string::const_iterator it = input.cbegin(); it != it_end; ++it) {
1377 const std::string::const_iterator it_end = input.end();
1378 for (std::string::const_iterator it = input.begin(); it != it_end; ++it) {
1380 out += std::tolower(*it);
1396#if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
1397 const std::string::const_iterator it_end = input.cend();
1398 for (std::string::const_iterator it = input.cbegin(); it != it_end; ++it) {
1400 const std::string::const_iterator it_end = input.end();
1401 for (std::string::const_iterator it = input.begin(); it != it_end; ++it) {
1403 out += std::toupper(*it);
1444 const char *fmt = name.c_str();
1445 int sz = std::snprintf(
nullptr, 0, fmt, val);
1447 std::vector<char> buf(sz);
1448 std::snprintf(buf.data(), sz, fmt, val);
1449 std::string str(buf.begin(), buf.end());
1450#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
1453 str.erase(str.end()-1, str.end());
1469#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1470 std::string real_path_str = pathname;
1471 char *real_path = realpath(pathname.c_str(),
nullptr);
1474 real_path_str = real_path;
1477 return real_path_str;
1478#elif defined(_WIN32)
1479#if (!defined(WINRT))
1480 std::string real_path_str = pathname;
1482 TCHAR buffer[4096] = TEXT(
"");
1484 retval = GetFullPathName(pathname.c_str(), 4096, buffer, 0);
1486 real_path_str = buffer;
1488 return real_path_str;
1491 "Cannot get absolute path of %s: not implemented on "
1492 "Universal Windows Platform",
1510 if ((child.size() == 0) && (parent.size() == 0)) {
1514 if (child.size() == 0) {
1518 if (parent.size() == 0) {
1525 std::stringstream ss;
1527 std::string stringSeparator;
1528 ss >> stringSeparator;
1530 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1531 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1533 if (lastConvertedParentChar == stringSeparator) {
1534 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1537 if (firstConvertedChildChar == stringSeparator) {
1538 convertedChild = convertedChild.substr(1);
1563 return (
path.size() > 0) && ((
path.substr(0, 1) ==
"/") || (
path.substr(0, 1) ==
"\\"));
1583 return (path1_normalize == path2_normalize);
1646#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1647 return std::pair<std::string, std::string>(
"", pathname);
1649 const std::string sep =
"\\";
1650 const std::string sepsep =
"\\\\";
1651 const std::string altsep =
"/";
1653 if (pathname.size() > 1) {
1654 std::string normPathname = pathname;
1655 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1657 if (normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1662 size_t index = normPathname.find(sep, 2);
1663 if (index == std::string::npos) {
1664 return std::pair<std::string, std::string>(
"", pathname);
1667 size_t index2 = normPathname.find(sep, index + 1);
1670 if (index2 == index + 1) {
1671 return std::pair<std::string, std::string>(
"", pathname);
1674 if (index2 == std::string::npos) {
1675 index2 = pathname.size();
1678 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1681 if (normPathname[1] ==
':') {
1682 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1686 return std::pair<std::string, std::string>(
"", pathname);
1744 size_t startIndex = 0;
1746 std::string chainToSplit = chain;
1747 std::vector<std::string> subChain;
1748 size_t sepIndex = chainToSplit.find(sep);
1750 while (sepIndex != std::string::npos) {
1751 std::string sub = chainToSplit.substr(startIndex, sepIndex);
1753 subChain.push_back(sub);
1755 chainToSplit = chainToSplit.substr(sepIndex + 1, chain.size() - 1);
1757 sepIndex = chainToSplit.find(sep);
1759 if (!chainToSplit.empty()) {
1760 subChain.push_back(chainToSplit);
1779 std::string dirName =
path(pathname);
1781#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1783 std::vector<std::string> files;
1784 struct dirent **list =
nullptr;
1785 int filesCount = scandir(dirName.c_str(), &list,
nullptr,
nullptr);
1786 if (filesCount == -1) {
1789 for (
int i = 0; i < filesCount; ++i) {
1790 std::string fileName = list[i]->d_name;
1791 if ((fileName !=
".") && (fileName !=
"..")) {
1792 files.push_back(fileName);
1797 std::sort(files.begin(), files.end());
1800#elif defined(_WIN32)
1801#if (!defined(WINRT))
1803 std::vector<std::string> files;
1804 std::string fileMask = dirName;
1805 fileMask.append(
"\\*");
1806 WIN32_FIND_DATA FindFileData;
1807 HANDLE hFind = FindFirstFile(fileMask.c_str(), &FindFileData);
1809 if (HandleToLong(&hFind) == ERROR_FILE_NOT_FOUND) {
1812 if (hFind == INVALID_HANDLE_VALUE) {
1816 std::string fileName = FindFileData.cFileName;
1817 if (fileName !=
"." && fileName !=
"..") {
1818 files.push_back(fileName);
1820 }
while (FindNextFile(hFind, &FindFileData));
1822 std::sort(files.begin(), files.end());
1827 "Cannot read files of directory %s: not implemented on "
1828 "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.