1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ploc_dir.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include <cosv/ploc_dir.hxx>
34 // NOT FULLY DECLARED SERVICES
35 #include <cosv/ploc.hxx>
43 Directory::Directory()
47 Directory::Directory( const Path
& i_rPath
)
53 Directory::Directory( const Directory
& i_rDir
)
54 : Persistent(), aPath(i_rDir
.aPath
)
59 Directory::Directory( const char * i_rLocation
)
60 : aPath(i_rLocation
, true)
64 Directory::Directory( const String
& i_rLocation
)
65 : aPath(i_rLocation
.c_str(), true)
69 Directory::~Directory()
74 Directory::operator+=( const String
& i_sName
)
77 aPath
.DirChain() += i_sName
;
82 Directory::operator+=( const DirectoryChain
& i_sDirChain
)
85 aPath
.DirChain() += i_sDirChain
;
90 Directory::operator-=( uintt i_nLevels
)
93 aPath
.DirChain().PopBack(i_nLevels
);
98 Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary
) const
100 bool ret
= PhysicalCreate_Dir( StrPath() );
101 if ( ret OR NOT i_bCreateParentsIfNecessary
)
104 ret
= Check_Parent();
106 ret
= PhysicalCreate_Dir( StrPath() );
111 Directory::Check_Parent() const
113 // There is no parent of root directories:
114 if ( aPath
.DirChain().Size() == 0 )
117 // Become my own parent:
118 String sLastToken
= aPath
.DirChain().Back();
119 const_cast< Directory
* >(this)->operator-=(1);
121 // Begin behaving as parent:
125 ret
= Check_Parent();
127 ret
= PhysicalCreate_Dir( StrPath() );
129 // End behaving as parent.
131 // Become myself again:
132 const_cast< Directory
* >(this)->operator+=(sLastToken
);
150 Directory::PhysicalCreate_Dir( const char * i_sStr
) const
152 return mkdir( i_sStr
) == 0;
156 Directory::GetContainedDirectories( StringVector
& o_rResult
) const
158 const char * c_sANYDIR
= "\\*.*";
161 StreamStr
sFilter(200);
167 long hFile
= _findfirst( sFilter
.c_str(), &aEntry
);
169 for ( int bFindMore
= (hFile
== -1 ? 1 : 0);
171 bFindMore
= _findnext( hFile
, &aEntry
) )
173 if ( (aEntry
.attrib
& _A_SUBDIR
) AND
*aEntry
.name
!= '.' )
176 o_rResult
.push_back( sNew
);
183 Directory::GetContainedFiles( StringVector
& o_rResult
,
184 const char * i_sFilter
,
185 E_Recursivity i_eRecursivity
) const
190 nStartFilename
= sNew
.tellp();
192 StreamStr
sFilter(200);
199 long hFile
= _findfirst( sFilter
.c_str(), &aEntry
);
200 for ( int bFindMore
= (hFile
== -1 ? 1 : 0);
202 bFindMore
= _findnext( hFile
, &aEntry
) )
204 sNew
.seekp(nStartFilename
);
206 String
sNewAsString( sNew
.c_str() );
207 o_rResult
.push_back(sNewAsString
);
211 if ( i_eRecursivity
== flat
)
214 // gathering from subdirectories:
215 StringVector aSubDirectories
;
216 GetContainedDirectories( aSubDirectories
);
218 StringVector::const_iterator dEnd
= aSubDirectories
.end();
219 for ( StringVector::const_iterator d
= aSubDirectories
.begin();
223 Directory
aSub(*this);
225 aSub
.GetContainedFiles( o_rResult
,
236 #include <sys/types.h>
237 #include <sys/stat.h>
246 Directory::PhysicalCreate_Dir( const char * i_sStr
) const
248 return mkdir( i_sStr
, 00777 ) == 0;
252 Directory::GetContainedDirectories( StringVector
& o_rResult
) const
257 nStartFilename
= sNew
.tellp();
259 DIR * pDir
= opendir( StrPath() );
261 struct stat aEntryStatus
;
263 while ( (pEntry
= readdir(pDir
)) != 0 )
265 sNew
.seekp(nStartFilename
);
266 sNew
<< pEntry
->d_name
;
268 stat(sNew
.c_str(), &aEntryStatus
);
269 if ( (aEntryStatus
.st_mode
& S_IFDIR
) == S_IFDIR
270 AND
*pEntry
->d_name
!= '.' )
272 String
sNew2(pEntry
->d_name
);
273 o_rResult
.push_back(sNew2
);
274 } // endif (aEntry.attrib == _A_SUBDIR)
280 Directory::GetContainedFiles( StringVector
& o_rResult
,
281 const char * i_sFilter
,
282 E_Recursivity i_eRecursivity
) const
287 nStartFilename
= sNew
.tellp();
289 bool bUseFilter
= strcmp( i_sFilter
, "*.*" ) != 0
290 AND
strncmp( i_sFilter
, "*.", 2) == 0;
292 DIR * pDir
= opendir( StrPath() );
294 struct stat aEntryStatus
;
296 while ( (pEntry
= readdir(pDir
)) != 0 )
298 sNew
.seekp(nStartFilename
);
299 sNew
<< pEntry
->d_name
;
301 stat(sNew
.c_str(), &aEntryStatus
);
302 if ( (aEntryStatus
.st_mode
& S_IFDIR
) == S_IFDIR
)
303 continue; // Don't gather directories.
307 const char * pEnding
= strrchr(pEntry
->d_name
,'.');
310 if ( strcasecmp( pEnding
+ 1, i_sFilter
+ 2 ) != 0 )
314 sNew
.seekp(nStartFilename
);
315 sNew
<< pEntry
->d_name
;
316 String
sNewAsString( sNew
.c_str() );
317 o_rResult
.push_back(sNewAsString
);
321 if ( i_eRecursivity
== flat
)
324 // gathering from subdirectories:
325 StringVector aSubDirectories
;
326 GetContainedDirectories( aSubDirectories
);
328 StringVector::const_iterator dEnd
= aSubDirectories
.end();
329 for ( StringVector::const_iterator d
= aSubDirectories
.begin();
333 Directory
aSub(*this);
335 aSub
.GetContainedFiles( o_rResult
,
346 #error For using csv::ploc there has to be defined: WNT or UNX.
356 Directory::inq_MyPath() const