Reformatted file_* classes.

BUG=
TEST=Trybots.

Review URL: https://webrtc-codereview.appspot.com/980004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3268 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
phoglund@webrtc.org
2012-12-12 12:52:15 +00:00
parent 4e16f25774
commit 740be44af5
3 changed files with 261 additions and 300 deletions

View File

@@ -13,17 +13,16 @@
#include <stddef.h> #include <stddef.h>
#include "common_types.h" #include "webrtc/common_types.h"
#include "typedefs.h" #include "webrtc/typedefs.h"
// Implementation of an InStream and OutStream that can read (exclusive) or // Implementation of an InStream and OutStream that can read (exclusive) or
// write from/to a file. // write from/to a file.
namespace webrtc { namespace webrtc {
class FileWrapper : public InStream, public OutStream class FileWrapper : public InStream, public OutStream {
{ public:
public:
static const size_t kMaxFileNameSize = 1024; static const size_t kMaxFileNameSize = 1024;
// Factory method. Constructor disabled. // Factory method. Constructor disabled.
@@ -32,9 +31,9 @@ public:
// Returns true if a file has been opened. // Returns true if a file has been opened.
virtual bool Open() const = 0; virtual bool Open() const = 0;
// Opens a file in read or write mode, decided by the readOnly parameter. // Opens a file in read or write mode, decided by the read_only parameter.
virtual int OpenFile(const char* fileNameUTF8, virtual int OpenFile(const char* file_name_utf8,
bool readOnly, bool read_only,
bool loop = false, bool loop = false,
bool text = false) = 0; bool text = false) = 0;
@@ -47,10 +46,10 @@ public:
// Flush any pending writes. // Flush any pending writes.
virtual int Flush() = 0; virtual int Flush() = 0;
// Returns the opened file's name in |fileNameUTF8|. Provide the size of // Returns the opened file's name in |file_name_utf8|. Provide the size of
// the buffer in bytes in |size|. The name will be truncated if |size| is // the buffer in bytes in |size|. The name will be truncated if |size| is
// too small. // too small.
virtual int FileName(char* fileNameUTF8, virtual int FileName(char* file_name_utf8,
size_t size) const = 0; size_t size) const = 0;
// Write |format| to the opened file. Arguments are taken in the same manner // Write |format| to the opened file. Arguments are taken in the same manner
@@ -66,7 +65,7 @@ public:
// Inherited from OutStream. // Inherited from OutStream.
// Writes |length| bytes from |buf| to file. The actual writing may happen // Writes |length| bytes from |buf| to file. The actual writing may happen
// some time later. Call Flush() to force a write. // some time later. Call Flush() to force a write.
virtual bool Write(const void *buf, int length) = 0; virtual bool Write(const void* buf, int length) = 0;
// Inherited from both Instream and OutStream. // Inherited from both Instream and OutStream.
// Rewinds the file to the start. Only available when OpenFile() has been // Rewinds the file to the start. Only available when OpenFile() has been

View File

@@ -8,7 +8,7 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#include "system_wrappers/source/file_impl.h" #include "webrtc/system_wrappers/source/file_impl.h"
#include <assert.h> #include <assert.h>
@@ -19,244 +19,211 @@
#include <string.h> #include <string.h>
#endif #endif
#include "system_wrappers/interface/rw_lock_wrapper.h" #include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
namespace webrtc { namespace webrtc {
FileWrapper* FileWrapper::Create() FileWrapper* FileWrapper::Create() {
{
return new FileWrapperImpl(); return new FileWrapperImpl();
} }
FileWrapperImpl::FileWrapperImpl() FileWrapperImpl::FileWrapperImpl()
: _rwLock(RWLockWrapper::CreateRWLock()), : rw_lock_(RWLockWrapper::CreateRWLock()),
_id(NULL), id_(NULL),
_open(false), open_(false),
_looping(false), looping_(false),
_readOnly(false), read_only_(false),
_maxSizeInBytes(0), max_size_in_bytes_(0),
_sizeInBytes(0) size_in_bytes_(0) {
{ memset(file_name_utf8_, 0, kMaxFileNameSize);
memset(_fileNameUTF8, 0, kMaxFileNameSize);
} }
FileWrapperImpl::~FileWrapperImpl() FileWrapperImpl::~FileWrapperImpl() {
{ if (id_ != NULL) {
if (_id != NULL) fclose(id_);
{
fclose(_id);
} }
} }
int FileWrapperImpl::CloseFile() int FileWrapperImpl::CloseFile() {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock);
return CloseFileImpl(); return CloseFileImpl();
} }
int FileWrapperImpl::Rewind() int FileWrapperImpl::Rewind() {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock); if (looping_ || !read_only_) {
if(_looping || !_readOnly) if (id_ != NULL) {
{ size_in_bytes_ = 0;
if (_id != NULL) return fseek(id_, 0, SEEK_SET);
{
_sizeInBytes = 0;
return fseek(_id, 0, SEEK_SET);
} }
} }
return -1; return -1;
} }
int FileWrapperImpl::SetMaxFileSize(size_t bytes) int FileWrapperImpl::SetMaxFileSize(size_t bytes) {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock); max_size_in_bytes_ = bytes;
_maxSizeInBytes = bytes;
return 0; return 0;
} }
int FileWrapperImpl::Flush() int FileWrapperImpl::Flush() {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock);
return FlushImpl(); return FlushImpl();
} }
int FileWrapperImpl::FileName(char* fileNameUTF8, int FileWrapperImpl::FileName(char* file_name_utf8,
size_t size) const size_t size) const {
{ ReadLockScoped read(*rw_lock_);
ReadLockScoped read(*_rwLock); size_t length = strlen(file_name_utf8_);
size_t length = strlen(_fileNameUTF8); if (length > kMaxFileNameSize) {
if(length > kMaxFileNameSize)
{
assert(false); assert(false);
return -1; return -1;
} }
if(length < 1) if (length < 1) {
{
return -1; return -1;
} }
// Make sure to NULL terminate // Make sure to NULL terminate
if(size < length) if (size < length) {
{
length = size - 1; length = size - 1;
} }
memcpy(fileNameUTF8, _fileNameUTF8, length); memcpy(file_name_utf8, file_name_utf8_, length);
fileNameUTF8[length] = 0; file_name_utf8[length] = 0;
return 0; return 0;
} }
bool FileWrapperImpl::Open() const bool FileWrapperImpl::Open() const {
{ ReadLockScoped read(*rw_lock_);
ReadLockScoped read(*_rwLock); return open_;
return _open;
} }
int FileWrapperImpl::OpenFile(const char *fileNameUTF8, bool readOnly, int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only,
bool loop, bool text) bool loop, bool text) {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock); size_t length = strlen(file_name_utf8);
size_t length = strlen(fileNameUTF8); if (length > kMaxFileNameSize - 1) {
if (length > kMaxFileNameSize - 1)
{
return -1; return -1;
} }
_readOnly = readOnly; read_only_ = read_only;
FILE *tmpId = NULL; FILE* tmp_id = NULL;
#if defined _WIN32 #if defined _WIN32
wchar_t wideFileName[kMaxFileNameSize]; wchar_t wide_file_name[kMaxFileNameSize];
wideFileName[0] = 0; wide_file_name[0] = 0;
MultiByteToWideChar(CP_UTF8, MultiByteToWideChar(CP_UTF8,
0 /*UTF8 flag*/, 0, // UTF8 flag
fileNameUTF8, file_name_utf8,
-1 /*Null terminated string*/, -1, // Null terminated string
wideFileName, wide_file_name,
kMaxFileNameSize); kMaxFileNameSize);
if(text) if (text) {
{ if (read_only) {
if(readOnly) tmp_id = _wfopen(wide_file_name, L"rt");
{
tmpId = _wfopen(wideFileName, L"rt");
} else { } else {
tmpId = _wfopen(wideFileName, L"wt"); tmp_id = _wfopen(wide_file_name, L"wt");
} }
} else { } else {
if(readOnly) if (read_only) {
{ tmp_id = _wfopen(wide_file_name, L"rb");
tmpId = _wfopen(wideFileName, L"rb");
} else { } else {
tmpId = _wfopen(wideFileName, L"wb"); tmp_id = _wfopen(wide_file_name, L"wb");
} }
} }
#else #else
if(text) if (text) {
{ if (read_only) {
if(readOnly) tmp_id = fopen(file_name_utf8, "rt");
{
tmpId = fopen(fileNameUTF8, "rt");
} else { } else {
tmpId = fopen(fileNameUTF8, "wt"); tmp_id = fopen(file_name_utf8, "wt");
} }
} else { } else {
if(readOnly) if (read_only) {
{ tmp_id = fopen(file_name_utf8, "rb");
tmpId = fopen(fileNameUTF8, "rb");
} else { } else {
tmpId = fopen(fileNameUTF8, "wb"); tmp_id = fopen(file_name_utf8, "wb");
} }
} }
#endif #endif
if (tmpId != NULL) if (tmp_id != NULL) {
{
// +1 comes from copying the NULL termination character. // +1 comes from copying the NULL termination character.
memcpy(_fileNameUTF8, fileNameUTF8, length + 1); memcpy(file_name_utf8_, file_name_utf8, length + 1);
if (_id != NULL) if (id_ != NULL) {
{ fclose(id_);
fclose(_id);
} }
_id = tmpId; id_ = tmp_id;
_looping = loop; looping_ = loop;
_open = true; open_ = true;
return 0; return 0;
} }
return -1; return -1;
} }
int FileWrapperImpl::Read(void* buf, int length) int FileWrapperImpl::Read(void* buf, int length) {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock);
if (length < 0) if (length < 0)
return -1; return -1;
if (_id == NULL) if (id_ == NULL)
return -1; return -1;
int bytes_read = static_cast<int>(fread(buf, 1, length, _id)); int bytes_read = static_cast<int>(fread(buf, 1, length, id_));
if (bytes_read != length && !_looping) if (bytes_read != length && !looping_) {
{
CloseFileImpl(); CloseFileImpl();
} }
return bytes_read; return bytes_read;
} }
int FileWrapperImpl::WriteText(const char* format, ...) int FileWrapperImpl::WriteText(const char* format, ...) {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock);
if (format == NULL) if (format == NULL)
return -1; return -1;
if (_readOnly) if (read_only_)
return -1; return -1;
if (_id == NULL) if (id_ == NULL)
return -1; return -1;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
int num_chars = vfprintf(_id, format, args); int num_chars = vfprintf(id_, format, args);
va_end(args); va_end(args);
if (num_chars >= 0) if (num_chars >= 0) {
{
return num_chars; return num_chars;
} } else {
else
{
CloseFileImpl(); CloseFileImpl();
return -1; return -1;
} }
} }
bool FileWrapperImpl::Write(const void* buf, int length) bool FileWrapperImpl::Write(const void* buf, int length) {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock);
if (buf == NULL) if (buf == NULL)
return false; return false;
if (length < 0) if (length < 0)
return false; return false;
if (_readOnly) if (read_only_)
return false; return false;
if (_id == NULL) if (id_ == NULL)
return false; return false;
// Check if it's time to stop writing. // Check if it's time to stop writing.
if (_maxSizeInBytes > 0 && (_sizeInBytes + length) > _maxSizeInBytes) if (max_size_in_bytes_ > 0 &&
{ (size_in_bytes_ + length) > max_size_in_bytes_) {
FlushImpl(); FlushImpl();
return false; return false;
} }
size_t num_bytes = fwrite(buf, 1, length, _id); size_t num_bytes = fwrite(buf, 1, length, id_);
if (num_bytes > 0) if (num_bytes > 0) {
{ size_in_bytes_ += num_bytes;
_sizeInBytes += num_bytes;
return true; return true;
} }
@@ -265,23 +232,20 @@ bool FileWrapperImpl::Write(const void* buf, int length)
} }
int FileWrapperImpl::CloseFileImpl() { int FileWrapperImpl::CloseFileImpl() {
if (_id != NULL) if (id_ != NULL) {
{ fclose(id_);
fclose(_id); id_ = NULL;
_id = NULL;
} }
memset(_fileNameUTF8, 0, kMaxFileNameSize); memset(file_name_utf8_, 0, kMaxFileNameSize);
_open = false; open_ = false;
return 0; return 0;
} }
int FileWrapperImpl::FlushImpl() { int FileWrapperImpl::FlushImpl() {
if (_id != NULL) if (id_ != NULL) {
{ return fflush(id_);
return fflush(_id);
} }
return -1; return -1;
} }
} // namespace webrtc } // namespace webrtc

View File

@@ -11,29 +11,27 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_ #ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_ #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_
#include "system_wrappers/interface/file_wrapper.h"
#include <stdio.h> #include <stdio.h>
#include "system_wrappers/interface/scoped_ptr.h" #include "webrtc/system_wrappers/interface/file_wrapper.h"
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
namespace webrtc { namespace webrtc {
class RWLockWrapper; class RWLockWrapper;
class FileWrapperImpl : public FileWrapper class FileWrapperImpl : public FileWrapper {
{ public:
public:
FileWrapperImpl(); FileWrapperImpl();
virtual ~FileWrapperImpl(); virtual ~FileWrapperImpl();
virtual int FileName(char* fileNameUTF8, virtual int FileName(char* file_name_utf8,
size_t size) const; size_t size) const;
virtual bool Open() const; virtual bool Open() const;
virtual int OpenFile(const char* fileNameUTF8, virtual int OpenFile(const char* file_name_utf8,
bool readOnly, bool read_only,
bool loop = false, bool loop = false,
bool text = false); bool text = false);
@@ -42,23 +40,23 @@ public:
virtual int Flush(); virtual int Flush();
virtual int Read(void* buf, int length); virtual int Read(void* buf, int length);
virtual bool Write(const void *buf, int length); virtual bool Write(const void* buf, int length);
virtual int WriteText(const char* format, ...); virtual int WriteText(const char* format, ...);
virtual int Rewind(); virtual int Rewind();
private: private:
int CloseFileImpl(); int CloseFileImpl();
int FlushImpl(); int FlushImpl();
scoped_ptr<RWLockWrapper> _rwLock; scoped_ptr<RWLockWrapper> rw_lock_;
FILE* _id; FILE* id_;
bool _open; bool open_;
bool _looping; bool looping_;
bool _readOnly; bool read_only_;
size_t _maxSizeInBytes; // -1 indicates file size limitation is off size_t max_size_in_bytes_; // -1 indicates file size limitation is off
size_t _sizeInBytes; size_t size_in_bytes_;
char _fileNameUTF8[kMaxFileNameSize]; char file_name_utf8_[kMaxFileNameSize];
}; };
} // namespace webrtc } // namespace webrtc