FileNameUtils 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGDB_FILENAMEUTILS
  14. #define OSGDB_FILENAMEUTILS 1
  15. #include <osgDB/Export>
  16. #include <string>
  17. #include <vector>
  18. namespace osgDB {
  19. /** Gets the parent path from full name (Ex: /a/b/c.Ext => /a/b). */
  20. extern OSGDB_EXPORT std::string getFilePath(const std::string& filename);
  21. /** Gets the extension without dot (Ex: /a/b/c.Ext => Ext). */
  22. extern OSGDB_EXPORT std::string getFileExtension(const std::string& filename);
  23. /** Gets the extension including dot (Ex: /a/b/c.Ext => .Ext). */
  24. extern OSGDB_EXPORT std::string getFileExtensionIncludingDot(const std::string& filename);
  25. /** Gets the lowercase extension without dot (Ex: /a/b/c.Ext => ext). */
  26. extern OSGDB_EXPORT std::string getLowerCaseFileExtension(const std::string& filename);
  27. /** Gets file name with extension (Ex: /a/b/c.Ext => c.Ext). */
  28. extern OSGDB_EXPORT std::string getSimpleFileName(const std::string& fileName);
  29. /** Gets file path without last extension (Ex: /a/b/c.Ext => /a/b/c ; file.ext1.ext2 => file.ext1). */
  30. extern OSGDB_EXPORT std::string getNameLessExtension(const std::string& fileName);
  31. /** Gets file path without \b all extensions (Ex: /a/b/c.Ext => /a/b/c ; file.ext1.ext2 => file). */
  32. extern OSGDB_EXPORT std::string getNameLessAllExtensions(const std::string& fileName);
  33. /** Gets file name without last extension (Ex: /a/b/c.Ext => c ; file.ext1.ext2 => file.ext1). */
  34. extern OSGDB_EXPORT std::string getStrippedName(const std::string& fileName);
  35. /** If 'to' is in a subdirectory of 'from' then this function returns the subpath, otherwise it just returns the file name.
  36. * The function does \b not automagically resolve paths as the system does, so be careful to give canonical paths.
  37. * However, the function interprets slashes ('/') and backslashes ('\') as they were equal.
  38. */
  39. extern OSGDB_EXPORT std::string getPathRelative(const std::string& from, const std::string& to);
  40. /** Gets root part of a path ("/" or "C:"), or an empty string if none found. */
  41. extern OSGDB_EXPORT std::string getPathRoot(const std::string& path);
  42. /** Tests if path is absolute, as !getPathRoot(path).empty(). */
  43. extern OSGDB_EXPORT bool isAbsolutePath(const std::string& path);
  44. /** Converts forward slashes (/) to back slashes (\). */
  45. extern OSGDB_EXPORT std::string convertFileNameToWindowsStyle(const std::string& fileName);
  46. /** Converts back slashes (\) to forward slashes (/). */
  47. extern OSGDB_EXPORT std::string convertFileNameToUnixStyle(const std::string& fileName);
  48. extern OSGDB_EXPORT std::string convertToLowerCase(const std::string& fileName);
  49. const char UNIX_PATH_SEPARATOR = '/';
  50. const char WINDOWS_PATH_SEPARATOR = '\\';
  51. /** Get the path separator for the current platform. */
  52. extern OSGDB_EXPORT char getNativePathSeparator();
  53. /** Check if the path contains only the current platform's path separators. */
  54. extern OSGDB_EXPORT bool isFileNameNativeStyle(const std::string& fileName);
  55. /** Convert the path to contain only the current platform's path separators. */
  56. extern OSGDB_EXPORT std::string convertFileNameToNativeStyle(const std::string& fileName);
  57. extern OSGDB_EXPORT bool equalCaseInsensitive(const std::string& lhs,const std::string& rhs);
  58. extern OSGDB_EXPORT bool equalCaseInsensitive(const std::string& lhs,const char* rhs);
  59. extern OSGDB_EXPORT bool containsServerAddress(const std::string& filename);
  60. extern OSGDB_EXPORT std::string getServerProtocol(const std::string& filename);
  61. extern OSGDB_EXPORT std::string getServerAddress(const std::string& filename);
  62. extern OSGDB_EXPORT std::string getServerFileName(const std::string& filename);
  63. /** Concatenates two paths */
  64. extern OSGDB_EXPORT std::string concatPaths(const std::string& left, const std::string& right);
  65. /** Removes .. and . dirs in a path */
  66. extern OSGDB_EXPORT std::string getRealPath(const std::string& path);
  67. /** Splits a path into elements between separators (including Windows' root, if any). */
  68. extern OSGDB_EXPORT void getPathElements(const std::string& path, std::vector<std::string> & out_elements);
  69. /** Functor for helping sort filename in alphabetical and numerical order when using in conjunction with std::sort.*/
  70. struct FileNameComparator
  71. {
  72. inline bool operator() (const std::string& lhs, const std::string& rhs) const
  73. {
  74. std::string::size_type size_lhs = lhs.size();
  75. std::string::size_type size_rhs = rhs.size();
  76. std::string::size_type pos_lhs = 0;
  77. std::string::size_type pos_rhs = 0;
  78. while(pos_lhs<size_lhs && pos_rhs<size_rhs)
  79. {
  80. char c_lhs = lhs[pos_rhs];
  81. char c_rhs = rhs[pos_rhs];
  82. bool numeric_lhs = lhs[pos_lhs]>='0' && lhs[pos_lhs]<='9';
  83. bool numeric_rhs = rhs[pos_rhs]>='0' && rhs[pos_rhs]<='9';
  84. if (numeric_lhs && numeric_rhs)
  85. {
  86. std::string::size_type start_lhs = pos_lhs;
  87. ++pos_lhs;
  88. while(pos_lhs<size_lhs && (lhs[pos_lhs]>='0' && lhs[pos_lhs]<='9')) ++pos_lhs;
  89. std::string::size_type start_rhs = pos_rhs;
  90. ++pos_rhs;
  91. while(pos_rhs<size_rhs && (rhs[pos_rhs]>='0' && rhs[pos_rhs]<='9')) ++pos_rhs;
  92. if (pos_lhs<pos_rhs) return true;
  93. else if (pos_rhs<pos_lhs) return false;
  94. while(start_lhs<pos_lhs && start_rhs<pos_rhs)
  95. {
  96. if (lhs[start_lhs]<rhs[start_rhs]) return true;
  97. if (lhs[start_lhs]>rhs[start_rhs]) return false;
  98. ++start_lhs;
  99. ++start_rhs;
  100. }
  101. }
  102. else
  103. {
  104. if (c_lhs<c_rhs) return true;
  105. else if (c_rhs<c_lhs) return false;
  106. ++pos_lhs;
  107. ++pos_rhs;
  108. }
  109. }
  110. return pos_lhs<pos_rhs;
  111. }
  112. };
  113. extern OSGDB_EXPORT void stringcopy(char* dest, const char* src, size_t length);
  114. #define stringcopyfixedsize(DEST, SRC) stringcopy(DEST, SRC, sizeof(DEST));
  115. }
  116. #endif