Globals.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <iostream>
  2. #include "core/basics/StringTools.h"
  3. #include "vislearning/baselib/Globals.h"
  4. #include "core/basics/ossettings.h"
  5. #include "core/basics/Exception.h"
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #ifdef WIN32
  9. #ifdef NICE_USELIB_BOOST
  10. #include "boost/filesystem.hpp"
  11. #endif
  12. #else
  13. #include <unistd.h>
  14. #endif
  15. using namespace OBJREC;
  16. using namespace NICE;
  17. using namespace std;
  18. std::string Globals::currentImgFN = "";
  19. void Globals::setCurrentImgFN ( const std::string & imgfn )
  20. {
  21. currentImgFN = imgfn;
  22. }
  23. std::string Globals::getCurrentImgFN ( )
  24. {
  25. return currentImgFN;
  26. }
  27. std::string Globals::getCacheFilename ( const std::string & root, int cache_mode )
  28. {
  29. std::string filename = Globals::getCurrentImgFN();
  30. if ( filename.length() == 0 ) {
  31. fthrow(Exception, "Globals::getCacheFilename: current image filename was not set, please use Globals::setCurrentImgFN()");
  32. }
  33. vector<string> mylistdir;
  34. StringTools::split ( filename, FILESEP, mylistdir );
  35. int dirpart = 2;
  36. if ( cache_mode == SORT_CATEGORIES_SECONDPART )
  37. {
  38. cache_mode = SORT_CATEGORIES;
  39. dirpart = 3;
  40. }
  41. if ( cache_mode == SORT_CATEGORIES )
  42. {
  43. if ( mylistdir.size() < 2 )
  44. {
  45. cerr << "Globals::getCacheFilename: unable to parse filename " << filename << endl;
  46. exit(-1);
  47. }
  48. std::string name = mylistdir[mylistdir.size()-1];
  49. vector<string> mylist;
  50. StringTools::split ( name, '.', mylist );
  51. name = "";
  52. for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
  53. if ( k == 0 )
  54. name = name + mylist[k];
  55. else
  56. name = name + "." + mylist[k];
  57. std::string dir = root + FILESEPSTRING + mylistdir[mylistdir.size()-dirpart];
  58. struct stat info;
  59. if ( stat (dir.c_str(), &info ) < 0 ) {
  60. #ifdef WIN32
  61. #ifdef NICE_USELIB_BOOST
  62. boost::filesystem::path t_path(dir.c_str());
  63. if ( boost::filesystem::create_directory(t_path) < 0 )
  64. {
  65. cerr << "Globals::getCacheFilename: unable to create directory: " << dir << endl;
  66. exit(-1);
  67. }
  68. #else
  69. fthrow(Exception,"mkdir function not defined on system. try using boost lib and rebuild library");
  70. #endif
  71. #else
  72. if ( mkdir(dir.c_str(), 0755) < 0 )
  73. {
  74. cerr << "Globals::getCacheFilename: unable to create directory: " << dir << endl;
  75. exit(-1);
  76. }
  77. #endif
  78. }
  79. std::string id = dir + FILESEPSTRING + name;
  80. return id;
  81. } else if ( cache_mode == SORT_NONE ) {
  82. vector<string> mylist;
  83. std::string name = mylistdir[ mylistdir.size() - 1 ];
  84. StringTools::split ( name, '.', mylist );
  85. if ( mylist.size() != 2 )
  86. {
  87. cerr << "Globals::getCacheFilename: unable to parse filename " << name << endl;
  88. exit(-1);
  89. }
  90. name = mylist[0];
  91. std::string id = root + FILESEPSTRING + name;
  92. return id;
  93. } else {
  94. fprintf (stderr, "Globals::getCacheFilename: unknown mode\n");
  95. }
  96. return "";
  97. }
  98. int Globals::getCacheMode ( const std::string & desc )
  99. {
  100. if ( desc == "cat" ) {
  101. return SORT_CATEGORIES;
  102. } else if ( desc == "cat2" ) {
  103. return SORT_CATEGORIES_SECONDPART;
  104. } else if ( desc == "none" ) {
  105. return SORT_NONE;
  106. } else {
  107. fprintf (stderr, "Globals::getCacheMode: mode unknown >%s<\n", desc.c_str() );
  108. exit(-1);
  109. }
  110. }