00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Teddy/SysSupport/FileScan.h"
00026
00027 #ifndef SWIG
00028 #if defined( _WIN32 )
00029 # include <windows.h>
00030 #else
00031 # include <cstdio>
00032 # include <sys/types.h>
00033 # include <errno.h>
00034 # include <glob.h>
00035 #endif
00036 #endif
00037
00038
00039 namespace Teddy {
00040 namespace SysSupport {
00041
00042
00043 const char *fix_file_name( const char *prefix, const char *file_name ){
00044 if( strlen(file_name)==0 ){
00045 return NULL;
00046 }
00047 if( file_name == NULL ){
00048 return NULL;
00049 }
00050
00051 const char *walker = file_name;
00052
00053
00054 while( *walker != 0 ){
00055 walker++;
00056 }
00057
00058
00059 while( walker != file_name ){
00060 walker--;
00061 if( *walker == '/' || *walker == '\\' ){
00062 walker++;
00063 break;
00064 }
00065 }
00066
00067 if( prefix != NULL ){
00068 const char *fixed_file_name = walker;
00069 int final_length = strlen( fixed_file_name ) + strlen( prefix ) + 1;
00070 char *final_file_name = new char[ final_length];
00071 sprintf( final_file_name, "%s%s", prefix, fixed_file_name );
00072 return final_file_name;
00073 }else{
00074 return walker+1;
00075 }
00076 }
00077
00078
00079 #if defined( _WIN32 ) // PLATFORM SELECTION - WIN32
00080
00081
00082 FileScan::FileScan( const char *mask ){
00083 WIN32_FIND_DATA fData;
00084 HANDLE search;
00085
00086 search = FindFirstFile( mask, &fData );
00087
00088 if( search == INVALID_HANDLE_VALUE ){
00089 return;
00090 }
00091
00092 do{
00093 char *fnam = new char[ strlen(fData.cFileName)+2 ];
00094 strcpy(fnam, fData.cFileName );
00095 files.push_back(fnam);
00096 }while( FindNextFile( search, &fData ) );
00097 }
00098
00099
00100 #elif 1 // PLATFORM SELECTION - ASSUME POSIX
00101
00102
00103
00104 FileScan::FileScan( const char *pattern ) : files() {
00105 glob_t g;
00106
00107 # ifdef GLOB_TILDE
00108 int r = glob( pattern, GLOB_MARK|GLOB_TILDE, NULL, &g );
00109 # else
00110 int r = glob( pattern, GLOB_MARK, NULL, &g );
00111 # endif
00112
00113 if( r!=0 ){
00114
00115 exit( EXIT_FAILURE );
00116 }
00117
00118 for( int i=0; (long)(i)<(long)(g.gl_pathc); i++ ){
00119 char *fnam = new char[ strlen(g.gl_pathv[i]) + 2 ];
00120 strcpy(fnam, g.gl_pathv[i] );
00121 files.push_back(fnam);
00122 }
00123 globfree( &g );
00124 }
00125
00126
00127 #else
00128
00129 #error "Target has no implementation for FileScan class"
00130
00131 #endif // PLATFORM_SELECTION
00132
00133
00134 };
00135 };
00136