coreutil.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __IRR_CORE_UTIL_H_INCLUDED__
  5. #define __IRR_CORE_UTIL_H_INCLUDED__
  6. #include "irrString.h"
  7. #include "path.h"
  8. namespace irr
  9. {
  10. namespace core
  11. {
  12. /*! \file coreutil.h
  13. \brief File containing useful basic utility functions
  14. */
  15. // ----------- some basic quite often used string functions -----------------
  16. //! search if a filename has a proper extension
  17. inline s32 isFileExtension ( const io::path& filename,
  18. const io::path& ext0,
  19. const io::path& ext1,
  20. const io::path& ext2)
  21. {
  22. s32 extPos = filename.findLast ( '.' );
  23. if ( extPos < 0 )
  24. return 0;
  25. extPos += 1;
  26. if ( filename.equals_substring_ignore_case ( ext0, extPos ) ) return 1;
  27. if ( filename.equals_substring_ignore_case ( ext1, extPos ) ) return 2;
  28. if ( filename.equals_substring_ignore_case ( ext2, extPos ) ) return 3;
  29. return 0;
  30. }
  31. //! search if a filename has a proper extension
  32. inline bool hasFileExtension ( const io::path& filename,
  33. const io::path& ext0,
  34. const io::path& ext1 = "",
  35. const io::path& ext2 = "")
  36. {
  37. return isFileExtension ( filename, ext0, ext1, ext2 ) > 0;
  38. }
  39. //! cut the filename extension from a source file path and store it in a dest file path
  40. inline io::path& cutFilenameExtension ( io::path &dest, const io::path &source )
  41. {
  42. s32 endPos = source.findLast ( '.' );
  43. dest = source.subString ( 0, endPos < 0 ? source.size () : endPos );
  44. return dest;
  45. }
  46. //! get the filename extension from a file path
  47. inline io::path& getFileNameExtension ( io::path &dest, const io::path &source )
  48. {
  49. s32 endPos = source.findLast ( '.' );
  50. if ( endPos < 0 )
  51. dest = "";
  52. else
  53. dest = source.subString ( endPos, source.size () );
  54. return dest;
  55. }
  56. //! delete path from filename
  57. inline io::path& deletePathFromFilename(io::path& filename)
  58. {
  59. // delete path from filename
  60. const fschar_t* s = filename.c_str();
  61. const fschar_t* p = s + filename.size();
  62. // search for path separator or beginning
  63. while ( *p != '/' && *p != '\\' && p != s )
  64. p--;
  65. if ( p != s )
  66. {
  67. ++p;
  68. filename = p;
  69. }
  70. return filename;
  71. }
  72. //! trim paths
  73. inline io::path& deletePathFromPath(io::path& filename, s32 pathCount)
  74. {
  75. // delete path from filename
  76. s32 i = filename.size();
  77. // search for path separator or beginning
  78. while ( i>=0 )
  79. {
  80. if ( filename[i] == '/' || filename[i] == '\\' )
  81. {
  82. if ( --pathCount <= 0 )
  83. break;
  84. }
  85. --i;
  86. }
  87. if ( i>0 )
  88. {
  89. filename [ i + 1 ] = 0;
  90. filename.validate();
  91. }
  92. else
  93. filename="";
  94. return filename;
  95. }
  96. //! looks if file is in the same directory of path. returns offset of directory.
  97. //! 0 means in same directory. 1 means file is direct child of path
  98. inline s32 isInSameDirectory ( const io::path& path, const io::path& file )
  99. {
  100. s32 subA = 0;
  101. s32 subB = 0;
  102. s32 pos;
  103. if ( path.size() && !path.equalsn ( file, path.size() ) )
  104. return -1;
  105. pos = 0;
  106. while ( (pos = path.findNext ( '/', pos )) >= 0 )
  107. {
  108. subA += 1;
  109. pos += 1;
  110. }
  111. pos = 0;
  112. while ( (pos = file.findNext ( '/', pos )) >= 0 )
  113. {
  114. subB += 1;
  115. pos += 1;
  116. }
  117. return subB - subA;
  118. }
  119. // splits a path into components
  120. static inline void splitFilename(const io::path &name, io::path* path=0,
  121. io::path* filename=0, io::path* extension=0, bool make_lower=false)
  122. {
  123. s32 i = name.size();
  124. s32 extpos = i;
  125. // search for path separator or beginning
  126. while ( i >= 0 )
  127. {
  128. if ( name[i] == '.' )
  129. {
  130. extpos = i;
  131. if ( extension )
  132. *extension = name.subString ( extpos + 1, name.size() - (extpos + 1), make_lower );
  133. }
  134. else
  135. if ( name[i] == '/' || name[i] == '\\' )
  136. {
  137. if ( filename )
  138. *filename = name.subString ( i + 1, extpos - (i + 1), make_lower );
  139. if ( path )
  140. {
  141. *path = name.subString ( 0, i + 1, make_lower );
  142. path->replace ( '\\', '/' );
  143. }
  144. return;
  145. }
  146. i -= 1;
  147. }
  148. if ( filename )
  149. *filename = name.subString ( 0, extpos, make_lower );
  150. }
  151. //! some standard function ( to remove dependencies )
  152. #undef isdigit
  153. #undef isspace
  154. #undef isupper
  155. inline s32 isdigit(s32 c) { return c >= '0' && c <= '9'; }
  156. inline s32 isspace(s32 c) { return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'; }
  157. inline s32 isupper(s32 c) { return c >= 'A' && c <= 'Z'; }
  158. } // end namespace core
  159. } // end namespace irr
  160. #endif