1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include <precomp.h> 29 #include <cosv/ploc_dir.hxx> 30 31 // NOT FULLY DECLARED SERVICES 32 #include <cosv/ploc.hxx> 33 34 35 namespace csv 36 { 37 namespace ploc 38 { 39 40 Directory::Directory() 41 { 42 } 43 44 Directory::Directory( const Path & i_rPath ) 45 : aPath(i_rPath) 46 // sPath 47 { 48 } 49 50 Directory::Directory( const Directory & i_rDir ) 51 : Persistent(), aPath(i_rDir.aPath) 52 // sPath 53 { 54 } 55 56 Directory::Directory( const char * i_rLocation ) 57 : aPath(i_rLocation, true) 58 { 59 } 60 61 Directory::Directory( const String & i_rLocation ) 62 : aPath(i_rLocation.c_str(), true) 63 { 64 } 65 66 Directory::~Directory() 67 { 68 } 69 70 Directory & 71 Directory::operator+=( const String & i_sName ) 72 { 73 InvalidatePath(); 74 aPath.DirChain() += i_sName; 75 return *this; 76 } 77 78 Directory & 79 Directory::operator+=( const DirectoryChain & i_sDirChain ) 80 { 81 InvalidatePath(); 82 aPath.DirChain() += i_sDirChain; 83 return *this; 84 } 85 86 Directory & 87 Directory::operator-=( uintt i_nLevels ) 88 { 89 InvalidatePath(); 90 aPath.DirChain().PopBack(i_nLevels); 91 return *this; 92 } 93 94 bool 95 Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary ) const 96 { 97 bool ret = PhysicalCreate_Dir( StrPath() ); 98 if ( ret OR NOT i_bCreateParentsIfNecessary ) 99 return ret; 100 101 ret = Check_Parent(); 102 if (ret) 103 ret = PhysicalCreate_Dir( StrPath() ); 104 return ret; 105 } 106 107 bool 108 Directory::Check_Parent() const 109 { 110 // There is no parent of root directories: 111 if ( aPath.DirChain().Size() == 0 ) 112 return false; 113 114 // Become my own parent: 115 String sLastToken = aPath.DirChain().Back(); 116 const_cast< Directory* >(this)->operator-=(1); 117 118 // Begin behaving as parent: 119 bool ret = Exists(); 120 if (NOT ret) 121 { 122 ret = Check_Parent(); 123 if (ret) 124 ret = PhysicalCreate_Dir( StrPath() ); 125 } 126 // End behaving as parent. 127 128 // Become myself again: 129 const_cast< Directory* >(this)->operator+=(sLastToken); 130 return ret; 131 } 132 133 } // namespace ploc 134 } // namespace csv 135 136 137 #ifdef WNT 138 #include <direct.h> 139 #include <io.h> 140 141 namespace csv 142 { 143 namespace ploc 144 { 145 146 bool 147 Directory::PhysicalCreate_Dir( const char * i_sStr ) const 148 { 149 return mkdir( i_sStr ) == 0; 150 } 151 152 void 153 Directory::GetContainedDirectories( StringVector & o_rResult ) const 154 { 155 const char * c_sANYDIR = "\\*.*"; 156 String sNew; 157 158 StreamStr sFilter(200); 159 sFilter << StrPath() 160 << c_sANYDIR; 161 162 struct _finddata_t 163 aEntry; 164 long hFile = _findfirst( sFilter.c_str(), &aEntry ); 165 166 for ( int bFindMore = (hFile == -1 ? 1 : 0); 167 bFindMore == 0; 168 bFindMore = _findnext( hFile, &aEntry ) ) 169 { 170 if ( (aEntry.attrib & _A_SUBDIR) AND *aEntry.name != '.' ) 171 { 172 sNew = aEntry.name; 173 o_rResult.push_back( sNew ); 174 } 175 } // end for 176 _findclose(hFile); 177 } 178 179 void 180 Directory::GetContainedFiles( StringVector & o_rResult, 181 const char * i_sFilter, 182 E_Recursivity i_eRecursivity ) const 183 { 184 StreamStr sNew(240); 185 sNew << aPath; 186 StreamStr::size_type 187 nStartFilename = sNew.tellp(); 188 189 StreamStr sFilter(200); 190 sFilter << StrPath() 191 << "\\" 192 << i_sFilter; 193 194 struct _finddata_t 195 aEntry; 196 long hFile = _findfirst( sFilter.c_str(), &aEntry ); 197 for ( int bFindMore = (hFile == -1 ? 1 : 0); 198 bFindMore == 0; 199 bFindMore = _findnext( hFile, &aEntry ) ) 200 { 201 sNew.seekp(nStartFilename); 202 sNew << aEntry.name; 203 String sNewAsString( sNew.c_str() ); 204 o_rResult.push_back(sNewAsString); 205 } // end for 206 207 _findclose(hFile); 208 if ( i_eRecursivity == flat ) 209 return; 210 211 // gathering from subdirectories: 212 StringVector aSubDirectories; 213 GetContainedDirectories( aSubDirectories ); 214 215 StringVector::const_iterator dEnd = aSubDirectories.end(); 216 for ( StringVector::const_iterator d = aSubDirectories.begin(); 217 d != dEnd; 218 ++d ) 219 { 220 Directory aSub(*this); 221 aSub += *d; 222 aSub.GetContainedFiles( o_rResult, 223 i_sFilter, 224 i_eRecursivity ); 225 } 226 } 227 228 } // namespace ploc 229 } // namespace csv 230 231 232 #elif defined(UNX) 233 #include <sys/types.h> 234 #include <sys/stat.h> 235 #include <dirent.h> 236 237 namespace csv 238 { 239 namespace ploc 240 { 241 242 bool 243 Directory::PhysicalCreate_Dir( const char * i_sStr ) const 244 { 245 return mkdir( i_sStr, 00777 ) == 0; 246 } 247 248 void 249 Directory::GetContainedDirectories( StringVector & o_rResult ) const 250 { 251 StreamStr sNew(240); 252 sNew << aPath; 253 StreamStr::size_type 254 nStartFilename = sNew.tellp(); 255 256 DIR * pDir = opendir( StrPath() ); 257 dirent * pEntry = 0; 258 struct stat aEntryStatus; 259 260 while ( (pEntry = readdir(pDir)) != 0 ) 261 { 262 sNew.seekp(nStartFilename); 263 sNew << pEntry->d_name; 264 265 stat(sNew.c_str(), &aEntryStatus); 266 if ( (aEntryStatus.st_mode & S_IFDIR) == S_IFDIR 267 AND *pEntry->d_name != '.' ) 268 { 269 String sNew2(pEntry->d_name); 270 o_rResult.push_back(sNew2); 271 } // endif (aEntry.attrib == _A_SUBDIR) 272 } // end while 273 closedir( pDir ); 274 } 275 276 void 277 Directory::GetContainedFiles( StringVector & o_rResult, 278 const char * i_sFilter, 279 E_Recursivity i_eRecursivity ) const 280 { 281 StreamStr sNew(240); 282 sNew << aPath; 283 StreamStr::size_type 284 nStartFilename = sNew.tellp(); 285 286 bool bUseFilter = strcmp( i_sFilter, "*.*" ) != 0 287 AND strncmp( i_sFilter, "*.", 2) == 0; 288 289 DIR * pDir = opendir( StrPath() ); 290 dirent * pEntry = 0; 291 struct stat aEntryStatus; 292 293 while ( (pEntry = readdir(pDir)) != 0 ) 294 { 295 sNew.seekp(nStartFilename); 296 sNew << pEntry->d_name; 297 298 stat(sNew.c_str(), &aEntryStatus); 299 if ( (aEntryStatus.st_mode & S_IFDIR) == S_IFDIR ) 300 continue; // Don't gather directories. 301 302 if ( bUseFilter ) 303 { 304 const char * pEnding = strrchr(pEntry->d_name,'.'); 305 if (pEnding == 0) 306 continue; 307 if ( strcasecmp( pEnding + 1, i_sFilter + 2 ) != 0 ) 308 continue; 309 } 310 311 sNew.seekp(nStartFilename); 312 sNew << pEntry->d_name; 313 String sNewAsString( sNew.c_str() ); 314 o_rResult.push_back(sNewAsString); 315 } // end while 316 317 closedir( pDir ); 318 if ( i_eRecursivity == flat ) 319 return; 320 321 // gathering from subdirectories: 322 StringVector aSubDirectories; 323 GetContainedDirectories( aSubDirectories ); 324 325 StringVector::const_iterator dEnd = aSubDirectories.end(); 326 for ( StringVector::const_iterator d = aSubDirectories.begin(); 327 d != dEnd; 328 ++d ) 329 { 330 Directory aSub(*this); 331 aSub += *d; 332 aSub.GetContainedFiles( o_rResult, 333 i_sFilter, 334 i_eRecursivity ); 335 } 336 } 337 338 } // namespace ploc 339 } // namespace csv 340 341 342 #else 343 #error For using csv::ploc there has to be defined: WNT or UNX. 344 #endif 345 346 347 namespace csv 348 { 349 namespace ploc 350 { 351 352 const Path & 353 Directory::inq_MyPath() const 354 { 355 return aPath; 356 } 357 358 359 360 } // namespace ploc 361 } // namespace csv 362 363 364 365