From 114c790be72b9a3c2b5802ca8b6a38ec23c1831d Mon Sep 17 00:00:00 2001 From: "andrew@webrtc.org" Date: Sat, 10 Dec 2011 02:33:33 +0000 Subject: [PATCH] Remove character limit in WriteText(). - vfprintf can be used directly here, removing the need for the interim buffer. This change allows us to remove the artificial character limit. - Fix bugs with _text. It wasn't actually getting set earlier, and the check was wrong. - Remove asserts that should use real error checks. TEST=DataLog and VoECallReport (through voe_auto_test), the only users of WriteText(). Review URL: http://webrtc-codereview.appspot.com/323001 git-svn-id: http://webrtc.googlecode.com/svn/trunk@1156 4adac7df-926f-26a2-2b94-8c16560cd09d --- src/system_wrappers/interface/file_wrapper.h | 10 ++- src/system_wrappers/source/file_impl.cc | 65 ++++++++++---------- src/system_wrappers/source/file_impl.h | 2 +- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/system_wrappers/interface/file_wrapper.h b/src/system_wrappers/interface/file_wrapper.h index 8f0cd8c24..123ab65ea 100644 --- a/src/system_wrappers/interface/file_wrapper.h +++ b/src/system_wrappers/interface/file_wrapper.h @@ -22,7 +22,6 @@ class FileWrapper : public InStream, public OutStream { public: enum { kMaxFileNameSize = 1024}; - enum { kFileMaxTextMessageSize = 1024}; // Factory method. Constructor disabled. static FileWrapper* Create(); @@ -52,15 +51,14 @@ public: // Write text to the opened file. The written text can contain plain text // and text with type specifiers in the same way as sprintf works. - virtual WebRtc_Word32 WriteText(const WebRtc_Word8* text, ...) = 0; + virtual WebRtc_Word32 WriteText(const char* format, ...) = 0; // Reads len number of bytes from buf to file. virtual int Read(void* buf, int len) = 0; - // Writes len number of bytes to buf from file. Please note that the actual - // writing to file may happen some time later. Call flush to force a write - // to take affect - virtual bool Write(const void *buf,int len) = 0; + // Writes len number of bytes to buf from file. The actual writing to file + // may happen some time later. Call flush to force a write to take effect. + virtual bool Write(const void *buf, int len) = 0; // Rewinds the file to the start. Only available when OpenFile() has been // called with loop argument set to true. Or readOnly argument has been set diff --git a/src/system_wrappers/source/file_impl.cc b/src/system_wrappers/source/file_impl.cc index 6046c2c77..f252fe212 100644 --- a/src/system_wrappers/source/file_impl.cc +++ b/src/system_wrappers/source/file_impl.cc @@ -10,13 +10,13 @@ #include "file_impl.h" -#include +#include #ifdef _WIN32 - #include +#include #else - #include - #include +#include +#include #endif namespace webrtc { @@ -125,6 +125,7 @@ WebRtc_Word32 FileWrapperImpl::OpenFile(const WebRtc_Word8 *fileNameUTF8, } _readOnly = readOnly; + _text = text; FILE *tmpId = NULL; #if defined _WIN32 @@ -174,7 +175,7 @@ WebRtc_Word32 FileWrapperImpl::OpenFile(const WebRtc_Word8 *fileNameUTF8, if (tmpId != NULL) { - // + 1 comes fro copying the NULL termination charachter too + // +1 comes from copying the NULL termination character. memcpy(_fileNameUTF8, fileNameUTF8, length + 1); if (_id != NULL) { @@ -196,7 +197,7 @@ int FileWrapperImpl::Read(void *buf, int len) } if (_id != NULL) { - WebRtc_Word32 res = static_cast(fread(buf, 1, len, _id)); + int res = static_cast(fread(buf, 1, len, _id)); if (res != len) { if(!_looping) @@ -209,41 +210,43 @@ int FileWrapperImpl::Read(void *buf, int len) return -1; } -WebRtc_Word32 FileWrapperImpl::WriteText(const WebRtc_Word8* text, ...) +WebRtc_Word32 FileWrapperImpl::WriteText(const char* format, ...) { - assert(!_readOnly); - assert(!_text); + if (_readOnly) + return -1; + + if (!_text) + return -1; if (_id == NULL) + return -1; + + if (format == NULL) + return -1; + + va_list args; + va_start(args, format); + int num_bytes = vfprintf(_id, format, args); + va_end(args); + + if (num_bytes > 0) { + return 0; + } + else + { + CloseFile(); return -1; } - - char tempBuff[kFileMaxTextMessageSize]; - if (text) - { - va_list args; - va_start(args, text); -#ifdef _WIN32 - _vsnprintf(tempBuff, kFileMaxTextMessageSize-1, text, args); -#else - vsnprintf(tempBuff, kFileMaxTextMessageSize-1, text, args); -#endif - va_end(args); - WebRtc_Word32 nBytes; - nBytes = fprintf(_id, "%s", tempBuff); - if (nBytes > 0) - { - return 0; - } - CloseFile(); - } - return -1; } bool FileWrapperImpl::Write(const void* buf, int len) { - assert(!_readOnly); + if (!_readOnly) + { + return false; + } + if (_id != NULL) { // Check if it's time to stop writing. diff --git a/src/system_wrappers/source/file_impl.h b/src/system_wrappers/source/file_impl.h index cf6b7347f..457b11026 100644 --- a/src/system_wrappers/source/file_impl.h +++ b/src/system_wrappers/source/file_impl.h @@ -40,7 +40,7 @@ public: virtual bool Write(const void *buf, int len); virtual int Rewind(); - virtual WebRtc_Word32 WriteText(const WebRtc_Word8* text, ...); + virtual WebRtc_Word32 WriteText(const char* format, ...); private: FILE* _id;