PLCnext API Documentation  22.9.0.33
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: // construction/destruction
18  Directory(void) = delete;
19  Directory(const Directory& arg) = delete;
20  Directory& operator=(const Directory& arg) = delete;
21  ~Directory(void) = delete;
22 
23 public: // static operations
27  static bool Exists(const String& path);
28 
35  static void Create(const String& path);
36 
44  static void Create(const String& path, bool sync);
45 
52  static void Delete(const String& path);
53 
61  static void Delete(const String& path, bool sync);
62 
69  static void Clear(const String& path);
70 
78  static void Clear(const String& path, bool sync);
79 
88  static void Copy(const String& sourcePath, const String& destinationPath, bool clear = false);
89 
99  static void Copy(const String& sourcePath, const String& destinationPath, bool clear, bool sync);
100 
109  static void Move(const String& sourcePath, const String& destinationPath, bool overwrite = false);
110 
120  static void Move(const String& sourcePath, const String& destinationPath, bool overwrite, bool sync);
121 
122  // <summary>Rename a directory or File</summary>
130  ARP_DEPRECATED("Please use Directory::Move() instead of Directory::Rename().")
131  static void Rename(const String& sourcePath, const String& destinationPath);
132 
136  static String GetCurrent(void);
137 
140  static void SyncAll(void);
141 
145  static void Sync(const String& path);
146 
153  static IEnumerator<String>::Ptr GetEnumerator(const String& path, bool recursive = false);
154 
162  static IEnumerator<String>::Ptr GetEnumerator(const String& path, const char* searchPattern, bool recursive = false);
163 
170  static IEnumerator<String>::Ptr GetFileEnumerator(const String& path, bool recursive = false);
171 
179  static IEnumerator<String>::Ptr GetFileEnumerator(const String& path, const char* searchPattern, bool recursive = false);
180 
187  static IEnumerator<String>::Ptr GetDirectoryEnumerator(const String& path, bool recursive = false);
188 
196  static IEnumerator<String>::Ptr GetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive = false);
197 
204  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, bool recursive = false);
205 
213  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, const char* searchPattern, bool recursive = false);
214 
221  static IEnumerator<String>::Ptr TryGetFileEnumerator(const String& path, bool recursive = false);
222 
230  static IEnumerator<String>::Ptr TryGetFileEnumerator(const String& path, const char* searchPattern, bool recursive = false);
231 
239  static IEnumerator<String>::Ptr TryGetDirectoryEnumerator(const String& path, bool recursive = false);
240 
248  static IEnumerator<String>::Ptr TryGetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive = false);
249 
250 private: // static methods
251  static IEnumerator<String>::Ptr GetEnumerator(const String& path, bool recursive, bool files, bool directories);
252  static IEnumerator<String>::Ptr GetSearchEnumerator(const String& path, const char* searchPattern, bool recursive, bool files, bool directories);
253  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, bool recursive, bool files, bool directories);
254  static IEnumerator<String>::Ptr TryGetSearchEnumerator(const String& path, const char* searchPattern, bool recursive, bool files, bool directories);
255 
256 private: // nested class Enumerator
257  template<class Iterator>
258  class Enumerator : public IEnumerator<String>
259  {
260  friend class Directory;
261 
262  private: // construction
263  Enumerator(const String& path, bool files, bool directories, bool& error);
264 
265  public: // Implementation of IEnumerator<String>
266  bool MoveNext(void)override;
267  String GetCurrent(void)override;
268 
269  private: // fields
270  Iterator boostIterator;
271  bool initialMoved;
272  bool files;
273  bool directories;
274  };
275 
276 private: // nested class SearchEnumerator
277  template<class Iterator>
278  class SearchEnumerator : public IEnumerator<String>
279  {
280  friend class Directory;
281 
282  private: // construction
283  SearchEnumerator(const String& path, const String& searchPattern, bool files, bool directories, bool& error);
284 
285  public: // Implementation of IEnumerator<String>
286  bool MoveNext(void)override;
287  String GetCurrent(void)override;
288 
289  private: // static methods
290  static String NormalizeSearchPattern(const String& searchPattern);
291 
292  private: // fields
293  boost::regex searchRegex;
294  Iterator boostIterator;
295  bool initialMoved;
296  bool files;
297  bool directories;
298  };
299 
300 private: // nested class Enumerator
301  class EmptyEnumerator : public IEnumerator<String>
302  {
303  friend class Directory;
304 
305  private: // construction
306  EmptyEnumerator() = default;
307 
308  public: // Implementation of IEnumerator<String>
309  bool MoveNext(void)override
310  {
311  return false;
312  }
313  String GetCurrent(void)override
314  {
315  return String::Empty;
316  }
317  };
318 };
319 
321 // inline methods of class Directory
322 
323 inline IEnumerator<String>::Ptr Directory::GetEnumerator(const String& path, bool recursive)
324 {
325  return Directory::GetEnumerator(path, recursive, true, true);
326 }
327 
328 inline IEnumerator<String>::Ptr Directory::GetEnumerator(const String& path, const char* searchPattern, bool recursive)
329 {
330  return Directory::GetSearchEnumerator(path, searchPattern, recursive, true, true);
331 }
332 
334 {
335  return Directory::GetEnumerator(path, recursive, true, false);
336 }
337 
338 inline IEnumerator<String>::Ptr Directory::GetFileEnumerator(const String& path, const char* searchPattern, bool recursive)
339 {
340  return Directory::GetSearchEnumerator(path, searchPattern, recursive, true, false);
341 }
342 
344 {
345  return Directory::GetEnumerator(path, recursive, false, true);
346 }
347 
348 inline IEnumerator<String>::Ptr Directory::GetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive)
349 {
350  return Directory::GetSearchEnumerator(path, searchPattern, recursive, false, true);
351 }
352 
354 {
355  return Directory::TryGetEnumerator(path, recursive, true, true);
356 }
357 
358 inline IEnumerator<String>::Ptr Directory::TryGetEnumerator(const String& path, const char* searchPattern, bool recursive)
359 {
360  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, true, true);
361 }
362 
364 {
365  return Directory::TryGetEnumerator(path, recursive, true, false);
366 }
367 
368 inline IEnumerator<String>::Ptr Directory::TryGetFileEnumerator(const String& path, const char* searchPattern, bool recursive)
369 {
370  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, true, false);
371 }
372 
374 {
375  return Directory::TryGetEnumerator(path, recursive, false, true);
376 }
377 
378 inline IEnumerator<String>::Ptr Directory::TryGetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive)
379 {
380  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, false, true);
381 }
382 
383 }}}} // end of namespace Arp::System::Commons::Io
Declares the interface of the enumerator pattern, which is leaned on .NET enumerator idiom.
Definition: IEnumerator.hxx:48
API for manipulation and examiniation of directories of a file system.
Definition: Directory.hpp:16
static void Create(const String &path)
Creates a new directory. After successful operation, the synchronization of the file system will be f...
static void Copy(const String &sourcePath, const String &destinationPath, bool clear, bool sync)
Copies a directory and its content to a new location.
static void Delete(const String &path)
Deletes a directory. After successful operation, the synchronization of the file system will be force...
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:343
static bool Exists(const String &path)
Checks if a specific directory exists.
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:333
static IEnumerator< String >::Ptr GetEnumerator(const String &path, bool recursive=false)
Returns an enumerator listing the content of a directory.
Definition: Directory.hpp:323
static void Sync(const String &path)
Force file system synchronization for the given file.
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:373
static void Create(const String &path, bool sync)
Creates a new directory.
static void Move(const String &sourcePath, const String &destinationPath, bool overwrite, bool sync)
Moves a directory and its content to a new location.
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:363
static void Clear(const String &path)
Deletes the complete content of a directory. After successful operation, the synchronization of the f...
static void Delete(const String &path, bool sync)
Deletes a directory.
static void Copy(const String &sourcePath, const String &destinationPath, bool clear=false)
Copies a directory and its content to a new location. After successful operation, the synchronization...
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:353
static String GetCurrent(void)
Returns the fully qualified path of the current directory.
static void SyncAll(void)
Force global file system synchronization.
ARP_DEPRECATED("Please use Directory::Move() instead of Directory::Rename().") static void Rename(const String &sourcePath
static void Move(const String &sourcePath, const String &destinationPath, bool overwrite=false)
Moves a directory and its content to a new location. After successful operation, the synchronization ...
static void Clear(const String &path, bool sync)
Deletes the complete content of a directory.
static const SelfType Empty
An emtpy static string instance.
Definition: BasicString.hxx:214
@ System
System components used by the System, Device, Plc or Io domains.
@ Enumerator
Enumerator type, handled by Rsc with IRscReadEnumerator and IRscWriteEnumerator
Root namespace for the PLCnext API