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

Revision 692, 3.8 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
26// Emulate _findfirst, _findnext on non-Windows platforms
27
28
29#include "OgreSearchOps.h"
30#include <stdio.h>
31#include <ctype.h>
32
33/* Win32 directory operations emulation */
34#if OGRE_PLATFORM != OGRE_PLATFORM_WIN32
35
36#include "OgreNoMemoryMacros.h"
37
38/* If we've initialized yet */
39static int G_searches_initialized = 0;
40
41/* The possible searches */
42static struct _find_search_t G_find_searches[MAX_FIND_SEARCHES];
43
44long _findfirst(const char *pattern, struct _finddata_t *data)
45{
46    long find_key = 0;
47   
48    /* Initialize the system if it's needed */
49    if (!G_searches_initialized)
50    {
51        int x;
52       
53        for (x = 0; x < MAX_FIND_SEARCHES; x++)
54        {
55            G_find_searches[x].in_use = 0;
56        }
57
58        G_searches_initialized = 1;
59    }
60
61    /* See if we have an available search slot */
62    for (find_key = 0; find_key < MAX_FIND_SEARCHES; find_key++)
63    {
64        if (!G_find_searches[find_key].in_use)
65            break;
66    }
67
68    if (find_key == MAX_FIND_SEARCHES)
69    {
70        /* uhoh, no more slots available */
71        return -1;
72    }
73    else
74    {
75        /* We're using the slot */
76        G_find_searches[find_key].in_use = 1;
77    }
78
79    if ( !(G_find_searches[find_key].dirfd = opendir(".")) )
80        return -1;
81
82    /* Hack for *.* from DOS/Windows */
83    if (strcmp(pattern, "*.*") == 0)
84        G_find_searches[find_key].pattern = strdup("*");
85    else
86        G_find_searches[find_key].pattern = strdup(pattern);
87
88    /* Get the first entry */
89    if (_findnext(find_key, data) < 0)
90    {
91        data = NULL;
92        _findclose(find_key);
93        return -1;
94    }
95
96    return find_key;
97}
98
99int _findnext(long id, struct _finddata_t *data)
100{
101    struct dirent *entry;
102    struct stat stat_buf;
103
104    /* Loop until we run out of entries or find the next one */
105    do
106    {
107        entry = readdir(G_find_searches[id].dirfd);
108
109        if (entry == NULL)
110            return -1;
111
112        /* See if the filename matches our pattern */
113        if (fnmatch(G_find_searches[id].pattern, entry->d_name, 0) == 0)
114            break;
115    } while ( entry != NULL );
116
117    data->name = entry->d_name;
118
119    /* Default type to a normal file */
120    data->attrib = _A_NORMAL;
121   
122    /* stat the file to get if it's a subdir */
123    stat(data->name, &stat_buf);
124    if (S_ISDIR(stat_buf.st_mode))
125    {
126        data->attrib = _A_SUBDIR;
127    }
128
129    data->size = stat_buf.st_size;
130
131    return 0;
132}
133
134int _findclose(long id)
135{
136    int ret;
137   
138    ret = closedir(G_find_searches[id].dirfd);
139    free(G_find_searches[id].pattern);
140    G_find_searches[id].in_use = 0;
141
142    return ret;
143}
144
145#include "OgreMemoryMacros.h"
146#endif
Note: See TracBrowser for help on using the repository browser.