merge the formfield patch from ooo-build
[ooovba.git] / cosv / source / storage / ploc_dir.cxx
blob4771c4367e29cb5e0fab4ae6f4fe0c1405750bb8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ploc_dir.cxx,v $
10 * $Revision: 1.9 $
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 ************************************************************************/
31 #include <precomp.h>
32 #include <cosv/ploc_dir.hxx>
34 // NOT FULLY DECLARED SERVICES
35 #include <cosv/ploc.hxx>
38 namespace csv
40 namespace ploc
43 Directory::Directory()
47 Directory::Directory( const Path & i_rPath )
48 : aPath(i_rPath)
49 // sPath
53 Directory::Directory( const Directory & i_rDir )
54 : Persistent(), aPath(i_rDir.aPath)
55 // sPath
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()
73 Directory &
74 Directory::operator+=( const String & i_sName )
76 InvalidatePath();
77 aPath.DirChain() += i_sName;
78 return *this;
81 Directory &
82 Directory::operator+=( const DirectoryChain & i_sDirChain )
84 InvalidatePath();
85 aPath.DirChain() += i_sDirChain;
86 return *this;
89 Directory &
90 Directory::operator-=( uintt i_nLevels )
92 InvalidatePath();
93 aPath.DirChain().PopBack(i_nLevels);
94 return *this;
97 bool
98 Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary ) const
100 bool ret = PhysicalCreate_Dir( StrPath() );
101 if ( ret OR NOT i_bCreateParentsIfNecessary )
102 return ret;
104 ret = Check_Parent();
105 if (ret)
106 ret = PhysicalCreate_Dir( StrPath() );
107 return ret;
110 bool
111 Directory::Check_Parent() const
113 // There is no parent of root directories:
114 if ( aPath.DirChain().Size() == 0 )
115 return false;
117 // Become my own parent:
118 String sLastToken = aPath.DirChain().Back();
119 const_cast< Directory* >(this)->operator-=(1);
121 // Begin behaving as parent:
122 bool ret = Exists();
123 if (NOT ret)
125 ret = Check_Parent();
126 if (ret)
127 ret = PhysicalCreate_Dir( StrPath() );
129 // End behaving as parent.
131 // Become myself again:
132 const_cast< Directory* >(this)->operator+=(sLastToken);
133 return ret;
136 } // namespace ploc
137 } // namespace csv
140 #ifdef WNT
141 #include <direct.h>
142 #include <io.h>
144 namespace csv
146 namespace ploc
149 bool
150 Directory::PhysicalCreate_Dir( const char * i_sStr ) const
152 return mkdir( i_sStr ) == 0;
155 void
156 Directory::GetContainedDirectories( StringVector & o_rResult ) const
158 const char * c_sANYDIR = "\\*.*";
159 String sNew;
161 StreamStr sFilter(200);
162 sFilter << StrPath()
163 << c_sANYDIR;
165 struct _finddata_t
166 aEntry;
167 long hFile = _findfirst( sFilter.c_str(), &aEntry );
169 for ( int bFindMore = (hFile == -1 ? 1 : 0);
170 bFindMore == 0;
171 bFindMore = _findnext( hFile, &aEntry ) )
173 if ( (aEntry.attrib & _A_SUBDIR) AND *aEntry.name != '.' )
175 sNew = aEntry.name;
176 o_rResult.push_back( sNew );
178 } // end for
179 _findclose(hFile);
182 void
183 Directory::GetContainedFiles( StringVector & o_rResult,
184 const char * i_sFilter,
185 E_Recursivity i_eRecursivity ) const
187 StreamStr sNew(240);
188 sNew << aPath;
189 StreamStr::size_type
190 nStartFilename = sNew.tellp();
192 StreamStr sFilter(200);
193 sFilter << StrPath()
194 << "\\"
195 << i_sFilter;
197 struct _finddata_t
198 aEntry;
199 long hFile = _findfirst( sFilter.c_str(), &aEntry );
200 for ( int bFindMore = (hFile == -1 ? 1 : 0);
201 bFindMore == 0;
202 bFindMore = _findnext( hFile, &aEntry ) )
204 sNew.seekp(nStartFilename);
205 sNew << aEntry.name;
206 String sNewAsString( sNew.c_str() );
207 o_rResult.push_back(sNewAsString);
208 } // end for
210 _findclose(hFile);
211 if ( i_eRecursivity == flat )
212 return;
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();
220 d != dEnd;
221 ++d )
223 Directory aSub(*this);
224 aSub += *d;
225 aSub.GetContainedFiles( o_rResult,
226 i_sFilter,
227 i_eRecursivity );
231 } // namespace ploc
232 } // namespace csv
235 #elif defined(UNX)
236 #include <sys/types.h>
237 #include <sys/stat.h>
238 #include <dirent.h>
240 namespace csv
242 namespace ploc
245 bool
246 Directory::PhysicalCreate_Dir( const char * i_sStr ) const
248 return mkdir( i_sStr, 00777 ) == 0;
251 void
252 Directory::GetContainedDirectories( StringVector & o_rResult ) const
254 StreamStr sNew(240);
255 sNew << aPath;
256 StreamStr::size_type
257 nStartFilename = sNew.tellp();
259 DIR * pDir = opendir( StrPath() );
260 dirent * pEntry = 0;
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)
275 } // end while
276 closedir( pDir );
279 void
280 Directory::GetContainedFiles( StringVector & o_rResult,
281 const char * i_sFilter,
282 E_Recursivity i_eRecursivity ) const
284 StreamStr sNew(240);
285 sNew << aPath;
286 StreamStr::size_type
287 nStartFilename = sNew.tellp();
289 bool bUseFilter = strcmp( i_sFilter, "*.*" ) != 0
290 AND strncmp( i_sFilter, "*.", 2) == 0;
292 DIR * pDir = opendir( StrPath() );
293 dirent * pEntry = 0;
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.
305 if ( bUseFilter )
307 const char * pEnding = strrchr(pEntry->d_name,'.');
308 if (pEnding == 0)
309 continue;
310 if ( strcasecmp( pEnding + 1, i_sFilter + 2 ) != 0 )
311 continue;
314 sNew.seekp(nStartFilename);
315 sNew << pEntry->d_name;
316 String sNewAsString( sNew.c_str() );
317 o_rResult.push_back(sNewAsString);
318 } // end while
320 closedir( pDir );
321 if ( i_eRecursivity == flat )
322 return;
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();
330 d != dEnd;
331 ++d )
333 Directory aSub(*this);
334 aSub += *d;
335 aSub.GetContainedFiles( o_rResult,
336 i_sFilter,
337 i_eRecursivity );
341 } // namespace ploc
342 } // namespace csv
345 #else
346 #error For using csv::ploc there has to be defined: WNT or UNX.
347 #endif
350 namespace csv
352 namespace ploc
355 const Path &
356 Directory::inq_MyPath() const
358 return aPath;
363 } // namespace ploc
364 } // namespace csv