merged tag ooo/OOO330_m14
[LibreOffice.git] / cosv / source / storage / ploc_dir.cxx
blob0f769a5f7e6fbb6aafcd8d68e3d188853c56f63f
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 ************************************************************************/
28 #include <precomp.h>
29 #include <cosv/ploc_dir.hxx>
31 // NOT FULLY DECLARED SERVICES
32 #include <cosv/ploc.hxx>
35 namespace csv
37 namespace ploc
40 Directory::Directory()
44 Directory::Directory( const Path & i_rPath )
45 : aPath(i_rPath)
46 // sPath
50 Directory::Directory( const Directory & i_rDir )
51 : Persistent(), aPath(i_rDir.aPath)
52 // sPath
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()
70 Directory &
71 Directory::operator+=( const String & i_sName )
73 InvalidatePath();
74 aPath.DirChain() += i_sName;
75 return *this;
78 Directory &
79 Directory::operator+=( const DirectoryChain & i_sDirChain )
81 InvalidatePath();
82 aPath.DirChain() += i_sDirChain;
83 return *this;
86 Directory &
87 Directory::operator-=( uintt i_nLevels )
89 InvalidatePath();
90 aPath.DirChain().PopBack(i_nLevels);
91 return *this;
94 bool
95 Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary ) const
97 bool ret = PhysicalCreate_Dir( StrPath() );
98 if ( ret OR NOT i_bCreateParentsIfNecessary )
99 return ret;
101 ret = Check_Parent();
102 if (ret)
103 ret = PhysicalCreate_Dir( StrPath() );
104 return ret;
107 bool
108 Directory::Check_Parent() const
110 // There is no parent of root directories:
111 if ( aPath.DirChain().Size() == 0 )
112 return false;
114 // Become my own parent:
115 String sLastToken = aPath.DirChain().Back();
116 const_cast< Directory* >(this)->operator-=(1);
118 // Begin behaving as parent:
119 bool ret = Exists();
120 if (NOT ret)
122 ret = Check_Parent();
123 if (ret)
124 ret = PhysicalCreate_Dir( StrPath() );
126 // End behaving as parent.
128 // Become myself again:
129 const_cast< Directory* >(this)->operator+=(sLastToken);
130 return ret;
133 } // namespace ploc
134 } // namespace csv
137 #ifdef WNT
138 #include <direct.h>
139 #include <io.h>
141 namespace csv
143 namespace ploc
146 bool
147 Directory::PhysicalCreate_Dir( const char * i_sStr ) const
149 return mkdir( i_sStr ) == 0;
152 void
153 Directory::GetContainedDirectories( StringVector & o_rResult ) const
155 const char * c_sANYDIR = "\\*.*";
156 String sNew;
158 StreamStr sFilter(200);
159 sFilter << StrPath()
160 << c_sANYDIR;
162 struct _finddata_t
163 aEntry;
164 long hFile = _findfirst( sFilter.c_str(), &aEntry );
166 for ( int bFindMore = (hFile == -1 ? 1 : 0);
167 bFindMore == 0;
168 bFindMore = _findnext( hFile, &aEntry ) )
170 if ( (aEntry.attrib & _A_SUBDIR) AND *aEntry.name != '.' )
172 sNew = aEntry.name;
173 o_rResult.push_back( sNew );
175 } // end for
176 _findclose(hFile);
179 void
180 Directory::GetContainedFiles( StringVector & o_rResult,
181 const char * i_sFilter,
182 E_Recursivity i_eRecursivity ) const
184 StreamStr sNew(240);
185 sNew << aPath;
186 StreamStr::size_type
187 nStartFilename = sNew.tellp();
189 StreamStr sFilter(200);
190 sFilter << StrPath()
191 << "\\"
192 << i_sFilter;
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 ) )
201 sNew.seekp(nStartFilename);
202 sNew << aEntry.name;
203 String sNewAsString( sNew.c_str() );
204 o_rResult.push_back(sNewAsString);
205 } // end for
207 _findclose(hFile);
208 if ( i_eRecursivity == flat )
209 return;
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();
217 d != dEnd;
218 ++d )
220 Directory aSub(*this);
221 aSub += *d;
222 aSub.GetContainedFiles( o_rResult,
223 i_sFilter,
224 i_eRecursivity );
228 } // namespace ploc
229 } // namespace csv
232 #elif defined(UNX)
233 #include <sys/types.h>
234 #include <sys/stat.h>
235 #include <dirent.h>
237 namespace csv
239 namespace ploc
242 bool
243 Directory::PhysicalCreate_Dir( const char * i_sStr ) const
245 return mkdir( i_sStr, 00777 ) == 0;
248 void
249 Directory::GetContainedDirectories( StringVector & o_rResult ) const
251 StreamStr sNew(240);
252 sNew << aPath;
253 StreamStr::size_type
254 nStartFilename = sNew.tellp();
256 DIR * pDir = opendir( StrPath() );
257 dirent * pEntry = 0;
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)
272 } // end while
273 closedir( pDir );
276 void
277 Directory::GetContainedFiles( StringVector & o_rResult,
278 const char * i_sFilter,
279 E_Recursivity i_eRecursivity ) const
281 StreamStr sNew(240);
282 sNew << aPath;
283 StreamStr::size_type
284 nStartFilename = sNew.tellp();
286 bool bUseFilter = strcmp( i_sFilter, "*.*" ) != 0
287 AND strncmp( i_sFilter, "*.", 2) == 0;
289 DIR * pDir = opendir( StrPath() );
290 dirent * pEntry = 0;
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.
302 if ( bUseFilter )
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;
311 sNew.seekp(nStartFilename);
312 sNew << pEntry->d_name;
313 String sNewAsString( sNew.c_str() );
314 o_rResult.push_back(sNewAsString);
315 } // end while
317 closedir( pDir );
318 if ( i_eRecursivity == flat )
319 return;
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();
327 d != dEnd;
328 ++d )
330 Directory aSub(*this);
331 aSub += *d;
332 aSub.GetContainedFiles( o_rResult,
333 i_sFilter,
334 i_eRecursivity );
338 } // namespace ploc
339 } // namespace csv
342 #else
343 #error For using csv::ploc there has to be defined: WNT or UNX.
344 #endif
347 namespace csv
349 namespace ploc
352 const Path &
353 Directory::inq_MyPath() const
355 return aPath;
360 } // namespace ploc
361 } // namespace csv