-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicReaderWriter.cpp
More file actions
169 lines (152 loc) · 4.81 KB
/
BasicReaderWriter.cpp
File metadata and controls
169 lines (152 loc) · 4.81 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
157
158
159
160
161
162
163
164
165
166
167
168
169
//// 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 Microsoft::WRL;
using namespace Windows::Storage;
using namespace Windows::Storage::FileProperties;
using namespace Windows::Storage::Streams;
using namespace Windows::Foundation;
using namespace Windows::ApplicationModel;
using namespace concurrency;
BasicReaderWriter::BasicReaderWriter()
{
m_location = Package::Current->InstalledLocation;
m_locationPath = Platform::String::Concat(m_location->Path, "\\");
}
BasicReaderWriter::BasicReaderWriter(
_In_ Windows::Storage::StorageFolder^ folder
)
{
m_location = folder;
Platform::String^ path = m_location->Path;
if (path->Length() == 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.
throw ref new Platform::FailureException();
}
m_locationPath = Platform::String::Concat(path, "\\");
}
Platform::Array<byte>^ BasicReaderWriter::ReadData(
_In_ Platform::String^ 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;
Wrappers::FileHandle file(
CreateFile2(
Platform::String::Concat(m_locationPath, filename)->Data(),
GENERIC_READ,
FILE_SHARE_READ,
OPEN_EXISTING,
&extendedParams
)
);
if (file.Get() == INVALID_HANDLE_VALUE)
{
throw ref new Platform::FailureException();
}
FILE_STANDARD_INFO fileInfo = {0};
if (!GetFileInformationByHandleEx(
file.Get(),
FileStandardInfo,
&fileInfo,
sizeof(fileInfo)
))
{
throw ref new Platform::FailureException();
}
if (fileInfo.EndOfFile.HighPart != 0)
{
throw ref new Platform::OutOfMemoryException();
}
Platform::Array<byte>^ fileData = ref new Platform::Array<byte>(fileInfo.EndOfFile.LowPart);
if (!ReadFile(
file.Get(),
fileData->Data,
fileData->Length,
nullptr,
nullptr
))
{
throw ref new Platform::FailureException();
}
return fileData;
}
task<Platform::Array<byte>^> BasicReaderWriter::ReadDataAsync(
_In_ Platform::String^ filename
)
{
return task<StorageFile^>(m_location->GetFileAsync(filename)).then([=](StorageFile^ file)
{
return FileIO::ReadBufferAsync(file);
}).then([=](IBuffer^ buffer)
{
auto fileData = ref new Platform::Array<byte>(buffer->Length);
DataReader::FromBuffer(buffer)->ReadBytes(fileData);
return fileData;
});
}
uint32 BasicReaderWriter::WriteData(
_In_ Platform::String^ filename,
_In_ const Platform::Array<byte>^ 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;
Wrappers::FileHandle file(
CreateFile2(
Platform::String::Concat(m_locationPath, filename)->Data(),
GENERIC_WRITE,
0,
CREATE_ALWAYS,
&extendedParams
)
);
if (file.Get() == INVALID_HANDLE_VALUE)
{
throw ref new Platform::FailureException();
}
DWORD numBytesWritten;
if (
!WriteFile(
file.Get(),
fileData->Data,
fileData->Length,
&numBytesWritten,
nullptr
) ||
numBytesWritten != fileData->Length
)
{
throw ref new Platform::FailureException();
}
return numBytesWritten;
}
task<void> BasicReaderWriter::WriteDataAsync(
_In_ Platform::String^ filename,
_In_ const Platform::Array<byte>^ fileData
)
{
return task<StorageFile^>(m_location->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting)).then([=](StorageFile^ file)
{
FileIO::WriteBytesAsync(file, fileData);
});
}