Globals.cpp 2.6 KB

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