OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/base/file.h" | |
12 | |
13 #include <io.h> | |
14 #include "webrtc/base/win32.h" | |
15 | |
16 #include <limits> // NOLINT: win32.h should be considered a system header | |
17 | |
18 #include "webrtc/base/checks.h" | |
19 | |
20 namespace rtc { | |
21 | |
22 size_t File::Write(const uint8_t* data, size_t length) { | |
23 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max()); | |
24 size_t total_written = 0; | |
25 do { | |
26 DWORD written; | |
27 if (!::WriteFile(file_, data + total_written, | |
28 static_cast<DWORD>(length - total_written), &written, | |
29 nullptr)) { | |
30 break; | |
31 } | |
32 total_written += written; | |
33 } while (total_written < length); | |
34 return total_written; | |
35 } | |
36 | |
37 size_t File::Read(uint8_t* buffer, size_t length) { | |
38 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max()); | |
39 size_t total_read = 0; | |
40 do { | |
41 DWORD read; | |
42 if (!::ReadFile(file_, buffer + total_read, | |
43 static_cast<DWORD>(length - total_read), &read, nullptr)) { | |
44 break; | |
45 } | |
46 total_read += read; | |
47 } while (total_read < length); | |
48 return total_read; | |
49 } | |
50 | |
51 size_t File::WriteAt(const uint8_t* data, size_t length, size_t offset) { | |
52 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max()); | |
53 size_t total_written = 0; | |
54 do { | |
55 DWORD written; | |
56 | |
57 LARGE_INTEGER offset_li; | |
58 offset_li.QuadPart = offset + total_written; | |
59 | |
60 OVERLAPPED overlapped = {0}; | |
61 overlapped.Offset = offset_li.LowPart; | |
62 overlapped.OffsetHigh = offset_li.HighPart; | |
63 | |
64 if (!::WriteFile(file_, data + total_written, | |
65 static_cast<DWORD>(length - total_written), &written, | |
66 &overlapped)) { | |
67 break; | |
68 } | |
69 | |
70 total_written += written; | |
71 } while (total_written < length); | |
72 return total_written; | |
73 } | |
74 | |
75 size_t File::ReadAt(uint8_t* buffer, size_t length, size_t offset) { | |
76 RTC_DCHECK_LT(length, std::numeric_limits<DWORD>::max()); | |
77 size_t total_read = 0; | |
78 do { | |
79 DWORD read; | |
80 | |
81 LARGE_INTEGER offset_li; | |
82 offset_li.QuadPart = offset + total_read; | |
83 | |
84 OVERLAPPED overlapped = {0}; | |
85 overlapped.Offset = offset_li.LowPart; | |
86 overlapped.OffsetHigh = offset_li.HighPart; | |
87 | |
88 if (!::ReadFile(file_, buffer + total_read, | |
89 static_cast<DWORD>(length - total_read), &read, | |
90 &overlapped)) { | |
91 break; | |
92 } | |
93 | |
94 total_read += read; | |
95 } while (total_read < length); | |
96 return total_read; | |
97 } | |
98 | |
99 bool File::Seek(size_t offset) { | |
100 LARGE_INTEGER distance; | |
101 distance.QuadPart = offset; | |
102 return SetFilePointerEx(file_, distance, nullptr, FILE_BEGIN) != 0; | |
103 } | |
104 | |
105 bool File::Close() { | |
106 if (file_ == kInvalidPlatformFileValue) | |
107 return false; | |
108 bool ret = CloseHandle(file_) != 0; | |
109 file_ = kInvalidPlatformFileValue; | |
110 return ret; | |
111 } | |
112 | |
113 } // namespace rtc | |
OLD | NEW |