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

Last change on this file since 56 was 56, checked in by Sam Hocevar, 11 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 2.1 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
29#include "jmalloc.hpp"
30
31void get_directory(char *path, char **&files, int &tfiles, char **&dirs, int &tdirs)
32{
33        struct dirent *de;
34        files = NULL;
35        dirs = NULL;
36        tfiles = 0;
37        tdirs = 0;
38        DIR *d = opendir( path );
39
40        if( !d )
41                return;
42
43        char **tlist = NULL;
44        int t = 0;
45        char curdir[200];
46        getcwd( curdir, 200 );
47        chdir( path );
48
49        do
50        {
51                de = readdir( d );
52                if( de )
53                {
54                        t++;
55                        tlist = (char **)jrealloc(tlist,sizeof(char *)*t,"tmp file list");
56                        tlist[t-1] = strcpy((char *)jmalloc(strlen(de->d_name)+1,"tmp file name"),de->d_name);     
57                }
58        } while( de );
59        closedir( d );
60
61        for( int i=0; i < t; i++ )
62        {
63                d = opendir( tlist[i] );
64                if( d )
65                {
66                        tdirs++;
67                        dirs = (char **)jrealloc(dirs,sizeof(char *)*tdirs,"dir list");
68                        dirs[tdirs-1] = strcpy((char *)jmalloc(strlen(tlist[i])+1,"tmp file name"),tlist[i]);
69                        closedir( d );
70                }
71                else
72                {
73                        tfiles++;
74                        files = (char **)jrealloc(files,sizeof(char *)*tfiles,"dir list");
75                        files[tfiles-1] = strcpy((char *)jmalloc(strlen(tlist[i])+1,"tmp file name"),tlist[i]);
76                }
77                jfree( tlist[i] );
78        }
79        if( t )
80                jfree( tlist );
81        chdir( curdir );
82}
Note: See TracBrowser for help on using the repository browser.