Back to index

extremetuxracer  0.5beta
Defines | Functions
file_util.cpp File Reference
#include "etracer.h"
#include "file_util.h"
#include "string_util.h"

Go to the source code of this file.

Defines

#define MAX_PATH   8192 /* this ought to be more than enough */

Functions

static void convert_path (char *new_path, char *orig_path)
bool file_exists (char *filename)
bool dir_exists (char *dirname)
std::list< char * > * get_dir_file_list (char *dirname)
void free_dir_file_list (std::list< char * > *dirList)

Define Documentation

#define MAX_PATH   8192 /* this ought to be more than enough */

Definition at line 34 of file file_util.cpp.


Function Documentation

static void convert_path ( char *  new_path,
char *  orig_path 
) [static]

Definition at line 38 of file file_util.cpp.

{
#if defined( __CYGWIN__ )
    cygwin_conv_to_posix_path( orig_path, new_path );
#else
    strcpy( new_path, orig_path );
#endif /* defined( __CYGWIN__ ) */
}

Here is the caller graph for this function:

bool dir_exists ( char *  dirname)

Definition at line 90 of file file_util.cpp.

{
#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__ ) */
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool file_exists ( char *  filename)

Definition at line 47 of file file_util.cpp.

{
#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__ ) */
}

Here is the call graph for this function:

Here is the caller graph for this function:

void free_dir_file_list ( std::list< char * > *  dirList)

Definition at line 201 of file file_util.cpp.

{
       std::list<char*>::iterator it;
       for(it=dirList->begin(); it!=dirList->end(); it++){
              free( *it );
       }
    delete dirList;
}
std::list<char*>* get_dir_file_list ( char *  dirname)

Definition at line 134 of file file_util.cpp.

{
       #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__ ) */
}

Here is the call graph for this function: