ViSP  2.6.2
vpIoTools.cpp
1 /****************************************************************************
2  *
3  * $Id: vpIoTools.cpp 3723 2012-05-10 13:28:16Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Directory management.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
46 #include <visp/vpIoTools.h>
47 #include <visp/vpDebug.h>
48 #include <visp/vpIoException.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <fstream>
52 #include <string.h>
53 #include <sys/stat.h>
54 #include <sys/types.h>
55 #include <fcntl.h>
56 #include <limits>
57 #include <cmath>
58 #if defined UNIX
59 # include <unistd.h>
60 #elif defined WIN32
61 # include <windows.h>
62 # include <direct.h>
63 #endif
64 
65 std::string vpIoTools::baseName = "";
66 std::string vpIoTools::baseDir = "";
67 std::string vpIoTools::configFile = "";
68 std::vector<std::string> vpIoTools::configVars = std::vector<std::string>();
69 std::vector<std::string> vpIoTools::configValues = std::vector<std::string>();
70 
71 
89 void
90 vpIoTools::getUserName(std::string &username)
91 {
92 #if defined UNIX
93  // Get the user name.
94  char *_username = NULL;
95  _username = ::getenv("LOGNAME");
96  if (_username == NULL) {
97  vpERROR_TRACE( "Cannot get the username. Check your LOGNAME environment variable" );
99  "Cannot get the username")) ;
100  }
101  username = _username;
102 #elif defined WIN32
103  unsigned int info_buffer_size = 1024;
104  TCHAR *infoBuf = new TCHAR [info_buffer_size];
105  DWORD bufCharCount = (DWORD) info_buffer_size;
106  // Get the user name.
107  if( ! GetUserName( infoBuf, &bufCharCount ) ) {
108  delete [] infoBuf;
109  vpERROR_TRACE( "Cannot get the username" );
111  "Cannot get the username")) ;
112 
113  }
114  username = infoBuf;
115  delete [] infoBuf;
116 #endif
117 }
135 std::string
137 {
138  std::string username;
139 #if defined UNIX
140  // Get the user name.
141  char *_username = NULL;
142  _username = ::getenv("LOGNAME");
143  if (_username == NULL) {
144  vpERROR_TRACE( "Cannot get the username. Check your LOGNAME environment variable" );
146  "Cannot get the username")) ;
147  }
148  username = _username;
149 #elif defined WIN32
150  unsigned int info_buffer_size = 1024;
151  TCHAR *infoBuf = new TCHAR [info_buffer_size];
152  DWORD bufCharCount = (DWORD) info_buffer_size;
153  // Get the user name.
154  if( ! GetUserName( infoBuf, &bufCharCount ) ) {
155  delete [] infoBuf;
156  vpERROR_TRACE( "Cannot get the username" );
158  "Cannot get the username")) ;
159 
160  }
161  username = infoBuf;
162  delete [] infoBuf;
163 #endif
164  return username;
165 }
166 
203 std::string
204 vpIoTools::getenv(const char *
205 #if defined UNIX
206  env
207 #endif
208  )
209 {
210  std::string value;
211 #if defined UNIX
212  // Get the environment variable value.
213  char *_value = NULL;
214  _value = ::getenv(env);
215  if (_value == NULL) {
216  vpERROR_TRACE( "Cannot get the environment variable value" );
218  "Cannot get the environment variable value")) ;
219  }
220  value = _value;
221 
222  return value;
223 #elif defined WIN32
224 
225  vpERROR_TRACE( "Not implemented!" );
227  "Not implemented!")) ;
228 #endif
229 }
230 
268 std::string
269 vpIoTools::getenv(std::string &env)
270 {
271  return (vpIoTools::getenv(env.c_str()));
272 }
273 
288 bool
289 vpIoTools::checkDirectory(const char *dirname )
290 {
291 #if defined UNIX
292  struct stat stbuf;
293 #elif defined WIN32
294  struct _stat stbuf;
295 #endif
296 
297  if ( dirname == NULL || dirname[0] == '\0' ) {
298  return false;
299  }
300 
301  std::string _dirname = path(dirname);
302 
303 #if defined UNIX
304  if ( stat( _dirname.c_str(), &stbuf ) != 0 )
305 #elif defined WIN32
306  if ( _stat( _dirname.c_str(), &stbuf ) != 0 )
307 #endif
308  {
309  return false;
310  }
311  if ( (stbuf.st_mode & S_IFDIR) == 0 ) {
312  return false;
313  }
314 #if defined UNIX
315  if ( (stbuf.st_mode & S_IWUSR) == 0 )
316 #elif defined WIN32
317  if ( (stbuf.st_mode & S_IWRITE) == 0 )
318 #endif
319  {
320  return false;
321  }
322  return true;
323 }
324 
338 bool
339 vpIoTools::checkDirectory(const std::string &dirname )
340 {
341  return vpIoTools::checkDirectory(dirname.c_str());
342 }
357 void
358 vpIoTools::makeDirectory(const char *dirname )
359 {
360 #if defined UNIX
361  struct stat stbuf;
362 #elif defined WIN32
363  struct _stat stbuf;
364 #endif
365 
366  if ( dirname == NULL || dirname[0] == '\0' ) {
367  vpERROR_TRACE( "invalid directory name\n");
369  "invalid directory name")) ;
370  }
371 
372  std::string _dirname = path(dirname);
373 
374 #if defined UNIX
375  if ( stat( _dirname.c_str(), &stbuf ) != 0 )
376 #elif defined WIN32
377  if ( _stat( _dirname.c_str(), &stbuf ) != 0 )
378 #endif
379  {
380 #if defined UNIX
381  if ( mkdir( _dirname.c_str(), (mode_t)0755 ) != 0 )
382 #elif defined WIN32
383  if ( _mkdir( _dirname.c_str()) != 0 )
384 #endif
385  {
386  vpERROR_TRACE("unable to create directory '%s'\n", dirname );
388  "unable to create directory")) ;
389  }
390  vpDEBUG_TRACE(2,"has created directory '%s'\n", dirname );
391  }
392 
393  if ( checkDirectory( dirname ) == false) {
394  vpERROR_TRACE("unable to create directory '%s'\n", dirname );
396  "unable to create directory")) ;
397  }
398 }
399 
412 void
413 vpIoTools::makeDirectory(const std::string &dirname )
414 {
415  try {
416  vpIoTools::makeDirectory(dirname.c_str());
417  }
418  catch (...) {
419  vpERROR_TRACE("unable to create directory '%s'\n",dirname.c_str());
421  "unable to create directory")) ;
422  }
423 }
424 
438 bool
439 vpIoTools::checkFilename(const char *filename)
440 {
441 #if defined UNIX
442  struct stat stbuf;
443 #elif defined WIN32
444  struct _stat stbuf;
445 #endif
446 
447  if ( filename == NULL || filename[0] == '\0' ) {
448  return false;
449  }
450 
451  std::string _filename = path(filename);
452 #if defined UNIX
453  if ( stat( _filename.c_str(), &stbuf ) != 0 )
454 #elif defined WIN32
455  if ( _stat( _filename.c_str(), &stbuf ) != 0 )
456 #endif
457  {
458  return false;
459  }
460  if ( (stbuf.st_mode & S_IFREG) == 0 ) {
461  return false;
462  }
463 #if defined UNIX
464  if ( (stbuf.st_mode & S_IRUSR) == 0 )
465 #elif defined WIN32
466  if ( (stbuf.st_mode & S_IREAD) == 0 )
467 #endif
468  {
469  return false;
470  }
471  return true;
472 }
473 
486 bool
487 vpIoTools::checkFilename(const std::string &filename)
488 {
489  return vpIoTools::checkFilename(filename.c_str());
490 }
491 
504 bool
505 vpIoTools::copy(const char *src, const char *dst)
506 {
507  char cmd[FILENAME_MAX];
508  int ret;
509  // Check if we have to consider a file or a directory
510  if ( vpIoTools::checkFilename(src) ) {
511  //std::cout << "copy file: " << src << " in " << dst << std::endl;
512 #ifdef UNIX
513  sprintf(cmd, "cp -p %s %s", src, dst);
514 #elif WIN32
515  std::string src_ = vpIoTools::path(src);
516  std::string dst_ = vpIoTools::path(dst);
517  sprintf(cmd, "copy %s %s", src_.c_str(), dst_.c_str());
518 #endif
519  ret = system( cmd );
520  //std::cout << cmd << " return value: " << ret << std::endl;
521  return true;
522  }
523  else if ( vpIoTools::checkDirectory(src) ) {
524  //std::cout << "copy directory: " << src << " in " << dst << std::endl;
525 #ifdef UNIX
526  sprintf(cmd, "cp -p -r %s %s", src, dst);
527 #elif WIN32
528  std::string src_ = vpIoTools::path(src);
529  std::string dst_ = vpIoTools::path(dst);
530  sprintf(cmd, "copy %s %s", src_.c_str(), dst_.c_str());
531 #endif
532  ret = system( cmd );
533  if(ret) {}; // to avoid a warning
534  //std::cout << cmd << " return value: " << ret << std::endl;
535  return true;
536  }
537  else {
538  std::cout << "Cannot copy: " << src << " in " << dst << std::endl;
539  return false;
540  }
541 }
554 bool
555 vpIoTools::copy(const std::string &src, const std::string &dst)
556 {
557  return vpIoTools::copy(src.c_str(), dst.c_str());
558 }
559 
570 bool
571 vpIoTools::remove(const char *file_or_dir)
572 {
573  // Check if we have to consider a file or a directory
574  if ( vpIoTools::checkFilename(file_or_dir) ) {
575  //std::cout << "remove file: " << file_or_dir << std::endl;
576  if (::remove(file_or_dir) != 0)
577  return false;
578  else
579  return true;
580  }
581  else if ( vpIoTools::checkDirectory(file_or_dir) ) {
582  //std::cout << "remove directory: " << file_or_dir << std::endl;
583  char cmd[FILENAME_MAX];
584 #ifdef UNIX
585  sprintf(cmd, "rm -rf %s", file_or_dir);
586 #elif WIN32
587  std::string file_or_dir_ = vpIoTools::path(file_or_dir);
588  sprintf(cmd, "rmdir /S /Q %s", file_or_dir_.c_str());
589 #endif
590  int ret = system( cmd );
591  if(ret) {}; // to avoid a warning
592  //std::cout << cmd << " return value: " << ret << std::endl;
593  return true;
594  }
595  else {
596  std::cout << "Cannot remove: " << file_or_dir << std::endl;
597  return false;
598  }
599 }
611 bool
612 vpIoTools::remove(const std::string &file_or_dir)
613 {
614  return vpIoTools::remove(file_or_dir.c_str());
615 }
616 
628 bool
629 vpIoTools::rename(const char *oldfilename, const char *newfilename)
630 {
631  if (::rename(oldfilename, newfilename) != 0)
632  return false;
633  else
634  return true;
635 }
636 
648 bool
649 vpIoTools::rename(const std::string &oldfilename, const std::string &newfilename)
650 {
651  return vpIoTools::rename(oldfilename.c_str(), newfilename.c_str());
652 }
653 
654 
655 
668 std::string
669 vpIoTools::path(const char *pathname)
670 {
671  std::string path(pathname);
672 
673 #ifdef WIN32
674  for(unsigned int i=0 ; i<path.length() ; i++)
675  if( path[i] == '/') path[i] = '\\';
676 #else
677  for(unsigned int i=0 ; i<path.length() ; i++)
678  if( path[i] == '\\') path[i] = '/';
679 #endif
680 
681  return path;
682 }
683 
696 std::string
697 vpIoTools::path(const std::string &pathname)
698 {
699  return path(pathname.c_str());
700 }
701 
702 
708 void vpIoTools::loadConfigFile(const std::string &confFile)
709 {
710  configFile = confFile;
711  configVars.clear();configValues.clear();
712  std::ifstream confContent(confFile.c_str(), std::ios::in);
713 
714  if(confContent)
715  {
716  std::string line,var,val;
717  long unsigned int k;
718  int c;
719  std::string stop[3] = {" ", "\t", "#"};
720  while(std::getline(confContent, line))
721  {
722  if((line.find("#",0,1) != 0) && (line.size() > 2))
723  {
724  try
725  {
726  // name of the variable
727  k = line.find(" ");
728  var = line.substr(0,k);
729  // look for the end of the actual value
730  c = 200;
731  for(unsigned i=0;i<3;++i)
732  c = vpMath::minimum(c,(int)line.find(stop[i],k+1));
733  if(c==-1)
734  c = (int)line.size();
735  long unsigned int c_ = (long unsigned int) c;
736  val = line.substr(k+1,c_-k-1);
737  configVars.push_back(var);
738  configValues.push_back(val);
739  }
740  catch(...){}
741  }
742  }
743  confContent.close();
744  }
745 }
746 
755 bool vpIoTools::readConfigVar(const std::string &var, double &value)
756 {
757  bool found = false;
758  for(unsigned int k=0;k<configVars.size() && found==false;++k)
759  {
760  if(configVars[k] == var)
761  {
762  value = atof(configValues[k].c_str());
763  found = true;
764  }
765  }
766  if(found == false)
767  std::cout << var << " not found in config file" << std::endl;
768  return found;
769 }
770 
779 bool vpIoTools::readConfigVar(const std::string &var, int &value)
780 {
781  bool found = false;
782  for(unsigned int k=0;k<configVars.size() && found==false;++k)
783  {
784  if(configVars[k] == var)
785  {
786  value = atoi(configValues[k].c_str());
787  found = true;
788  }
789  }
790  if(found == false)
791  std::cout << var << " not found in config file" << std::endl;
792  return found;
793 }
794 
803 bool vpIoTools::readConfigVar(const std::string &var, unsigned int &value)
804 {
805  int v = 0;
806  bool found = readConfigVar(var,v);
807  value = (unsigned int) v;
808  return found;
809 }
810 
819 bool vpIoTools::readConfigVar(const std::string &var, bool &value)
820 {
821  int v = 0;
822  bool found = readConfigVar(var,v);
823  value = (v!=0);
824  return found;
825 }
826 
835 bool vpIoTools::readConfigVar(const std::string &var, vpColor &value)
836 {
837  unsigned int v = 0;
838  bool found = readConfigVar(var,v);
839  value = vpColor::getColor(v);
840  return found;
841 }
842 
851 bool vpIoTools::readConfigVar(const std::string &var, std::string &value)
852 {
853  bool found = false;
854  for(unsigned int k=0;k<configVars.size() && found==false;++k)
855  {
856  if(configVars[k] == var)
857  {
858  value = configValues[k];
859  found = true;
860  }
861  }
862  if(found == false)
863  std::cout << var << " not found in config file" << std::endl;
864  return found;
865 }
866 
879 bool vpIoTools::readConfigVar(const std::string &var, vpMatrix &value, const unsigned int &nCols, const unsigned int &nRows)
880 {
881  bool found = false;
882  for(unsigned int k=0;k<configVars.size() && found==false;++k)
883  {
884  if(configVars[k] == var)
885  {
886  found = true;
887  // resize or not
888  if(nCols != 0 && nRows != 0)
889  value.resize(nRows, nCols);
890  long unsigned int ind=0,ind2;
891  for(unsigned int i=0;i<value.getRows();++i)
892  for(unsigned int j=0;j<value.getCols();++j)
893  {
894  ind2 = configValues[k].find(",",ind);
895  value[i][j] = atof(configValues[k].substr(ind,ind2-ind).c_str());
896  ind = ind2+1;
897  }
898  }
899  }
900  if(found == false)
901  std::cout << var << " not found in config file" << std::endl;
902  return found;
903 }
904 
905 // construct experiment filename & path
906 
914 void vpIoTools::addNameElement(const std::string &strTrue, const bool &cond, const std::string &strFalse)
915 {
916  if(cond)
917  baseName += "_" + strTrue;
918  else if(strFalse != "")
919  baseName += "_" + strFalse;
920 }
921 
929 void vpIoTools::addNameElement(const std::string &strTrue, const double &val)
930 {
931  //if(val != 0.)
932  if(std::fabs(val) < std::numeric_limits<double>::epsilon())
933  {
934  char valC[256];
935  sprintf(valC, "%.3f", val);
936  std::string valS(valC);
937  baseName += "_" + strTrue + valS;
938  }
939 }
940 
949 void vpIoTools::createBaseNamePath(const bool &empty)
950 {
951  if(vpIoTools::checkDirectory(baseDir + baseName) == false) {
953  std::cout << "creating directory " + baseDir + baseName << std::endl;
954  }
955  else {
956  if(empty) {
957  std::cout << "emptying directory " + baseDir + baseName << std::endl;
959  }
960  }
961 }
962 
969 void vpIoTools::saveConfigFile(const bool &actuallySave)
970 {
971  if(actuallySave) {
972  std::string dest = baseDir + "/" + baseName + "_config.txt";
973  // file copy
975  }
976 }
#define vpDEBUG_TRACE
Definition: vpDebug.h:454
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
static bool remove(const char *filename)
Definition: vpIoTools.cpp:571
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:174
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:289
#define vpERROR_TRACE
Definition: vpDebug.h:379
Class to define colors available for display functionnalities.
Definition: vpColor.h:123
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:204
static bool rename(const char *oldfilename, const char *newfilename)
Definition: vpIoTools.cpp:629
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:669
Error that can be emited by the vpIoTools class and its derivates.
Definition: vpIoException.h:72
static bool readConfigVar(const std::string &var, double &value)
Definition: vpIoTools.cpp:755
static void createBaseNamePath(const bool &empty=false)
Definition: vpIoTools.cpp:949
static std::string baseDir
Definition: vpIoTools.h:228
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
static bool checkFilename(const char *filename)
Definition: vpIoTools.cpp:439
static bool copy(const char *src, const char *dst)
Definition: vpIoTools.cpp:505
static std::string getUserName()
Definition: vpIoTools.cpp:136
static Type minimum(const Type &a, const Type &b)
Definition: vpMath.h:148
static void saveConfigFile(const bool &actuallySave=true)
Definition: vpIoTools.cpp:969
static std::vector< std::string > configVars
Definition: vpIoTools.h:230
static void loadConfigFile(const std::string &confFile)
Definition: vpIoTools.cpp:708
static std::string baseName
Definition: vpIoTools.h:227
static std::string configFile
Definition: vpIoTools.h:229
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:159
static vpColor getColor(const unsigned int &i)
Definition: vpColor.h:232
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:157
static std::vector< std::string > configValues
Definition: vpIoTools.h:231
static void addNameElement(const std::string &strTrue, const bool &cond=true, const std::string &strFalse="")
Definition: vpIoTools.cpp:914