mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 22:00:47 +01:00

This is a reland of 80b95de765
Original change's description:
> Trim down FileWrapper class to be merely a wrapper owning a FILE*
>
> Bug: webrtc:6463
> Change-Id: If71e2f3a75dc1863bc805ab71de1e2d33294f805
> Reviewed-on: https://webrtc-review.googlesource.com/c/117881
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Reviewed-by: Alex Loiko <aleloi@webrtc.org>
> Commit-Queue: Niels Moller <nisse@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#26311}
Bug: webrtc:6463
Change-Id: I12154ef65744c1b7811974a1d871e05ed3fbbc27
Reviewed-on: https://webrtc-review.googlesource.com/c/118660
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26337}
86 lines
2 KiB
C++
86 lines
2 KiB
C++
/*
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "rtc_base/system/file_wrapper.h"
|
|
|
|
#ifdef _WIN32
|
|
#include <Windows.h>
|
|
#else
|
|
#include <string.h>
|
|
#endif
|
|
|
|
#include <utility>
|
|
|
|
namespace webrtc {
|
|
namespace {
|
|
FILE* FileOpen(const char* file_name_utf8, bool read_only) {
|
|
#if defined(_WIN32)
|
|
int len = MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, nullptr, 0);
|
|
std::wstring wstr(len, 0);
|
|
MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, &wstr[0], len);
|
|
FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb");
|
|
#else
|
|
FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb");
|
|
#endif
|
|
return file;
|
|
}
|
|
} // namespace
|
|
|
|
// static
|
|
FileWrapper FileWrapper::OpenReadOnly(const char* file_name_utf8) {
|
|
return FileWrapper(FileOpen(file_name_utf8, true));
|
|
}
|
|
|
|
// static
|
|
FileWrapper FileWrapper::OpenWriteOnly(const char* file_name_utf8) {
|
|
return FileWrapper(FileOpen(file_name_utf8, false));
|
|
}
|
|
|
|
FileWrapper::FileWrapper(FileWrapper&& other) {
|
|
operator=(std::move(other));
|
|
}
|
|
|
|
FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
|
|
Close();
|
|
file_ = other.file_;
|
|
other.file_ = nullptr;
|
|
return *this;
|
|
}
|
|
|
|
bool FileWrapper::Rewind() {
|
|
RTC_DCHECK(file_);
|
|
return fseek(file_, 0, SEEK_SET) == 0;
|
|
}
|
|
|
|
bool FileWrapper::Flush() {
|
|
RTC_DCHECK(file_);
|
|
return fflush(file_) == 0;
|
|
}
|
|
|
|
size_t FileWrapper::Read(void* buf, size_t length) {
|
|
RTC_DCHECK(file_);
|
|
return fread(buf, 1, length, file_);
|
|
}
|
|
|
|
bool FileWrapper::Write(const void* buf, size_t length) {
|
|
RTC_DCHECK(file_);
|
|
return fwrite(buf, 1, length, file_) == length;
|
|
}
|
|
|
|
bool FileWrapper::Close() {
|
|
if (file_ == nullptr)
|
|
return true;
|
|
|
|
bool success = fclose(file_) == 0;
|
|
file_ = nullptr;
|
|
return success;
|
|
}
|
|
|
|
} // namespace webrtc
|