FileName.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <iostream>
  5. #include "Exception.h"
  6. #include "FileName.h"
  7. using namespace std;
  8. void FileName::setPath(const std::string& n)
  9. {
  10. if (n.size() == 0)
  11. throw Exception("FileName::setPath", "empty pathname");
  12. absolute = n[0] == pathdel;
  13. path.clear();
  14. unsigned int i = 0;
  15. string thisPart;
  16. while (i < n.size())
  17. {
  18. if (n[i] != pathdel)
  19. thisPart += n[i];
  20. else
  21. {
  22. if (!thisPart.empty())
  23. path.push_back(thisPart);
  24. thisPart.clear();
  25. }
  26. ++i;
  27. }
  28. if (!thisPart.empty())
  29. path.push_back(thisPart);
  30. }
  31. FileName::FileName(const std::string& n)
  32. {
  33. if (n.size() == 0)
  34. throw Exception("FileName", "empty filename");
  35. setPath(n);
  36. // last path component is name
  37. if (path.size() > 0)
  38. {
  39. name = path.back();
  40. path.pop_back();
  41. size_t lastExtensionDelimiterPosition = name.rfind(".");
  42. if (lastExtensionDelimiterPosition == std::string::npos)
  43. extension = "";
  44. else
  45. {
  46. extension = name.substr(lastExtensionDelimiterPosition + 1);
  47. name.resize(lastExtensionDelimiterPosition);
  48. }
  49. }
  50. }
  51. void FileName::setName(const string& n)
  52. {
  53. for (unsigned int i = 0; i < n.size(); ++i)
  54. if (n[i] == pathdel)
  55. throw Exception("Filename", "path delimiter in name");
  56. name = n;
  57. }
  58. void FileName::setExtension(const string& n)
  59. {
  60. for (unsigned int i = 0; i < n.size(); ++i)
  61. {
  62. if (n[i] == extdel)
  63. throw Exception("Filename", "extension delimiter in extension");
  64. if (n[i] == pathdel)
  65. throw Exception("Filename", "path delimiter in extension");
  66. }
  67. extension = n;
  68. }
  69. FileName::FileName(const std::string& path,
  70. const std::string& name,
  71. const std::string& ext)
  72. {
  73. setPath(path);
  74. setName(name);
  75. setExtension(ext);
  76. }
  77. std::string FileName::getFileName() const
  78. {
  79. std::string res = getPath();
  80. if (res != "/")
  81. res += '/';
  82. res += name;
  83. if (!extension.empty())
  84. res += extdel + extension;
  85. return res;
  86. }
  87. std::string FileName::getPath() const
  88. {
  89. string res;
  90. if (absolute)
  91. res += '/';
  92. if (path.size() > 0)
  93. {
  94. for (unsigned int i = 0; i < path.size() - 1; ++i)
  95. res += path[i] + '/';
  96. res += path.back();
  97. }
  98. return res;
  99. }
  100. #if 0
  101. using namespace std;
  102. int main(int argc, char** argv)
  103. {
  104. try
  105. {
  106. if (argc == 2)
  107. {
  108. FileName fn(argv[1]);
  109. cout << fn.getPath() << endl;
  110. cout << fn.getName() << endl;
  111. cout << fn.getExtension() << endl;
  112. cout << fn.getFileName() << endl;
  113. }
  114. else if (argc == 4)
  115. {
  116. FileName fn(argv[1], argv[2], argv[3]);
  117. cout << fn.getPath() << endl;
  118. cout << fn.getName() << endl;
  119. cout << fn.getExtension() << endl;
  120. cout << fn.getFileName() << endl;
  121. }
  122. }
  123. catch (const char* msg)
  124. {
  125. cout << "exception: " << msg << endl;
  126. }
  127. }
  128. #endif