FileInfo.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * FileInfo.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "FileInfo.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. namespace FileInfo
  14. {
  15. boost::string_ref GetFilename(boost::string_ref path)
  16. {
  17. const auto pos = path.find_last_of("/\\");
  18. if (pos != boost::string_ref::npos)
  19. return path.substr(pos + 1);
  20. return path;
  21. }
  22. boost::string_ref GetExtension(boost::string_ref path)
  23. {
  24. const auto dotPos = path.find_last_of('.');
  25. if(dotPos != boost::string_ref::npos)
  26. return path.substr(dotPos);
  27. return boost::string_ref{};
  28. }
  29. boost::string_ref GetStem(boost::string_ref path)
  30. {
  31. auto begin = path.find_last_of("/\\");
  32. auto end = path.find_last_of('.');
  33. if (begin == boost::string_ref::npos)
  34. begin = 0;
  35. else
  36. begin += 1;
  37. if (end < begin)
  38. end = boost::string_ref::npos;
  39. return path.substr(begin, end);
  40. }
  41. boost::string_ref GetParentPath(boost::string_ref path)
  42. {
  43. const auto pos = path.find_last_of("/\\");
  44. return path.substr(0, pos);
  45. }
  46. boost::string_ref GetPathStem(boost::string_ref path)
  47. {
  48. const auto dotPos = path.find_last_of('.');
  49. return path.substr(0, dotPos);
  50. }
  51. }
  52. VCMI_LIB_NAMESPACE_END