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
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au>
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]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
[2]23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
[131]26
27#include <sys/types.h>
28#include <dirent.h>
[2]29#include <unistd.h>
[56]30
[2]31void get_directory(char *path, char **&files, int &tfiles, char **&dirs, int &tdirs)
32{
[124]33    struct dirent *de;
34    files = NULL;
35    dirs = NULL;
36    tfiles = 0;
37    tdirs = 0;
38    DIR *d = opendir( path );
[2]39
[124]40    if( !d )
41        return;
[2]42
[124]43    char **tlist = NULL;
44    int t = 0;
45    char curdir[200];
46    getcwd( curdir, 200 );
47    chdir( path );
[2]48
[124]49    do
50    {
51        de = readdir( d );
52        if( de )
53        {
54            t++;
[129]55            tlist = (char **)realloc(tlist,sizeof(char *)*t);
[131]56            tlist[t-1] = strdup(de->d_name);
[124]57        }
58    } while( de );
59    closedir( d );
[2]60
[124]61    for( int i=0; i < t; i++ )
62    {
63        d = opendir( tlist[i] );
64        if( d )
65        {
66            tdirs++;
[129]67            dirs = (char **)realloc(dirs,sizeof(char *)*tdirs);
[131]68            dirs[tdirs-1] = strdup(tlist[i]);
[124]69            closedir( d );
70        }
71        else
72        {
73            tfiles++;
[129]74            files = (char **)realloc(files,sizeof(char *)*tfiles);
[131]75            files[tfiles-1] = strdup(tlist[i]);
[124]76        }
[129]77        free( tlist[i] );
[124]78    }
79    if( t )
[129]80        free( tlist );
[124]81    chdir( curdir );
[2]82}
Note: See TracBrowser for help on using the repository browser.