00001
00002
00003
00004
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
00036
00037
00038
00039
00040
00041
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
00072 std::string filename = file;
00073 filename = utils::trim( utils::rtrim( filename ) );
00074
00075
00076 if( filename.length( ) < 1 ) return "";
00077
00078
00079 if( filename[ 0 ] != '/' ) {
00080
00081 char cwd[ 1024 ];
00082
00083
00084
00085 filename = std::string( cwd ) + "/" + filename;
00086 }
00087
00088
00089 std::vector<std::string> stack;
00090
00091
00092 std::string::size_type start = 0;
00093 std::string::size_type pos = 0;
00094 std::string token;
00095
00096
00097 while( (pos = filename.find_first_of( "/", start )) != std::string::npos ) {
00098 token = filename.substr( start, pos - start );
00099 if( token == ".." ) {
00100
00101 if( stack.size( ) > 0 ) {
00102 stack.pop_back( );
00103 }
00104 } else if( (token.length( ) > 0) && (token != ".") ) {
00105
00106 stack.push_back( token );
00107 }
00108
00109
00110 start = pos + 1;
00111 }
00112
00113
00114 if( start < filename.length( ) ) {
00115 token = filename.substr( start, filename.length( ) - start );
00116 if( token == ".." ) {
00117
00118 if( stack.size( ) > 0 ) {
00119 stack.pop_back( );
00120 }
00121 } else if( (token.length( ) > 0) && (token != ".") ) {
00122
00123 stack.push_back( token );
00124 }
00125 }
00126
00127
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
00136 if( old.length( ) < 1 ) {
00137 return "/";
00138 } else {
00139 return old;
00140 }
00141 }
00142 }
00143
00144
00145 return filename;
00146 } else if( stack.size( ) == 1 ) {
00147
00148 filename += "/" + stack[ 0 ];
00149 if( utils::is_dir( filename.c_str( ) ) ) {
00150 return filename;
00151 } else {
00152 return "/";
00153 }
00154 } else {
00155
00156 return "";
00157 }
00158 }
00159
00161 std::string
00162 utils::get_absolute_dir( const std::string& file )
00163 {
00164
00165 std::string filename = file;
00166 filename = utils::trim( utils::rtrim( filename ) );
00167
00168
00169 if( filename.length( ) < 1 ) return "";
00170
00171
00172 if( filename[ 0 ] != '/' ) {
00173
00174 char cwd[ 1024 ];
00175
00176
00177
00178 filename = std::string( cwd ) + "/" + filename;
00179 }
00180
00181
00182 std::vector<std::string> stack;
00183
00184
00185 std::string::size_type start = 0;
00186 std::string::size_type pos = 0;
00187 std::string token;
00188
00189
00190 while( (pos = filename.find_first_of( "/", start )) != std::string::npos ) {
00191 token = filename.substr( start, pos - start );
00192 if( token == ".." ) {
00193
00194 if( stack.size( ) > 0 ) {
00195 stack.pop_back( );
00196 }
00197 } else if( (token.length( ) > 0) && (token != ".") ) {
00198
00199 stack.push_back( token );
00200 }
00201
00202
00203 start = pos + 1;
00204 }
00205
00206
00207 if( start < filename.length( ) ) {
00208 token = filename.substr( start, filename.length( ) - start );
00209 if( token == ".." ) {
00210
00211 if( stack.size( ) > 0 ) {
00212 stack.pop_back( );
00213 }
00214 } else if( (token.length( ) > 0) && (token != ".") ) {
00215
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
00238 return file;
00239 } else {
00240 return file.substr( pos + 1, file.length( ) - (pos + 1) );
00241 }
00242 }