source: abuse/trunk/src/sdlport/jdir.cpp @ 130

Last change on this file since 130 was 129, checked in by Sam Hocevar, 14 years ago
  • Get rid of jmalloc and replace it with standard malloc. Modern operating systems certainly perform a lot better than this custom implementation, and we have superior tools (eg. valgrind) to debug and profile memory usage without interfering with the code itself.
File size: 2.2 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au>
4 *
5 *  This program is free software; you can redistribute it and/or modify
6 *  it under the terms of the GNU General Public License as published by
7 *  the Free Software Foundation; either version 2 of the License, or
8 *  (at your option) any later version.
9 *
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU General Public License for more details.
14 *
15 *  You should have received a copy of the GNU General Public License
16 *  along with this program; if not, write to the Free Software Foundation,
17 *  Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
18 */
19
20#include "config.h"
21
22#include <sys/types.h>
23#include <stdio.h>
24#include <dirent.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28
29void get_directory(char *path, char **&files, int &tfiles, char **&dirs, int &tdirs)
30{
31    struct dirent *de;
32    files = NULL;
33    dirs = NULL;
34    tfiles = 0;
35    tdirs = 0;
36    DIR *d = opendir( path );
37
38    if( !d )
39        return;
40
41    char **tlist = NULL;
42    int t = 0;
43    char curdir[200];
44    getcwd( curdir, 200 );
45    chdir( path );
46
47    do
48    {
49        de = readdir( d );
50        if( de )
51        {
52            t++;
53            tlist = (char **)realloc(tlist,sizeof(char *)*t);
54            tlist[t-1] = strcpy((char *)malloc(strlen(de->d_name)+1),de->d_name);
55        }
56    } while( de );
57    closedir( d );
58
59    for( int i=0; i < t; i++ )
60    {
61        d = opendir( tlist[i] );
62        if( d )
63        {
64            tdirs++;
65            dirs = (char **)realloc(dirs,sizeof(char *)*tdirs);
66            dirs[tdirs-1] = strcpy((char *)malloc(strlen(tlist[i])+1),tlist[i]);
67            closedir( d );
68        }
69        else
70        {
71            tfiles++;
72            files = (char **)realloc(files,sizeof(char *)*tfiles);
73            files[tfiles-1] = strcpy((char *)malloc(strlen(tlist[i])+1),tlist[i]);
74        }
75        free( tlist[i] );
76    }
77    if( t )
78        free( tlist );
79    chdir( curdir );
80}
Note: See TracBrowser for help on using the repository browser.