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()
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(UNICODE) && !defined(ZENLIB_USEWX)
53 // 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
54 // 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
55 // page exists. Ztring::From_Unicode() gives this name as UTF-8 text, as FileName_From_Native(FileName_To_Native()) on the other systems.
56 std::wstring FileName_To_Wide_Existing(const Ztring &File_Name);
57 #endif
58 
59 //***************************************************************************
60 // Directorues
61 //***************************************************************************
62 
63 Ztring OpenFolder_Show(void* Handle, const Ztring &Title, const Ztring &Caption);
64 
65 //***************************************************************************
66 // WinRT Helpers
67 //***************************************************************************
68 #ifdef WINDOWS_UWP
69 template <typename T>
70 inline HRESULT Await(const Microsoft::WRL::ComPtr<T> &Operation)
71 {
72  if (!Operation)
73  return E_POINTER;
74 
75  Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncInfo> Info;
76  HRESULT Result=Operation.As(&Info);
77  if (FAILED(Result))
78  return Result;
79 
80  ABI::Windows::Foundation::AsyncStatus Status;
81  while (SUCCEEDED(Result=Info->get_Status(&Status)) && Status==ABI::Windows::Foundation::AsyncStatus::Started)
82  Sleep(0); //Yield
83 
84  if (FAILED(Result) || Status!=ABI::Windows::Foundation::AsyncStatus::Completed) {
85  HRESULT Error_Code;
86  Result=Info->get_ErrorCode(&Error_Code);
87  if (FAILED(Result))
88  return Result;
89  return Error_Code;
90  }
91 
92  return Result;
93 }
94 
95 // Name for the WinRT functions, e.g. Get_File(WinRT_Name(File_Name).Ref(), File): in wide characters, also without Unicode (the UTF-8
96 // text converted, see Ztring::To_Unicode()). The reference given by Ref() is valid while this object exists, e.g. until the end of the
97 // expression which creates it
98 class WinRT_Name
99 {
100 public:
101  explicit WinRT_Name(const Ztring &Name) : Wide(Name.To_Unicode()) {}
102  Microsoft::WRL::Wrappers::HStringReference Ref() const {return Microsoft::WRL::Wrappers::HStringReference(Wide.c_str(), (unsigned int)Wide.size());}
103 
104 private:
105  const std::wstring Wide;
106 };
107 
108 HRESULT Add_Item_To_FUA(Microsoft::WRL::Wrappers::HStringReference Path, Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageItem> &Item);
109 HRESULT Get_File(Microsoft::WRL::Wrappers::HStringReference File_Name, Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageFile> &File);
110 HRESULT Get_Folder(Microsoft::WRL::Wrappers::HStringReference Folder_Name, Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageFolder> &Folder);
111 #endif
112 
113 } //namespace ZenLib
114 #endif
String manipulation (based on std::(w)string)
Definition: Ztring.h:50
Definition: BitStream.h:24
Ztring FileName_From_Native(const char *File_Name)
Ztring OpenFolder_Show(void *Handle, const Ztring &Title, const Ztring &Caption)
void Shell_Execute(const Ztring &ToExecute)
std::string FileName_To_Native(const Ztring &File_Name)
bool IsWin9X()