1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | |
---|
30 | // Emulate _findfirst, _findnext on non-Windows platforms |
---|
31 | |
---|
32 | |
---|
33 | #include "OgreSearchOps.h" |
---|
34 | #include <stdio.h> |
---|
35 | #include <ctype.h> |
---|
36 | |
---|
37 | /* Win32 directory operations emulation */ |
---|
38 | #if OGRE_PLATFORM != OGRE_PLATFORM_WIN32 |
---|
39 | |
---|
40 | #include "OgreNoMemoryMacros.h" |
---|
41 | |
---|
42 | struct _find_search_t |
---|
43 | { |
---|
44 | char *pattern; |
---|
45 | char *curfn; |
---|
46 | char *directory; |
---|
47 | int dirlen; |
---|
48 | DIR *dirfd; |
---|
49 | }; |
---|
50 | |
---|
51 | long _findfirst(const char *pattern, struct _finddata_t *data) |
---|
52 | { |
---|
53 | _find_search_t *fs = new _find_search_t; |
---|
54 | fs->curfn = NULL; |
---|
55 | fs->pattern = NULL; |
---|
56 | |
---|
57 | // Separate the mask from directory name |
---|
58 | const char *mask = strrchr (pattern, '/'); |
---|
59 | if (mask) |
---|
60 | { |
---|
61 | fs->dirlen = mask - pattern; |
---|
62 | mask++; |
---|
63 | fs->directory = (char *)malloc (fs->dirlen + 1); |
---|
64 | memcpy (fs->directory, pattern, fs->dirlen); |
---|
65 | fs->directory [fs->dirlen] = 0; |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | mask = pattern; |
---|
70 | fs->directory = strdup ("."); |
---|
71 | fs->dirlen = 1; |
---|
72 | } |
---|
73 | |
---|
74 | fs->dirfd = opendir (fs->directory); |
---|
75 | if (!fs->dirfd) |
---|
76 | { |
---|
77 | _findclose ((long)fs); |
---|
78 | return -1; |
---|
79 | } |
---|
80 | |
---|
81 | /* Hack for "*.*" -> "*' from DOS/Windows */ |
---|
82 | if (strcmp (mask, "*.*") == 0) |
---|
83 | mask += 2; |
---|
84 | fs->pattern = strdup (mask); |
---|
85 | |
---|
86 | /* Get the first entry */ |
---|
87 | if (_findnext ((long)fs, data) < 0) |
---|
88 | { |
---|
89 | _findclose ((long)fs); |
---|
90 | return -1; |
---|
91 | } |
---|
92 | |
---|
93 | return (long)fs; |
---|
94 | } |
---|
95 | |
---|
96 | int _findnext(long id, struct _finddata_t *data) |
---|
97 | { |
---|
98 | _find_search_t *fs = (_find_search_t *)id; |
---|
99 | |
---|
100 | /* Loop until we run out of entries or find the next one */ |
---|
101 | dirent *entry; |
---|
102 | for (;;) |
---|
103 | { |
---|
104 | if (!(entry = readdir (fs->dirfd))) |
---|
105 | return -1; |
---|
106 | |
---|
107 | /* See if the filename matches our pattern */ |
---|
108 | if (fnmatch (fs->pattern, entry->d_name, 0) == 0) |
---|
109 | break; |
---|
110 | } |
---|
111 | |
---|
112 | if (fs->curfn) |
---|
113 | free (fs->curfn); |
---|
114 | data->name = fs->curfn = strdup (entry->d_name); |
---|
115 | |
---|
116 | size_t namelen = strlen (entry->d_name); |
---|
117 | char *xfn = new char [fs->dirlen + 1 + namelen + 1]; |
---|
118 | sprintf (xfn, "%s/%s", fs->directory, entry->d_name); |
---|
119 | |
---|
120 | /* stat the file to get if it's a subdir and to find its length */ |
---|
121 | struct stat stat_buf; |
---|
122 | if (stat (xfn, &stat_buf)) |
---|
123 | { |
---|
124 | // Hmm strange, imitate a zero-length file then |
---|
125 | data->attrib = _A_NORMAL; |
---|
126 | data->size = 0; |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | if (S_ISDIR(stat_buf.st_mode)) |
---|
131 | data->attrib = _A_SUBDIR; |
---|
132 | else |
---|
133 | /* Default type to a normal file */ |
---|
134 | data->attrib = _A_NORMAL; |
---|
135 | |
---|
136 | data->size = stat_buf.st_size; |
---|
137 | } |
---|
138 | |
---|
139 | delete [] xfn; |
---|
140 | |
---|
141 | /* Files starting with a dot are hidden files in Unix */ |
---|
142 | if (data->name [0] == '.') |
---|
143 | data->attrib |= _A_HIDDEN; |
---|
144 | |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | int _findclose(long id) |
---|
149 | { |
---|
150 | int ret; |
---|
151 | _find_search_t *fs = (_find_search_t *)id; |
---|
152 | |
---|
153 | ret = fs->dirfd ? closedir (fs->dirfd) : 0; |
---|
154 | free (fs->pattern); |
---|
155 | free (fs->directory); |
---|
156 | if (fs->curfn) |
---|
157 | free (fs->curfn); |
---|
158 | delete fs; |
---|
159 | |
---|
160 | return ret; |
---|
161 | } |
---|
162 | |
---|
163 | #include "OgreMemoryMacros.h" |
---|
164 | #endif |
---|