Irrlicht 3D Engine
coreutil.h
Go to the documentation of this file.
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 
5 #ifndef __IRR_CORE_UTIL_H_INCLUDED__
6 #define __IRR_CORE_UTIL_H_INCLUDED__
7 
8 #include "irrString.h"
9 #include "path.h"
10 
11 namespace irr
12 {
13 namespace core
14 {
15 
20 // ----------- some basic quite often used string functions -----------------
21 
23 inline s32 isFileExtension ( const io::path& filename,
24  const io::path& ext0,
25  const io::path& ext1,
26  const io::path& ext2)
27 {
28  s32 extPos = filename.findLast ( '.' );
29  if ( extPos < 0 )
30  return 0;
31 
32  extPos += 1;
33  if ( filename.equals_substring_ignore_case ( ext0, extPos ) ) return 1;
34  if ( filename.equals_substring_ignore_case ( ext1, extPos ) ) return 2;
35  if ( filename.equals_substring_ignore_case ( ext2, extPos ) ) return 3;
36  return 0;
37 }
38 
40 inline bool hasFileExtension ( const io::path& filename,
41  const io::path& ext0,
42  const io::path& ext1 = "",
43  const io::path& ext2 = "")
44 {
45  return isFileExtension ( filename, ext0, ext1, ext2 ) > 0;
46 }
47 
49 inline io::path& cutFilenameExtension ( io::path &dest, const io::path &source )
50 {
51  s32 endPos = source.findLast ( '.' );
52  dest = source.subString ( 0, endPos < 0 ? source.size () : endPos );
53  return dest;
54 }
55 
57 inline io::path& getFileNameExtension ( io::path &dest, const io::path &source )
58 {
59  s32 endPos = source.findLast ( '.' );
60  if ( endPos < 0 )
61  dest = "";
62  else
63  dest = source.subString ( endPos, source.size () );
64  return dest;
65 }
66 
69 {
70  // delete path from filename
71  const fschar_t* s = filename.c_str();
72  const fschar_t* p = s + filename.size();
73 
74  // search for path separator or beginning
75  while ( *p != '/' && *p != '\\' && p != s )
76  p--;
77 
78  if ( p != s )
79  {
80  ++p;
81  filename = p;
82  }
83  return filename;
84 }
85 
87 inline io::path& deletePathFromPath(io::path& filename, s32 pathCount)
88 {
89  // delete path from filename
90  s32 i = filename.size();
91 
92  // search for path separator or beginning
93  while ( i>=0 )
94  {
95  if ( filename[i] == '/' || filename[i] == '\\' )
96  {
97  if ( --pathCount <= 0 )
98  break;
99  }
100  --i;
101  }
102 
103  if ( i>0 )
104  {
105  filename [ i + 1 ] = 0;
106  filename.validate();
107  }
108  else
109  filename="";
110  return filename;
111 }
112 
115 inline s32 isInSameDirectory ( const io::path& path, const io::path& file )
116 {
117  s32 subA = 0;
118  s32 subB = 0;
119  s32 pos;
120 
121  if ( path.size() && !path.equalsn ( file, path.size() ) )
122  return -1;
123 
124  pos = 0;
125  while ( (pos = path.findNext ( '/', pos )) >= 0 )
126  {
127  subA += 1;
128  pos += 1;
129  }
130 
131  pos = 0;
132  while ( (pos = file.findNext ( '/', pos )) >= 0 )
133  {
134  subB += 1;
135  pos += 1;
136  }
137 
138  return subB - subA;
139 }
140 
141 // splits a path into components
142 static inline void splitFilename(const io::path &name, io::path* path=0,
143  io::path* filename=0, io::path* extension=0, bool make_lower=false)
144 {
145  s32 i = name.size();
146  s32 extpos = i;
147 
148  // search for path separator or beginning
149  while ( i >= 0 )
150  {
151  if ( name[i] == '.' )
152  {
153  extpos = i;
154  if ( extension )
155  *extension = name.subString ( extpos + 1, name.size() - (extpos + 1), make_lower );
156  }
157  else
158  if ( name[i] == '/' || name[i] == '\\' )
159  {
160  if ( filename )
161  *filename = name.subString ( i + 1, extpos - (i + 1), make_lower );
162  if ( path )
163  {
164  *path = name.subString ( 0, i + 1, make_lower );
165  path->replace ( '\\', '/' );
166  }
167  return;
168  }
169  i -= 1;
170  }
171  if ( filename )
172  *filename = name.subString ( 0, extpos, make_lower );
173 }
174 
175 
177 #undef isdigit
178 #undef isspace
179 #undef isupper
180 inline s32 isdigit(s32 c) { return c >= '0' && c <= '9'; }
181 inline s32 isspace(s32 c) { return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'; }
182 inline s32 isupper(s32 c) { return c >= 'A' && c <= 'Z'; }
183 
184 
185 } // end namespace core
186 } // end namespace irr
187 
188 #endif
core::string< fschar_t > path
Type used for all file system related strings.
Definition: path.h:17
u32 size() const
Returns length of the string&#39;s content.
Definition: irrString.h:481
bool hasFileExtension(const io::path &filename, const io::path &ext0, const io::path &ext1="", const io::path &ext2="")
search if a filename has a proper extension
Definition: coreutil.h:40
bool equals_substring_ignore_case(const string< T, TAlloc > &other, const s32 sourcePos=0) const
Compares the strings ignoring case.
Definition: irrString.h:535
s32 findLast(T c, s32 start=-1) const
finds last occurrence of character in string
Definition: irrString.h:822
s32 isdigit(s32 c)
some standard function ( to remove dependencies )
Definition: coreutil.h:180
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
s32 isInSameDirectory(const io::path &path, const io::path &file)
Definition: coreutil.h:115
s32 isspace(s32 c)
Definition: coreutil.h:181
s32 isupper(s32 c)
Definition: coreutil.h:182
io::path & getFileNameExtension(io::path &dest, const io::path &source)
get the filename extension from a file path
Definition: coreutil.h:57
io::path & deletePathFromFilename(io::path &filename)
delete path from filename
Definition: coreutil.h:68
signed int s32
32 bit signed variable.
Definition: irrTypes.h:66
const T * c_str() const
Returns character string.
Definition: irrString.h:495
string< T, TAlloc > & replace(T toReplace, T replaceWith)
Replaces all characters of a special type with another one.
Definition: irrString.h:1007
s32 isFileExtension(const io::path &filename, const io::path &ext0, const io::path &ext1, const io::path &ext2)
search if a filename has a proper extension
Definition: coreutil.h:23
string< T > subString(u32 begin, s32 length, bool make_lower=false) const
Returns a substring.
Definition: irrString.h:891
bool equalsn(const string< T, TAlloc > &other, u32 n) const
compares the first n characters of the strings
Definition: irrString.h:569
io::path & deletePathFromPath(io::path &filename, s32 pathCount)
trim paths
Definition: coreutil.h:87
s32 findNext(T c, u32 startPos) const
finds next occurrence of character in string
Definition: irrString.h:807
string< T, TAlloc > & validate()
verify the existing string.
Definition: irrString.h:1247
io::path & cutFilenameExtension(io::path &dest, const io::path &source)
cut the filename extension from a source file path and store it in a dest file path ...
Definition: coreutil.h:49
char fschar_t
Type name for character type used by the file system.
Definition: irrTypes.h:158