source: OGRE/trunk/ogrenew/OgreMain/src/OgreFileSystem.cpp @ 692

Revision 692, 9.5 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreStableHeaders.h"
26#include "OgreFileSystem.h"
27#include "OgreLogManager.h"
28#include "OgreException.h"
29#include "OgreStringVector.h"
30#include "OgreRoot.h"
31
32#include <sys/types.h>
33#include <sys/stat.h>
34
35#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE
36#   include "OgreSearchOps.h"
37#   include <sys/param.h>
38#   define MAX_PATH MAXPATHLEN
39#endif
40
41#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
42#   include <windows.h>
43#   include <direct.h>
44#   include <io.h>
45#endif
46
47namespace Ogre {
48
49    //-----------------------------------------------------------------------
50    FileSystemArchive::FileSystemArchive(const String& name, const String& archType )
51        : Archive(name, archType)
52    {
53    }
54    //-----------------------------------------------------------------------
55    bool FileSystemArchive::isCaseSensitive(void) const
56    {
57        #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
58            return false;
59        #else
60            return true;
61        #endif
62
63    }
64    //-----------------------------------------------------------------------
65    void FileSystemArchive::findFiles(const String& pattern, bool recursive,
66        StringVector* simpleList, FileInfoList* detailList,
67        const String& currentDir)
68    {
69                // parsing requires locking due to saved returns
70                OGRE_LOCK_AUTO_MUTEX
71
72        long lHandle, res;
73        struct _finddata_t tagData;
74
75        lHandle = _findfirst(pattern.c_str(), &tagData);
76        res = 0;
77        while (lHandle != -1 && res != -1)
78        {
79            if(!(tagData.attrib & _A_SUBDIR))
80            {
81                if (simpleList)
82                {
83                    simpleList->push_back(currentDir + tagData.name);
84                }
85                else if (detailList)
86                {
87                    FileInfo fi;
88                                        fi.archive = this;
89                    fi.filename = currentDir + tagData.name;
90                    fi.basename = tagData.name;
91                    fi.path = currentDir;
92                    fi.compressedSize = tagData.size;
93                    fi.uncompressedSize = tagData.size;
94                    detailList->push_back(fi);
95                }
96            }
97            res = _findnext( lHandle, &tagData );
98        }
99        // Close if we found any files
100        if(lHandle != -1)
101        {
102            _findclose(lHandle);
103        }
104
105        // Now find directories
106        if (recursive)
107        {
108
109            lHandle = _findfirst("*", &tagData);
110            res = 0;
111            while (lHandle != -1 && res != -1)
112            {
113                if((tagData.attrib & _A_SUBDIR)
114                    && strcmp(tagData.name, ".")
115                    && strcmp(tagData.name, ".."))
116                {
117                    // recurse
118                    String dir = currentDir + tagData.name + "/";
119                    pushDirectory(tagData.name);
120                    findFiles(pattern, recursive, simpleList, detailList, dir);
121                    popDirectory();
122                }
123                res = _findnext( lHandle, &tagData );
124            }
125            // Close if we found any files
126            if(lHandle != -1)
127            {
128                _findclose(lHandle);
129            }
130
131        }
132
133    }
134    //-----------------------------------------------------------------------
135    void FileSystemArchive::changeDirectory(const String& dir) const
136    {
137        if(chdir(dir.c_str()) == -1)
138        {
139            OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND,
140                "Cannot open requested directory " + dir,
141                "FileSystemArchive::changeDirectory");
142        }
143    }
144    //-----------------------------------------------------------------------
145    void FileSystemArchive::pushDirectory(const String& dir) const
146    {
147        // get current directory and push it onto the stack
148        getcwd(mTmpPath, OGRE_MAX_PATH);
149        mDirectoryStack.push_back(String(mTmpPath));
150        changeDirectory(dir);
151
152    }
153    //-----------------------------------------------------------------------
154    void FileSystemArchive::popDirectory(void) const
155    {
156        if (mDirectoryStack.empty())
157        {
158            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
159                "No directories left in the stack.",
160                "FileSystemArchive::popDirectory");
161        }
162        changeDirectory(mDirectoryStack.back());
163        mDirectoryStack.pop_back();
164
165    }
166    //-----------------------------------------------------------------------
167    FileSystemArchive::~FileSystemArchive()
168    {
169        unload();
170    }
171    //-----------------------------------------------------------------------
172    void FileSystemArchive::load()
173    {
174        mBasePath = mName;
175        // Check we can change to it
176        pushDirectory(mBasePath);
177        // return to previous
178        popDirectory();
179    }
180    //-----------------------------------------------------------------------
181    void FileSystemArchive::unload()
182    {
183        // nothing to see here, move along
184    }
185    //-----------------------------------------------------------------------
186    DataStreamPtr FileSystemArchive::open(const String& filename) const
187    {
188                // directory change requires locking due to saved returns
189                OGRE_LOCK_AUTO_MUTEX
190
191                pushDirectory(mBasePath);
192        // Use filesystem to determine size
193        // (quicker than streaming to the end and back)
194        struct stat tagStat;
195        int ret = stat(filename.c_str(), &tagStat);
196        assert(ret == 0 && "Problem getting file size" );
197
198
199        // Always open in binary mode
200        std::ifstream *origStream = new std::ifstream();
201        origStream->open(filename.c_str(), std::ios::in | std::ios::binary);
202
203        popDirectory();
204        /// Construct return stream, tell it to delete on destroy
205        FileStreamDataStream* stream = new FileStreamDataStream(filename,
206            origStream, tagStat.st_size, true);
207        return DataStreamPtr(stream);
208    }
209    //-----------------------------------------------------------------------
210    StringVectorPtr FileSystemArchive::list(bool recursive)
211    {
212                // directory change requires locking due to saved returns
213                OGRE_LOCK_AUTO_MUTEX
214
215                pushDirectory(mBasePath);
216        StringVectorPtr ret(new StringVector());
217
218        findFiles("*", recursive, ret.getPointer(), 0);
219
220        popDirectory();
221
222        return ret;
223    }
224    //-----------------------------------------------------------------------
225    FileInfoListPtr FileSystemArchive::listFileInfo(bool recursive)
226    {
227                // directory change requires locking due to saved returns
228                OGRE_LOCK_AUTO_MUTEX
229
230        pushDirectory(mBasePath);
231        FileInfoListPtr ret(new FileInfoList());
232
233        findFiles("*", recursive, 0, ret.getPointer());
234
235        popDirectory();
236
237        return ret;
238    }
239    //-----------------------------------------------------------------------
240    StringVectorPtr FileSystemArchive::find(const String& pattern, bool recursive)
241    {
242                // directory change requires locking due to saved returns
243                OGRE_LOCK_AUTO_MUTEX
244
245        pushDirectory(mBasePath);
246        StringVectorPtr ret(new StringVector());
247
248        findFiles(pattern, recursive, ret.getPointer(), 0);
249
250        popDirectory();
251
252        return ret;
253
254    }
255    //-----------------------------------------------------------------------
256    FileInfoListPtr FileSystemArchive::findFileInfo(const String& pattern,
257        bool recursive)
258    {
259                // directory change requires locking due to saved returns
260                OGRE_LOCK_AUTO_MUTEX
261
262        pushDirectory(mBasePath);
263        FileInfoListPtr ret(new FileInfoList());
264
265        findFiles(pattern, recursive, 0, ret.getPointer());
266
267        popDirectory();
268
269        return ret;
270    }
271    //-----------------------------------------------------------------------
272        bool FileSystemArchive::exists(const String& filename)
273        {
274                // directory change requires locking due to saved returns
275                OGRE_LOCK_AUTO_MUTEX
276
277                bool ret;
278        pushDirectory(mBasePath);
279
280        struct stat tagStat;
281        ret = (stat(filename.c_str(), &tagStat) == 0);
282
283                popDirectory();
284
285                return ret;
286               
287        }
288    //-----------------------------------------------------------------------
289    const String& FileSystemArchiveFactory::getType(void) const
290    {
291        static String name = "FileSystem";
292        return name;
293    }
294
295}
Note: See TracBrowser for help on using the repository browser.