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