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,67 +13,66 @@
#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.
static FileWrapper* Create(); static FileWrapper* Create();
// 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;
virtual int CloseFile() = 0; virtual int CloseFile() = 0;
// Limits the file size to |bytes|. Writing will fail after the cap // Limits the file size to |bytes|. Writing will fail after the cap
// is hit. Pass zero to use an unlimited size. // is hit. Pass zero to use an unlimited size.
virtual int SetMaxFileSize(size_t bytes) = 0; virtual int SetMaxFileSize(size_t bytes) = 0;
// 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
// as printf. That is, supply a format string containing text and // as printf. That is, supply a format string containing text and
// specifiers. Returns the number of characters written or -1 on error. // specifiers. Returns the number of characters written or -1 on error.
virtual int WriteText(const char* format, ...) = 0; virtual int WriteText(const char* format, ...) = 0;
// Inherited from Instream. // Inherited from Instream.
// Reads |length| bytes from file to |buf|. Returns the number of bytes read // Reads |length| bytes from file to |buf|. Returns the number of bytes read
// or -1 on error. // or -1 on error.
virtual int Read(void* buf, int length) = 0; virtual int Read(void* buf, int length) = 0;
// 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
// called with |loop| == true or |readOnly| == true. // called with |loop| == true or |readOnly| == true.
virtual int Rewind() = 0; virtual int Rewind() = 0;
}; };
} // namespace webrtc } // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_

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,269 +19,233 @@
#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() {
WriteLockScoped write(*rw_lock_);
return 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;
} }
int FileWrapperImpl::CloseFile() int FileWrapperImpl::SetMaxFileSize(size_t bytes) {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock); max_size_in_bytes_ = bytes;
return CloseFileImpl(); return 0;
} }
int FileWrapperImpl::Rewind() int FileWrapperImpl::Flush() {
{ WriteLockScoped write(*rw_lock_);
WriteLockScoped write(*_rwLock); return FlushImpl();
if(_looping || !_readOnly) }
{
if (_id != NULL) int FileWrapperImpl::FileName(char* file_name_utf8,
{ size_t size) const {
_sizeInBytes = 0; ReadLockScoped read(*rw_lock_);
return fseek(_id, 0, SEEK_SET); size_t length = strlen(file_name_utf8_);
} if (length > kMaxFileNameSize) {
} assert(false);
return -1; return -1;
}
if (length < 1) {
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;
} }
int FileWrapperImpl::SetMaxFileSize(size_t bytes) bool FileWrapperImpl::Open() const {
{ ReadLockScoped read(*rw_lock_);
WriteLockScoped write(*_rwLock); return open_;
_maxSizeInBytes = bytes;
return 0;
} }
int FileWrapperImpl::Flush() int FileWrapperImpl::OpenFile(const char* file_name_utf8, bool read_only,
{ bool loop, bool text) {
WriteLockScoped write(*_rwLock); WriteLockScoped write(*rw_lock_);
return FlushImpl(); size_t length = strlen(file_name_utf8);
} if (length > kMaxFileNameSize - 1) {
return -1;
}
int FileWrapperImpl::FileName(char* fileNameUTF8, read_only_ = read_only;
size_t size) const
{
ReadLockScoped read(*_rwLock);
size_t length = strlen(_fileNameUTF8);
if(length > kMaxFileNameSize)
{
assert(false);
return -1;
}
if(length < 1)
{
return -1;
}
// Make sure to NULL terminate FILE* tmp_id = NULL;
if(size < length)
{
length = size - 1;
}
memcpy(fileNameUTF8, _fileNameUTF8, length);
fileNameUTF8[length] = 0;
return 0;
}
bool FileWrapperImpl::Open() const
{
ReadLockScoped read(*_rwLock);
return _open;
}
int FileWrapperImpl::OpenFile(const char *fileNameUTF8, bool readOnly,
bool loop, bool text)
{
WriteLockScoped write(*_rwLock);
size_t length = strlen(fileNameUTF8);
if (length > kMaxFileNameSize - 1)
{
return -1;
}
_readOnly = readOnly;
FILE *tmpId = 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 {
tmpId = _wfopen(wideFileName, L"wt");
}
} else { } else {
if(readOnly) tmp_id = _wfopen(wide_file_name, L"wt");
{
tmpId = _wfopen(wideFileName, L"rb");
} else {
tmpId = _wfopen(wideFileName, L"wb");
}
} }
} else {
if (read_only) {
tmp_id = _wfopen(wide_file_name, L"rb");
} else {
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 {
tmpId = fopen(fileNameUTF8, "wt");
}
} else { } else {
if(readOnly) tmp_id = fopen(file_name_utf8, "wt");
{
tmpId = fopen(fileNameUTF8, "rb");
} else {
tmpId = fopen(fileNameUTF8, "wb");
}
} }
} else {
if (read_only) {
tmp_id = fopen(file_name_utf8, "rb");
} else {
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(file_name_utf8_, file_name_utf8, length + 1);
memcpy(_fileNameUTF8, fileNameUTF8, length + 1); if (id_ != NULL) {
if (_id != NULL) fclose(id_);
{
fclose(_id);
}
_id = tmpId;
_looping = loop;
_open = true;
return 0;
} }
id_ = tmp_id;
looping_ = loop;
open_ = true;
return 0;
}
return -1;
}
int FileWrapperImpl::Read(void* buf, int length) {
WriteLockScoped write(*rw_lock_);
if (length < 0)
return -1; return -1;
}
int FileWrapperImpl::Read(void* buf, int length) if (id_ == NULL)
{ return -1;
WriteLockScoped write(*_rwLock);
if (length < 0)
return -1;
if (_id == NULL)
return -1;
int bytes_read = static_cast<int>(fread(buf, 1, length, _id));
if (bytes_read != length && !_looping)
{
CloseFileImpl();
}
return bytes_read;
}
int FileWrapperImpl::WriteText(const char* format, ...)
{
WriteLockScoped write(*_rwLock);
if (format == NULL)
return -1;
if (_readOnly)
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, int length)
{
WriteLockScoped write(*_rwLock);
if (buf == NULL)
return false;
if (length < 0)
return false;
if (_readOnly)
return false;
if (_id == NULL)
return false;
// Check if it's time to stop writing.
if (_maxSizeInBytes > 0 && (_sizeInBytes + length) > _maxSizeInBytes)
{
FlushImpl();
return false;
}
size_t num_bytes = fwrite(buf, 1, length, _id);
if (num_bytes > 0)
{
_sizeInBytes += num_bytes;
return true;
}
int bytes_read = static_cast<int>(fread(buf, 1, length, id_));
if (bytes_read != length && !looping_) {
CloseFileImpl(); CloseFileImpl();
}
return 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, int length) {
WriteLockScoped write(*rw_lock_);
if (buf == NULL)
return false; return false;
if (length < 0)
return false;
if (read_only_)
return false;
if (id_ == NULL)
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();
return false;
}
size_t num_bytes = fwrite(buf, 1, length, id_);
if (num_bytes > 0) {
size_in_bytes_ += num_bytes;
return true;
}
CloseFileImpl();
return false;
} }
int FileWrapperImpl::CloseFileImpl() { int FileWrapperImpl::CloseFileImpl() {
if (_id != NULL) if (id_ != NULL) {
{ fclose(id_);
fclose(_id); id_ = NULL;
_id = NULL; }
} memset(file_name_utf8_, 0, kMaxFileNameSize);
memset(_fileNameUTF8, 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,56 +11,54 @@
#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);
virtual int CloseFile(); virtual int CloseFile();
virtual int SetMaxFileSize(size_t bytes); virtual int SetMaxFileSize(size_t bytes);
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
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_ #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_