blob: 350aaeb15bd890368588ed4a5b84d3040bb7c6f8 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "system_wrappers/include/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 <stdarg.h>
17#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000018#endif
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +000021
niklase@google.com470e71d2011-07-07 08:21:25 +000022namespace webrtc {
tommia6219cc2016-06-15 10:30:14 -070023namespace {
24FILE* FileOpen(const char* file_name_utf8, bool read_only) {
25#if defined(_WIN32)
26 int len = MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, nullptr, 0);
27 std::wstring wstr(len, 0);
28 MultiByteToWideChar(CP_UTF8, 0, file_name_utf8, -1, &wstr[0], len);
29 FILE* file = _wfopen(wstr.c_str(), read_only ? L"rb" : L"wb");
niklase@google.com470e71d2011-07-07 08:21:25 +000030#else
tommia6219cc2016-06-15 10:30:14 -070031 FILE* file = fopen(file_name_utf8, read_only ? "rb" : "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +000032#endif
tommia6219cc2016-06-15 10:30:14 -070033 return file;
34}
35} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000036
tommia6219cc2016-06-15 10:30:14 -070037// static
38FileWrapper* FileWrapper::Create() {
39 return new FileWrapper();
phoglund@webrtc.org740be442012-12-12 12:52:15 +000040}
41
tommia6219cc2016-06-15 10:30:14 -070042// static
43FileWrapper FileWrapper::Open(const char* file_name_utf8, bool read_only) {
44 return FileWrapper(FileOpen(file_name_utf8, read_only), 0);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000045}
46
tommia6219cc2016-06-15 10:30:14 -070047FileWrapper::FileWrapper() {}
andrew@webrtc.org114c7902011-12-10 02:33:33 +000048
tommia6219cc2016-06-15 10:30:14 -070049FileWrapper::FileWrapper(FILE* file, size_t max_size)
50 : file_(file), max_size_in_bytes_(max_size) {}
51
52FileWrapper::~FileWrapper() {
53 CloseFileImpl();
phoglund@webrtc.org740be442012-12-12 12:52:15 +000054}
55
tommia6219cc2016-06-15 10:30:14 -070056FileWrapper::FileWrapper(FileWrapper&& other) {
57 operator=(std::move(other));
phoglund@webrtc.org740be442012-12-12 12:52:15 +000058}
59
tommia6219cc2016-06-15 10:30:14 -070060FileWrapper& FileWrapper::operator=(FileWrapper&& other) {
61 file_ = other.file_;
62 max_size_in_bytes_ = other.max_size_in_bytes_;
63 position_ = other.position_;
64 other.file_ = nullptr;
65 return *this;
niklase@google.com470e71d2011-07-07 08:21:25 +000066}
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000067
tommia6219cc2016-06-15 10:30:14 -070068void FileWrapper::CloseFile() {
69 rtc::CritScope lock(&lock_);
70 CloseFileImpl();
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +000071}
72
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000073int FileWrapper::Rewind() {
tommia6219cc2016-06-15 10:30:14 -070074 rtc::CritScope lock(&lock_);
75 if (file_ != nullptr) {
76 position_ = 0;
77 return fseek(file_, 0, SEEK_SET);
78 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000079 return -1;
80}
81
tommia6219cc2016-06-15 10:30:14 -070082void FileWrapper::SetMaxFileSize(size_t bytes) {
83 rtc::CritScope lock(&lock_);
84 max_size_in_bytes_ = bytes;
85}
86
87int FileWrapper::Flush() {
88 rtc::CritScope lock(&lock_);
89 return FlushImpl();
90}
91
92bool FileWrapper::OpenFile(const char* file_name_utf8, bool read_only) {
93 size_t length = strlen(file_name_utf8);
94 if (length > kMaxFileNameSize - 1)
95 return false;
96
97 rtc::CritScope lock(&lock_);
98 if (file_ != nullptr)
99 return false;
100
101 file_ = FileOpen(file_name_utf8, read_only);
102 return file_ != nullptr;
103}
104
105bool FileWrapper::OpenFromFileHandle(FILE* handle) {
106 if (!handle)
107 return false;
108 rtc::CritScope lock(&lock_);
109 CloseFileImpl();
110 file_ = handle;
111 return true;
112}
113
114int FileWrapper::Read(void* buf, size_t length) {
115 rtc::CritScope lock(&lock_);
116 if (file_ == nullptr)
117 return -1;
118
119 size_t bytes_read = fread(buf, 1, length, file_);
120 return static_cast<int>(bytes_read);
121}
122
123bool FileWrapper::Write(const void* buf, size_t length) {
124 if (buf == nullptr)
125 return false;
126
127 rtc::CritScope lock(&lock_);
128
129 if (file_ == nullptr)
130 return false;
131
132 // Check if it's time to stop writing.
133 if (max_size_in_bytes_ > 0 && (position_ + length) > max_size_in_bytes_)
134 return false;
135
136 size_t num_bytes = fwrite(buf, 1, length, file_);
137 position_ += num_bytes;
138
139 return num_bytes == length;
140}
141
142void FileWrapper::CloseFileImpl() {
143 if (file_ != nullptr)
144 fclose(file_);
145 file_ = nullptr;
146}
147
148int FileWrapper::FlushImpl() {
149 return (file_ != nullptr) ? fflush(file_) : -1;
150}
151
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000152} // namespace webrtc