blob: dbea1ca9076a40419be770e0a0e934baf5619d5e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrike@webrtc.org143abd92012-02-08 19:39:38 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Wiberg6a4d4112018-03-23 10:39:34 +010011#include "rtc_base/system/file_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#ifdef _WIN32
andrew@webrtc.org114c7902011-12-10 02:33:33 +000014#include <Windows.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000015#else
andrew@webrtc.org114c7902011-12-10 02:33:33 +000016#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000017#endif
18
Karl Wiberg6a4d4112018-03-23 10:39:34 +010019#include <utility>
20
niklase@google.com470e71d2011-07-07 08:21:25 +000021namespace webrtc {
tommia6219cc2016-06-15 10:30:14 -070022namespace {
23FILE* 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.com470e71d2011-07-07 08:21:25 +000029#else
tommia6219cc2016-06-15 10:30:14 -070030 FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +000031#endif
tommia6219cc2016-06-15 10:30:14 -070032 return file;
33}
34} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000035
tommia6219cc2016-06-15 10:30:14 -070036// static
Niels Möller5a6ae022019-01-21 11:59:10 +010037FileWrapper FileWrapper::OpenReadOnly(const char* file_name_utf8) {
38 return FileWrapper(FileOpen(file_name_utf8, true));
phoglund@webrtc.org740be442012-12-12 12:52:15 +000039}
40
tommia6219cc2016-06-15 10:30:14 -070041// static
Niels Möller5a6ae022019-01-21 11:59:10 +010042FileWrapper FileWrapper::OpenWriteOnly(const char* file_name_utf8) {
43 return FileWrapper(FileOpen(file_name_utf8, false));
phoglund@webrtc.org740be442012-12-12 12:52:15 +000044}
45
tommia6219cc2016-06-15 10:30:14 -070046FileWrapper::FileWrapper(FileWrapper&& other) {
47 operator=(std::move(other));
phoglund@webrtc.org740be442012-12-12 12:52:15 +000048}
49
tommia6219cc2016-06-15 10:30:14 -070050FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
Niels Möller5a6ae022019-01-21 11:59:10 +010051 Close();
tommia6219cc2016-06-15 10:30:14 -070052 file_ = other.file_;
tommia6219cc2016-06-15 10:30:14 -070053 other.file_ = nullptr;
54 return *this;
niklase@google.com470e71d2011-07-07 08:21:25 +000055}
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000056
Niels Möller5a6ae022019-01-21 11:59:10 +010057bool FileWrapper::Rewind() {
58 RTC_DCHECK(file_);
59 return fseek(file_, 0, SEEK_SET) == 0;
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +000060}
61
Niels Möller5a6ae022019-01-21 11:59:10 +010062bool FileWrapper::Flush() {
63 RTC_DCHECK(file_);
64 return fflush(file_) == 0;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065}
66
Niels Möller5a6ae022019-01-21 11:59:10 +010067size_t FileWrapper::Read(void* buf, size_t length) {
68 RTC_DCHECK(file_);
69 return fread(buf, 1, length, file_);
tommia6219cc2016-06-15 10:30:14 -070070}
71
72bool FileWrapper::Write(const void* buf, size_t length) {
Niels Möller5a6ae022019-01-21 11:59:10 +010073 RTC_DCHECK(file_);
74 return fwrite(buf, 1, length, file_) == length;
75}
Niels Moller46647272019-01-18 12:04:43 +000076
Niels Möller5a6ae022019-01-21 11:59:10 +010077bool FileWrapper::Close() {
Niels Moller46647272019-01-18 12:04:43 +000078 if (file_ == nullptr)
Niels Möller5a6ae022019-01-21 11:59:10 +010079 return true;
Niels Moller46647272019-01-18 12:04:43 +000080
Niels Möller5a6ae022019-01-21 11:59:10 +010081 bool success = fclose(file_) == 0;
tommia6219cc2016-06-15 10:30:14 -070082 file_ = nullptr;
Niels Möller5a6ae022019-01-21 11:59:10 +010083 return success;
tommia6219cc2016-06-15 10:30:14 -070084}
85
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000086} // namespace webrtc