FileInfo.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. namespace FileInfo
  13. {
  14. boost::string_ref GetFilename(boost::string_ref path)
  15. {
  16. const auto pos = path.find_last_of("/\\");
  17. if (pos != boost::string_ref::npos)
  18. return path.substr(pos + 1);
  19. return path;
  20. }
  21. boost::string_ref GetExtension(boost::string_ref path)
  22. {
  23. const auto dotPos = path.find_last_of('.');
  24. if(dotPos != boost::string_ref::npos)
  25. return path.substr(dotPos);
  26. return boost::string_ref{};
  27. }
  28. boost::string_ref GetStem(boost::string_ref path)
  29. {
  30. auto begin = path.find_last_of("/\\");
  31. auto end = path.find_last_of('.');
  32. if (begin == boost::string_ref::npos)
  33. begin = 0;
  34. else
  35. begin += 1;
  36. if (end < begin)
  37. end = boost::string_ref::npos;
  38. return path.substr(begin, end);
  39. }
  40. boost::string_ref GetParentPath(boost::string_ref path)
  41. {
  42. const auto pos = path.find_last_of("/\\");
  43. return path.substr(0, pos);
  44. }
  45. boost::string_ref GetPathStem(boost::string_ref path)
  46. {
  47. const auto dotPos = path.find_last_of('.');
  48. return path.substr(0, dotPos);
  49. }
  50. }