PLCnext API Documentation  21.0.0.35466
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 overwrite = false);
83 
84  // <summary>Rename a directory or File</summary>
92  ARP_DEPRECATED("Please use Directory::Move() instead of Directory::Rename().")
93  static void Rename(const String& sourcePath, const String& destinationPath);
94 
98  static String GetCurrent(void);
99 
106  static IEnumerator<String>::Ptr GetEnumerator(const String& path, bool recursive = false);
107 
115  static IEnumerator<String>::Ptr GetEnumerator(const String& path, const char* searchPattern, bool recursive = false);
116 
123  static IEnumerator<String>::Ptr GetFileEnumerator(const String& path, bool recursive = false);
124 
132  static IEnumerator<String>::Ptr GetFileEnumerator(const String& path, const char* searchPattern, bool recursive = false);
133 
140  static IEnumerator<String>::Ptr GetDirectoryEnumerator(const String& path, bool recursive = false);
141 
149  static IEnumerator<String>::Ptr GetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive = false);
150 
157  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, bool recursive = false);
158 
166  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, const char* searchPattern, bool recursive = false);
167 
174  static IEnumerator<String>::Ptr TryGetFileEnumerator(const String& path, bool recursive = false);
175 
183  static IEnumerator<String>::Ptr TryGetFileEnumerator(const String& path, const char* searchPattern, bool recursive = false);
184 
192  static IEnumerator<String>::Ptr TryGetDirectoryEnumerator(const String& path, bool recursive = false);
193 
201  static IEnumerator<String>::Ptr TryGetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive = false);
202 
203 private: // static methods
204  static IEnumerator<String>::Ptr GetEnumerator(const String& path, bool recursive, bool files, bool directories);
205  static IEnumerator<String>::Ptr GetSearchEnumerator(const String& path, const char* searchPattern, bool recursive, bool files, bool directories);
206  static IEnumerator<String>::Ptr TryGetEnumerator(const String& path, bool recursive, bool files, bool directories);
207  static IEnumerator<String>::Ptr TryGetSearchEnumerator(const String& path, const char* searchPattern, bool recursive, bool files, bool directories);
208 
209 private: // nested class Enumerator
210  template<class Iterator>
211  class Enumerator : public IEnumerator<String>
212  {
213  friend class Directory;
214 
215  private: // construction
216  Enumerator(const String& path, bool files, bool directories, bool& error);
217 
218  public: // Implementation of IEnumerator<String>
219  bool MoveNext(void)override;
220  String GetCurrent(void)override;
221 
222  private: // fields
223  Iterator boostIterator;
224  bool initialMoved;
225  bool files;
226  bool directories;
227  };
228 
229 private: // nested class SearchEnumerator
230  template<class Iterator>
231  class SearchEnumerator : public IEnumerator<String>
232  {
233  friend class Directory;
234 
235  private: // construction
236  SearchEnumerator(const String& path, const String& searchPattern, bool files, bool directories, bool& error);
237 
238  public: // Implementation of IEnumerator<String>
239  bool MoveNext(void)override;
240  String GetCurrent(void)override;
241 
242  private: // static methods
243  static String NormalizeSearchPattern(const String& searchPattern);
244 
245  private: // fields
246  boost::regex searchRegex;
247  Iterator boostIterator;
248  bool initialMoved;
249  bool files;
250  bool directories;
251  };
252 
253 private: // nested class Enumerator
254  class EmptyEnumerator : public IEnumerator<String>
255  {
256  friend class Directory;
257 
258  private: // construction
259  EmptyEnumerator() = default;
260 
261  public: // Implementation of IEnumerator<String>
262  bool MoveNext(void)override
263  {
264  return false;
265  }
266  String GetCurrent(void)override
267  {
268  return String::Empty;
269  }
270  };
271 };
272 
274 // inline methods of class Directory
275 
276 inline IEnumerator<String>::Ptr Directory::GetEnumerator(const String& path, bool recursive)
277 {
278  return Directory::GetEnumerator(path, recursive, true, true);
279 }
280 
281 inline IEnumerator<String>::Ptr Directory::GetEnumerator(const String& path, const char* searchPattern, bool recursive)
282 {
283  return Directory::GetSearchEnumerator(path, searchPattern, recursive, true, true);
284 }
285 
287 {
288  return Directory::GetEnumerator(path, recursive, true, false);
289 }
290 
291 inline IEnumerator<String>::Ptr Directory::GetFileEnumerator(const String& path, const char* searchPattern, bool recursive)
292 {
293  return Directory::GetSearchEnumerator(path, searchPattern, recursive, true, false);
294 }
295 
297 {
298  return Directory::GetEnumerator(path, recursive, false, true);
299 }
300 
301 inline IEnumerator<String>::Ptr Directory::GetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive)
302 {
303  return Directory::GetSearchEnumerator(path, searchPattern, recursive, false, true);
304 }
305 
307 {
308  return Directory::TryGetEnumerator(path, recursive, true, true);
309 }
310 
311 inline IEnumerator<String>::Ptr Directory::TryGetEnumerator(const String& path, const char* searchPattern, bool recursive)
312 {
313  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, true, true);
314 }
315 
317 {
318  return Directory::TryGetEnumerator(path, recursive, true, false);
319 }
320 
321 inline IEnumerator<String>::Ptr Directory::TryGetFileEnumerator(const String& path, const char* searchPattern, bool recursive)
322 {
323  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, true, false);
324 }
325 
327 {
328  return Directory::TryGetEnumerator(path, recursive, false, true);
329 }
330 
331 inline IEnumerator<String>::Ptr Directory::TryGetDirectoryEnumerator(const String& path, const char* searchPattern, bool recursive)
332 {
333  return Directory::TryGetSearchEnumerator(path, searchPattern, recursive, false, true);
334 }
335 
336 }}}} // 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:296
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:286
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:316
~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:326
static void Move(const String &sourcePath, const String &destinationPath, bool overwrite=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:276
static const SelfType Empty
An emtpy static string instance.
Definition: BasicString.hxx:222
Directory(void)=delete
Constructs an Directory instance.
Enumerator type, handled by Rsc with IRscReadEnumerator and IRscWriteEnumerator
Root namespace for the PLCnext API
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:306
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.
ARP_DEPRECATED("Please use Directory::Move() instead of Directory::Rename().") static void Rename(const String &sourcePath