xref: /aoo41x/main/cosv/source/storage/ploc_dir.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <precomp.h>
29*cdf0e10cSrcweir #include <cosv/ploc_dir.hxx>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
32*cdf0e10cSrcweir #include <cosv/ploc.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir namespace csv
36*cdf0e10cSrcweir {
37*cdf0e10cSrcweir namespace ploc
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir Directory::Directory()
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir }
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir Directory::Directory( const Path & i_rPath )
45*cdf0e10cSrcweir     :   aPath(i_rPath)
46*cdf0e10cSrcweir         // sPath
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir }
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir Directory::Directory( const Directory & i_rDir )
51*cdf0e10cSrcweir     :   Persistent(), aPath(i_rDir.aPath)
52*cdf0e10cSrcweir         // sPath
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir Directory::Directory( const char * i_rLocation )
57*cdf0e10cSrcweir     :   aPath(i_rLocation, true)
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir Directory::Directory( const String & i_rLocation )
62*cdf0e10cSrcweir     :   aPath(i_rLocation.c_str(), true)
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir Directory::~Directory()
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir Directory &
71*cdf0e10cSrcweir Directory::operator+=( const String & i_sName )
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir     InvalidatePath();
74*cdf0e10cSrcweir     aPath.DirChain() += i_sName;
75*cdf0e10cSrcweir     return *this;
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir Directory &
79*cdf0e10cSrcweir Directory::operator+=( const DirectoryChain & i_sDirChain )
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     InvalidatePath();
82*cdf0e10cSrcweir     aPath.DirChain() += i_sDirChain;
83*cdf0e10cSrcweir     return *this;
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir Directory &
87*cdf0e10cSrcweir Directory::operator-=( uintt i_nLevels )
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir     InvalidatePath();
90*cdf0e10cSrcweir     aPath.DirChain().PopBack(i_nLevels);
91*cdf0e10cSrcweir     return *this;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir bool
95*cdf0e10cSrcweir Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary ) const
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir     bool ret = PhysicalCreate_Dir( StrPath() );
98*cdf0e10cSrcweir     if ( ret OR NOT i_bCreateParentsIfNecessary )
99*cdf0e10cSrcweir         return ret;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     ret = Check_Parent();
102*cdf0e10cSrcweir     if (ret)
103*cdf0e10cSrcweir        	ret = PhysicalCreate_Dir( StrPath() );
104*cdf0e10cSrcweir     return ret;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir bool
108*cdf0e10cSrcweir Directory::Check_Parent() const
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     // There is no parent of root directories:
111*cdf0e10cSrcweir     if ( aPath.DirChain().Size() == 0 )
112*cdf0e10cSrcweir         return false;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     // Become my own parent:
115*cdf0e10cSrcweir     String sLastToken = aPath.DirChain().Back();
116*cdf0e10cSrcweir     const_cast< Directory* >(this)->operator-=(1);
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     // Begin behaving as parent:
119*cdf0e10cSrcweir     bool ret = Exists();
120*cdf0e10cSrcweir     if (NOT ret)
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         ret = Check_Parent();
123*cdf0e10cSrcweir         if (ret)
124*cdf0e10cSrcweir        	    ret = PhysicalCreate_Dir( StrPath() );
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir     // End behaving as parent.
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     // Become myself again:
129*cdf0e10cSrcweir     const_cast< Directory* >(this)->operator+=(sLastToken);
130*cdf0e10cSrcweir     return ret;
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir } // namespace ploc
134*cdf0e10cSrcweir } // namespace csv
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir #ifdef WNT
138*cdf0e10cSrcweir #include <direct.h>
139*cdf0e10cSrcweir #include <io.h>
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir namespace csv
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir namespace ploc
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir bool
147*cdf0e10cSrcweir Directory::PhysicalCreate_Dir( const char * i_sStr ) const
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir     return mkdir( i_sStr ) == 0;
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir void
153*cdf0e10cSrcweir Directory::GetContainedDirectories( StringVector & o_rResult ) const
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     const char *    c_sANYDIR = "\\*.*";
156*cdf0e10cSrcweir     String          sNew;
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir     StreamStr       sFilter(200);
159*cdf0e10cSrcweir     sFilter << StrPath()
160*cdf0e10cSrcweir             << c_sANYDIR;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	struct _finddata_t
163*cdf0e10cSrcweir                     aEntry;
164*cdf0e10cSrcweir 	long            hFile = _findfirst( sFilter.c_str(), &aEntry );
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	for ( int bFindMore = (hFile == -1 ? 1 : 0);
167*cdf0e10cSrcweir 		  bFindMore == 0;
168*cdf0e10cSrcweir 		  bFindMore = _findnext( hFile, &aEntry ) )
169*cdf0e10cSrcweir 	{
170*cdf0e10cSrcweir 		if ( (aEntry.attrib & _A_SUBDIR) AND *aEntry.name != '.' )
171*cdf0e10cSrcweir         {
172*cdf0e10cSrcweir 		    sNew = aEntry.name;
173*cdf0e10cSrcweir 			o_rResult.push_back( sNew );
174*cdf0e10cSrcweir 		}
175*cdf0e10cSrcweir 	}   // end for
176*cdf0e10cSrcweir 	_findclose(hFile);
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir void
180*cdf0e10cSrcweir Directory::GetContainedFiles( StringVector &    o_rResult,
181*cdf0e10cSrcweir                               const char *	    i_sFilter,
182*cdf0e10cSrcweir                               E_Recursivity     i_eRecursivity ) const
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir     StreamStr       sNew(240);
185*cdf0e10cSrcweir     sNew << aPath;
186*cdf0e10cSrcweir     StreamStr::size_type
187*cdf0e10cSrcweir                     nStartFilename = sNew.tellp();
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     StreamStr       sFilter(200);
190*cdf0e10cSrcweir     sFilter << StrPath()
191*cdf0e10cSrcweir             << "\\"
192*cdf0e10cSrcweir             << i_sFilter;
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	struct _finddata_t
195*cdf0e10cSrcweir                     aEntry;
196*cdf0e10cSrcweir 	long            hFile = _findfirst( sFilter.c_str(), &aEntry );
197*cdf0e10cSrcweir 	for ( int bFindMore = (hFile == -1 ? 1 : 0);
198*cdf0e10cSrcweir 		  bFindMore == 0;
199*cdf0e10cSrcweir 		  bFindMore = _findnext( hFile, &aEntry ) )
200*cdf0e10cSrcweir 	{
201*cdf0e10cSrcweir         sNew.seekp(nStartFilename);
202*cdf0e10cSrcweir 		sNew << aEntry.name;
203*cdf0e10cSrcweir         String sNewAsString( sNew.c_str() );
204*cdf0e10cSrcweir 		o_rResult.push_back(sNewAsString);
205*cdf0e10cSrcweir 	}	// end for
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 	_findclose(hFile);
208*cdf0e10cSrcweir     if ( i_eRecursivity == flat )
209*cdf0e10cSrcweir         return;
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	//  gathering from subdirectories:
212*cdf0e10cSrcweir 	StringVector    aSubDirectories;
213*cdf0e10cSrcweir 	GetContainedDirectories( aSubDirectories );
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	StringVector::const_iterator dEnd = aSubDirectories.end();
216*cdf0e10cSrcweir 	for ( StringVector::const_iterator d = aSubDirectories.begin();
217*cdf0e10cSrcweir           d != dEnd;
218*cdf0e10cSrcweir           ++d )
219*cdf0e10cSrcweir 	{
220*cdf0e10cSrcweir         Directory       aSub(*this);
221*cdf0e10cSrcweir         aSub += *d;
222*cdf0e10cSrcweir         aSub.GetContainedFiles( o_rResult,
223*cdf0e10cSrcweir                                 i_sFilter,
224*cdf0e10cSrcweir                                 i_eRecursivity );
225*cdf0e10cSrcweir 	}
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir } // namespace ploc
229*cdf0e10cSrcweir } // namespace csv
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir #elif defined(UNX)
233*cdf0e10cSrcweir #include <sys/types.h>
234*cdf0e10cSrcweir #include <sys/stat.h>
235*cdf0e10cSrcweir #include <dirent.h>
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir namespace csv
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir namespace ploc
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir bool
243*cdf0e10cSrcweir Directory::PhysicalCreate_Dir( const char * i_sStr ) const
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir     return mkdir( i_sStr, 00777 ) == 0;
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir void
249*cdf0e10cSrcweir Directory::GetContainedDirectories( StringVector & o_rResult ) const
250*cdf0e10cSrcweir {
251*cdf0e10cSrcweir     StreamStr       sNew(240);
252*cdf0e10cSrcweir     sNew << aPath;
253*cdf0e10cSrcweir     StreamStr::size_type
254*cdf0e10cSrcweir                     nStartFilename = sNew.tellp();
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 	DIR *           pDir = opendir( StrPath() );
257*cdf0e10cSrcweir 	dirent *        pEntry = 0;
258*cdf0e10cSrcweir 	struct stat 	aEntryStatus;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 	while ( (pEntry = readdir(pDir)) != 0 )
261*cdf0e10cSrcweir 	{
262*cdf0e10cSrcweir         sNew.seekp(nStartFilename);
263*cdf0e10cSrcweir         sNew << pEntry->d_name;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 		stat(sNew.c_str(), &aEntryStatus);
266*cdf0e10cSrcweir 		if ( (aEntryStatus.st_mode & S_IFDIR) == S_IFDIR
267*cdf0e10cSrcweir              AND *pEntry->d_name != '.' )
268*cdf0e10cSrcweir 		{
269*cdf0e10cSrcweir 	        String sNew2(pEntry->d_name);
270*cdf0e10cSrcweir             o_rResult.push_back(sNew2);
271*cdf0e10cSrcweir 		}   // endif (aEntry.attrib == _A_SUBDIR)
272*cdf0e10cSrcweir 	}	// end while
273*cdf0e10cSrcweir 	closedir( pDir );
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir void
277*cdf0e10cSrcweir Directory::GetContainedFiles( StringVector &    o_rResult,
278*cdf0e10cSrcweir                               const char *	    i_sFilter,
279*cdf0e10cSrcweir                               E_Recursivity     i_eRecursivity ) const
280*cdf0e10cSrcweir {
281*cdf0e10cSrcweir     StreamStr       sNew(240);
282*cdf0e10cSrcweir     sNew << aPath;
283*cdf0e10cSrcweir     StreamStr::size_type
284*cdf0e10cSrcweir                     nStartFilename = sNew.tellp();
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir     bool            bUseFilter = strcmp( i_sFilter, "*.*" ) != 0
287*cdf0e10cSrcweir                                  AND strncmp( i_sFilter, "*.", 2) == 0;
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 	DIR *           pDir = opendir( StrPath() );
290*cdf0e10cSrcweir 	dirent *        pEntry = 0;
291*cdf0e10cSrcweir 	struct stat 	aEntryStatus;
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir 	while ( (pEntry = readdir(pDir)) != 0 )
294*cdf0e10cSrcweir 	{
295*cdf0e10cSrcweir         sNew.seekp(nStartFilename);
296*cdf0e10cSrcweir         sNew << pEntry->d_name;
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir 		stat(sNew.c_str(), &aEntryStatus);
299*cdf0e10cSrcweir 		if ( (aEntryStatus.st_mode & S_IFDIR) == S_IFDIR )
300*cdf0e10cSrcweir             continue;   // Don't gather directories.
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir         if ( bUseFilter )
303*cdf0e10cSrcweir         {
304*cdf0e10cSrcweir             const char * pEnding = strrchr(pEntry->d_name,'.');
305*cdf0e10cSrcweir             if (pEnding == 0)
306*cdf0e10cSrcweir                 continue;
307*cdf0e10cSrcweir             if ( strcasecmp( pEnding + 1, i_sFilter + 2 ) != 0 )
308*cdf0e10cSrcweir                 continue;
309*cdf0e10cSrcweir         }
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir         sNew.seekp(nStartFilename);
312*cdf0e10cSrcweir 		sNew << pEntry->d_name;
313*cdf0e10cSrcweir         String sNewAsString( sNew.c_str() );
314*cdf0e10cSrcweir 		o_rResult.push_back(sNewAsString);
315*cdf0e10cSrcweir 	}	// end while
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir 	closedir( pDir );
318*cdf0e10cSrcweir     if ( i_eRecursivity == flat )
319*cdf0e10cSrcweir         return;
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir 	//  gathering from subdirectories:
322*cdf0e10cSrcweir 	StringVector    aSubDirectories;
323*cdf0e10cSrcweir 	GetContainedDirectories( aSubDirectories );
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 	StringVector::const_iterator dEnd = aSubDirectories.end();
326*cdf0e10cSrcweir 	for ( StringVector::const_iterator d = aSubDirectories.begin();
327*cdf0e10cSrcweir           d != dEnd;
328*cdf0e10cSrcweir           ++d )
329*cdf0e10cSrcweir 	{
330*cdf0e10cSrcweir         Directory       aSub(*this);
331*cdf0e10cSrcweir         aSub += *d;
332*cdf0e10cSrcweir         aSub.GetContainedFiles( o_rResult,
333*cdf0e10cSrcweir                                 i_sFilter,
334*cdf0e10cSrcweir                                 i_eRecursivity );
335*cdf0e10cSrcweir 	}
336*cdf0e10cSrcweir }
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir } // namespace ploc
339*cdf0e10cSrcweir } // namespace csv
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir #else
343*cdf0e10cSrcweir #error  For using csv::ploc there has to be defined: WNT or UNX.
344*cdf0e10cSrcweir #endif
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir namespace csv
348*cdf0e10cSrcweir {
349*cdf0e10cSrcweir namespace ploc
350*cdf0e10cSrcweir {
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir const Path &
353*cdf0e10cSrcweir Directory::inq_MyPath() const
354*cdf0e10cSrcweir {
355*cdf0e10cSrcweir     return aPath;
356*cdf0e10cSrcweir }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir } // namespace ploc
361*cdf0e10cSrcweir } // namespace csv
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir 
365