]> git.lizzy.rs Git - irrlicht.git/blob - include/coreutil.h
Bump revision
[irrlicht.git] / include / coreutil.h
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
4 \r
5 #ifndef __IRR_CORE_UTIL_H_INCLUDED__\r
6 #define __IRR_CORE_UTIL_H_INCLUDED__\r
7 \r
8 #include "irrString.h"\r
9 #include "path.h"\r
10 \r
11 namespace irr\r
12 {\r
13 namespace core\r
14 {\r
15 \r
16 /*! \file coreutil.h\r
17         \brief File containing useful basic utility functions\r
18 */\r
19 \r
20 // ----------- some basic quite often used string functions -----------------\r
21 \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
25 {\r
26         s32 extPos = filename.findLast ( '.' );\r
27         if ( extPos < 0 )\r
28                 return 0;\r
29 \r
30         extPos += 1;\r
31         if ( filename.equals_substring_ignore_case ( ext0, extPos ) )\r
32                 return 1;\r
33         if ( filename.equals_substring_ignore_case ( ext1, extPos ) )\r
34                 return 2;\r
35         if ( filename.equals_substring_ignore_case ( ext2, extPos ) )\r
36                 return 3;\r
37         return 0;\r
38 }\r
39 \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
43 {\r
44         return isFileExtension ( filename, ext0, ext1, ext2 ) > 0;\r
45 }\r
46 \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
49 {\r
50         s32 endPos = source.findLast ( '.' );\r
51         dest = source.subString ( 0, endPos < 0 ? source.size () : endPos );\r
52         return dest;\r
53 }\r
54 \r
55 //! get the filename extension from a file path\r
56 inline io::path& getFileNameExtension ( io::path &dest, const io::path &source )\r
57 {\r
58         s32 endPos = source.findLast ( '.' );\r
59         if ( endPos < 0 )\r
60                 dest = "";\r
61         else\r
62                 dest = source.subString ( endPos, source.size () );\r
63         return dest;\r
64 }\r
65 \r
66 //! delete path from filename\r
67 inline io::path& deletePathFromFilename(io::path& filename)\r
68 {\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
72 \r
73         // search for path separator or beginning\r
74         while ( *p != '/' && *p != '\\' && p != s )\r
75                 p--;\r
76 \r
77         if ( p != s )\r
78         {\r
79                 ++p;\r
80                 filename = p;\r
81         }\r
82         return filename;\r
83 }\r
84 \r
85 //! trim paths\r
86 inline io::path& deletePathFromPath(io::path& filename, s32 pathCount)\r
87 {\r
88         // delete path from filename\r
89         s32 i = filename.size();\r
90 \r
91         // search for path separator or beginning\r
92         while ( i>=0 )\r
93         {\r
94                 if ( filename[i] == '/' || filename[i] == '\\' )\r
95                 {\r
96                         if ( --pathCount <= 0 )\r
97                                 break;\r
98                 }\r
99                 --i;\r
100         }\r
101 \r
102         if ( i>0 )\r
103         {\r
104                 filename [ i + 1 ] = 0;\r
105                 filename.validate();\r
106         }\r
107         else\r
108                 filename="";\r
109         return filename;\r
110 }\r
111 \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
115 {\r
116         if ( path.size() && !path.equalsn ( file, path.size() ) )\r
117                 return -1;\r
118 \r
119         s32 subA = 0;\r
120         s32 subB = 0;\r
121         s32 pos = 0;\r
122         while ( (pos = path.findNext ( '/', pos )) >= 0 )\r
123         {\r
124                 subA += 1;\r
125                 pos += 1;\r
126         }\r
127 \r
128         pos = 0;\r
129         while ( (pos = file.findNext ( '/', pos )) >= 0 )\r
130         {\r
131                 subB += 1;\r
132                 pos += 1;\r
133         }\r
134 \r
135         return subB - subA;\r
136 }\r
137 \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
141 {\r
142         s32 i = name.size();\r
143         s32 extpos = i;\r
144 \r
145         // search for path separator or beginning\r
146         while ( i >= 0 )\r
147         {\r
148                 if ( name[i] == '.' )\r
149                 {\r
150                         extpos = i;\r
151                         if ( extension )\r
152                                 *extension = name.subString ( extpos + 1, name.size() - (extpos + 1), make_lower );\r
153                 }\r
154                 else\r
155                 if ( name[i] == '/' || name[i] == '\\' )\r
156                 {\r
157                         if ( filename )\r
158                                 *filename = name.subString ( i + 1, extpos - (i + 1), make_lower );\r
159                         if ( path )\r
160                         {\r
161                                 *path = name.subString ( 0, i + 1, make_lower );\r
162                                 path->replace ( '\\', '/' );\r
163                         }\r
164                         return;\r
165                 }\r
166                 i -= 1;\r
167         }\r
168         if ( filename )\r
169                 *filename = name.subString ( 0, extpos, make_lower );\r
170 }\r
171 \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
174 {\r
175         io::path result(path);\r
176 \r
177         if ( !result.empty() )\r
178         {\r
179                 fschar_t last = result.lastChar();\r
180                 if ( last != _IRR_TEXT('/') && last != _IRR_TEXT('\\') )\r
181                         result += _IRR_TEXT('/');\r
182         }\r
183         if ( !filename.empty() )\r
184                 result += filename;\r
185         if ( !extension.empty() )\r
186         {\r
187                 if ( !result.empty() && extension[0] != _IRR_TEXT('.') )\r
188                         result += _IRR_TEXT('.');\r
189                 result += extension;\r
190         }\r
191 \r
192         return result;\r
193 }\r
194 \r
195 \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
200 \r
201 \r
202 } // end namespace core\r
203 } // end namespace irr\r
204 \r
205 #endif\r