CmPath.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include <boost/filesystem.hpp>
  4. namespace CamelotFramework
  5. {
  6. /**
  7. * @brief Various string manipulations of file paths.
  8. */
  9. class Path
  10. {
  11. public:
  12. static String getExtension(const String& path)
  13. {
  14. return boost::filesystem::extension(boost::filesystem::path(path.c_str())).c_str();
  15. }
  16. static bool hasExtension(const String& path, const String& extension)
  17. {
  18. return getExtension(path) == extension;
  19. }
  20. static String combine(const String& base, const String& name)
  21. {
  22. if (base.empty())
  23. return name;
  24. else
  25. return base + '/' + name;
  26. }
  27. /**
  28. * @brief Method for standardizing paths - use forward slashes only, end with slash.
  29. */
  30. String standardisePath(const String& inPath)
  31. {
  32. String path = inPath;
  33. std::replace(path.begin(), path.end(), '\\', '/');
  34. if(path[path.length() - 1] != '/')
  35. path += '/';
  36. return path;
  37. }
  38. /**
  39. * @brief Method for splitting a fully qualified filename into the base name and path.
  40. */
  41. void splitFilename(const String& qualifiedName, String& outBasename, String& outPath)
  42. {
  43. String path = qualifiedName;
  44. // Replace \ with / first
  45. std::replace( path.begin(), path.end(), '\\', '/' );
  46. // split based on final /
  47. size_t i = path.find_last_of('/');
  48. if (i == String::npos)
  49. {
  50. outPath.clear();
  51. outBasename = qualifiedName;
  52. }
  53. else
  54. {
  55. outBasename = path.substr(i+1, path.size() - i - 1);
  56. outPath = path.substr(0, i+1);
  57. }
  58. }
  59. /**
  60. * @brief Method for splitting a filename into the base name and extension.
  61. */
  62. void splitBaseFilename(const String& fullName, String& outBasename, String& outExtension)
  63. {
  64. size_t i = fullName.find_last_of(".");
  65. if (i == CamelotFramework::String::npos)
  66. {
  67. outExtension.clear();
  68. outBasename = fullName;
  69. }
  70. else
  71. {
  72. outExtension = fullName.substr(i+1);
  73. outBasename = fullName.substr(0, i);
  74. }
  75. }
  76. /**
  77. * @brief Method for splitting a fully qualified filename into the base name, extension and path.
  78. */
  79. void splitFullFilename(const String& qualifiedName, String& outBasename, String& outExtention, String& outPath)
  80. {
  81. String fullName;
  82. splitFilename(qualifiedName, fullName, outPath);
  83. splitBaseFilename(fullName, outBasename, outExtention);
  84. }
  85. };
  86. }