-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicReaderWriter.cpp
More file actions
156 lines (139 loc) · 4.24 KB
/
Copy pathBasicReaderWriter.cpp
File metadata and controls
156 lines (139 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#include "pch.h"
#include "BasicReaderWriter.h"
using namespace winrt::Windows::Storage;
using namespace winrt::Windows::Storage::Streams;
using namespace winrt::Windows::ApplicationModel;
BasicReaderWriter::BasicReaderWriter()
{
m_location = Package::Current().InstalledLocation();
}
BasicReaderWriter::BasicReaderWriter(
_In_ winrt::Windows::Storage::StorageFolder folder
)
: m_location(std::move(folder))
{
winrt::hstring path = m_location.Path();
if (path.size() == 0)
{
// Applications are not permitted to access certain
// folders, such as the Documents folder, using this
// code path. In such cases, the Path property for
// the folder will be an empty string.
winrt::throw_hresult(E_FAIL);
}
}
std::vector<byte> BasicReaderWriter::ReadData(
_In_ winrt::hstring const& filename
)
{
CREATEFILE2_EXTENDED_PARAMETERS extendedParams = { 0 };
extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
extendedParams.lpSecurityAttributes = nullptr;
extendedParams.hTemplateFile = nullptr;
winrt::file_handle file{
CreateFile2(
filename.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
OPEN_EXISTING,
&extendedParams
)
};
winrt::check_bool(bool{ file });
if (file.get() == INVALID_HANDLE_VALUE)
{
winrt::throw_hresult(E_FAIL);
}
FILE_STANDARD_INFO fileInfo = { 0 };
if (!GetFileInformationByHandleEx(
file.get(),
FileStandardInfo,
&fileInfo,
sizeof(fileInfo)
))
{
winrt::throw_hresult(E_FAIL);
}
if (fileInfo.EndOfFile.HighPart != 0)
{
winrt::throw_hresult(E_OUTOFMEMORY);
}
std::vector<byte> fileData(fileInfo.EndOfFile.LowPart);
if (!ReadFile(
file.get(),
fileData.data(),
(DWORD)fileData.size(),
nullptr,
nullptr
))
{
winrt::throw_hresult(E_FAIL);
}
return fileData;
}
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::Streams::IBuffer> BasicReaderWriter::ReadDataAsync(
_In_ winrt::hstring const& filename
)
{
StorageFile file{ co_await m_location.GetFileAsync(filename) };
co_return co_await FileIO::ReadBufferAsync(file);
}
uint32_t BasicReaderWriter::WriteData(
_In_ winrt::hstring const& filename,
_In_ std::vector<byte> const& fileData
)
{
CREATEFILE2_EXTENDED_PARAMETERS extendedParams = { 0 };
extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
extendedParams.lpSecurityAttributes = nullptr;
extendedParams.hTemplateFile = nullptr;
winrt::file_handle file{
CreateFile2(
filename.c_str(),
GENERIC_WRITE,
0,
CREATE_ALWAYS,
&extendedParams
)
};
winrt::check_bool(bool{ file });
if (file.get() == INVALID_HANDLE_VALUE)
{
winrt::throw_hresult(E_FAIL);
}
DWORD numBytesWritten{ 0 };
if (
!WriteFile(
file.get(),
fileData.data(),
(DWORD)fileData.size(),
&numBytesWritten,
nullptr
) ||
numBytesWritten != fileData.size()
)
{
winrt::throw_hresult(E_FAIL);
}
return numBytesWritten;
}
winrt::Windows::Foundation::IAsyncAction BasicReaderWriter::WriteDataAsync(
_In_ winrt::hstring const& filename,
_In_ std::vector<byte> const& fileData
)
{
StorageFile file{ co_await m_location.CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting) };
co_await FileIO::WriteBytesAsync(file, fileData);
}