Globals.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <unistd.h>
  9. using namespace OBJREC;
  10. using namespace NICE;
  11. using namespace std;
  12. std::string Globals::currentImgFN = "";
  13. void Globals::setCurrentImgFN ( const std::string & imgfn )
  14. {
  15. currentImgFN = imgfn;
  16. }
  17. std::string Globals::getCurrentImgFN ( )
  18. {
  19. return currentImgFN;
  20. }
  21. std::string Globals::getCacheFilename ( const std::string & root, int cache_mode )
  22. {
  23. std::string filename = Globals::getCurrentImgFN();
  24. if ( filename.length() == 0 ) {
  25. fthrow(Exception, "Globals::getCacheFilename: current image filename was not set, please use Globals::setCurrentImgFN()");
  26. }
  27. vector<string> mylistdir;
  28. StringTools::split ( filename, FILESEP, mylistdir );
  29. int dirpart = 2;
  30. if ( cache_mode == SORT_CATEGORIES_SECONDPART )
  31. {
  32. cache_mode = SORT_CATEGORIES;
  33. dirpart = 3;
  34. }
  35. if ( cache_mode == SORT_CATEGORIES )
  36. {
  37. if ( mylistdir.size() < 2 )
  38. {
  39. cerr << "Globals::getCacheFilename: unable to parse filename " << filename << endl;
  40. exit(-1);
  41. }
  42. std::string name = mylistdir[mylistdir.size()-1];
  43. vector<string> mylist;
  44. StringTools::split ( name, '.', mylist );
  45. name = "";
  46. for ( uint k = 0 ; k < mylist.size()-1 ; k++ )
  47. if ( k == 0 )
  48. name = name + mylist[k];
  49. else
  50. name = name + "." + mylist[k];
  51. std::string dir = root + FILESEPSTRING + mylistdir[mylistdir.size()-dirpart];
  52. struct stat info;
  53. if ( stat (dir.c_str(), &info ) < 0 ) {
  54. if ( mkdir(dir.c_str(), 0755) < 0 )
  55. {
  56. cerr << "Globals::getCacheFilename: unable to create directory: " << dir << endl;
  57. exit(-1);
  58. }
  59. }
  60. std::string id = dir + FILESEPSTRING + name;
  61. return id;
  62. } else if ( cache_mode == SORT_NONE ) {
  63. vector<string> mylist;
  64. std::string name = mylistdir[ mylistdir.size() - 1 ];
  65. StringTools::split ( name, '.', mylist );
  66. if ( mylist.size() != 2 )
  67. {
  68. cerr << "Globals::getCacheFilename: unable to parse filename " << name << endl;
  69. exit(-1);
  70. }
  71. name = mylist[0];
  72. std::string id = root + FILESEPSTRING + name;
  73. return id;
  74. } else {
  75. fprintf (stderr, "Globals::getCacheFilename: unknown mode\n");
  76. }
  77. return "";
  78. }
  79. int Globals::getCacheMode ( const std::string & desc )
  80. {
  81. if ( desc == "cat" ) {
  82. return SORT_CATEGORIES;
  83. } else if ( desc == "cat2" ) {
  84. return SORT_CATEGORIES_SECONDPART;
  85. } else if ( desc == "none" ) {
  86. return SORT_NONE;
  87. } else {
  88. fprintf (stderr, "Globals::getCacheMode: mode unknown >%s<\n", desc.c_str() );
  89. exit(-1);
  90. }
  91. }