1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
29 #include <cosv/ploc_dir.hxx>
31 // NOT FULLY DECLARED SERVICES
32 #include <cosv/ploc.hxx>
40 Directory::Directory()
44 Directory::Directory( const Path
& i_rPath
)
50 Directory::Directory( const Directory
& i_rDir
)
51 : Persistent(), aPath(i_rDir
.aPath
)
56 Directory::Directory( const char * i_rLocation
)
57 : aPath(i_rLocation
, true)
61 Directory::Directory( const String
& i_rLocation
)
62 : aPath(i_rLocation
.c_str(), true)
66 Directory::~Directory()
71 Directory::operator+=( const String
& i_sName
)
74 aPath
.DirChain() += i_sName
;
79 Directory::operator+=( const DirectoryChain
& i_sDirChain
)
82 aPath
.DirChain() += i_sDirChain
;
87 Directory::operator-=( uintt i_nLevels
)
90 aPath
.DirChain().PopBack(i_nLevels
);
95 Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary
) const
97 bool ret
= PhysicalCreate_Dir( StrPath() );
98 if ( ret OR NOT i_bCreateParentsIfNecessary
)
101 ret
= Check_Parent();
103 ret
= PhysicalCreate_Dir( StrPath() );
108 Directory::Check_Parent() const
110 // There is no parent of root directories:
111 if ( aPath
.DirChain().Size() == 0 )
114 // Become my own parent:
115 String sLastToken
= aPath
.DirChain().Back();
116 const_cast< Directory
* >(this)->operator-=(1);
118 // Begin behaving as parent:
122 ret
= Check_Parent();
124 ret
= PhysicalCreate_Dir( StrPath() );
126 // End behaving as parent.
128 // Become myself again:
129 const_cast< Directory
* >(this)->operator+=(sLastToken
);
147 Directory::PhysicalCreate_Dir( const char * i_sStr
) const
149 return mkdir( i_sStr
) == 0;
153 Directory::GetContainedDirectories( StringVector
& o_rResult
) const
155 const char * c_sANYDIR
= "\\*.*";
158 StreamStr
sFilter(200);
164 long hFile
= _findfirst( sFilter
.c_str(), &aEntry
);
166 for ( int bFindMore
= (hFile
== -1 ? 1 : 0);
168 bFindMore
= _findnext( hFile
, &aEntry
) )
170 if ( (aEntry
.attrib
& _A_SUBDIR
) AND
*aEntry
.name
!= '.' )
173 o_rResult
.push_back( sNew
);
180 Directory::GetContainedFiles( StringVector
& o_rResult
,
181 const char * i_sFilter
,
182 E_Recursivity i_eRecursivity
) const
187 nStartFilename
= sNew
.tellp();
189 StreamStr
sFilter(200);
196 long hFile
= _findfirst( sFilter
.c_str(), &aEntry
);
197 for ( int bFindMore
= (hFile
== -1 ? 1 : 0);
199 bFindMore
= _findnext( hFile
, &aEntry
) )
201 sNew
.seekp(nStartFilename
);
203 String
sNewAsString( sNew
.c_str() );
204 o_rResult
.push_back(sNewAsString
);
208 if ( i_eRecursivity
== flat
)
211 // gathering from subdirectories:
212 StringVector aSubDirectories
;
213 GetContainedDirectories( aSubDirectories
);
215 StringVector::const_iterator dEnd
= aSubDirectories
.end();
216 for ( StringVector::const_iterator d
= aSubDirectories
.begin();
220 Directory
aSub(*this);
222 aSub
.GetContainedFiles( o_rResult
,
233 #include <sys/types.h>
234 #include <sys/stat.h>
243 Directory::PhysicalCreate_Dir( const char * i_sStr
) const
245 return mkdir( i_sStr
, 00777 ) == 0;
249 Directory::GetContainedDirectories( StringVector
& o_rResult
) const
254 nStartFilename
= sNew
.tellp();
256 DIR * pDir
= opendir( StrPath() );
258 struct stat aEntryStatus
;
260 while ( (pEntry
= readdir(pDir
)) != 0 )
262 sNew
.seekp(nStartFilename
);
263 sNew
<< pEntry
->d_name
;
265 stat(sNew
.c_str(), &aEntryStatus
);
266 if ( (aEntryStatus
.st_mode
& S_IFDIR
) == S_IFDIR
267 AND
*pEntry
->d_name
!= '.' )
269 String
sNew2(pEntry
->d_name
);
270 o_rResult
.push_back(sNew2
);
271 } // endif (aEntry.attrib == _A_SUBDIR)
277 Directory::GetContainedFiles( StringVector
& o_rResult
,
278 const char * i_sFilter
,
279 E_Recursivity i_eRecursivity
) const
284 nStartFilename
= sNew
.tellp();
286 bool bUseFilter
= strcmp( i_sFilter
, "*.*" ) != 0
287 AND
strncmp( i_sFilter
, "*.", 2) == 0;
289 DIR * pDir
= opendir( StrPath() );
291 struct stat aEntryStatus
;
293 while ( (pEntry
= readdir(pDir
)) != 0 )
295 sNew
.seekp(nStartFilename
);
296 sNew
<< pEntry
->d_name
;
298 stat(sNew
.c_str(), &aEntryStatus
);
299 if ( (aEntryStatus
.st_mode
& S_IFDIR
) == S_IFDIR
)
300 continue; // Don't gather directories.
304 const char * pEnding
= strrchr(pEntry
->d_name
,'.');
307 if ( strcasecmp( pEnding
+ 1, i_sFilter
+ 2 ) != 0 )
311 sNew
.seekp(nStartFilename
);
312 sNew
<< pEntry
->d_name
;
313 String
sNewAsString( sNew
.c_str() );
314 o_rResult
.push_back(sNewAsString
);
318 if ( i_eRecursivity
== flat
)
321 // gathering from subdirectories:
322 StringVector aSubDirectories
;
323 GetContainedDirectories( aSubDirectories
);
325 StringVector::const_iterator dEnd
= aSubDirectories
.end();
326 for ( StringVector::const_iterator d
= aSubDirectories
.begin();
330 Directory
aSub(*this);
332 aSub
.GetContainedFiles( o_rResult
,
343 #error For using csv::ploc there has to be defined: WNT or UNX.
353 Directory::inq_MyPath() const