FileName.cpp 2.6 KB

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