| Irrlicht 3D Engine |

coreutil.h
Go to the documentation of this file.

    00001 // Copyright (C) 2002-2012 Nikolaus Gebhardt
    00002 // This file is part of the "Irrlicht Engine".
    00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
    00004
    00005 #ifndef __IRR_CORE_UTIL_H_INCLUDED__
    00006 #define __IRR_CORE_UTIL_H_INCLUDED__
    00007
    00008 #include "irrString.h"
    00009 #include "path.h"
    00010
    00011 namespace irr
    00012 {
    00013 namespace core
    00014 {
    00015
    00020 // ----------- some basic quite often used string functions -----------------
    00021
    00023 inline s32 isFileExtension (    const io::path& filename,
    00024                                 const io::path& ext0,
    00025                                 const io::path& ext1,
    00026                                 const io::path& ext2)
    00027 {
    00028     s32 extPos = filename.findLast ( '.' );
    00029     if ( extPos < 0 )
    00030         return 0;
    00031
    00032     extPos += 1;
    00033     if ( filename.equals_substring_ignore_case ( ext0, extPos ) ) return 1;
    00034     if ( filename.equals_substring_ignore_case ( ext1, extPos ) ) return 2;
    00035     if ( filename.equals_substring_ignore_case ( ext2, extPos ) ) return 3;
    00036     return 0;
    00037 }
    00038
    00040 inline bool hasFileExtension (  const io::path& filename,
    00041                                 const io::path& ext0,
    00042                                 const io::path& ext1 = "",
    00043                                 const io::path& ext2 = "")
    00044 {
    00045     return isFileExtension ( filename, ext0, ext1, ext2 ) > 0;
    00046 }
    00047
    00049 inline io::path& cutFilenameExtension ( io::path &dest, const io::path &source )
    00050 {
    00051     s32 endPos = source.findLast ( '.' );
    00052     dest = source.subString ( 0, endPos < 0 ? source.size () : endPos );
    00053     return dest;
    00054 }
    00055
    00057 inline io::path& getFileNameExtension ( io::path &dest, const io::path &source )
    00058 {
    00059     s32 endPos = source.findLast ( '.' );
    00060     if ( endPos < 0 )
    00061         dest = "";
    00062     else
    00063         dest = source.subString ( endPos, source.size () );
    00064     return dest;
    00065 }
    00066
    00068 inline io::path& deletePathFromFilename(io::path& filename)
    00069 {
    00070     // delete path from filename
    00071     const fschar_t* s = filename.c_str();
    00072     const fschar_t* p = s + filename.size();
    00073
    00074     // search for path separator or beginning
    00075     while ( *p != '/' && *p != '\\' && p != s )
    00076         p--;
    00077
    00078     if ( p != s )
    00079     {
    00080         ++p;
    00081         filename = p;
    00082     }
    00083     return filename;
    00084 }
    00085
    00087 inline io::path& deletePathFromPath(io::path& filename, s32 pathCount)
    00088 {
    00089     // delete path from filename
    00090     s32 i = filename.size();
    00091
    00092     // search for path separator or beginning
    00093     while ( i>=0 )
    00094     {
    00095         if ( filename[i] == '/' || filename[i] == '\\' )
    00096         {
    00097             if ( --pathCount <= 0 )
    00098                 break;
    00099         }
    00100         --i;
    00101     }
    00102
    00103     if ( i>0 )
    00104     {
    00105         filename [ i + 1 ] = 0;
    00106         filename.validate();
    00107     }
    00108     else
    00109         filename="";
    00110     return filename;
    00111 }
    00112
    00115 inline s32 isInSameDirectory ( const io::path& path, const io::path& file )
    00116 {
    00117     s32 subA = 0;
    00118     s32 subB = 0;
    00119     s32 pos;
    00120
    00121     if ( path.size() && !path.equalsn ( file, path.size() ) )
    00122         return -1;
    00123
    00124     pos = 0;
    00125     while ( (pos = path.findNext ( '/', pos )) >= 0 )
    00126     {
    00127         subA += 1;
    00128         pos += 1;
    00129     }
    00130
    00131     pos = 0;
    00132     while ( (pos = file.findNext ( '/', pos )) >= 0 )
    00133     {
    00134         subB += 1;
    00135         pos += 1;
    00136     }
    00137
    00138     return subB - subA;
    00139 }
    00140
    00141 // splits a path into components
    00142 static inline void splitFilename(const io::path &name, io::path* path=0,
    00143         io::path* filename=0, io::path* extension=0, bool make_lower=false)
    00144 {
    00145     s32 i = name.size();
    00146     s32 extpos = i;
    00147
    00148     // search for path separator or beginning
    00149     while ( i >= 0 )
    00150     {
    00151         if ( name[i] == '.' )
    00152         {
    00153             extpos = i;
    00154             if ( extension )
    00155                 *extension = name.subString ( extpos + 1, name.size() - (extpos + 1), make_lower );
    00156         }
    00157         else
    00158         if ( name[i] == '/' || name[i] == '\\' )
    00159         {
    00160             if ( filename )
    00161                 *filename = name.subString ( i + 1, extpos - (i + 1), make_lower );
    00162             if ( path )
    00163             {
    00164                 *path = name.subString ( 0, i + 1, make_lower );
    00165                 path->replace ( '\\', '/' );
    00166             }
    00167             return;
    00168         }
    00169         i -= 1;
    00170     }
    00171     if ( filename )
    00172         *filename = name.subString ( 0, extpos, make_lower );
    00173 }
    00174
    00175
    00177 #undef isdigit
    00178 #undef isspace
    00179 #undef isupper
    00180 inline s32 isdigit(s32 c) { return c >= '0' && c <= '9'; }
    00181 inline s32 isspace(s32 c) { return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'; }
    00182 inline s32 isupper(s32 c) { return c >= 'A' && c <= 'Z'; }
    00183
    00184
    00185 } // end namespace core
    00186 } // end namespace irr
    00187
    00188 #endif