filename.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "filename.h"
  17. #include "sysinfo.h"
  18. namespace embree
  19. {
  20. #ifdef __WIN32__
  21. const char path_sep = '\\';
  22. #else
  23. const char path_sep = '/';
  24. #endif
  25. /*! create an empty filename */
  26. FileName::FileName () {}
  27. /*! create a valid filename from a string */
  28. FileName::FileName (const char* in) {
  29. filename = in;
  30. for (size_t i=0; i<filename.size(); i++)
  31. if (filename[i] == '\\' || filename[i] == '/')
  32. filename[i] = path_sep;
  33. while (!filename.empty() && filename[filename.size()-1] == path_sep)
  34. filename.resize(filename.size()-1);
  35. }
  36. /*! create a valid filename from a string */
  37. FileName::FileName (const std::string& in) {
  38. filename = in;
  39. for (size_t i=0; i<filename.size(); i++)
  40. if (filename[i] == '\\' || filename[i] == '/')
  41. filename[i] = path_sep;
  42. while (!filename.empty() && filename[filename.size()-1] == path_sep)
  43. filename.resize(filename.size()-1);
  44. }
  45. /*! returns path to home folder */
  46. FileName FileName::homeFolder()
  47. {
  48. #ifdef __WIN32__
  49. const char* home = getenv("UserProfile");
  50. #else
  51. const char* home = getenv("HOME");
  52. #endif
  53. if (home) return home;
  54. return "";
  55. }
  56. /*! returns path to executable */
  57. FileName FileName::executableFolder() {
  58. return FileName(getExecutableFileName()).path();
  59. }
  60. /*! returns the path */
  61. FileName FileName::path() const {
  62. size_t pos = filename.find_last_of(path_sep);
  63. if (pos == std::string::npos) return FileName();
  64. return filename.substr(0,pos);
  65. }
  66. /*! returns the basename */
  67. std::string FileName::base() const {
  68. size_t pos = filename.find_last_of(path_sep);
  69. if (pos == std::string::npos) return filename;
  70. return filename.substr(pos+1);
  71. }
  72. /*! returns the extension */
  73. std::string FileName::ext() const {
  74. size_t pos = filename.find_last_of('.');
  75. if (pos == std::string::npos) return "";
  76. return filename.substr(pos+1);
  77. }
  78. /*! returns the extension */
  79. FileName FileName::dropExt() const {
  80. size_t pos = filename.find_last_of('.');
  81. if (pos == std::string::npos) return filename;
  82. return filename.substr(0,pos);
  83. }
  84. /*! returns the basename without extension */
  85. std::string FileName::name() const {
  86. size_t start = filename.find_last_of(path_sep);
  87. if (start == std::string::npos) start = 0; else start++;
  88. size_t end = filename.find_last_of('.');
  89. if (end == std::string::npos || end < start) end = filename.size();
  90. return filename.substr(start, end - start);
  91. }
  92. /*! replaces the extension */
  93. FileName FileName::setExt(const std::string& ext) const {
  94. size_t start = filename.find_last_of(path_sep);
  95. if (start == std::string::npos) start = 0; else start++;
  96. size_t end = filename.find_last_of('.');
  97. if (end == std::string::npos || end < start) return FileName(filename+ext);
  98. return FileName(filename.substr(0,end)+ext);
  99. }
  100. /*! adds the extension */
  101. FileName FileName::addExt(const std::string& ext) const {
  102. return FileName(filename+ext);
  103. }
  104. /*! concatenates two filenames to this/other */
  105. FileName FileName::operator +( const FileName& other ) const {
  106. if (filename == "") return FileName(other);
  107. else return FileName(filename + path_sep + other.filename);
  108. }
  109. /*! concatenates two filenames to this/other */
  110. FileName FileName::operator +( const std::string& other ) const {
  111. return operator+(FileName(other));
  112. }
  113. /*! removes the base from a filename (if possible) */
  114. FileName FileName::operator -( const FileName& base ) const {
  115. size_t pos = filename.find_first_of(base);
  116. if (pos == std::string::npos) return *this;
  117. return FileName(filename.substr(pos+1));
  118. }
  119. /*! == operator */
  120. bool operator== (const FileName& a, const FileName& b) {
  121. return a.filename == b.filename;
  122. }
  123. /*! != operator */
  124. bool operator!= (const FileName& a, const FileName& b) {
  125. return a.filename != b.filename;
  126. }
  127. /*! output operator */
  128. std::ostream& operator<<(std::ostream& cout, const FileName& filename) {
  129. return cout << filename.filename;
  130. }
  131. }