1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #define CSV_USE_CSV_ASSERTIONS
21 #include <cosv/csv_env.hxx>
23 #include <cosv/comfunc.hxx>
24 #include <cosv/string.hxx>
25 #include <cosv/streamstr.hxx>
26 #include <cosv/std_outp.hxx>
27 #include <cosv/tpl/dyn.hxx>
28 #include <cosv/ploc_dir.hxx>
30 // NOT FULLY DECLARED SERVICES
31 #include <cosv/ploc.hxx>
39 Directory::Directory()
43 Directory::Directory( const Path
& i_rPath
)
49 Directory::Directory( const Directory
& i_rDir
)
50 : Persistent(), aPath(i_rDir
.aPath
)
55 Directory::Directory( const char * i_rLocation
)
56 : aPath(i_rLocation
, true)
60 Directory::~Directory()
65 Directory::operator+=( const String
& i_sName
)
68 aPath
.DirChain() += i_sName
;
73 Directory::operator+=( const DirectoryChain
& i_sDirChain
)
76 aPath
.DirChain() += i_sDirChain
;
81 Directory::operator-=( uintt i_nLevels
)
84 aPath
.DirChain().PopBack(i_nLevels
);
89 Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary
) const
91 bool ret
= PhysicalCreate_Dir( StrPath() );
92 if ( ret OR NOT i_bCreateParentsIfNecessary
)
97 ret
= PhysicalCreate_Dir( StrPath() );
102 Directory::Check_Parent() const
104 // There is no parent of root directories:
105 if ( aPath
.DirChain().Size() == 0 )
108 // Become my own parent:
109 String sLastToken
= aPath
.DirChain().Back();
110 const_cast< Directory
* >(this)->operator-=(1);
112 // Begin behaving as parent:
116 ret
= Check_Parent();
118 ret
= PhysicalCreate_Dir( StrPath() );
120 // End behaving as parent.
122 // Become myself again:
123 const_cast< Directory
* >(this)->operator+=(sLastToken
);
141 Directory::PhysicalCreate_Dir( const char * i_sStr
) const
143 return mkdir( i_sStr
) == 0;
147 Directory::GetContainedDirectories( StringVector
& o_rResult
) const
149 const char * c_sANYDIR
= "\\*.*";
152 StreamStr
sFilter(200);
158 long hFile
= _findfirst( sFilter
.c_str(), &aEntry
);
160 for ( int bFindMore
= (hFile
== -1 ? 1 : 0);
162 bFindMore
= _findnext( hFile
, &aEntry
) )
164 if ( (aEntry
.attrib
& _A_SUBDIR
) AND
*aEntry
.name
!= '.' )
167 o_rResult
.push_back( sNew
);
174 Directory::GetContainedFiles( StringVector
& o_rResult
,
175 const char * i_sFilter
,
176 E_Recursivity i_eRecursivity
) const
181 nStartFilename
= sNew
.tellp();
183 StreamStr
sFilter(200);
190 long hFile
= _findfirst( sFilter
.c_str(), &aEntry
);
191 for ( int bFindMore
= (hFile
== -1 ? 1 : 0);
193 bFindMore
= _findnext( hFile
, &aEntry
) )
195 sNew
.seekp(nStartFilename
);
197 String
sNewAsString( sNew
.c_str() );
198 o_rResult
.push_back(sNewAsString
);
202 if ( i_eRecursivity
== flat
)
205 // gathering from subdirectories:
206 StringVector aSubDirectories
;
207 GetContainedDirectories( aSubDirectories
);
209 StringVector::const_iterator dEnd
= aSubDirectories
.end();
210 for ( StringVector::const_iterator d
= aSubDirectories
.begin();
214 Directory
aSub(*this);
216 aSub
.GetContainedFiles( o_rResult
,
227 #include <sys/types.h>
228 #include <sys/stat.h>
237 Directory::PhysicalCreate_Dir( const char * i_sStr
) const
239 return mkdir( i_sStr
, 00777 ) == 0;
243 Directory::GetContainedDirectories( StringVector
& o_rResult
) const
248 nStartFilename
= sNew
.tellp();
250 DIR * pDir
= opendir( StrPath() );
252 struct stat aEntryStatus
;
254 while ( (pEntry
= readdir(pDir
)) != 0 )
256 sNew
.seekp(nStartFilename
);
257 sNew
<< pEntry
->d_name
;
259 stat(sNew
.c_str(), &aEntryStatus
);
260 if ( (aEntryStatus
.st_mode
& S_IFDIR
) == S_IFDIR
261 AND
*pEntry
->d_name
!= '.' )
263 String
sNew2(pEntry
->d_name
);
264 o_rResult
.push_back(sNew2
);
265 } // endif (aEntry.attrib == _A_SUBDIR)
271 Directory::GetContainedFiles( StringVector
& o_rResult
,
272 const char * i_sFilter
,
273 E_Recursivity i_eRecursivity
) const
278 nStartFilename
= sNew
.tellp();
280 bool bUseFilter
= strcmp( i_sFilter
, "*.*" ) != 0
281 AND
strncmp( i_sFilter
, "*.", 2) == 0;
283 DIR * pDir
= opendir( StrPath() );
285 struct stat aEntryStatus
;
287 while ( (pEntry
= readdir(pDir
)) != 0 )
289 sNew
.seekp(nStartFilename
);
290 sNew
<< pEntry
->d_name
;
292 stat(sNew
.c_str(), &aEntryStatus
);
293 if ( (aEntryStatus
.st_mode
& S_IFDIR
) == S_IFDIR
)
294 continue; // Don't gather directories.
298 const char * pEnding
= strrchr(pEntry
->d_name
,'.');
301 if ( strcasecmp( pEnding
+ 1, i_sFilter
+ 2 ) != 0 )
305 sNew
.seekp(nStartFilename
);
306 sNew
<< pEntry
->d_name
;
307 String
sNewAsString( sNew
.c_str() );
308 o_rResult
.push_back(sNewAsString
);
312 if ( i_eRecursivity
== flat
)
315 // gathering from subdirectories:
316 StringVector aSubDirectories
;
317 GetContainedDirectories( aSubDirectories
);
319 StringVector::const_iterator dEnd
= aSubDirectories
.end();
320 for ( StringVector::const_iterator d
= aSubDirectories
.begin();
324 Directory
aSub(*this);
326 aSub
.GetContainedFiles( o_rResult
,
337 #error For using csv::ploc there has to be defined: WNT or UNX.
347 Directory::inq_MyPath() const
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */