1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* PPRacer
* Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
*
* Copyright (C) 1999-2001 Jasmin F. Patry
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "ppracer.h"
#include "file_util.h"
#include "string_util.h"
#if defined( __CYGWIN__ )
# include <sys/cygwin.h>
#endif
#ifndef MAX_PATH
# ifdef PATH_MAX
# define MAX_PATH PATH_MAX
# else
# define MAX_PATH 8192 /* this ought to be more than enough */
# endif
#endif
static void convert_path( char *new_path, char *orig_path )
{
#if defined( __CYGWIN__ )
cygwin_conv_to_posix_path( orig_path, new_path );
#else
strcpy( new_path, orig_path );
#endif /* defined( __CYGWIN__ ) */
}
bool file_exists( char *filename )
{
#if defined( WIN32 ) && !defined( __CYGWIN__ )
/* Test existence by opening file -- I'm not a Win32 programmer,
so if there's a better way let me know */
FILE *file;
file = fopen( filename, "r" );
if ( file == NULL ) {
return false;
} else {
if ( fclose( file ) != 0 ) {
handle_error( 1, "error closing file %s", filename );
}
return true;
}
#else
/* Unix/Linux/Cygwin */
struct stat stat_info;
bool file_exists = false;
char filename_copy[MAX_PATH];
convert_path( filename_copy, filename );
if ( stat( filename_copy, &stat_info ) != 0 ) {
if ( errno != ENOENT ) {
handle_system_error(1, "couldn't stat %s", filename_copy);
}
file_exists = false;
} else {
file_exists = true;
}
return file_exists;
#endif /* defined( WIN32 ) && !defined( __CYGWIN__ ) */
}
bool dir_exists( char *dirname )
{
#if defined( WIN32 ) && !defined( __CYGWIN__ )
/* Win32 */
char curdir[MAX_PATH];
bool dir_exists = false;
if ( getcwd( curdir, BUFF_LEN - 1 ) == NULL ) {
handle_system_error( 1, "getcwd failed" );
}
if ( chdir( dirname ) == -1 ) {
return false;
}
if ( chdir( curdir ) == -1 ) {
handle_system_error( 1, "Couldn't access directory %s", curdir );
}
return true;
#else
/* Unix/Linux/Cygwin */
char dir_copy[MAX_PATH];
DIR *d;
convert_path( dir_copy, dirname );
if ( ( d = opendir( dir_copy ) ) == NULL ) {
return bool ((errno != ENOENT) && (errno != ENOTDIR));
}
if ( closedir( d ) != 0 ) {
handle_system_error( 1, "Couldn't close directory %s", dirname );
}
return true;
#endif /* defined( WIN32 ) && !defined( __CYGWIN__ ) */
}
std::list<char*>* get_dir_file_list( char *dirname )
{
#if defined ( WIN32 ) && !defined( __CYGWIN__ )
/* Win32 */
std::list<char*> *dirList = new std::list<char*>();
char curdir[MAX_PATH];
HANDLE hFind;
WIN32_FIND_DATA finddata;
if ( getcwd( curdir, BUFF_LEN - 1 ) == NULL ) {
handle_system_error( 1, "getcwd failed" );
}
if ( chdir( dirname ) == -1 ) {
return dirList;
}
if ( ( hFind = FindFirstFile( "*.*", &finddata ) ) ==
INVALID_HANDLE_VALUE )
{
return dirList;
}
do {
dirList->push_back(string_copy( finddata.cFileName ));
} while ( FindNextFile( hFind, &finddata ) );
if ( !FindClose( hFind ) ) {
handle_system_error( 1, "Couldn't close directory %s", dirname );
}
if ( chdir( curdir ) == -1 ) {
handle_system_error( 1, "Couldn't access directory %s", curdir );
}
return dirList;
#else
/* Unix/Linux/Cygwin */
DIR *dir_stream;
std::list<char*> *dirList = new std::list<char*>();
struct dirent* cur_entry;
char dir_copy[MAX_PATH];
convert_path( dir_copy, dirname );
dir_stream = opendir( dir_copy );
if ( dir_stream == NULL ) {
return dirList;
}
while ( ( cur_entry = readdir( dir_stream ) ) != NULL ) {
dirList->push_back(string_copy( cur_entry->d_name ));
}
if ( closedir( dir_stream ) != 0 ) {
handle_system_error( 1, "Couldn't close directory %s", dirname );
}
return dirList;
#endif /* defined ( WIN32 ) && !defined( __CYGWIN__ ) */
}
void free_dir_file_list( std::list<char*>* dirList )
{
std::list<char*>::iterator it;
for(it=dirList->begin(); it!=dirList->end(); it++){
free( *it );
}
delete dirList;
}
|