FileName.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "core/basics/FileName.h"
  2. #include <sys/stat.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <sys/param.h> //for MAXPATHLEN - the maximal path length
  6. using namespace std;
  7. namespace NICE {
  8. FileName::FileName() {
  9. }
  10. FileName::FileName(const std::string& _fileName)
  11. : fileName(_fileName) {
  12. }
  13. FileName::FileName(const char* _fileName)
  14. : fileName(_fileName) {
  15. }
  16. FileName::FileName(const FileName& _fileName)
  17. : fileName(_fileName.str()) {
  18. }
  19. FileName& FileName::operator=(const FileName& _fileName) {
  20. fileName = _fileName.str();
  21. return *this;
  22. }
  23. FileName& FileName::operator=(const std::string& _fileName) {
  24. fileName = _fileName;
  25. return *this;
  26. }
  27. FileName& FileName::operator=(const char* _fileName) {
  28. fileName = _fileName;
  29. return *this;
  30. }
  31. FileName::~FileName() {
  32. }
  33. const string& FileName::str() const {
  34. return fileName;
  35. }
  36. FileName::operator const std::string&() const {
  37. return str();
  38. }
  39. FileName::operator const char*() const {
  40. return str().c_str();
  41. }
  42. FileName FileName::extractPath() const {
  43. int pos = (int)fileName.find_last_of('/');
  44. if (pos < 0) {
  45. return FileName("");
  46. } else {
  47. return FileName(fileName.substr(0, pos + 1));
  48. }
  49. }
  50. FileName FileName::extractFileName() const {
  51. int pos = (int)fileName.find_last_of('/');
  52. if (pos < 0) {
  53. return FileName(fileName);
  54. } else {
  55. return FileName(fileName.substr(pos + 1));
  56. }
  57. }
  58. FileName FileName::extractExtension() const {
  59. int pos = (int)fileName.find_last_of('.');
  60. if (pos < 0) {
  61. return FileName("");
  62. } else {
  63. return FileName(fileName.substr(pos));
  64. }
  65. }
  66. void FileName::removeExtension() {
  67. int pos = (int)fileName.find_last_of('.');
  68. if (pos > 0) {
  69. fileName.erase(pos);
  70. }
  71. }
  72. void FileName::addSlash() {
  73. if (fileName.length() == 0) {
  74. return;
  75. }
  76. if (fileName[fileName.length() - 1] != '/') {
  77. fileName += "/";
  78. }
  79. }
  80. void FileName::removeSlash() {
  81. if (fileName[fileName.length() - 1] == '/') {
  82. fileName.erase(fileName.end() - 1);
  83. }
  84. }
  85. void FileName::setExtension(const std::string& extension) {
  86. if (extractExtension().str() != extension) {
  87. fileName += extension;
  88. }
  89. }
  90. bool FileName::fileExists() const {
  91. struct stat my_stat;
  92. return (stat(fileName.c_str(), &my_stat) == 0);
  93. }
  94. bool FileName::isDirectory() const {
  95. struct stat my_stat;
  96. if (stat(fileName.c_str(), &my_stat) != 0) {
  97. return false;
  98. } else {
  99. return ((my_stat.st_mode & S_IFDIR) != 0);
  100. }
  101. }
  102. void FileName::createDirectory() const {
  103. if (fileName.length() == 0) {
  104. return;
  105. }
  106. string command = string("mkdir -p ") + fileName;
  107. system(command.c_str());
  108. }
  109. void FileName::deleteFile() const {
  110. remove(fileName.c_str());
  111. }
  112. bool FileName::isRelative() const {
  113. if(fileName.empty())
  114. return false;
  115. return fileName.substr(0,1) != "/";
  116. }
  117. bool FileName::convertToRealPath()
  118. {
  119. char actualpath [MAXPATHLEN];
  120. char *pRet = realpath( fileName.c_str(), actualpath);
  121. //TODO: how to treat the return value???
  122. // if( pRet == NULL)
  123. // return false;
  124. this->fileName = string( actualpath);
  125. return true;
  126. }
  127. } // namespace