fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / cosv / source / storage / ploc_dir.cxx
blob234a2d3fbf79d01fbdb93c913e202b329c556858
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
34 namespace csv
36 namespace ploc
39 Directory::Directory()
43 Directory::Directory( const Path & i_rPath )
44 : aPath(i_rPath)
45 // sPath
49 Directory::Directory( const Directory & i_rDir )
50 : Persistent(), aPath(i_rDir.aPath)
51 // sPath
55 Directory::Directory( const char * i_rLocation )
56 : aPath(i_rLocation, true)
60 Directory::~Directory()
64 Directory &
65 Directory::operator+=( const String & i_sName )
67 InvalidatePath();
68 aPath.DirChain() += i_sName;
69 return *this;
72 Directory &
73 Directory::operator+=( const DirectoryChain & i_sDirChain )
75 InvalidatePath();
76 aPath.DirChain() += i_sDirChain;
77 return *this;
80 Directory &
81 Directory::operator-=( uintt i_nLevels )
83 InvalidatePath();
84 aPath.DirChain().PopBack(i_nLevels);
85 return *this;
88 bool
89 Directory::PhysicalCreate( bool i_bCreateParentsIfNecessary ) const
91 bool ret = PhysicalCreate_Dir( StrPath() );
92 if ( ret OR NOT i_bCreateParentsIfNecessary )
93 return ret;
95 ret = Check_Parent();
96 if (ret)
97 ret = PhysicalCreate_Dir( StrPath() );
98 return ret;
101 bool
102 Directory::Check_Parent() const
104 // There is no parent of root directories:
105 if ( aPath.DirChain().Size() == 0 )
106 return false;
108 // Become my own parent:
109 String sLastToken = aPath.DirChain().Back();
110 const_cast< Directory* >(this)->operator-=(1);
112 // Begin behaving as parent:
113 bool ret = Exists();
114 if (NOT ret)
116 ret = Check_Parent();
117 if (ret)
118 ret = PhysicalCreate_Dir( StrPath() );
120 // End behaving as parent.
122 // Become myself again:
123 const_cast< Directory* >(this)->operator+=(sLastToken);
124 return ret;
127 } // namespace ploc
128 } // namespace csv
131 #ifdef WNT
132 #include <direct.h>
133 #include <io.h>
135 namespace csv
137 namespace ploc
140 bool
141 Directory::PhysicalCreate_Dir( const char * i_sStr ) const
143 return mkdir( i_sStr ) == 0;
146 void
147 Directory::GetContainedDirectories( StringVector & o_rResult ) const
149 const char * c_sANYDIR = "\\*.*";
150 String sNew;
152 StreamStr sFilter(200);
153 sFilter << StrPath()
154 << c_sANYDIR;
156 struct _finddata_t
157 aEntry;
158 long hFile = _findfirst( sFilter.c_str(), &aEntry );
160 for ( int bFindMore = (hFile == -1 ? 1 : 0);
161 bFindMore == 0;
162 bFindMore = _findnext( hFile, &aEntry ) )
164 if ( (aEntry.attrib & _A_SUBDIR) AND *aEntry.name != '.' )
166 sNew = aEntry.name;
167 o_rResult.push_back( sNew );
169 } // end for
170 _findclose(hFile);
173 void
174 Directory::GetContainedFiles( StringVector & o_rResult,
175 const char * i_sFilter,
176 E_Recursivity i_eRecursivity ) const
178 StreamStr sNew(240);
179 sNew << aPath;
180 StreamStr::size_type
181 nStartFilename = sNew.tellp();
183 StreamStr sFilter(200);
184 sFilter << StrPath()
185 << "\\"
186 << i_sFilter;
188 struct _finddata_t
189 aEntry;
190 long hFile = _findfirst( sFilter.c_str(), &aEntry );
191 for ( int bFindMore = (hFile == -1 ? 1 : 0);
192 bFindMore == 0;
193 bFindMore = _findnext( hFile, &aEntry ) )
195 sNew.seekp(nStartFilename);
196 sNew << aEntry.name;
197 String sNewAsString( sNew.c_str() );
198 o_rResult.push_back(sNewAsString);
199 } // end for
201 _findclose(hFile);
202 if ( i_eRecursivity == flat )
203 return;
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();
211 d != dEnd;
212 ++d )
214 Directory aSub(*this);
215 aSub += *d;
216 aSub.GetContainedFiles( o_rResult,
217 i_sFilter,
218 i_eRecursivity );
222 } // namespace ploc
223 } // namespace csv
226 #elif defined(UNX)
227 #include <sys/types.h>
228 #include <sys/stat.h>
229 #include <dirent.h>
231 namespace csv
233 namespace ploc
236 bool
237 Directory::PhysicalCreate_Dir( const char * i_sStr ) const
239 return mkdir( i_sStr, 00777 ) == 0;
242 void
243 Directory::GetContainedDirectories( StringVector & o_rResult ) const
245 StreamStr sNew(240);
246 sNew << aPath;
247 StreamStr::size_type
248 nStartFilename = sNew.tellp();
250 DIR * pDir = opendir( StrPath() );
251 dirent * pEntry = 0;
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)
266 } // end while
267 closedir( pDir );
270 void
271 Directory::GetContainedFiles( StringVector & o_rResult,
272 const char * i_sFilter,
273 E_Recursivity i_eRecursivity ) const
275 StreamStr sNew(240);
276 sNew << aPath;
277 StreamStr::size_type
278 nStartFilename = sNew.tellp();
280 bool bUseFilter = strcmp( i_sFilter, "*.*" ) != 0
281 AND strncmp( i_sFilter, "*.", 2) == 0;
283 DIR * pDir = opendir( StrPath() );
284 dirent * pEntry = 0;
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.
296 if ( bUseFilter )
298 const char * pEnding = strrchr(pEntry->d_name,'.');
299 if (pEnding == 0)
300 continue;
301 if ( strcasecmp( pEnding + 1, i_sFilter + 2 ) != 0 )
302 continue;
305 sNew.seekp(nStartFilename);
306 sNew << pEntry->d_name;
307 String sNewAsString( sNew.c_str() );
308 o_rResult.push_back(sNewAsString);
309 } // end while
311 closedir( pDir );
312 if ( i_eRecursivity == flat )
313 return;
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();
321 d != dEnd;
322 ++d )
324 Directory aSub(*this);
325 aSub += *d;
326 aSub.GetContainedFiles( o_rResult,
327 i_sFilter,
328 i_eRecursivity );
332 } // namespace ploc
333 } // namespace csv
336 #else
337 #error For using csv::ploc there has to be defined: WNT or UNX.
338 #endif
341 namespace csv
343 namespace ploc
346 const Path &
347 Directory::inq_MyPath() const
349 return aPath;
354 } // namespace ploc
355 } // namespace csv
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */