Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

FileScan.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library 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 GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: FileScan.cpp,v 1.6 2002/02/16 16:38:11 tksuoran Exp $
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     //  Scan for end of string
00054     while( *walker != 0 ){
00055         walker++;
00056     }
00057 
00058     //  Scan to last /, if any
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 // FIXME: this could use some better error handling...
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         //fprintf(  stderr, "glob(3) failed for \"%s\": %s\n", pattern, strerror(errno)  );
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 };  //  namespace SysSupport
00135 };  //  namespace Teddy
00136