arsa  2.7
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, const io::path& ext0,
24  const io::path& ext1, const io::path& ext2)
25 {
26  s32 extPos = filename.findLast ( '.' );
27  if ( extPos < 0 )
28  return 0;
29 
30  extPos += 1;
31  if ( filename.equals_substring_ignore_case ( ext0, extPos ) )
32  return 1;
33  if ( filename.equals_substring_ignore_case ( ext1, extPos ) )
34  return 2;
35  if ( filename.equals_substring_ignore_case ( ext2, extPos ) )
36  return 3;
37  return 0;
38 }
39 
41 inline bool hasFileExtension(const io::path& filename, const io::path& ext0,
42  const io::path& ext1 = "", const io::path& ext2 = "")
43 {
44  return isFileExtension ( filename, ext0, ext1, ext2 ) > 0;
45 }
46 
49 {
50  s32 endPos = source.findLast ( '.' );
51  dest = source.subString ( 0, endPos < 0 ? source.size () : endPos );
52  return dest;
53 }
54 
57 {
58  s32 endPos = source.findLast ( '.' );
59  if ( endPos < 0 )
60  dest = "";
61  else
62  dest = source.subString ( endPos, source.size () );
63  return dest;
64 }
65 
68 {
69  // delete path from filename
70  const fschar_t* s = filename.c_str();
71  const fschar_t* p = s + filename.size();
72 
73  // search for path separator or beginning
74  while ( *p != '/' && *p != '\\' && p != s )
75  p--;
76 
77  if ( p != s )
78  {
79  ++p;
80  filename = p;
81  }
82  return filename;
83 }
84 
86 inline io::path& deletePathFromPath(io::path& filename, s32 pathCount)
87 {
88  // delete path from filename
89  s32 i = filename.size();
90 
91  // search for path separator or beginning
92  while ( i>=0 )
93  {
94  if ( filename[i] == '/' || filename[i] == '\\' )
95  {
96  if ( --pathCount <= 0 )
97  break;
98  }
99  --i;
100  }
101 
102  if ( i>0 )
103  {
104  filename [ i + 1 ] = 0;
105  filename.validate();
106  }
107  else
108  filename="";
109  return filename;
110 }
111 
114 inline s32 isInSameDirectory ( const io::path& path, const io::path& file )
115 {
116  if ( path.size() && !path.equalsn ( file, path.size() ) )
117  return -1;
118 
119  s32 subA = 0;
120  s32 subB = 0;
121  s32 pos = 0;
122  while ( (pos = path.findNext ( '/', pos )) >= 0 )
123  {
124  subA += 1;
125  pos += 1;
126  }
127 
128  pos = 0;
129  while ( (pos = file.findNext ( '/', pos )) >= 0 )
130  {
131  subB += 1;
132  pos += 1;
133  }
134 
135  return subB - subA;
136 }
137 
139 static inline void splitFilename(const io::path &name, io::path* path=0,
140  io::path* filename=0, io::path* extension=0, bool make_lower=false)
141 {
142  s32 i = name.size();
143  s32 extpos = i;
144 
145  // search for path separator or beginning
146  while ( i >= 0 )
147  {
148  if ( name[i] == '.' )
149  {
150  extpos = i;
151  if ( extension )
152  *extension = name.subString ( extpos + 1, name.size() - (extpos + 1), make_lower );
153  }
154  else
155  if ( name[i] == '/' || name[i] == '\\' )
156  {
157  if ( filename )
158  *filename = name.subString ( i + 1, extpos - (i + 1), make_lower );
159  if ( path )
160  {
161  *path = name.subString ( 0, i + 1, make_lower );
162  path->replace ( '\\', '/' );
163  }
164  return;
165  }
166  i -= 1;
167  }
168  if ( filename )
169  *filename = name.subString ( 0, extpos, make_lower );
170 }
171 
173 static inline io::path mergeFilename(const io::path& path, const io::path& filename, const io::path& extension = "")
174 {
176 
177  if ( !result.empty() )
178  {
179  fschar_t last = result.lastChar();
180  if ( last != _IRR_TEXT('/') && last != _IRR_TEXT('\\') )
181  result += _IRR_TEXT('/');
182  }
183  if ( !filename.empty() )
184  result += filename;
185  if ( !extension.empty() )
186  {
187  if ( !result.empty() && extension[0] != _IRR_TEXT('.') )
188  result += _IRR_TEXT('.');
189  result += extension;
190  }
191 
192  return result;
193 }
194 
195 
197 inline s32 isdigit(s32 c) { return c >= '0' && c <= '9'; }
198 inline s32 isspace(s32 c) { return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'; }
199 inline s32 isupper(s32 c) { return c >= 'A' && c <= 'Z'; }
200 
201 
202 } // end namespace core
203 } // end namespace irr
204 
205 #endif
core::string< fschar_t > path
Type used for all file system related strings.
Definition: path.h:17
GLuint64EXT * result
u32 size() const
Returns length of the string's content.
Definition: irrString.h:502
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:41
bool equals_substring_ignore_case(const string< T, TAlloc > &other, const s32 sourcePos=0) const
Compares the strings ignoring case.
Definition: irrString.h:566
s32 findLast(T c, s32 start=-1) const
finds last occurrence of character in string
Definition: irrString.h:879
GLfloat GLfloat p
s32 isdigit(s32 c)
some standard function ( to remove dependencies )
Definition: coreutil.h:197
GLuint const GLchar * name
Everything in the Irrlicht Engine can be found in this namespace.
Definition: CARSADPad.h:6
s32 isInSameDirectory(const io::path &path, const io::path &file)
Definition: coreutil.h:114
s32 isspace(s32 c)
Definition: coreutil.h:198
s32 isupper(s32 c)
Definition: coreutil.h:199
io::path & getFileNameExtension(io::path &dest, const io::path &source)
get the filename extension from a file path
Definition: coreutil.h:56
io::path & deletePathFromFilename(io::path &filename)
delete path from filename
Definition: coreutil.h:67
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
const T * c_str() const
Returns character string.
Definition: irrString.h:526
GLsizei GLsizei GLchar * source
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:948
GLdouble s
Definition: SDL_opengl.h:2063
GLsizei const GLchar *const * path
io::path & deletePathFromPath(io::path &filename, s32 pathCount)
trim paths
Definition: coreutil.h:86
s32 findNext(T c, u32 startPos) const
finds next occurrence of character in string
Definition: irrString.h:864
#define _IRR_TEXT(X)
Definition: irrTypes.h:166
string< T, TAlloc > & validate()
verify the existing string.
Definition: irrString.h:1338
const GLubyte * c
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:48
char fschar_t
Type name for character type used by the file system.
Definition: irrTypes.h:165