ZenLib
OS_Utils.h
Go to the documentation of this file.
1 /* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  * Use of this source code is governed by a zlib-style license that can
4  * be found in the License.txt file in the root of the source tree.
5  */
6 
7 //---------------------------------------------------------------------------
8 #ifndef ZenOS_UtilsH
9 #define ZenOS_UtilsH
10 //---------------------------------------------------------------------------
11 
12 //---------------------------------------------------------------------------
13 #include "ZenLib/Ztring.h"
14 //---------------------------------------------------------------------------
15 
16 //---------------------------------------------------------------------------
17 #ifdef WINDOWS_UWP
18 #ifndef _HIDE_GLOBAL_ASYNC_STATUS
19 #define _HIDE_GLOBAL_ASYNC_STATUS //Else AsyncInfo.h declares a global Error (and Started, Completed, Canceled), ambiguous with ZenLib::Error with "using namespace ZenLib"
20 #endif
21 #include <wrl.h>
22 #include <windows.foundation.h>
23 #include <windows.storage.h>
24 #endif
25 //---------------------------------------------------------------------------
26 
27 namespace ZenLib
28 {
29 
30 //***************************************************************************
31 // OS Information
32 //***************************************************************************
33 
34 //---------------------------------------------------------------------------
35 bool IsWin9X ();
36 // Execute
37 //***************************************************************************
38 
39 void Shell_Execute(const Ztring &ToExecute);
40 
41 //***************************************************************************
42 // File names
43 //***************************************************************************
44 
45 // Names for the narrow functions of the C library or of the system (e.g. fopen(), the arguments of main()), in the encoding of the locale
46 // (the code page on Windows), or UTF-8 where the file names are UTF-8 (e.g. macOS). Without Unicode, the strings of ZenLib, also the names
47 // it gives (e.g. Dir::GetAllFileNames()), are UTF-8 text: a name for these functions, or to show in the encoding of the locale, must be
48 // converted with FileName_To_Native() or Ztring::To_Local(). FileName_From_Native(NULL) is an empty name, as Ztring::From_Local(NULL).
49 std::string FileName_To_Native(const Ztring &File_Name);
50 Ztring FileName_From_Native(const char* File_Name);
51 
52 #if defined(WINDOWS) && !defined(WINDOWS_UWP) && !defined(ZENLIB_USEWX)
53 // Name for the wide functions of Windows (e.g. CreateFileW()), also without Unicode (UTF-8 text). If the process does not support long
54 // paths (Windows before 10 version 1607, LongPathsEnabled not set in the registry, or no longPathAware in the manifest of the program, e.g.
55 // a program using the library) and the name is too long for these functions (MAX_PATH characters including the null character, 12 less
56 // to create a directory), the full path with the extended-length prefix ("\\?\", "\\?\UNC\" for a network path), else the name as it is.
57 // The names given by ZenLib (e.g. Dir::GetAllFileNames()) are built from the names given to it, so with this prefix only if given with it.
58 std::wstring FileName_To_Wide(const Ztring &File_Name);
59 #endif
60 
61 #if defined(WINDOWS) && !defined(WINDOWS_UWP) && !defined(UNICODE) && !defined(ZENLIB_USEWX)
62 // Without Unicode on Windows, name of an existing file or directory for the wide functions, as File and Dir open it: UTF-8 text, or the
63 // bytes of the code page given as they are (see Ztring::To_Unicode()), also when these bytes are valid UTF-8 but only the name in the code
64 // page exists. Ztring::From_Unicode() gives this name as UTF-8 text, as FileName_From_Native(FileName_To_Native()) on the other systems.
65 // This name is without the extended-length prefix of FileName_To_Wide().
66 std::wstring FileName_To_Wide_Existing(const Ztring &File_Name);
67 #endif
68 
69 //***************************************************************************
70 // Directorues
71 //***************************************************************************
72 
73 Ztring OpenFolder_Show(void* Handle, const Ztring &Title, const Ztring &Caption);
74 
75 //***************************************************************************
76 // WinRT Helpers
77 //***************************************************************************
78 #ifdef WINDOWS_UWP
79 template <typename T>
80 inline HRESULT Await(const Microsoft::WRL::ComPtr<T> &Operation)
81 {
82  if (!Operation)
83  return E_POINTER;
84 
85  Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncInfo> Info;
86  HRESULT Result=Operation.As(&Info);
87  if (FAILED(Result))
88  return Result;
89 
90  ABI::Windows::Foundation::AsyncStatus Status;
91  while (SUCCEEDED(Result=Info->get_Status(&Status)) && Status==ABI::Windows::Foundation::AsyncStatus::Started)
92  Sleep(0); //Yield
93 
94  if (FAILED(Result) || Status!=ABI::Windows::Foundation::AsyncStatus::Completed) {
95  HRESULT Error_Code;
96  Result=Info->get_ErrorCode(&Error_Code);
97  if (FAILED(Result))
98  return Result;
99  return Error_Code;
100  }
101 
102  return Result;
103 }
104 
105 // Name for the WinRT functions, e.g. Get_File(WinRT_Name(File_Name).Ref(), File): in wide characters, also without Unicode (the UTF-8
106 // text converted, see Ztring::To_Unicode()). The reference given by Ref() is valid while this object exists, e.g. until the end of the
107 // expression which creates it
108 class WinRT_Name
109 {
110 public:
111  explicit WinRT_Name(const Ztring &Name) : Wide(Name.To_Unicode()) {}
112  Microsoft::WRL::Wrappers::HStringReference Ref() const {return Microsoft::WRL::Wrappers::HStringReference(Wide.c_str(), (unsigned int)Wide.size());}
113 
114 private:
115  const std::wstring Wide;
116 };
117 
118 HRESULT Add_Item_To_FUA(Microsoft::WRL::Wrappers::HStringReference Path, Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageItem> &Item);
119 HRESULT Get_File(Microsoft::WRL::Wrappers::HStringReference File_Name, Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageFile> &File);
120 HRESULT Get_Folder(Microsoft::WRL::Wrappers::HStringReference Folder_Name, Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageFolder> &Folder);
121 #endif
122 
123 } //namespace ZenLib
124 #endif
void Shell_Execute(const Ztring &ToExecute)
bool IsWin9X()
Definition: BitStream.h:23
Ztring FileName_From_Native(const char *File_Name)
std::string FileName_To_Native(const Ztring &File_Name)
Ztring OpenFolder_Show(void *Handle, const Ztring &Title, const Ztring &Caption)