PLCnext API Documentation  20.3.1.28622
Directory.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/IEnumerator.hxx"
9 #include <boost/regex.hpp>
10 
11 namespace Arp { namespace System { namespace Commons { namespace Io
12 {
13 
15 class Directory
16 {
17 public: // typedefs
18 
19 public: // construction/destruction
20 
22  Directory(void) = delete;
23 
25  Directory(const Directory& arg) = delete;
26 
28  Directory& operator=(const Directory& arg) = delete;
29 
31  ~Directory(void) = delete;
32 
33 public: // static operations
34 
38  static bool Exists(const String& path);
39 
46  static void Create(const String& path);
47 
54  static void Delete(const String& path);
55 
62  static void Clear(const String& path);
63 
72  static void Copy(const String& sourcePath, const String& destinationPath, bool clear = false);
73 
82  static void Move(const String& sourcePath, const String& destinationPath, bool clear = false);
83 
84  // <summary>Rename a directory or File</summary>
91  static void Rename(const String& sourcePath, const String& destinationPath);
92 
96  static String GetCurrent(void);
97 
104  static IEnumerator<String>::Ptr GetEnumerator(const String& path, bool recursive = false);
105 
113  static IEnumerator<String>::Ptr GetEnumerator(const String& path, const char* searchPattern, bool recursive = false);
114 
121  static IEnumerator<String>::Ptr GetFileEnumerator(const String& path, bool recursive = false);
122 
130  static IEnumerator<String>::Ptr GetFileEnumerator(const String& path, const char* searchPattern, bool recursive = false);
131 
138  static IEnumerator<String>::Ptr GetDirectoryEnumerator(const String& path, bool recursive = false);
139 
147  static IEnumerator<String>::Ptr GetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive = false);
148 
155  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, bool recursive = false);
156 
164  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, const char* searchPattern, bool recursive = false);
165 
172  static IEnumerator<String>::Ptr TryGetFileEnumerator(const String& path, bool recursive = false);
173 
181  static IEnumerator<String>::Ptr TryGetFileEnumerator(const String& path, const char* searchPattern, bool recursive = false);
182 
190  static IEnumerator<String>::Ptr TryGetDirectoryEnumerator(const String& path, bool recursive = false);
191 
199  static IEnumerator<String>::Ptr TryGetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive = false);
200 
201 private: // static methods
202  static IEnumerator<String>::Ptr GetEnumerator(const String& path, bool recursive, bool files, bool directories);
203  static IEnumerator<String>::Ptr GetSearchEnumerator(const String& path, const char* searchPattern, bool recursive, bool files, bool directories);
204  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, bool recursive, bool files, bool directories);
205  static IEnumerator<String>::Ptr TryGetSearchEnumerator(const String& path, const char* searchPattern, bool recursive, bool files, bool directories);
206 
207 private: // nested class Enumerator
208  template<class Iterator>
209  class Enumerator : public IEnumerator<String>
210  {
211  friend class Directory;
212 
213  private: // construction
214  Enumerator(const String& path, bool files, bool directories, bool& error);
215 
216  public: // Implementation of IEnumerator<String>
217  bool MoveNext(void)override;
218  String GetCurrent(void)override;
219 
220  private: // fields
221  Iterator boostIterator;
222  bool initialMoved;
223  bool files;
224  bool directories;
225  };
226 
227 private: // nested class SearchEnumerator
228  template<class Iterator>
229  class SearchEnumerator : public IEnumerator<String>
230  {
231  friend class Directory;
232 
233  private: // construction
234  SearchEnumerator(const String& path, const String& searchPattern, bool files, bool directories, bool& error);
235 
236  public: // Implementation of IEnumerator<String>
237  bool MoveNext(void)override;
238  String GetCurrent(void)override;
239 
240  private: // static methods
241  static String NormalizeSearchPattern(const String& searchPattern);
242 
243  private: // fields
244  boost::regex searchRegex;
245  Iterator boostIterator;
246  bool initialMoved;
247  bool files;
248  bool directories;
249  };
250 
251 private: // nested class Enumerator
252  class EmptyEnumerator : public IEnumerator<String>
253  {
254  friend class Directory;
255 
256  private: // construction
257  EmptyEnumerator() = default;
258 
259  public: // Implementation of IEnumerator<String>
260  bool MoveNext(void)override
261  {
262  return false;
263  }
264  String GetCurrent(void)override
265  {
266  return String::Empty;
267  }
268  };
269 };
270 
272 // inline methods of class Directory
273 
274 inline IEnumerator<String>::Ptr Directory::GetEnumerator(const String& path, bool recursive)
275 {
276  return Directory::GetEnumerator(path, recursive, true, true);
277 }
278 
279 inline IEnumerator<String>::Ptr Directory::GetEnumerator(const String& path, const char* searchPattern, bool recursive)
280 {
281  return Directory::GetSearchEnumerator(path, searchPattern, recursive, true, true);
282 }
283 
285 {
286  return Directory::GetEnumerator(path, recursive, true, false);
287 }
288 
289 inline IEnumerator<String>::Ptr Directory::GetFileEnumerator(const String& path, const char* searchPattern, bool recursive)
290 {
291  return Directory::GetSearchEnumerator(path, searchPattern, recursive, true, false);
292 }
293 
295 {
296  return Directory::GetEnumerator(path, recursive, false, true);
297 }
298 
299 inline IEnumerator<String>::Ptr Directory::GetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive)
300 {
301  return Directory::GetSearchEnumerator(path, searchPattern, recursive, false, true);
302 }
303 
305 {
306  return Directory::TryGetEnumerator(path, recursive, true, true);
307 }
308 
309 inline IEnumerator<String>::Ptr Directory::TryGetEnumerator(const String& path, const char* searchPattern, bool recursive)
310 {
311  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, true, true);
312 }
313 
315 {
316  return Directory::TryGetEnumerator(path, recursive, true, false);
317 }
318 
319 inline IEnumerator<String>::Ptr Directory::TryGetFileEnumerator(const String& path, const char* searchPattern, bool recursive)
320 {
321  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, true, false);
322 }
323 
325 {
326  return Directory::TryGetEnumerator(path, recursive, false, true);
327 }
328 
329 inline IEnumerator<String>::Ptr Directory::TryGetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive)
330 {
331  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, false, true);
332 }
333 
334 }}}} // end of namespace Arp::System::Commons::Io
static IEnumerator< String >::Ptr GetDirectoryEnumerator(const String &path, bool recursive=false)
Like Arp::System::Commons::Io::Directory::GetEnumerator but only directories are listed by the return...
Definition: Directory.hpp:294
Directory & operator=(const Directory &arg)=delete
Assignment operator.
static IEnumerator< String >::Ptr GetFileEnumerator(const String &path, bool recursive=false)
Like Arp::System::Commons::Io::Directory::GetEnumerator but only files are listed by the returned enu...
Definition: Directory.hpp:284
Declares the interface of the enumerator pattern, which is leaned on .NET enumerator idiom...
Definition: IEnumerator.hxx:47
static IEnumerator< String >::Ptr TryGetFileEnumerator(const String &path, bool recursive=false)
Like Arp::System::Commons::Io::Directory::GetFileEnumerator but will not throw any exception...
Definition: Directory.hpp:314
~Directory(void)=delete
Destructs this instance and frees all resources.
static IEnumerator< String >::Ptr TryGetDirectoryEnumerator(const String &path, bool recursive=false)
Like Arp::System::Commons::Io::Directory::GetDirectoryEnumerator but will not throw any exception...
Definition: Directory.hpp:324
static void Move(const String &sourcePath, const String &destinationPath, bool clear=false)
Moves a directory and its content to a new location.
static void Delete(const String &path)
Deletes a directory.
static IEnumerator< String >::Ptr GetEnumerator(const String &path, bool recursive=false)
Returns an enumerator listing the content of a directory.
Definition: Directory.hpp:274
Directory(void)=delete
Constructs an Directory instance.
Enumerator type, handled by Rsc with IRscReadEnumerator and IRscWriteEnumerator
Root namespace for the PLCnext API
static void Rename(const String &sourcePath, const String &destinationPath)
static void Create(const String &path)
Creates a new directory.
static void Clear(const String &path)
Deletes the complete content of a directory.
static bool Exists(const String &path)
Checks if a specific directory exists.
static String GetCurrent(void)
Returns the fully qualified path of the current directory.
System components used by the System, Device, Plc or Io domains.
static IEnumerator< String >::Ptr TryGetEnumerator(const String &path, bool recursive=false)
Like Arp::System::Commons::Io::Directory::GetEnumerator but will not throw any exception.
Definition: Directory.hpp:304
API for manipulation and examiniation of directories of a file system.
Definition: Directory.hpp:15
static void Copy(const String &sourcePath, const String &destinationPath, bool clear=false)
Copies a directory and its content to a new location.