1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #ifndef __IRR_CORE_UTIL_H_INCLUDED__
\r
6 #define __IRR_CORE_UTIL_H_INCLUDED__
\r
8 #include "irrString.h"
\r
16 /*! \file coreutil.h
\r
17 \brief File containing useful basic utility functions
\r
20 // ----------- some basic quite often used string functions -----------------
\r
22 //! search if a filename has a proper extension
\r
23 inline s32 isFileExtension (const io::path& filename, const io::path& ext0,
\r
24 const io::path& ext1, const io::path& ext2)
\r
26 s32 extPos = filename.findLast ( '.' );
\r
31 if ( filename.equals_substring_ignore_case ( ext0, extPos ) )
\r
33 if ( filename.equals_substring_ignore_case ( ext1, extPos ) )
\r
35 if ( filename.equals_substring_ignore_case ( ext2, extPos ) )
\r
40 //! search if a filename has a proper extension
\r
41 inline bool hasFileExtension(const io::path& filename, const io::path& ext0,
\r
42 const io::path& ext1 = "", const io::path& ext2 = "")
\r
44 return isFileExtension ( filename, ext0, ext1, ext2 ) > 0;
\r
47 //! cut the filename extension from a source file path and store it in a dest file path
\r
48 inline io::path& cutFilenameExtension ( io::path &dest, const io::path &source )
\r
50 s32 endPos = source.findLast ( '.' );
\r
51 dest = source.subString ( 0, endPos < 0 ? source.size () : endPos );
\r
55 //! get the filename extension from a file path
\r
56 inline io::path& getFileNameExtension ( io::path &dest, const io::path &source )
\r
58 s32 endPos = source.findLast ( '.' );
\r
62 dest = source.subString ( endPos, source.size () );
\r
66 //! delete path from filename
\r
67 inline io::path& deletePathFromFilename(io::path& filename)
\r
69 // delete path from filename
\r
70 const fschar_t* s = filename.c_str();
\r
71 const fschar_t* p = s + filename.size();
\r
73 // search for path separator or beginning
\r
74 while ( *p != '/' && *p != '\\' && p != s )
\r
86 inline io::path& deletePathFromPath(io::path& filename, s32 pathCount)
\r
88 // delete path from filename
\r
89 s32 i = filename.size();
\r
91 // search for path separator or beginning
\r
94 if ( filename[i] == '/' || filename[i] == '\\' )
\r
96 if ( --pathCount <= 0 )
\r
104 filename [ i + 1 ] = 0;
\r
105 filename.validate();
\r
112 //! looks if file is in the same directory of path. returns offset of directory.
\r
113 //! 0 means in same directory. 1 means file is direct child of path
\r
114 inline s32 isInSameDirectory ( const io::path& path, const io::path& file )
\r
116 if ( path.size() && !path.equalsn ( file, path.size() ) )
\r
122 while ( (pos = path.findNext ( '/', pos )) >= 0 )
\r
129 while ( (pos = file.findNext ( '/', pos )) >= 0 )
\r
135 return subB - subA;
\r
138 //! splits a path into components
\r
139 static inline void splitFilename(const io::path &name, io::path* path=0,
\r
140 io::path* filename=0, io::path* extension=0, bool make_lower=false)
\r
142 s32 i = name.size();
\r
145 // search for path separator or beginning
\r
148 if ( name[i] == '.' )
\r
152 *extension = name.subString ( extpos + 1, name.size() - (extpos + 1), make_lower );
\r
155 if ( name[i] == '/' || name[i] == '\\' )
\r
158 *filename = name.subString ( i + 1, extpos - (i + 1), make_lower );
\r
161 *path = name.subString ( 0, i + 1, make_lower );
\r
162 path->replace ( '\\', '/' );
\r
169 *filename = name.subString ( 0, extpos, make_lower );
\r
172 //! create a filename from components
\r
173 static inline io::path mergeFilename(const io::path& path, const io::path& filename, const io::path& extension = "")
\r
175 io::path result(path);
\r
177 if ( !result.empty() )
\r
179 fschar_t last = result.lastChar();
\r
180 if ( last != _IRR_TEXT('/') && last != _IRR_TEXT('\\') )
\r
181 result += _IRR_TEXT('/');
\r
183 if ( !filename.empty() )
\r
184 result += filename;
\r
185 if ( !extension.empty() )
\r
187 if ( !result.empty() && extension[0] != _IRR_TEXT('.') )
\r
188 result += _IRR_TEXT('.');
\r
189 result += extension;
\r
196 //! some standard function ( to remove dependencies )
\r
197 inline bool isdigit(s32 c) { return c >= '0' && c <= '9'; }
\r
198 inline bool isspace(s32 c) { return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'; }
\r
199 inline bool isupper(s32 c) { return c >= 'A' && c <= 'Z'; }
\r
202 } // end namespace core
\r
203 } // end namespace irr
\r