diff options
Diffstat (limited to 'libraries/irrlicht-1.8.1/include/coreutil.h')
-rw-r--r-- | libraries/irrlicht-1.8.1/include/coreutil.h | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8.1/include/coreutil.h b/libraries/irrlicht-1.8.1/include/coreutil.h new file mode 100644 index 0000000..2ab4855 --- /dev/null +++ b/libraries/irrlicht-1.8.1/include/coreutil.h | |||
@@ -0,0 +1,188 @@ | |||
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 | |||
16 | /*! \file coreutil.h | ||
17 | \brief File containing useful basic utility functions | ||
18 | */ | ||
19 | |||
20 | // ----------- some basic quite often used string functions ----------------- | ||
21 | |||
22 | //! search if a filename has a proper extension | ||
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 | |||
39 | //! search if a filename has a proper extension | ||
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 | |||
48 | //! cut the filename extension from a source file path and store it in a dest file path | ||
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 | |||
56 | //! get the filename extension from a file path | ||
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 | |||
67 | //! delete path from filename | ||
68 | inline io::path& deletePathFromFilename(io::path& filename) | ||
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 | |||
86 | //! trim paths | ||
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 | |||
113 | //! looks if file is in the same directory of path. returns offset of directory. | ||
114 | //! 0 means in same directory. 1 means file is direct child of path | ||
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 | |||
176 | //! some standard function ( to remove dependencies ) | ||
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 | ||