Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(989)

Unified Diff: webrtc/system_wrappers/source/file_impl.cc

Issue 2054373002: FileWrapper[Impl] modifications and actually remove the "Impl" class. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix use of ASSERT instead of ASSERT_TRUE in test Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/system_wrappers/source/file_impl.h ('k') | webrtc/system_wrappers/source/trace_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/system_wrappers/source/file_impl.cc
diff --git a/webrtc/system_wrappers/source/file_impl.cc b/webrtc/system_wrappers/source/file_impl.cc
index e033c0eb7347f86227d0114e24b74fcf4502dd45..8e1ba24f8df72b3596679954eee5e77e7fc65464 100644
--- a/webrtc/system_wrappers/source/file_impl.cc
+++ b/webrtc/system_wrappers/source/file_impl.cc
@@ -8,9 +8,7 @@
* be found in the AUTHORS file in the root of the source tree.
*/
-#include "webrtc/system_wrappers/source/file_impl.h"
-
-#include <assert.h>
+#include "webrtc/system_wrappers/include/file_wrapper.h"
#ifdef _WIN32
#include <Windows.h>
@@ -20,258 +18,135 @@
#endif
#include "webrtc/base/checks.h"
-#include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
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::Create() {
- return new FileWrapperImpl();
+ return new FileWrapper();
}
-FileWrapperImpl::FileWrapperImpl()
- : rw_lock_(RWLockWrapper::CreateRWLock()),
- id_(NULL),
- managed_file_handle_(true),
- open_(false),
- looping_(false),
- read_only_(false),
- max_size_in_bytes_(0),
- size_in_bytes_(0) {
- memset(file_name_utf8_, 0, kMaxFileNameSize);
+// static
+FileWrapper FileWrapper::Open(const char* file_name_utf8, bool read_only) {
+ return FileWrapper(FileOpen(file_name_utf8, read_only), 0);
}
-FileWrapperImpl::~FileWrapperImpl() {
- if (id_ != NULL && managed_file_handle_) {
- fclose(id_);
- }
-}
+FileWrapper::FileWrapper() {}
-int FileWrapperImpl::CloseFile() {
- WriteLockScoped write(*rw_lock_);
- return CloseFileImpl();
+FileWrapper::FileWrapper(FILE* file, size_t max_size)
+ : file_(file), max_size_in_bytes_(max_size) {}
+
+FileWrapper::~FileWrapper() {
+ CloseFileImpl();
}
-int FileWrapperImpl::Rewind() {
- WriteLockScoped write(*rw_lock_);
- if (looping_ || !read_only_) {
- if (id_ != NULL) {
- size_in_bytes_ = 0;
- return fseek(id_, 0, SEEK_SET);
- }
- }
- return -1;
+FileWrapper::FileWrapper(FileWrapper&& other) {
+ operator=(std::move(other));
}
-int FileWrapperImpl::SetMaxFileSize(size_t bytes) {
- WriteLockScoped write(*rw_lock_);
- max_size_in_bytes_ = bytes;
- return 0;
+FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
+ file_ = other.file_;
+ max_size_in_bytes_ = other.max_size_in_bytes_;
+ position_ = other.position_;
+ other.file_ = nullptr;
+ return *this;
}
-int FileWrapperImpl::Flush() {
- WriteLockScoped write(*rw_lock_);
- return FlushImpl();
+void FileWrapper::CloseFile() {
+ rtc::CritScope lock(&lock_);
+ CloseFileImpl();
}
-int FileWrapperImpl::FileName(char* file_name_utf8, size_t size) const {
- ReadLockScoped read(*rw_lock_);
- size_t length = strlen(file_name_utf8_);
- if (length > kMaxFileNameSize) {
- assert(false);
- return -1;
- }
- if (length < 1) {
- return -1;
+int FileWrapper::Rewind() {
+ rtc::CritScope lock(&lock_);
+ if (file_ != nullptr) {
+ position_ = 0;
+ return fseek(file_, 0, SEEK_SET);
}
+ return -1;
+}
- // Make sure to NULL terminate
- if (size < length) {
- length = size - 1;
- }
- memcpy(file_name_utf8, file_name_utf8_, length);
- file_name_utf8[length] = 0;
- return 0;
+void FileWrapper::SetMaxFileSize(size_t bytes) {
+ rtc::CritScope lock(&lock_);
+ max_size_in_bytes_ = bytes;
}
-bool FileWrapperImpl::Open() const {
- ReadLockScoped read(*rw_lock_);
- return open_;
+int FileWrapper::Flush() {
+ rtc::CritScope lock(&lock_);
+ return FlushImpl();
}
-int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only,
- bool loop, bool text) {
- WriteLockScoped write(*rw_lock_);
- if (id_ != NULL && !managed_file_handle_)
- return -1;
+bool FileWrapper::OpenFile(const char* file_name_utf8, bool read_only) {
size_t length = strlen(file_name_utf8);
- if (length > kMaxFileNameSize - 1) {
- return -1;
- }
-
- read_only_ = read_only;
-
- FILE* tmp_id = NULL;
-#if defined _WIN32
- wchar_t wide_file_name[kMaxFileNameSize];
- wide_file_name[0] = 0;
+ if (length > kMaxFileNameSize - 1)
+ return false;
- MultiByteToWideChar(CP_UTF8,
- 0, // UTF8 flag
- file_name_utf8,
- -1, // Null terminated string
- wide_file_name,
- kMaxFileNameSize);
- if (text) {
- if (read_only) {
- tmp_id = _wfopen(wide_file_name, L"rt");
- } else {
- tmp_id = _wfopen(wide_file_name, L"wt");
- }
- } else {
- if (read_only) {
- tmp_id = _wfopen(wide_file_name, L"rb");
- } else {
- tmp_id = _wfopen(wide_file_name, L"wb");
- }
- }
-#else
- if (text) {
- if (read_only) {
- tmp_id = fopen(file_name_utf8, "rt");
- } else {
- tmp_id = fopen(file_name_utf8, "wt");
- }
- } else {
- if (read_only) {
- tmp_id = fopen(file_name_utf8, "rb");
- } else {
- tmp_id = fopen(file_name_utf8, "wb");
- }
- }
-#endif
+ rtc::CritScope lock(&lock_);
+ if (file_ != nullptr)
+ return false;
- if (tmp_id != NULL) {
- // +1 comes from copying the NULL termination character.
- memcpy(file_name_utf8_, file_name_utf8, length + 1);
- if (id_ != NULL) {
- fclose(id_);
- }
- id_ = tmp_id;
- managed_file_handle_ = true;
- looping_ = loop;
- open_ = true;
- return 0;
- }
- return -1;
+ file_ = FileOpen(file_name_utf8, read_only);
+ return file_ != nullptr;
}
-int FileWrapperImpl::OpenFromFileHandle(FILE* handle,
- bool manage_file,
- bool read_only,
- bool loop) {
- WriteLockScoped write(*rw_lock_);
+bool FileWrapper::OpenFromFileHandle(FILE* handle) {
if (!handle)
- return -1;
-
- if (id_ != NULL) {
- if (managed_file_handle_)
- fclose(id_);
- else
- return -1;
- }
-
- id_ = handle;
- managed_file_handle_ = manage_file;
- read_only_ = read_only;
- looping_ = loop;
- open_ = true;
- return 0;
+ return false;
+ rtc::CritScope lock(&lock_);
+ CloseFileImpl();
+ file_ = handle;
+ return true;
}
-int FileWrapperImpl::Read(void* buf, size_t length) {
- WriteLockScoped write(*rw_lock_);
- if (id_ == NULL)
+int FileWrapper::Read(void* buf, size_t length) {
+ rtc::CritScope lock(&lock_);
+ if (file_ == nullptr)
return -1;
- size_t bytes_read = fread(buf, 1, length, id_);
- if (bytes_read != length && !looping_) {
- CloseFileImpl();
- }
+ size_t bytes_read = fread(buf, 1, length, file_);
return static_cast<int>(bytes_read);
}
-int FileWrapperImpl::WriteText(const char* format, ...) {
- WriteLockScoped write(*rw_lock_);
- if (format == NULL)
- return -1;
-
- if (read_only_)
- return -1;
-
- if (id_ == NULL)
- return -1;
-
- va_list args;
- va_start(args, format);
- int num_chars = vfprintf(id_, format, args);
- va_end(args);
-
- if (num_chars >= 0) {
- return num_chars;
- } else {
- CloseFileImpl();
- return -1;
- }
-}
-
-bool FileWrapperImpl::Write(const void* buf, size_t length) {
- WriteLockScoped write(*rw_lock_);
- if (buf == NULL)
+bool FileWrapper::Write(const void* buf, size_t length) {
+ if (buf == nullptr)
return false;
- if (read_only_)
- return false;
+ rtc::CritScope lock(&lock_);
- if (id_ == NULL)
+ if (file_ == nullptr)
return false;
// Check if it's time to stop writing.
- if (max_size_in_bytes_ > 0 &&
- (size_in_bytes_ + length) > max_size_in_bytes_) {
- FlushImpl();
+ if (max_size_in_bytes_ > 0 && (position_ + length) > max_size_in_bytes_)
return false;
- }
- size_t num_bytes = fwrite(buf, 1, length, id_);
- size_in_bytes_ += num_bytes;
- if (num_bytes != length) {
- CloseFileImpl();
- return false;
- }
- return true;
-}
+ size_t num_bytes = fwrite(buf, 1, length, file_);
+ position_ += num_bytes;
-int FileWrapperImpl::CloseFileImpl() {
- if (id_ != NULL) {
- if (managed_file_handle_)
- fclose(id_);
- id_ = NULL;
- }
- memset(file_name_utf8_, 0, kMaxFileNameSize);
- open_ = false;
- return 0;
+ return num_bytes == length;
}
-int FileWrapperImpl::FlushImpl() {
- if (id_ != NULL) {
- return fflush(id_);
- }
- return -1;
+void FileWrapper::CloseFileImpl() {
+ if (file_ != nullptr)
+ fclose(file_);
+ file_ = nullptr;
}
-int FileWrapper::Rewind() {
- RTC_DCHECK(false);
- return -1;
+int FileWrapper::FlushImpl() {
+ return (file_ != nullptr) ? fflush(file_) : -1;
}
} // namespace webrtc
« no previous file with comments | « webrtc/system_wrappers/source/file_impl.h ('k') | webrtc/system_wrappers/source/trace_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698