niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
henrike@webrtc.org | 143abd9 | 2012-02-08 19:39:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Karl Wiberg | 6a4d411 | 2018-03-23 10:39:34 +0100 | [diff] [blame] | 11 | #include "rtc_base/system/file_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #ifdef _WIN32 |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 14 | #include <Windows.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | #else |
andrew@webrtc.org | 114c790 | 2011-12-10 02:33:33 +0000 | [diff] [blame] | 16 | #include <string.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | #endif |
| 18 | |
Karl Wiberg | 6a4d411 | 2018-03-23 10:39:34 +0100 | [diff] [blame] | 19 | #include <utility> |
| 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | namespace webrtc { |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | FILE* FileOpen(const char* file_name_utf8, bool read_only) { |
| 24 | #if defined(_WIN32) |
| 25 | int len = MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, nullptr, 0); |
| 26 | std::wstring wstr(len, 0); |
| 27 | MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, &wstr[0], len); |
| 28 | FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | #else |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 30 | FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | #endif |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 32 | return file; |
| 33 | } |
| 34 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 36 | // static |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 37 | FileWrapper FileWrapper::OpenReadOnly(const char* file_name_utf8) { |
| 38 | return FileWrapper(FileOpen(file_name_utf8, true)); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 39 | } |
| 40 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 41 | // static |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 42 | FileWrapper FileWrapper::OpenWriteOnly(const char* file_name_utf8) { |
| 43 | return FileWrapper(FileOpen(file_name_utf8, false)); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 44 | } |
| 45 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 46 | FileWrapper::FileWrapper(FileWrapper&& other) { |
| 47 | operator=(std::move(other)); |
phoglund@webrtc.org | 740be44 | 2012-12-12 12:52:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 50 | FileWrapper& FileWrapper::operator=(FileWrapper&& other) { |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 51 | Close(); |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 52 | file_ = other.file_; |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 53 | other.file_ = nullptr; |
| 54 | return *this; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | } |
andrew@webrtc.org | 5ae19de | 2011-12-13 22:59:33 +0000 | [diff] [blame] | 56 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 57 | bool FileWrapper::Rewind() { |
| 58 | RTC_DCHECK(file_); |
| 59 | return fseek(file_, 0, SEEK_SET) == 0; |
henrike@webrtc.org | 9a6dac4 | 2012-09-27 22:20:34 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 62 | bool FileWrapper::Flush() { |
| 63 | RTC_DCHECK(file_); |
| 64 | return fflush(file_) == 0; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 67 | size_t FileWrapper::Read(void* buf, size_t length) { |
| 68 | RTC_DCHECK(file_); |
| 69 | return fread(buf, 1, length, file_); |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool FileWrapper::Write(const void* buf, size_t length) { |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 73 | RTC_DCHECK(file_); |
| 74 | return fwrite(buf, 1, length, file_) == length; |
| 75 | } |
Niels Moller | 4664727 | 2019-01-18 12:04:43 +0000 | [diff] [blame] | 76 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 77 | bool FileWrapper::Close() { |
Niels Moller | 4664727 | 2019-01-18 12:04:43 +0000 | [diff] [blame] | 78 | if (file_ == nullptr) |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 79 | return true; |
Niels Moller | 4664727 | 2019-01-18 12:04:43 +0000 | [diff] [blame] | 80 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 81 | bool success = fclose(file_) == 0; |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 82 | file_ = nullptr; |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame] | 83 | return success; |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 84 | } |
| 85 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 86 | } // namespace webrtc |