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

Last change on this file since 494 was 494, checked in by Sam Hocevar, 12 years ago

style: remove trailing spaces, fix copyright statements.

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 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software Foundation,
18 *  Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 */
20
21#include "config.h"
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <sys/types.h>
28#include <dirent.h>
29#include <unistd.h>
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 **)realloc(tlist,sizeof(char *)*t);
56            tlist[t-1] = strdup(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 **)realloc(dirs,sizeof(char *)*tdirs);
68            dirs[tdirs-1] = strdup(tlist[i]);
69            closedir( d );
70        }
71        else
72        {
73            tfiles++;
74            files = (char **)realloc(files,sizeof(char *)*tfiles);
75            files[tfiles-1] = strdup(tlist[i]);
76        }
77        free( tlist[i] );
78    }
79    if( t )
80        free( tlist );
81    chdir( curdir );
82}
Note: See TracBrowser for help on using the repository browser.