42 #include <visp3/core/vpIoTools.h>
43 #include <visp3/core/vpDebug.h>
44 #include <visp3/core/vpIoException.h>
50 #include <sys/types.h>
55 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
117 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
119 char *_username = NULL;
121 if (_username == NULL) {
122 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable" );
124 "Cannot get the username")) ;
126 username = _username;
127 #elif defined(_WIN32)
128 unsigned int info_buffer_size = 1024;
129 TCHAR *infoBuf =
new TCHAR [info_buffer_size];
130 DWORD bufCharCount = (DWORD) info_buffer_size;
132 if( ! GetUserName( infoBuf, &bufCharCount ) ) {
136 "Cannot get the username")) ;
163 std::string username;
164 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
166 char *_username = NULL;
168 if (_username == NULL) {
169 vpERROR_TRACE(
"Cannot get the username. Check your LOGNAME environment variable" );
171 "Cannot get the username")) ;
173 username = _username;
174 #elif defined(_WIN32)
175 unsigned int info_buffer_size = 1024;
176 TCHAR *infoBuf =
new TCHAR [info_buffer_size];
177 DWORD bufCharCount = (DWORD) info_buffer_size;
179 if( ! GetUserName( infoBuf, &bufCharCount ) ) {
183 "Cannot get the username")) ;
230 if (_value == NULL) {
231 vpERROR_TRACE(
"Cannot get the environment variable value" );
233 "Cannot get the environment variable value")) ;
290 if(version.size() == 0){
296 size_t major_pos = version.find(
'.');
297 std::string major_str = version.substr(0, major_pos);
298 major = (unsigned)atoi(major_str.c_str());
300 if(major_pos != std::string::npos){
301 size_t minor_pos = version.find(
'.', major_pos+1);
302 std::string minor_str = version.substr(major_pos+1, (minor_pos - (major_pos+1)));
303 minor = (unsigned)atoi(minor_str.c_str());
305 if(minor_pos != std::string::npos){
306 std::string patch_str = version.substr(minor_pos+1);
307 patch = (unsigned)atoi(patch_str.c_str());
337 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
339 #elif defined(_WIN32)
343 if ( dirname == NULL || dirname[0] ==
'\0' ) {
347 std::string _dirname =
path(dirname);
349 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
350 if ( stat( _dirname.c_str(), &stbuf ) != 0 )
351 #elif defined(_WIN32)
352 if ( _stat( _dirname.c_str(), &stbuf ) != 0 )
357 if ( (stbuf.st_mode & S_IFDIR) == 0 ) {
360 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
361 if ( (stbuf.st_mode & S_IWUSR) == 0 )
362 #elif defined(_WIN32)
363 if ( (stbuf.st_mode & S_IWRITE) == 0 )
406 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
408 #elif defined(_WIN32)
412 if ( dirname == NULL || dirname[0] ==
'\0' ) {
415 "invalid directory name")) ;
418 std::string _dirname =
path(dirname);
420 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
421 if ( stat( _dirname.c_str(), &stbuf ) != 0 )
422 #elif defined(_WIN32)
423 if ( _stat( _dirname.c_str(), &stbuf ) != 0 )
426 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
427 if ( mkdir( _dirname.c_str(), (mode_t)0755 ) != 0 )
428 #elif defined(_WIN32)
429 if ( _mkdir( _dirname.c_str()) != 0 )
432 vpERROR_TRACE(
"unable to create directory '%s'\n", dirname );
434 "unable to create directory")) ;
440 vpERROR_TRACE(
"unable to create directory '%s'\n", dirname );
442 "unable to create directory")) ;
465 vpERROR_TRACE(
"unable to create directory '%s'\n",dirname.c_str());
467 "unable to create directory")) ;
487 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
489 #elif defined(_WIN32)
493 if ( filename == NULL || filename[0] ==
'\0' ) {
497 std::string _filename =
path(filename);
498 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
499 if ( stat( _filename.c_str(), &stbuf ) != 0 )
500 #elif defined(_WIN32)
501 if ( _stat( _filename.c_str(), &stbuf ) != 0 )
506 if ( (stbuf.st_mode & S_IFREG) == 0 ) {
509 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
510 if ( (stbuf.st_mode & S_IRUSR) == 0 )
511 #elif defined(_WIN32)
512 if ( (stbuf.st_mode & S_IREAD) == 0 )
553 char cmd[FILENAME_MAX];
558 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
559 sprintf(cmd,
"cp -p %s %s", src, dst);
560 #elif defined(_WIN32)
563 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
571 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
572 sprintf(cmd,
"cp -p -r %s %s", src, dst);
573 #elif defined(_WIN32)
576 sprintf(cmd,
"copy %s %s", src_.c_str(), dst_.c_str());
584 std::cout <<
"Cannot copy: " << src <<
" in " << dst << std::endl;
622 if (::
remove(file_or_dir) != 0)
629 char cmd[FILENAME_MAX];
630 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
631 sprintf(cmd,
"rm -rf %s", file_or_dir);
632 #elif defined(_WIN32)
634 sprintf(cmd,
"rmdir /S /Q %s", file_or_dir_.c_str());
636 int ret = system( cmd );
642 std::cout <<
"Cannot remove: " << file_or_dir << std::endl;
677 if (::
rename(oldfilename, newfilename) != 0)
717 std::string
path(pathname);
720 for(
unsigned int i=0 ; i<path.length() ; i++)
721 if( path[i] ==
'/') path[i] =
'\\';
723 for(
unsigned int i=0 ; i<path.length() ; i++)
724 if( path[i] ==
'\\') path[i] =
'/';
725 wordexp_t exp_result;
726 wordexp(path.c_str(), &exp_result, 0);
727 path = std::string(exp_result.we_wordv[0]);
728 wordfree(&exp_result);
749 return path(pathname.c_str());
764 std::ifstream confContent(
configFile.c_str(), std::ios::in);
766 if(confContent.is_open())
768 std::string line,var,val;
771 std::string stop[3] = {
" ",
"\t",
"#"};
772 while(std::getline(confContent, line))
774 if((line.find(
"#",0,1) != 0) && (line.size() > 2))
779 k = (
unsigned long)line.find(
" ");
780 var = line.substr(0,k);
783 for(
unsigned i=0;i<3;++i)
786 c = (int)line.size();
787 long unsigned int c_ = (
long unsigned int) c;
788 val = line.substr(k+1,c_-k-1);
814 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
819 value = (
float) M_PI;
821 value = (
float) (M_PI/2.0);
823 value = (
float) (-M_PI/2.0);
830 std::cout << var <<
" not found in config file" << std::endl;
844 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
860 std::cout << var <<
" not found in config file" << std::endl;
875 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
884 std::cout << var <<
" not found in config file" << std::endl;
900 value = (
unsigned int) v;
947 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
956 std::cout << var <<
" not found in config file" << std::endl;
976 for(
unsigned int k=0;k<
configVars.size() && found==
false;++k)
982 if(nCols != 0 && nRows != 0)
983 value.
resize(nRows, nCols);
985 for(
unsigned int i=0;i<value.
getRows();++i)
986 for(
unsigned int j=0;j<value.
getCols();++j)
990 if(nb.compare(
"PI") == 0)
992 else if(nb.compare(
"PI/2") == 0)
993 value[i][j] = M_PI/2;
994 else if(nb.compare(
"-PI/2") == 0)
995 value[i][j] = -M_PI/2;
997 value[i][j] = atof(nb.c_str());
1003 std::cout << var <<
" not found in config file" << std::endl;
1020 else if(strFalse !=
"")
1034 if(std::fabs(val) < std::numeric_limits<double>::epsilon())
1037 sprintf(valC,
"%.3f", val);
1038 std::string valS(valC);
1093 std::string data_path;
1094 std::string file_to_test(
"ViSP-images/mbt/cube.cao");
1095 std::string filename;
1096 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
1098 data_path =
"/usr/share/visp-images-data";
1099 filename = data_path +
"/" + file_to_test;
1106 filename = data_path +
"/" + file_to_test;
1132 std::string sep =
"/";
1133 std::string altsep =
"";
1134 std::string extsep =
".";
1172 int sepIndex = (int)pathname.rfind(sep);
1173 if(!altsep.empty()) {
1174 int altsepIndex = (int)pathname.rfind(altsep);
1175 sepIndex = (std::max)(sepIndex, altsepIndex);
1178 size_t dotIndex = pathname.rfind(extsep);
1179 if(dotIndex != std::string::npos) {
1181 if((sepIndex != (
int)std::string::npos && (
int)dotIndex > sepIndex) || sepIndex == (
int)std::string::npos) {
1182 if(sepIndex == (
int)std::string::npos) {
1185 size_t filenameIndex = (size_t)(sepIndex + 1);
1187 while(filenameIndex < dotIndex) {
1188 if(pathname.compare(filenameIndex, 1, extsep) != 0) {
1189 return pathname.substr(dotIndex);
1207 if(pathname.size() > 0)
1212 if(index != std::string::npos) {
1213 return convertedPathname.substr(index + 1);
1216 return convertedPathname;
1230 size_t found = name.find_last_of(
".");
1231 std::string name_we = name.substr(0, found);
1242 if(pathname.size() > 0)
1247 if(index != std::string::npos) {
1248 return convertedPathname.substr(0, index);
1267 if(child.size() == 0 && parent.size() == 0)
1272 if(child.size() == 0)
1277 if(parent.size() == 0)
1285 std::stringstream ss;
1287 std::string stringSeparator;
1288 ss >> stringSeparator;
1290 std::string lastConvertedParentChar = convertedParent.substr(convertedParent.size() - 1);
1291 std::string firstConvertedChildChar = convertedChild.substr(0, 1);
1293 if(lastConvertedParentChar == stringSeparator)
1295 convertedParent = convertedParent.substr(0, convertedParent.size() - 1);
1298 if(firstConvertedChildChar == stringSeparator)
1300 convertedChild = convertedChild.substr(1);
1303 return std::string(convertedParent + vpIoTools::separator + convertedChild);
1325 return path.size() > 0 && (path.substr(0, 1) ==
"/" || path.substr(0, 1) ==
"\\");
1385 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
1386 return std::pair<std::string, std::string>(
"", pathname);
1388 const std::string sep =
"\\";
1389 const std::string sepsep =
"\\\\";
1390 const std::string altsep =
"/";
1392 if(pathname.size() > 1) {
1393 std::string normPathname = pathname;
1394 std::replace(normPathname.begin(), normPathname.end(), *altsep.c_str(), *sep.c_str());
1396 if(normPathname.substr(0, 2) == sepsep && normPathname.substr(2, 1) != sep) {
1401 size_t index = normPathname.find(sep, 2);
1402 if(index == std::string::npos) {
1403 return std::pair<std::string, std::string>(
"", pathname);
1406 size_t index2 = normPathname.find(sep, index + 1);
1409 if(index2 == index + 1) {
1410 return std::pair<std::string, std::string>(
"", pathname);
1413 if(index2 == std::string::npos) {
1414 index2 = pathname.size();
1417 return std::pair<std::string, std::string>(pathname.substr(0, index2), pathname.substr(index2));
1420 if(normPathname[1] ==
':') {
1421 return std::pair<std::string, std::string>(pathname.substr(0, 2), pathname.substr(2));
1425 return std::pair<std::string, std::string>(
"", pathname);
1479 size_t startIndex = 0;
1481 std::string chainToSplit = chain;
1482 std::vector<std::string> subChain;
1483 size_t sepIndex = chainToSplit.find(sep);
1485 while(sepIndex != std::string::npos) {
1487 subChain.push_back( chainToSplit.substr(startIndex, sepIndex) );
1488 chainToSplit = chainToSplit.substr(sepIndex+1, chain.size()-1);
1490 sepIndex = chainToSplit.find(sep);
1492 subChain.push_back(chainToSplit);
void resize(const unsigned int nrows, const unsigned int ncols, const bool flagNullify=true)
Class to define colors available for display functionnalities.
Error that can be emited by the vpIoTools class and its derivates.
unsigned int getCols() const
Return the number of columns of the 2D array.
static Type minimum(const Type &a, const Type &b)
unsigned int getRows() const
Return the number of rows of the 2D array.
static vpColor getColor(const unsigned int &i)