blob: eb199a8482623aff1076d272446b3bb024d4ab0c [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
phoglund@webrtc.org740be442012-12-12 12:52:15 +000011#include "webrtc/system_wrappers/source/file_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
andrew@webrtc.org114c7902011-12-10 02:33:33 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
15#ifdef _WIN32
andrew@webrtc.org114c7902011-12-10 02:33:33 +000016#include <Windows.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000017#else
andrew@webrtc.org114c7902011-12-10 02:33:33 +000018#include <stdarg.h>
19#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000020#endif
21
phoglund@webrtc.org740be442012-12-12 12:52:15 +000022#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +000023
niklase@google.com470e71d2011-07-07 08:21:25 +000024namespace webrtc {
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +000025
phoglund@webrtc.org740be442012-12-12 12:52:15 +000026FileWrapper* FileWrapper::Create() {
27 return new FileWrapperImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000028}
29
30FileWrapperImpl::FileWrapperImpl()
phoglund@webrtc.org740be442012-12-12 12:52:15 +000031 : rw_lock_(RWLockWrapper::CreateRWLock()),
32 id_(NULL),
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000033 managed_file_handle_(true),
phoglund@webrtc.org740be442012-12-12 12:52:15 +000034 open_(false),
35 looping_(false),
36 read_only_(false),
37 max_size_in_bytes_(0),
38 size_in_bytes_(0) {
39 memset(file_name_utf8_, 0, kMaxFileNameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
phoglund@webrtc.org740be442012-12-12 12:52:15 +000042FileWrapperImpl::~FileWrapperImpl() {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000043 if (id_ != NULL && managed_file_handle_) {
phoglund@webrtc.org740be442012-12-12 12:52:15 +000044 fclose(id_);
45 }
46}
47
48int FileWrapperImpl::CloseFile() {
49 WriteLockScoped write(*rw_lock_);
50 return CloseFileImpl();
51}
52
53int FileWrapperImpl::Rewind() {
54 WriteLockScoped write(*rw_lock_);
55 if (looping_ || !read_only_) {
56 if (id_ != NULL) {
57 size_in_bytes_ = 0;
58 return fseek(id_, 0, SEEK_SET);
niklase@google.com470e71d2011-07-07 08:21:25 +000059 }
phoglund@webrtc.org740be442012-12-12 12:52:15 +000060 }
61 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
phoglund@webrtc.org740be442012-12-12 12:52:15 +000064int FileWrapperImpl::SetMaxFileSize(size_t bytes) {
65 WriteLockScoped write(*rw_lock_);
66 max_size_in_bytes_ = bytes;
67 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000068}
69
phoglund@webrtc.org740be442012-12-12 12:52:15 +000070int FileWrapperImpl::Flush() {
71 WriteLockScoped write(*rw_lock_);
72 return FlushImpl();
73}
74
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000075int FileWrapperImpl::FileName(char* file_name_utf8, size_t size) const {
phoglund@webrtc.org740be442012-12-12 12:52:15 +000076 ReadLockScoped read(*rw_lock_);
77 size_t length = strlen(file_name_utf8_);
78 if (length > kMaxFileNameSize) {
79 assert(false);
niklase@google.com470e71d2011-07-07 08:21:25 +000080 return -1;
phoglund@webrtc.org740be442012-12-12 12:52:15 +000081 }
82 if (length < 1) {
83 return -1;
84 }
85
86 // Make sure to NULL terminate
87 if (size < length) {
88 length = size - 1;
89 }
90 memcpy(file_name_utf8, file_name_utf8_, length);
91 file_name_utf8[length] = 0;
92 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
phoglund@webrtc.org740be442012-12-12 12:52:15 +000095bool FileWrapperImpl::Open() const {
96 ReadLockScoped read(*rw_lock_);
97 return open_;
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000100int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only,
101 bool loop, bool text) {
102 WriteLockScoped write(*rw_lock_);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000103 if (id_ != NULL && !managed_file_handle_)
104 return -1;
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000105 size_t length = strlen(file_name_utf8);
106 if (length > kMaxFileNameSize - 1) {
107 return -1;
108 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000110 read_only_ = read_only;
andrew@webrtc.org986fab12011-12-15 19:11:41 +0000111
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000112 FILE* tmp_id = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113#if defined _WIN32
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000114 wchar_t wide_file_name[kMaxFileNameSize];
115 wide_file_name[0] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000117 MultiByteToWideChar(CP_UTF8,
118 0, // UTF8 flag
119 file_name_utf8,
120 -1, // Null terminated string
121 wide_file_name,
122 kMaxFileNameSize);
123 if (text) {
124 if (read_only) {
125 tmp_id = _wfopen(wide_file_name, L"rt");
niklase@google.com470e71d2011-07-07 08:21:25 +0000126 } else {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000127 tmp_id = _wfopen(wide_file_name, L"wt");
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 }
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000129 } else {
130 if (read_only) {
131 tmp_id = _wfopen(wide_file_name, L"rb");
132 } else {
133 tmp_id = _wfopen(wide_file_name, L"wb");
134 }
135 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000136#else
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000137 if (text) {
138 if (read_only) {
139 tmp_id = fopen(file_name_utf8, "rt");
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 } else {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000141 tmp_id = fopen(file_name_utf8, "wt");
niklase@google.com470e71d2011-07-07 08:21:25 +0000142 }
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000143 } else {
144 if (read_only) {
145 tmp_id = fopen(file_name_utf8, "rb");
146 } else {
147 tmp_id = fopen(file_name_utf8, "wb");
148 }
149 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000150#endif
151
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000152 if (tmp_id != NULL) {
153 // +1 comes from copying the NULL termination character.
154 memcpy(file_name_utf8_, file_name_utf8, length + 1);
155 if (id_ != NULL) {
156 fclose(id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 }
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000158 id_ = tmp_id;
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000159 managed_file_handle_ = true;
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000160 looping_ = loop;
161 open_ = true;
162 return 0;
163 }
164 return -1;
165}
166
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000167int FileWrapperImpl::OpenFromFileHandle(FILE* handle,
168 bool manage_file,
169 bool read_only,
170 bool loop) {
171 WriteLockScoped write(*rw_lock_);
172 if (!handle)
173 return -1;
174
175 if (id_ != NULL) {
176 if (managed_file_handle_)
177 fclose(id_);
178 else
179 return -1;
180 }
181
182 id_ = handle;
183 managed_file_handle_ = manage_file;
184 read_only_ = read_only;
185 looping_ = loop;
186 open_ = true;
187 return 0;
188}
189
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000190int FileWrapperImpl::Read(void* buf, size_t length) {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000191 WriteLockScoped write(*rw_lock_);
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000192 if (id_ == NULL)
193 return -1;
andrew@webrtc.org114c7902011-12-10 02:33:33 +0000194
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000195 size_t bytes_read = fread(buf, 1, length, id_);
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000196 if (bytes_read != length && !looping_) {
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +0000197 CloseFileImpl();
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000198 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000199 return static_cast<int>(bytes_read);
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000200}
201
202int FileWrapperImpl::WriteText(const char* format, ...) {
203 WriteLockScoped write(*rw_lock_);
204 if (format == NULL)
205 return -1;
206
207 if (read_only_)
208 return -1;
209
210 if (id_ == NULL)
211 return -1;
212
213 va_list args;
214 va_start(args, format);
215 int num_chars = vfprintf(id_, format, args);
216 va_end(args);
217
218 if (num_chars >= 0) {
219 return num_chars;
220 } else {
221 CloseFileImpl();
222 return -1;
223 }
224}
225
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000226bool FileWrapperImpl::Write(const void* buf, size_t length) {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000227 WriteLockScoped write(*rw_lock_);
228 if (buf == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 return false;
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000230
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000231 if (read_only_)
232 return false;
233
234 if (id_ == NULL)
235 return false;
236
237 // Check if it's time to stop writing.
238 if (max_size_in_bytes_ > 0 &&
239 (size_in_bytes_ + length) > max_size_in_bytes_) {
240 FlushImpl();
241 return false;
242 }
243
244 size_t num_bytes = fwrite(buf, 1, length, id_);
245 if (num_bytes > 0) {
246 size_in_bytes_ += num_bytes;
247 return true;
248 }
249
250 CloseFileImpl();
251 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252}
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +0000253
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +0000254int FileWrapperImpl::CloseFileImpl() {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000255 if (id_ != NULL) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000256 if (managed_file_handle_)
257 fclose(id_);
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000258 id_ = NULL;
259 }
260 memset(file_name_utf8_, 0, kMaxFileNameSize);
261 open_ = false;
262 return 0;
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +0000263}
264
265int FileWrapperImpl::FlushImpl() {
phoglund@webrtc.org740be442012-12-12 12:52:15 +0000266 if (id_ != NULL) {
267 return fflush(id_);
268 }
269 return -1;
henrike@webrtc.org9a6dac42012-09-27 22:20:34 +0000270}
271
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000272} // namespace webrtc