Back to index

extremetuxracer  0.5beta
file_util.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  *
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "etracer.h"
00023 #include "file_util.h"
00024 #include "string_util.h"
00025 
00026 #if defined( __CYGWIN__ )
00027 #  include <sys/cygwin.h>
00028 #endif
00029 
00030 #ifndef MAX_PATH
00031 #  ifdef PATH_MAX
00032 #    define MAX_PATH PATH_MAX
00033 #  else
00034 #    define MAX_PATH 8192 /* this ought to be more than enough */
00035 #  endif
00036 #endif
00037 
00038 static void convert_path( char *new_path, char *orig_path ) 
00039 {
00040 #if defined( __CYGWIN__ )
00041     cygwin_conv_to_posix_path( orig_path, new_path );
00042 #else
00043     strcpy( new_path, orig_path );
00044 #endif /* defined( __CYGWIN__ ) */
00045 }
00046 
00047 bool file_exists( char *filename )
00048 {
00049 #if defined( WIN32 ) && !defined( __CYGWIN__ )
00050 
00051     /* Test existence by opening file -- I'm not a Win32 programmer,
00052        so if there's a better way let me know */
00053     FILE *file;
00054     file = fopen( filename, "r" );
00055 
00056     if ( file == NULL ) {
00057        return false;
00058     } else {
00059        if ( fclose( file ) != 0 ) {
00060            handle_error( 1, "error closing file %s", filename );
00061        }
00062        return true;
00063     }
00064 
00065 #else
00066 
00067     /* Unix/Linux/Cygwin */
00068     
00069     struct stat stat_info;
00070     bool file_exists = false;
00071     char filename_copy[MAX_PATH];
00072 
00073     convert_path( filename_copy, filename );
00074 
00075     if ( stat( filename_copy, &stat_info ) != 0 ) {
00076        if ( errno != ENOENT ) {
00077            handle_system_error(1, "couldn't stat %s", filename_copy);
00078        }
00079        file_exists = false;
00080     } else {
00081        file_exists = true;
00082     }
00083 
00084     return file_exists;
00085 
00086 #endif /* defined( WIN32 ) && !defined( __CYGWIN__ ) */
00087 }
00088 
00089 
00090 bool dir_exists( char *dirname )
00091 {
00092 #if defined( WIN32 ) && !defined( __CYGWIN__ )
00093 
00094     /* Win32 */
00095 
00096     char curdir[MAX_PATH];
00097     bool dir_exists = false;
00098 
00099     if ( getcwd( curdir, BUFF_LEN - 1 ) == NULL ) {
00100        handle_system_error( 1, "getcwd failed" );
00101     }
00102 
00103     if ( chdir( dirname ) == -1 ) {
00104        return false;
00105     }
00106 
00107     if ( chdir( curdir ) == -1 ) {
00108        handle_system_error( 1, "Couldn't access directory %s", curdir );
00109     }
00110     return true;
00111 
00112 #else
00113 
00114     /* Unix/Linux/Cygwin */
00115 
00116     char dir_copy[MAX_PATH];
00117     DIR *d;
00118 
00119     convert_path( dir_copy, dirname );
00120 
00121     if ( ( d = opendir( dir_copy ) ) == NULL ) {
00122        return bool ((errno != ENOENT) && (errno != ENOTDIR));
00123     } 
00124 
00125     if ( closedir( d ) != 0 ) {
00126        handle_system_error( 1, "Couldn't close directory %s", dirname );
00127     }
00128 
00129     return true;
00130 
00131 #endif /* defined( WIN32 ) && !defined( __CYGWIN__ ) */
00132 }
00133 
00134 std::list<char*>* get_dir_file_list( char *dirname ) 
00135 {
00136        #if defined ( WIN32 ) && !defined( __CYGWIN__ ) 
00137 
00138     /* Win32 */
00139        std::list<char*> *dirList = new std::list<char*>();
00140     char curdir[MAX_PATH];
00141     HANDLE hFind;
00142     WIN32_FIND_DATA finddata;
00143 
00144     if ( getcwd( curdir, BUFF_LEN - 1 ) == NULL ) {
00145               handle_system_error( 1, "getcwd failed" );
00146     }
00147 
00148     if ( chdir( dirname ) == -1 ) {
00149               return dirList;
00150     }
00151 
00152     if ( ( hFind = FindFirstFile( "*.*", &finddata ) ) == 
00153         INVALID_HANDLE_VALUE ) 
00154     {
00155               return dirList;
00156     }
00157 
00158     do {
00159               dirList->push_back(string_copy( finddata.cFileName ));
00160     } while ( FindNextFile( hFind, &finddata ) );
00161 
00162     if ( !FindClose( hFind ) ) {
00163               handle_system_error( 1, "Couldn't close directory %s", dirname );
00164     }
00165 
00166     if ( chdir( curdir ) == -1 ) {
00167               handle_system_error( 1, "Couldn't access directory %s", curdir );
00168     }
00169 
00170     return dirList;
00171 
00172 #else
00173 
00174     /* Unix/Linux/Cygwin */
00175     DIR *dir_stream;
00176        std::list<char*> *dirList = new std::list<char*>();
00177        
00178     struct dirent* cur_entry;
00179     char dir_copy[MAX_PATH];
00180 
00181     convert_path( dir_copy, dirname );
00182     dir_stream = opendir( dir_copy );
00183 
00184     if ( dir_stream == NULL ) {
00185               return dirList;
00186     }
00187 
00188     while ( ( cur_entry = readdir( dir_stream ) ) != NULL ) {
00189               dirList->push_back(string_copy( cur_entry->d_name ));
00190     }
00191 
00192     if ( closedir( dir_stream ) != 0 ) {
00193               handle_system_error( 1, "Couldn't close directory %s", dirname );
00194     }
00195 
00196     return dirList;
00197 
00198 #endif /* defined ( WIN32 ) && !defined( __CYGWIN__ ) */
00199 }
00200 
00201 void free_dir_file_list(  std::list<char*>* dirList )
00202 {
00203        std::list<char*>::iterator it;
00204        for(it=dirList->begin(); it!=dirList->end(); it++){
00205               free( *it );
00206        }
00207     delete dirList;
00208 }