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

win32.cpp

00001 //FILE:         win32.cpp
00002 //AUTHOR:       Nathan Cournia <nathan@cournia.com>
00003 //DESCRIPTION:  Windows compatable utility functions.
00004 //\todo Actually write the win32 utils implementation.
00005 
00006 #ifdef WIN32
00007 #define WIN32_LEAN_AND_MEAN
00008 #include <windows.h>
00009 #endif
00010 
00011 #include <sys/stat.h>
00012 #include <vector>
00013 #include <cerrno>
00014 #include <cstring>
00015 #include "utils.h"
00016 
00018 bool
00019 utils::mkdir( const std::string& filename )
00020 {
00021         return false;
00022 }
00023 
00025 bool
00026 utils::exists( const std::string& filename )
00027 {
00028         return false;
00029 }
00030 
00032 bool
00033 utils::can_read( const std::string& filename )
00034 {
00035         /* this is actually pretty portable
00036         if( !(access( filename.c_str( ), R_OK ) == 0) ) {
00037                 set_error( ::strerror( errno ) );
00038                 return false;
00039         }
00040         clear_error( );
00041         return true;
00042         */
00043         return false;
00044 }
00045 
00047 bool
00048 utils::is_dir( const std::string& filename )
00049 {
00050         return false;
00051 }
00052 
00054 bool
00055 utils::is_link( const std::string& filename )
00056 {
00057         return false;
00058 }
00059 
00061 bool
00062 utils::chdir( const std::string& filename )
00063 {
00064         return false;
00065 }
00066 
00068 std::string
00069 utils::get_valid_absolute_dir( const std::string& file )
00070 {
00071         //trim whitespace
00072         std::string filename = file;
00073         filename = utils::trim( utils::rtrim( filename ) );
00074 
00075         //make sure the string is not empty
00076         if( filename.length( ) < 1 ) return "";
00077         
00078         //is this file path absolute
00079         if( filename[ 0 ] != '/' ) {
00080                 //relative path, get cwd
00081                 char cwd[ 1024 ]; //hope this is enough
00082                 //FIXME getcwd( cwd, 1024 );
00083 
00084                 //add relative path to full path
00085                 filename = std::string( cwd ) + "/" + filename;
00086         }
00087 
00088         //create a stack
00089         std::vector<std::string> stack;
00090 
00091         //place dirs on stack
00092         std::string::size_type start = 0;
00093         std::string::size_type pos = 0;
00094         std::string token;
00095 
00096         //delimit by "/"
00097         while( (pos = filename.find_first_of( "/", start )) != std::string::npos ) {
00098                 token = filename.substr( start, pos - start );
00099                 if( token == ".." ) {
00100                         //go back a dir, pop the stack
00101                         if( stack.size( ) > 0 ) {
00102                                 stack.pop_back( );
00103                         }
00104                 } else if( (token.length( ) > 0) && (token != ".") ) {
00105                         //place token on stack
00106                         stack.push_back( token );
00107                 }
00108 
00109                 //move current string position
00110                 start = pos + 1;
00111         }
00112 
00113         //there still may be a dir at the end
00114         if( start < filename.length( ) ) {
00115                 token = filename.substr( start, filename.length( ) - start );
00116                 if( token == ".." ) {
00117                         //go back a dir, pop the stack
00118                         if( stack.size( ) > 0 ) {
00119                                 stack.pop_back( );
00120                         }
00121                 } else if( (token.length( ) > 0) && (token != ".") ) {
00122                         //place token on stack
00123                         stack.push_back( token );
00124                 }
00125         }
00126 
00127         //if the stack size is greater than one, see if the dir
00128         filename = "";
00129         if( stack.size( ) > 1 ) {
00130                 std::string old;
00131                 for( unsigned int i = 0; i < stack.size( ); ++i ) {
00132                         old = filename;
00133                         filename += "/" + stack[ i ];
00134                         if( !(utils::is_dir( filename.c_str( ) )) ) {
00135                                 //not a dir, go back to old as the last valid dir
00136                                 if( old.length( ) < 1 ) {
00137                                         return "/";
00138                                 } else {
00139                                         return old;
00140                                 }
00141                         }
00142                 }
00143                 
00144                 //the whole string was a dir
00145                 return filename;
00146         } else if( stack.size( ) == 1 ) {
00147                 //looks like the file looks like /filename
00148                 filename += "/" + stack[ 0 ];
00149                 if( utils::is_dir( filename.c_str( ) ) ) {
00150                         return filename;
00151                 } else {
00152                         return "/";
00153                 }
00154         } else {
00155                 //fuck, this shouldn't happen
00156                 return "";
00157         }
00158 }
00159 
00161 std::string
00162 utils::get_absolute_dir( const std::string& file )
00163 {
00164         //trim whitespace
00165         std::string filename = file;
00166         filename = utils::trim( utils::rtrim( filename ) );
00167 
00168         //make sure the string is not empty
00169         if( filename.length( ) < 1 ) return "";
00170         
00171         //is this file path absolute
00172         if( filename[ 0 ] != '/' ) {
00173                 //relative path, get cwd
00174                 char cwd[ 1024 ]; //hope this is enough
00175                 //FIXME getcwd( cwd, 1024 );
00176 
00177                 //add relative path to full path
00178                 filename = std::string( cwd ) + "/" +  filename;
00179         }
00180 
00181         //create a stack
00182         std::vector<std::string> stack;
00183 
00184         //place dirs on stack
00185         std::string::size_type start = 0;
00186         std::string::size_type pos = 0;
00187         std::string token;
00188 
00189         //delimit by "/"
00190         while( (pos = filename.find_first_of( "/", start )) != std::string::npos ) {
00191                 token = filename.substr( start, pos - start );
00192                 if( token == ".." ) {
00193                         //go back a dir, pop the stack
00194                         if( stack.size( ) > 0 ) {
00195                                 stack.pop_back( );
00196                         }
00197                 } else if( (token.length( ) > 0) && (token != ".") ) {
00198                         //place token on stack
00199                         stack.push_back( token );
00200                 }
00201 
00202                 //move current string position
00203                 start = pos + 1;
00204         }
00205 
00206         //there still may be a dir at the end
00207         if( start < filename.length( ) ) {
00208                 token = filename.substr( start, filename.length( ) - start );
00209                 if( token == ".." ) {
00210                         //go back a dir, pop the stack
00211                         if( stack.size( ) > 0 ) {
00212                                 stack.pop_back( );
00213                         }
00214                 } else if( (token.length( ) > 0) && (token != ".") ) {
00215                         //place token on stack
00216                         stack.push_back( token );
00217                 }
00218         }
00219 
00220         filename = "";
00221         if( stack.size( ) > 1 ) {
00222                 for( unsigned int i = 0; i < stack.size( ) - 1; ++i ) {
00223                         filename += "/" + stack[ i ];
00224                 }
00225                 return filename;
00226         } else {
00227                 return "/";
00228         }
00229 }
00230 
00232 std::string
00233 utils::get_filename( const std::string& file )
00234 {
00235         std::string::size_type pos = file.find_last_of( "/", file.length( ) );
00236         if( pos == std::string::npos ) {
00237                 //no slashes
00238                 return file;
00239         } else {
00240                 return file.substr( pos + 1, file.length( ) - (pos + 1) );
00241         }
00242 }

Generated on Tue Feb 11 18:49:42 2003 for uber by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002