BeFileName.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
12 
13 #include <string.h>
14 #include <stdlib.h>
15 #include "Bentley.h"
16 #include "WString.h"
17 #include "BeStringUtilities.h"
18 
19 struct _FILETIME;
20 
21 #ifndef MAX_PATH
22  #define MAX_PATH 260
23 #endif
24 
26 
29 
30 #if defined (_WIN32) // Windows && WinRT
31  static CharCP const DIR_SEPARATOR = "\\";
32  static char const DIR_SEPARATOR_CHAR = '\\';
33  static WCharCP const WCSDIR_SEPARATOR = L"\\";
34  static WChar const WCSDIR_SEPARATOR_CHAR = L'\\';
35  static char const ALT_DIR_SEPARATOR_CHAR = '/';
36  static WChar const WCSALT_DIR_SEPARATOR_CHAR = L'/';
37  static WCharCP const WCSALT_DIR_SEPARATOR = L"/";
38  static CharCP const ALL_FILES_FILTER = "*.*";
39  static WCharCP const WCSALL_FILES_FILTER = L"*.*";
40  static WCharCP const WCSDLL_EXT = L"dll";
41  static WCharCP const WCSSTLIB_EXT = L"lib";
42 #elif defined (__APPLE__) || defined (ANDROID)
43  static CharCP const DIR_SEPARATOR = "/";
44  static char const DIR_SEPARATOR_CHAR = '/';
45  static WCharCP const WCSDIR_SEPARATOR = L"/";
46  static WChar const WCSDIR_SEPARATOR_CHAR = L'/';
47  static char const ALT_DIR_SEPARATOR_CHAR = '\\';
48  static WChar const WCSALT_DIR_SEPARATOR_CHAR = L'\\';
49  static WCharCP const WCSALT_DIR_SEPARATOR = L"\\";
50  static CharCP const ALL_FILES_FILTER = "*";
51  static WCharCP const WCSALL_FILES_FILTER = L"*";
52  #if defined (__APPLE__)
53  static WCharCP const WCSDLL_EXT = L"dylib";
54  #else
55  static WCharCP const WCSDLL_EXT = L"so";
56  #endif
57  static WCharCP const WCSSTLIB_EXT = L"a";
58 #else
59  #error unknown platform
60 #endif
61 static CharCP const PATH_SEPARATOR = ";";
62 static char const PATH_SEPARATOR_CHAR = ';';
63 
64 static WCharCP const WCSPATH_SEPARATOR = L";";
65 static WChar const WCSPATH_SEPARATOR_CHAR = L';';
66 
67 enum class BeFileNameStatus
68  {
69  Success = SUCCESS,
70  IllegalName = 1,
71  AlreadyExists = 2,
72  CantCreate = 3,
73  FileNotFound = 4,
74  CantDeleteFile = 5,
75  AccessViolation = 6,
76  CantDeleteDir = 7,
78  };
79 
80 enum class BeFileNameAccess
81  {
82  Exists = 0,
83  Read = 4,
84  Write = 2,
85  ReadWrite = BeFileNameAccess::Read|BeFileNameAccess::Write,
86  };
87 
88 /*=================================================================================**/
96 struct BeFileName : WString
97 {
98 
99 public:
101 
102 
104  BeFileName () {}
105 
107  explicit BeFileName (WCharCP name) {SetName(name);}
108 
111  BENTLEYDLL_EXPORT void SupplyDefaultNameParts (WCharCP defaultName);
112 
115  BENTLEYDLL_EXPORT void OverrideNameParts (WCharCP overrideName);
116 
117  enum FileNameParts {Device=1, Directory=2, Basename=4, Extension=8, DevAndDir=(Device|Directory), NameAndExt=(Basename|Extension), All=(DevAndDir|NameAndExt) };
121  BENTLEYDLL_EXPORT BeFileName (FileNameParts mask, WCharCP fullName);
122 
124  BeFileName (WCharCP dev, WCharCP dir, WCharCP name, WCharCP ext) {BuildName (dev, dir, name, ext);}
125 
127  BeFileName (BeFileName const& from) {*this = from;}
128 
130  // BeFileName& operator=(BeFileName const& from) { *this = from; return *this;}
131 
133  void Clear() { clear(); }
134 
137  BENTLEYDLL_EXPORT void SetName (WCharCP name);
138  void SetName (WStringCR name) { assign (name); }
139 
141  BENTLEYDLL_EXPORT void BuildName (WCharCP dev, WCharCP dir, WCharCP name, WCharCP ext);
142 
144  BENTLEYDLL_EXPORT static void BuildName (WStringR combinedPath, WCharCP dev, WCharCP dir, WCharCP name, WCharCP ext);
145 
147  BeFileNameR AppendToPath (BeFileNameCR additionComponent) {return AppendToPath (additionComponent.GetName());}
148 
150  BENTLEYDLL_EXPORT BeFileNameR AppendToPath (WCharCP additionComponent);
151 
153  BENTLEYDLL_EXPORT BeFileNameR AppendExtension (WCharCP extension);
154 
156  BENTLEYDLL_EXPORT static void AppendToPath (WStringR dir, WCharCP additionComponent);
157 
159  BENTLEYDLL_EXPORT static void AppendSeparator (WCharP dir);
160 
162  BENTLEYDLL_EXPORT static void AppendSeparator (WStringR dir);
163 
165  BENTLEYDLL_EXPORT void AppendSeparator ();
166 
168  void AppendString (WCharCP string) { append (string); }
169 
171  BENTLEYDLL_EXPORT static void StripSeparatorAtEnd (WStringR dir);
172 
174  BENTLEYDLL_EXPORT void StripSeparatorAtEnd ();
175 
177  BENTLEYDLL_EXPORT void RemoveQuotes ();
178 
180  void StripWhiteSpace () { Trim (); }
181 
182 
183  // ! Abbreviate
184  BENTLEYDLL_EXPORT void Abbreviate (size_t maxLength);
185 
187 
189 
190 
194  BENTLEYDLL_EXPORT BeFileName (CharCP name, bool isUtf8);
195 
199  BENTLEYDLL_EXPORT BeFileName (CharCP name, BentleyCharEncoding encoding);
200 
202  explicit BeFileName (Utf8StringCR name) {SetNameUtf8(name);}
203 
205  void GetName (WCharP name, size_t size) const { wcsncpy (name, c_str(), size-1); name[size-1] = 0; }
206 
209  BENTLEYDLL_EXPORT void GetNameA (char name[MAX_PATH]) const;
210 
214  BENTLEYDLL_EXPORT void GetNameA (CharP name, size_t numBytes) const;
215 
217  BENTLEYDLL_EXPORT Utf8String GetNameUtf8 () const;
218 
220  BENTLEYDLL_EXPORT Utf8String GetUri () const;
221 
223  BENTLEYDLL_EXPORT void SetNameA (CharCP name);
224 
226  void SetNameUtf8 (Utf8StringCR name) {SetNameUtf8 (name.c_str ());}
227 
229  BENTLEYDLL_EXPORT void SetNameUtf8 (Utf8CP name);
230 
232 
234 
235 
242  BENTLEYDLL_EXPORT static void ParseName (WStringP dev, WStringP dir, WStringP name, WStringP ext, WCharCP fullFileName);
243 
251  BENTLEYDLL_EXPORT static void ParseNameNoClear (WStringP dev, WStringP dir, WStringP name, WStringP ext, WCharCP fullFileName);
252 
258  void ParseName (WStringP dev, WStringP dir, WStringP name, WStringP ext) const {ParseName (dev, dir, name, ext, c_str());}
259 
266  void ParseNameNoClear (WStringP dev, WStringP dir, WStringP name, WStringP ext) const {ParseNameNoClear (dev, dir, name, ext, c_str());}
267 
268 
270  BENTLEYDLL_EXPORT void PopDir();
271 
273  BENTLEYDLL_EXPORT static WString GetDirectoryName (WCharCP path);
274 
276  BENTLEYDLL_EXPORT static WString GetDirectoryWithoutDevice (WCharCP path);
277 
279  BENTLEYDLL_EXPORT static WString GetExtension (WCharCP path);
280 
282  static WString GetFileNameAndExtension (WCharCP path) {return WString(BeFileName(NameAndExt, path));}
283 
285  BENTLEYDLL_EXPORT static WString GetFileNameWithoutExtension (WCharCP path);
286 
288  BENTLEYDLL_EXPORT static WString GetDevice (WCharCP path);
289 
291 
293 
294 
300  BENTLEYDLL_EXPORT static void FindRelativePath (WStringR relativePath, WCharCP targetFileName, WCharCP rootFileName);
301 
309  BENTLEYDLL_EXPORT static BentleyStatus ResolveRelativePath (WStringR fullPath, WCharCP relativeFileName, WCharCP basePath);
310 
318  BENTLEYDLL_EXPORT static BeFileNameStatus FixPathName (WStringR path, WCharCP original);
319 
322  BENTLEYDLL_EXPORT static BeFileNameStatus BeGetFullPathName (WStringR path, WCharCP src);
323 
326  BENTLEYDLL_EXPORT BeFileNameStatus BeGetFullPathName ();
327 
329 
331 
332 
334  BENTLEYDLL_EXPORT static bool IsSymbolicLink(WCharCP);
335 
337  BENTLEYDLL_EXPORT static BentleyStatus GetTargetOfSymbolicLink(BeFileNameR, WCharCP);
338 
340  BENTLEYDLL_EXPORT static BentleyStatus GetTargetOfSymbolicLink(BeFileNameR, WCharCP, bool shouldRecurse);
341 
343 
345 
346 
351  BENTLEYDLL_EXPORT static bool AreSameFile (WCharCP file1, WCharCP file2);
352 
356  BENTLEYDLL_EXPORT static bool DoesPathExist (WCharCP path);
357 
359  BENTLEYDLL_EXPORT bool IsAbsolutePath () const;
360 
362  BENTLEYDLL_EXPORT bool IsUrl() const;
363 
365  BENTLEYDLL_EXPORT static bool IsUrl(WCharCP filename);
366 
368  WCharCP GetName () const {return c_str();}
369 
370  operator WCharCP() const {return c_str();}
371 
373  bool IsEmpty () const { return empty(); }
374 
376  size_t GetNameSize () { return size(); }
377 
379 
381 
382 
386  BENTLEYDLL_EXPORT static bool IsDirectory(WCharCP path);
387 
392  BENTLEYDLL_EXPORT static BeFileNameStatus CreateNewDirectory(WCharCP path);
393 
397  BENTLEYDLL_EXPORT static BeFileNameStatus RemoveDirectory (WCharCP path);
398 
402  BENTLEYDLL_EXPORT static BeFileNameStatus EmptyAndRemoveDirectory(WCharCP path);
403 
409  BENTLEYDLL_EXPORT static BeFileNameStatus CloneDirectory (WCharCP sourceDir, WCharCP destDir, bool includeSubDirs=true);
410 
412 
414 
415 
421  BENTLEYDLL_EXPORT static BeFileNameStatus BeCopyFile (WCharCP existingFileName, WCharCP newFileName, bool failIfFileExists = false);
422 
427  BENTLEYDLL_EXPORT static BeFileNameStatus BeDeleteFile (WCharCP fileNameP, bool removeReadOnlyAttribute = false);
428 
434  BENTLEYDLL_EXPORT static BeFileNameStatus BeMoveFile (WCharCP oldFileName, WCharCP newFileName, int numRetries = 0);
435 
440  BENTLEYDLL_EXPORT static BeFileNameStatus SetFileReadOnly (WCharCP fileName, bool readOnly);
441 
445  BENTLEYDLL_EXPORT static BeFileNameStatus MoveToRecycleBin (WCharCP path);
447 
450 
454  BENTLEYDLL_EXPORT static BeFileNameStatus GetFileSize (UInt64& sz, WCharCP fileName);
455 
462  BENTLEYDLL_EXPORT static BeFileNameStatus GetFileTime (::_FILETIME* ctime, ::_FILETIME* atime, ::_FILETIME* mtime, WCharCP fileName);
463 
469  BENTLEYDLL_EXPORT static BeFileNameStatus SetFileTime (WCharCP fileName, ::_FILETIME const* atime, ::_FILETIME const* mtime);
470 
475  BENTLEYDLL_EXPORT static BeFileNameStatus CheckAccess (WCharCP fileName, BeFileNameAccess accs);
476 
478 
481 
486  BENTLEYDLL_EXPORT static BeFileNameStatus BeGetDiskFreeSpace (UInt64& freeBytes, WCharCP dirName);
487 
491  BENTLEYDLL_EXPORT static bool IsRemovableMedia (WCharCP fn);
492 
498  BENTLEYDLL_EXPORT static BentleyStatus BeCompactPath (WStringR shortened, WCharCP path, UInt32 length);
499 
506  BENTLEYDLL_EXPORT static BeFileNameStatus BeGetTempFileName (BeFileName& tempFileName, BeFileName const& pathName, WCharCP prefixString);
507 
511  BENTLEYDLL_EXPORT static BeFileNameStatus BeGetTempPath (BeFileName& tempFileName);
512 
513 
515 
516 };
517 
522 BENTLEYDLL_EXPORT BentleyStatus BeGetModuleFileName (BeFileName& filename, void* module);
523 
526 BENTLEYDLL_EXPORT UInt32 BeGetCurrentProcessId ();
527 
530 BENTLEYDLL_EXPORT WString BeGetCwd ();
531 
533 
534 
536 
char * CharP
Definition: Bentley.h:227
bool empty() const
Definition: stdcxx/bstdmap.h:210
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
struct Bentley::BeFileName const & BeFileNameCR
Definition: Bentley.h:242
Definition: Bentley.h:212
wchar_t * WCharP
Definition: Bentley.h:225
wchar_t WChar
Definition: Bentley.h:223
Definition: TextTableHandler.h:216
Open for write access.
struct Bentley::WString const & WStringCR
Definition: Bentley.h:239
struct Bentley::Utf8String const & Utf8StringCR
Definition: Bentley.h:241
wchar_t const * WCharCP
Definition: Bentley.h:224
struct Bentley::WString * WStringP
Definition: Bentley.h:239
uint32_t UInt32
Definition: Bentley.r.h:128
struct Bentley::WString & WStringR
Definition: Bentley.h:239
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
Provides Bentley specific string utility functions (Bentley/BeStringUtilities.h). ...
BentleyStatus
Definition: Bentley.h:208
Open for both read and write.
BentleyCharEncoding
Definition: Bentley.h:216
Defines typedefs and constants that can be used across other namespaces. All Bentley-authored C++ sou...
Open for read access.
uint64_t UInt64
Definition: Bentley.r.h:131
Utf8Char const * Utf8CP
Definition: Bentley.h:229
char const * CharCP
Definition: Bentley.h:226
struct Bentley::BeFileName & BeFileNameR
Definition: Bentley.h:242
Definition: Bentley.h:210
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
void clear()
Definition: stdcxx/bstdmap.h:257
size_type size() const
Definition: stdcxx/bstdmap.h:214

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.