Clean up file wrapper a bit further.
- Make error handling in Read, Write and WriteText consistent. - Improve the interface comments a bit. TEST=voe_auto_test, vie_auto_test Review URL: http://webrtc-codereview.appspot.com/321012 git-svn-id: http://webrtc.googlecode.com/svn/trunk@1210 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
0c839fe873
commit
986fab1496
@ -53,20 +53,20 @@ public:
|
||||
virtual int FileName(char* fileNameUTF8,
|
||||
size_t size) const = 0;
|
||||
|
||||
// Write |format| to the opened file. Supply arguments to this function as
|
||||
// you would printf. Returns the number of characters written or -1 on
|
||||
// error.
|
||||
// Write |format| to the opened file. Arguments are taken in the same manner
|
||||
// as printf. That is, supply a format string containing text and
|
||||
// specifiers. Returns the number of characters written or -1 on error.
|
||||
virtual int WriteText(const char* format, ...) = 0;
|
||||
|
||||
// Inherited from Instream.
|
||||
// Reads |len| 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.
|
||||
virtual int Read(void* buf, int len) = 0;
|
||||
virtual int Read(void* buf, int length) = 0;
|
||||
|
||||
// Inherited from OutStream.
|
||||
// Writes |len| 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.
|
||||
virtual bool Write(const void *buf, int len) = 0;
|
||||
virtual bool Write(const void *buf, int length) = 0;
|
||||
|
||||
// Inherited from both Instream and OutStream.
|
||||
// Rewinds the file to the start. Only available when OpenFile() has been
|
||||
|
@ -88,23 +88,24 @@ int FileWrapperImpl::Flush()
|
||||
int FileWrapperImpl::FileName(char* fileNameUTF8,
|
||||
size_t size) const
|
||||
{
|
||||
size_t len = strlen(_fileNameUTF8);
|
||||
if(len > kMaxFileNameSize)
|
||||
size_t length = strlen(_fileNameUTF8);
|
||||
if(length > kMaxFileNameSize)
|
||||
{
|
||||
assert(false);
|
||||
return -1;
|
||||
}
|
||||
if(len < 1)
|
||||
if(length < 1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Make sure to NULL terminate
|
||||
if(size < len)
|
||||
if(size < length)
|
||||
{
|
||||
len = size - 1;
|
||||
length = size - 1;
|
||||
}
|
||||
memcpy(fileNameUTF8, _fileNameUTF8, len);
|
||||
fileNameUTF8[len] = 0;
|
||||
memcpy(fileNameUTF8, _fileNameUTF8, length);
|
||||
fileNameUTF8[length] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -186,38 +187,33 @@ int FileWrapperImpl::OpenFile(const char *fileNameUTF8, bool readOnly,
|
||||
return -1;
|
||||
}
|
||||
|
||||
int FileWrapperImpl::Read(void *buf, int len)
|
||||
int FileWrapperImpl::Read(void* buf, int length)
|
||||
{
|
||||
if (len < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (_id != NULL)
|
||||
{
|
||||
int res = static_cast<int>(fread(buf, 1, len, _id));
|
||||
if (res != len)
|
||||
{
|
||||
if(!_looping)
|
||||
{
|
||||
CloseFile();
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int FileWrapperImpl::WriteText(const char* format, ...)
|
||||
{
|
||||
if (_readOnly)
|
||||
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)
|
||||
{
|
||||
CloseFile();
|
||||
}
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
int FileWrapperImpl::WriteText(const char* format, ...)
|
||||
{
|
||||
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);
|
||||
@ -234,30 +230,35 @@ int FileWrapperImpl::WriteText(const char* format, ...)
|
||||
}
|
||||
}
|
||||
|
||||
bool FileWrapperImpl::Write(const void* buf, int len)
|
||||
bool FileWrapperImpl::Write(const void* buf, int length)
|
||||
{
|
||||
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)
|
||||
{
|
||||
Flush();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_id != NULL)
|
||||
size_t num_bytes = fwrite(buf, 1, length, _id);
|
||||
if (num_bytes > 0)
|
||||
{
|
||||
// Check if it's time to stop writing.
|
||||
if (_maxSizeInBytes > 0 && (_sizeInBytes + len) > _maxSizeInBytes)
|
||||
{
|
||||
Flush();
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t num_bytes = fwrite(buf, 1, len, _id);
|
||||
if (num_bytes > 0)
|
||||
{
|
||||
_sizeInBytes += num_bytes;
|
||||
return true;
|
||||
}
|
||||
CloseFile();
|
||||
_sizeInBytes += num_bytes;
|
||||
return true;
|
||||
}
|
||||
|
||||
CloseFile();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -37,8 +37,8 @@ public:
|
||||
virtual int SetMaxFileSize(size_t bytes);
|
||||
virtual int Flush();
|
||||
|
||||
virtual int Read(void* buf, int len);
|
||||
virtual bool Write(const void *buf, int len);
|
||||
virtual int Read(void* buf, int length);
|
||||
virtual bool Write(const void *buf, int length);
|
||||
virtual int WriteText(const char* format, ...);
|
||||
virtual int Rewind();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user