filename.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "filename.h"
  4. #include "sysinfo.h"
  5. namespace embree
  6. {
  7. #ifdef __WIN32__
  8. const char path_sep = '\\';
  9. #else
  10. const char path_sep = '/';
  11. #endif
  12. /*! create an empty filename */
  13. FileName::FileName () {}
  14. /*! create a valid filename from a string */
  15. FileName::FileName (const char* in) {
  16. filename = in;
  17. for (size_t i=0; i<filename.size(); i++)
  18. if (filename[i] == '\\' || filename[i] == '/')
  19. filename[i] = path_sep;
  20. while (!filename.empty() && filename[filename.size()-1] == path_sep)
  21. filename.resize(filename.size()-1);
  22. }
  23. /*! create a valid filename from a string */
  24. FileName::FileName (const std::string& in) {
  25. filename = in;
  26. for (size_t i=0; i<filename.size(); i++)
  27. if (filename[i] == '\\' || filename[i] == '/')
  28. filename[i] = path_sep;
  29. while (!filename.empty() && filename[filename.size()-1] == path_sep)
  30. filename.resize(filename.size()-1);
  31. }
  32. /*! returns path to home folder */
  33. FileName FileName::homeFolder()
  34. {
  35. #ifdef __WIN32__
  36. const char* home = getenv("UserProfile");
  37. #else
  38. const char* home = getenv("HOME");
  39. #endif
  40. if (home) return home;
  41. return "";
  42. }
  43. /*! returns path to executable */
  44. FileName FileName::executableFolder() {
  45. return FileName(getExecutableFileName()).path();
  46. }
  47. /*! returns the path */
  48. FileName FileName::path() const {
  49. size_t pos = filename.find_last_of(path_sep);
  50. if (pos == std::string::npos) return FileName();
  51. return filename.substr(0,pos);
  52. }
  53. /*! returns the basename */
  54. std::string FileName::base() const {
  55. size_t pos = filename.find_last_of(path_sep);
  56. if (pos == std::string::npos) return filename;
  57. return filename.substr(pos+1);
  58. }
  59. /*! returns the extension */
  60. std::string FileName::ext() const {
  61. size_t pos = filename.find_last_of('.');
  62. if (pos == std::string::npos) return "";
  63. return filename.substr(pos+1);
  64. }
  65. /*! returns the extension */
  66. FileName FileName::dropExt() const {
  67. size_t pos = filename.find_last_of('.');
  68. if (pos == std::string::npos) return filename;
  69. return filename.substr(0,pos);
  70. }
  71. /*! returns the basename without extension */
  72. std::string FileName::name() const {
  73. size_t start = filename.find_last_of(path_sep);
  74. if (start == std::string::npos) start = 0; else start++;
  75. size_t end = filename.find_last_of('.');
  76. if (end == std::string::npos || end < start) end = filename.size();
  77. return filename.substr(start, end - start);
  78. }
  79. /*! replaces the extension */
  80. FileName FileName::setExt(const std::string& ext) const {
  81. size_t start = filename.find_last_of(path_sep);
  82. if (start == std::string::npos) start = 0; else start++;
  83. size_t end = filename.find_last_of('.');
  84. if (end == std::string::npos || end < start) return FileName(filename+ext);
  85. return FileName(filename.substr(0,end)+ext);
  86. }
  87. /*! adds the extension */
  88. FileName FileName::addExt(const std::string& ext) const {
  89. return FileName(filename+ext);
  90. }
  91. /*! concatenates two filenames to this/other */
  92. FileName FileName::operator +( const FileName& other ) const {
  93. if (filename == "") return FileName(other);
  94. else return FileName(filename + path_sep + other.filename);
  95. }
  96. /*! concatenates two filenames to this/other */
  97. FileName FileName::operator +( const std::string& other ) const {
  98. return operator+(FileName(other));
  99. }
  100. /*! removes the base from a filename (if possible) */
  101. FileName FileName::operator -( const FileName& base ) const {
  102. size_t pos = filename.find_first_of(base);
  103. if (pos == std::string::npos) return *this;
  104. return FileName(filename.substr(pos+1));
  105. }
  106. /*! == operator */
  107. bool operator== (const FileName& a, const FileName& b) {
  108. return a.filename == b.filename;
  109. }
  110. /*! != operator */
  111. bool operator!= (const FileName& a, const FileName& b) {
  112. return a.filename != b.filename;
  113. }
  114. /*! output operator */
  115. std::ostream& operator<<(std::ostream& cout, const FileName& filename) {
  116. return cout << filename.filename;
  117. }
  118. }